From 7fb1285065faf2c3f6bc3e10c5f845f6d0122772 Mon Sep 17 00:00:00 2001 From: mtgnoah Date: Mon, 5 Feb 2024 08:45:43 -0600 Subject: [PATCH] Commitment --- archiver/orm_archiver.go | 65 ++++++++++++++++++++++++++ controller/resolutions.go | 7 +++ rpc/dependencies.go | 4 ++ rpc/jsonrpc_client.go | 4 +- rpc/jsonrpc_server.go | 98 ++------------------------------------- types/types.go | 27 +++++++++++ 6 files changed, 108 insertions(+), 97 deletions(-) diff --git a/archiver/orm_archiver.go b/archiver/orm_archiver.go index e904eb4..63df178 100644 --- a/archiver/orm_archiver.go +++ b/archiver/orm_archiver.go @@ -3,6 +3,7 @@ package archiver import ( "encoding/json" "log" + "math/big" "strings" "github.com/AnomalyFi/hypersdk/chain" @@ -310,3 +311,67 @@ func (oa *ORMArchiver) GetByStart(args *types.GetBlockHeadersByStartArgs, reply return nil } + +func (oa *ORMArchiver) GetByCommitment(args *types.GetBlockCommitmentArgs, reply *types.SequencerWarpBlockResponse, blockParser chain.Parser) error { + + if args.First < 1 { + return nil + } + type BlockInfoWithParent struct { + BlockId string `json:"id"` + Parent string `json:"parent"` + Timestamp int64 `json:"timestamp"` + L1Head uint64 `json:"l1_head"` + Height uint64 `json:"height"` + } + + //TODO check if this works + + var blocks []BlockInfoWithParent + + if err := oa.db.Raw("SELECT BlockId, Timestamp, L1Head, Height FROM DBBlock WHERE Height >= ? AND Height < ? ORDER BY Height LIMIT ?", args.First, args.CurrentHeight, args.MaxBlocks).Scan(&blocks).Error; err != nil { + return err + } + + blocksCommitment := make([]types.SequencerWarpBlock, 0) + + for _, blk := range blocks { + + id, err := ids.FromString(blk.BlockId) + if err != nil { + return err + } + + header := &types.Header{ + Height: blk.Height, + Timestamp: uint64(blk.Timestamp), + L1Head: uint64(blk.L1Head), + TransactionsRoot: types.NmtRoot{ + Root: id[:], + }, + } + + comm := header.Commit() + + idParent, err := ids.FromString(blk.Parent) + if err != nil { + return err + } + + parentRoot := types.NewU256().SetBytes(idParent) + bigParentRoot := parentRoot.Int + + blocksCommitment = append(blocksCommitment, types.SequencerWarpBlock{ + BlockId: id.String(), + Timestamp: blk.Timestamp, + L1Head: uint64(blk.L1Head), + Height: big.NewInt(int64(blk.Height)), + BlockRoot: &comm.Uint256().Int, + ParentRoot: &bigParentRoot, + }) + + } + + reply.Blocks = blocksCommitment + return nil +} diff --git a/controller/resolutions.go b/controller/resolutions.go index 44a29cd..7193b1e 100644 --- a/controller/resolutions.go +++ b/controller/resolutions.go @@ -87,3 +87,10 @@ func (c *Controller) GetByStart( ) error { return c.archiver.GetByStart(args, reply, c.inner) } + +func (c *Controller) GetByCommitment( + args *types.GetBlockCommitmentArgs, + reply *types.SequencerWarpBlockResponse, +) error { + return c.archiver.GetByCommitment(args, reply, c.inner) +} diff --git a/rpc/dependencies.go b/rpc/dependencies.go index 8d33414..0cf7811 100644 --- a/rpc/dependencies.go +++ b/rpc/dependencies.go @@ -45,4 +45,8 @@ type Controller interface { args *types.GetBlockHeadersByStartArgs, reply *types.BlockHeadersResponse, ) error + GetByCommitment( + args *types.GetBlockCommitmentArgs, + reply *types.SequencerWarpBlockResponse, + ) error } diff --git a/rpc/jsonrpc_client.go b/rpc/jsonrpc_client.go index 3eba0b7..a68180b 100644 --- a/rpc/jsonrpc_client.go +++ b/rpc/jsonrpc_client.go @@ -226,8 +226,8 @@ func (cli *JSONRPCClient) GetBlockTransactionsByNamespace( ctx context.Context, height uint64, namespace string, -) (*SEQTransactionResponse, error) { - resp := new(SEQTransactionResponse) +) (*types.SEQTransactionResponse, error) { + resp := new(types.SEQTransactionResponse) // TODO does this need to be lowercase for the string? err := cli.requester.SendRequest( ctx, diff --git a/rpc/jsonrpc_server.go b/rpc/jsonrpc_server.go index 0b456aa..49be1b4 100644 --- a/rpc/jsonrpc_server.go +++ b/rpc/jsonrpc_server.go @@ -8,7 +8,6 @@ import ( "context" "encoding/hex" "fmt" - "math/big" "net/http" "time" @@ -275,37 +274,10 @@ type TransactionResponse struct { BlockId string `json:"id"` } -type SEQTransactionResponse struct { - Txs []*types.SEQTransaction `json:"txs"` - BlockId string `json:"id"` - Timestamp int64 `json:"timestamp"` - L1Head uint64 `json:"l1_head"` - Height uint64 `json:"height"` -} - -type SequencerWarpBlockResponse struct { - Blocks []SequencerWarpBlock `json:"blocks"` -} - -type SequencerWarpBlock struct { - BlockId string `json:"id"` - Timestamp int64 `json:"timestamp"` - L1Head uint64 `json:"l1_head"` - Height *big.Int `json:"height"` - BlockRoot *big.Int `json:"root"` - ParentRoot *big.Int `json:"parent"` -} - type GetBlockTransactionsArgs struct { ID string `json:"block_id"` } -type GetBlockCommitmentArgs struct { - First uint64 `json:"first"` - CurrentHeight uint64 `json:"current_height"` - MaxBlocks int `json:"max_blocks"` -} - type GetBlockTransactionsByNamespaceArgs struct { Height uint64 `json:"height"` Namespace string `json:"namespace"` @@ -346,75 +318,11 @@ func (j *JSONRPCServer) GetBlockTransactions(req *http.Request, args *GetBlockTr return nil } -func (j *JSONRPCServer) GetCommitmentBlocks(req *http.Request, args *GetBlockCommitmentArgs, reply *SequencerWarpBlockResponse) error { - // Parse query parameters - if args.First < 1 { - return nil - } - - blocks := make([]SequencerWarpBlock, 0) - - j.idsByHeight.Ascend(args.First, func(heightKey uint64, id ids.ID) bool { - // Does heightKey match the given block's height for the id - if len(blocks) >= args.MaxBlocks { - return false - } - - blockTemp, success := j.headers.Get(id.String()) - if !success { - return success - } - - header := &types.Header{ - Height: blockTemp.Hght, - Timestamp: uint64(blockTemp.Tmstmp), - L1Head: uint64(blockTemp.L1Head), - TransactionsRoot: types.NmtRoot{ - Root: id[:], - }, - } - - comm := header.Commit() - - //TODO swapped these 2 functions so now it exits earlier. Need to test - if blockTemp.Hght >= args.CurrentHeight { - parentRoot := types.NewU256().SetBytes(blockTemp.Prnt) - bigParentRoot := parentRoot.Int - - blocks = append(blocks, SequencerWarpBlock{ - BlockId: id.String(), - Timestamp: blockTemp.Tmstmp, - L1Head: uint64(blockTemp.L1Head), - Height: big.NewInt(int64(blockTemp.Hght)), - BlockRoot: &comm.Uint256().Int, - ParentRoot: &bigParentRoot, - }) - return false - } - - if blockTemp.Hght == heightKey { - parentRoot := types.NewU256().SetBytes(blockTemp.Prnt) - bigParentRoot := parentRoot.Int - - blocks = append(blocks, SequencerWarpBlock{ - BlockId: id.String(), - Timestamp: blockTemp.Tmstmp, - L1Head: uint64(blockTemp.L1Head), - Height: big.NewInt(int64(blockTemp.Hght)), - BlockRoot: &comm.Uint256().Int, - ParentRoot: &bigParentRoot, - }) - } - - return true - }) - - reply.Blocks = blocks - - return nil +func (j *JSONRPCServer) GetCommitmentBlocks(req *http.Request, args *types.GetBlockCommitmentArgs, reply *types.SequencerWarpBlockResponse) error { + return j.c.GetByCommitment(args, reply) } -func (j *JSONRPCServer) GetBlockTransactionsByNamespace(req *http.Request, args *GetBlockTransactionsByNamespaceArgs, reply *SEQTransactionResponse) error { +func (j *JSONRPCServer) GetBlockTransactionsByNamespace(req *http.Request, args *GetBlockTransactionsByNamespaceArgs, reply *types.SEQTransactionResponse) error { ctx, span := j.c.Tracer().Start(req.Context(), "Server.GetBlockTransactionsByNamespace") defer span.End() diff --git a/types/types.go b/types/types.go index adace21..ad6a4eb 100644 --- a/types/types.go +++ b/types/types.go @@ -16,6 +16,14 @@ type SEQTransaction struct { Transaction []byte `json:"transaction"` } +type SEQTransactionResponse struct { + Txs []*SEQTransaction `json:"txs"` + BlockId string `json:"id"` + Timestamp int64 `json:"timestamp"` + L1Head uint64 `json:"l1_head"` + Height uint64 `json:"height"` +} + type SequencerBlock struct { StateRoot ids.ID `json:"state_root"` Prnt ids.ID `json:"parent"` @@ -46,6 +54,12 @@ type GetBlockHeadersByStartArgs struct { End int64 `json:"end"` } +type GetBlockCommitmentArgs struct { + First uint64 `json:"first"` + CurrentHeight uint64 `json:"current_height"` + MaxBlocks int `json:"max_blocks"` +} + type BlockHeadersResponse struct { From uint64 `json:"from"` Blocks []BlockInfo `json:"blocks"` @@ -53,6 +67,19 @@ type BlockHeadersResponse struct { Next BlockInfo `json:"next"` } +type SequencerWarpBlockResponse struct { + Blocks []SequencerWarpBlock `json:"blocks"` +} + +type SequencerWarpBlock struct { + BlockId string `json:"id"` + Timestamp int64 `json:"timestamp"` + L1Head uint64 `json:"l1_head"` + Height *big.Int `json:"height"` + BlockRoot *big.Int `json:"root"` + ParentRoot *big.Int `json:"parent"` +} + // A BigInt type which serializes to JSON a a hex string. type U256 struct { big.Int