Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgnoah committed Jan 31, 2024
1 parent 03026b9 commit f7bd3ec
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 364 deletions.
5 changes: 5 additions & 0 deletions actions/burn_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,8 @@ func (*BurnAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*BurnAsset) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
2 changes: 2 additions & 0 deletions actions/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ const (
MaxMetadataSize = 256
MaxDecimals = 9
)

var DefaultNMTNamespace = make([]byte, 8)
5 changes: 5 additions & 0 deletions actions/create_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@ func (*CreateAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*CreateAsset) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
5 changes: 5 additions & 0 deletions actions/export_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,8 @@ func (*ExportAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*ExportAsset) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
5 changes: 5 additions & 0 deletions actions/export_block_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ func (*ExportBlockMsg) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*ExportBlockMsg) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
5 changes: 5 additions & 0 deletions actions/import_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,8 @@ func (*ImportAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*ImportAsset) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
5 changes: 5 additions & 0 deletions actions/import_block_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,8 @@ func (*ImportBlockMsg) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*ImportBlockMsg) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
5 changes: 5 additions & 0 deletions actions/mint_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,8 @@ func (*MintAsset) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*MintAsset) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
4 changes: 4 additions & 0 deletions actions/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ func (*SequencerMsg) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (m *SequencerMsg) NMTNamespace() []byte {
return m.ChainId
}
5 changes: 5 additions & 0 deletions actions/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ func (*Transfer) ValidRange(chain.Rules) (int64, int64) {
// Returning -1, -1 means that the action is always valid.
return -1, -1
}

func (*Transfer) NMTNamespace() []byte {
// byte array with 8 zeros
return DefaultNMTNamespace
}
210 changes: 199 additions & 11 deletions archiver/orm_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"

"github.com/AnomalyFi/hypersdk/chain"
"github.com/AnomalyFi/nodekit-seq/types"
"github.com/ava-labs/avalanchego/ids"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand All @@ -23,7 +25,7 @@ type ORMArchiver struct {

type DBBlock struct {
gorm.Model
BlockID string `gorm:"index"`
BlockId string `gorm:"index"`
Parent string
Timestamp int64
Height uint64 `gorm:"index"`
Expand Down Expand Up @@ -66,18 +68,16 @@ func NewORMArchiverFromConfigBytes(configBytes []byte) (*ORMArchiver, error) {
}
}

func (oa *ORMArchiver) InsertBlock(block *chain.StatefulBlock) error {
blkID, err := block.ID()
if err != nil {
return err
}
func (oa *ORMArchiver) InsertBlock(block *chain.StatelessBlock) error {
blkID := block.ID()
blkBytes, err := block.Marshal()
if err != nil {
return err
}

//TODO need to add L1Head and real Id
newBlock := DBBlock{
BlockID: blkID.String(),
BlockId: blkID.String(),
Parent: block.Prnt.String(),
Timestamp: block.Tmstmp,
Height: block.Hght,
Expand All @@ -97,18 +97,23 @@ func (oa *ORMArchiver) InsertBlock(block *chain.StatefulBlock) error {
return nil
}

func (oa *ORMArchiver) GetBlock(dbBlock *DBBlock, blockParser chain.Parser) (*chain.StatefulBlock, error) {
func (oa *ORMArchiver) GetBlock(dbBlock *DBBlock, blockParser chain.Parser) (*chain.StatefulBlock, *ids.ID, error) {
tx := oa.db.Last(dbBlock)
if tx.Error != nil {
return nil, tx.Error
return nil, nil, tx.Error
}

blk, err := chain.UnmarshalBlock(dbBlock.Bytes, blockParser)
if err != nil {
return nil, err
return nil, nil, err
}

id, err := ids.FromString(dbBlock.BlockId)
if err != nil {
return nil, nil, err
}

return blk, nil
return blk, &id, nil
}

func (oa *ORMArchiver) DeleteBlock(dbBlock *DBBlock) (bool, error) {
Expand All @@ -122,3 +127,186 @@ func (oa *ORMArchiver) DeleteBlock(dbBlock *DBBlock) (bool, error) {

return true, nil
}

func (oa *ORMArchiver) GetByHeight(height uint64, end int64, blockParser chain.Parser, reply *types.BlockHeadersResponse) error {
Prev := types.BlockInfo{}

if height > 1 {
dbBlock := DBBlock{
BlockId: "",
Height: height - 1,
}
tx := oa.db.Last(dbBlock)
if tx.Error != nil {
return tx.Error
}

blk, err := chain.UnmarshalBlock(dbBlock.Bytes, blockParser)
if err != nil {
return err
}

Prev = types.BlockInfo{
BlockId: dbBlock.BlockId,
Timestamp: blk.Tmstmp,
L1Head: uint64(blk.L1Head),
Height: blk.Hght,
}

}

var blocks []types.BlockInfo

if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Height >= ? AND Timestamp < ? ORDER BY Height", height, end).Scan(&blocks).Error; err != nil {
return err
}

var Next types.BlockInfo

if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Timestamp >= ? ORDER BY Height LIMIT 1", end).Scan(&Next).Error; err != nil {
return err
}

//TODO return prev, next, blocks

reply.From = height
reply.Blocks = blocks
reply.Prev = Prev
reply.Next = Next

return nil
}

func (oa *ORMArchiver) GetByID(args *types.GetBlockHeadersIDArgs, reply *types.BlockHeadersResponse, blockParser chain.Parser) error {

var firstBlock uint64

if args.ID != "" {
dbBlock := DBBlock{
BlockId: args.ID,
}
tx := oa.db.Last(dbBlock)
if tx.Error != nil {
return tx.Error
}

blk, err := chain.UnmarshalBlock(dbBlock.Bytes, blockParser)
if err != nil {
return err
}

firstBlock = blk.Hght
// Handle hash parameter
// ...
} else {
firstBlock = 1
}

Prev := types.BlockInfo{}
if firstBlock > 1 {
dbBlock := DBBlock{
BlockId: "",
Height: firstBlock - 1,
}
tx := oa.db.Last(dbBlock)
if tx.Error != nil {
return tx.Error
}

blk, err := chain.UnmarshalBlock(dbBlock.Bytes, blockParser)
if err != nil {
return err
}

Prev = types.BlockInfo{
BlockId: dbBlock.BlockId,
Timestamp: blk.Tmstmp,
L1Head: uint64(blk.L1Head),
Height: blk.Hght,
}
}

var blocks []types.BlockInfo

if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Height >= ? AND Timestamp < ? ORDER BY Height", firstBlock, args.End).Scan(&blocks).Error; err != nil {
return err
}

var Next types.BlockInfo

if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Timestamp >= ? ORDER BY Height LIMIT 1", args.End).Scan(&Next).Error; err != nil {
return err
}

reply.From = firstBlock
reply.Blocks = blocks
reply.Prev = Prev
reply.Next = Next

return nil
}

func (oa *ORMArchiver) GetByStart(args *types.GetBlockHeadersByStartArgs, reply *types.BlockHeadersResponse, blockParser chain.Parser) error {

var firstBlock uint64

Prev := types.BlockInfo{}

//TODO check if this works
dbBlock := DBBlock{
Timestamp: args.Start,
}
tx := oa.db.Last(dbBlock)
if tx.Error != nil {
return tx.Error
}

blk, err := chain.UnmarshalBlock(dbBlock.Bytes, blockParser)
if err != nil {
return err
}

firstBlock = blk.Hght

if firstBlock > 1 {
dbBlock := DBBlock{
BlockId: "",
Height: firstBlock - 1,
}
tx := oa.db.Last(dbBlock)
if tx.Error != nil {
return tx.Error
}

blk, err := chain.UnmarshalBlock(dbBlock.Bytes, blockParser)
if err != nil {
return err
}

Prev = types.BlockInfo{
BlockId: dbBlock.BlockId,
Timestamp: blk.Tmstmp,
L1Head: uint64(blk.L1Head),
Height: blk.Hght,
}
}

var blocks []types.BlockInfo

if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Height >= ? AND Timestamp < ? ORDER BY Height", firstBlock, args.End).Scan(&blocks).Error; err != nil {
return err
}

var Next types.BlockInfo

if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Timestamp >= ? ORDER BY Height LIMIT 1", args.End).Scan(&Next).Error; err != nil {
return err
}

reply.From = firstBlock
reply.Blocks = blocks
reply.Prev = Prev
reply.Next = Next

return nil
}
3 changes: 1 addition & 2 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,11 @@ func (c *Controller) Submit(
return c.inner.Submit(ctx, verifySig, txs)
}

// TODO I can add the blocks to the JSON RPC Server here instead of REST API
func (c *Controller) Accepted(ctx context.Context, blk *chain.StatelessBlock) error {
batch := c.metaDB.NewBatch()
defer batch.Reset()

err := c.archiver.InsertBlock(blk.StatefulBlock)
err := c.archiver.InsertBlock(blk)
if err != nil {
c.inner.Logger().Warn("archiving block failed", zap.Error(err))
}
Expand Down
25 changes: 24 additions & 1 deletion controller/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/AnomalyFi/nodekit-seq/archiver"
"github.com/AnomalyFi/nodekit-seq/genesis"
"github.com/AnomalyFi/nodekit-seq/storage"
"github.com/AnomalyFi/nodekit-seq/types"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/trace"
"github.com/ava-labs/avalanchego/utils/logging"
Expand Down Expand Up @@ -61,6 +62,28 @@ func (c *Controller) GetLoanFromState(
func (c *Controller) GetBlockFromArchiver(
ctx context.Context,
dbBlock *archiver.DBBlock,
) (*chain.StatefulBlock, error) {
) (*chain.StatefulBlock, *ids.ID, error) {
return c.archiver.GetBlock(dbBlock, c.inner)
}

func (c *Controller) GetByHeight(
height uint64,
end int64,
reply *types.BlockHeadersResponse,
) error {
return c.archiver.GetByHeight(height, end, c.inner, reply)
}

func (c *Controller) GetByID(
args *types.GetBlockHeadersIDArgs,
reply *types.BlockHeadersResponse,
) error {
return c.archiver.GetByID(args, reply, c.inner)
}

func (c *Controller) GetByStart(
args *types.GetBlockHeadersByStartArgs,
reply *types.BlockHeadersResponse,
) error {
return c.archiver.GetByStart(args, reply, c.inner)
}
Loading

0 comments on commit f7bd3ec

Please sign in to comment.