Skip to content

Commit

Permalink
Commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgnoah committed Feb 5, 2024
1 parent f7bd3ec commit 7fb1285
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 97 deletions.
65 changes: 65 additions & 0 deletions archiver/orm_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package archiver
import (
"encoding/json"
"log"
"math/big"
"strings"

"github.com/AnomalyFi/hypersdk/chain"
Expand Down Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions controller/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions rpc/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ type Controller interface {
args *types.GetBlockHeadersByStartArgs,
reply *types.BlockHeadersResponse,
) error
GetByCommitment(
args *types.GetBlockCommitmentArgs,
reply *types.SequencerWarpBlockResponse,
) error
}
4 changes: 2 additions & 2 deletions rpc/jsonrpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
98 changes: 3 additions & 95 deletions rpc/jsonrpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"encoding/hex"
"fmt"
"math/big"
"net/http"
"time"

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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()

Expand Down
27 changes: 27 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -46,13 +54,32 @@ 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"`
Prev BlockInfo `json:"prev"`
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
Expand Down

0 comments on commit 7fb1285

Please sign in to comment.