Skip to content

Commit

Permalink
blockWithTxHashes RPC (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawliet-Chan authored Apr 16, 2024
1 parent acf4b07 commit c8431d8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The genesis configs of Itachi chain is same as Madara. You can learn more detail
- [x] getClassAt
- [x] getClassHashAt
- [ ] blockHashAndNumber
- [ ] getBlockWithTxHashes
- [ ] getBlockWithTxs
- [x] chainId
- [ ] syncing
Expand Down Expand Up @@ -93,6 +94,7 @@ The genesis configs of Itachi chain is same as Madara. You can learn more detail
- [x] getClassAt
- [x] getClassHashAt
- [ ] blockHashAndNumber
- [x] getBlockWithTxHashes
- [x] getBlockWithTxs
- [x] chainId
- [ ] syncing
Expand Down
87 changes: 69 additions & 18 deletions cairo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,45 @@ func (c *Cairo) getReceipt(hash felt.Felt) (*rpc.TransactionReceipt, error) {
return starkReceipt, err
}

type BlockWithTxHashesRequest struct {
BlockID BlockID `json:"block_id"`
}

type BlockWithTxHashesResponse struct {
BlockWithTxHashes *rpc.BlockWithTxHashes `json:"block_with_tx_hashes"`
Err *jsonrpc.Error `json:"err"`
}

func (c *Cairo) GetBlockWithTxHashes(ctx *context.ReadContext) {
var br BlockWithTxHashesRequest
err := ctx.BindJson(&br)
if err != nil {
ctx.Json(http.StatusBadRequest, &BlockWithTxsResponse{Err: jsonrpc.Err(jsonrpc.InvalidJSON, err.Error())})
return
}

var compactBlock *types.CompactBlock
compactBlock, err = c.getYuBlock(br.BlockID)
if err != nil {
ctx.Json(http.StatusInternalServerError, &BlockWithTxsResponse{Err: jsonrpc.Err(jsonrpc.InternalError, err.Error())})
return
}

status := rpc.BlockAcceptedL2
if br.BlockID.Pending {
status = rpc.BlockPending
}
txHashes := make([]*felt.Felt, 0)
for _, txHash := range compactBlock.TxnsHashes {
txHashes = append(txHashes, new(felt.Felt).SetBytes(txHash.Bytes()))
}
ctx.JsonOk(&BlockWithTxHashesResponse{BlockWithTxHashes: &rpc.BlockWithTxHashes{
Status: status,
BlockHeader: c.adaptStarkBlockHeader(compactBlock),
TxnHashes: txHashes,
}})
}

type BlockWithTxsRequest struct {
BlockID BlockID `json:"block_id"`
}
Expand All @@ -119,12 +158,7 @@ func (c *Cairo) GetBlockWithTxs(ctx *context.ReadContext) {
}

var compactBlock *types.CompactBlock
switch {
case br.BlockID.Latest || br.BlockID.Pending:
compactBlock, err = c.Chain.GetEndBlock()
default:
compactBlock, err = c.Chain.GetBlockByHeight(common.BlockNum(br.BlockID.Number))
}
compactBlock, err = c.getYuBlock(br.BlockID)
if err != nil {
ctx.Json(http.StatusInternalServerError, &BlockWithTxsResponse{Err: jsonrpc.Err(jsonrpc.InternalError, err.Error())})
return
Expand All @@ -147,25 +181,42 @@ func (c *Cairo) GetBlockWithTxs(ctx *context.ReadContext) {
starkTxs = append(starkTxs, &txReq.Tx.Transaction)
}

num := uint64(compactBlock.Height)
status := rpc.BlockAcceptedL2
if br.BlockID.Pending {
status = rpc.BlockPending
}
blockWithTxs := &rpc.BlockWithTxs{
Status: rpc.BlockAcceptedL2,
BlockHeader: rpc.BlockHeader{
Hash: new(felt.Felt).SetBytes(compactBlock.Hash.Bytes()),
ParentHash: new(felt.Felt).SetBytes(compactBlock.PrevHash.Bytes()),
Number: &num,
// FIXME
NewRoot: new(felt.Felt).SetBytes(compactBlock.StateRoot.Bytes()),
Timestamp: compactBlock.Timestamp,
SequencerAddress: c.sequencerAddr,
// TODO:L1GasPrice, StarknetVersion
},
Status: status,
BlockHeader: c.adaptStarkBlockHeader(compactBlock),
Transactions: starkTxs,
}

ctx.JsonOk(&BlockWithTxsResponse{BlockWithTxs: blockWithTxs})
}

func (c *Cairo) getYuBlock(id BlockID) (*types.CompactBlock, error) {
switch {
case id.Latest || id.Pending:
return c.Chain.GetEndBlock()
default:
return c.Chain.GetBlockByHeight(common.BlockNum(id.Number))
}
}

func (c *Cairo) adaptStarkBlockHeader(yuBlock *types.CompactBlock) rpc.BlockHeader {
num := uint64(yuBlock.Height)
return rpc.BlockHeader{
Hash: new(felt.Felt).SetBytes(yuBlock.Hash.Bytes()),
ParentHash: new(felt.Felt).SetBytes(yuBlock.PrevHash.Bytes()),
Number: &num,
// FIXME
NewRoot: new(felt.Felt).SetBytes(yuBlock.StateRoot.Bytes()),
Timestamp: yuBlock.Timestamp,
SequencerAddress: c.sequencerAddr,
// TODO:L1GasPrice, StarknetVersion
}
}

type TransactionStatusRequest struct {
Hash felt.Felt `json:"hash"`
}
Expand Down
2 changes: 1 addition & 1 deletion cairo/cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewCairo(cfg *config.Config) *Cairo {
cairo.GetClassHashAt, cairo.GetNonce, cairo.GetStorage,
cairo.GetTransaction, cairo.GetTransactionStatus, cairo.GetReceipt,
cairo.SimulateTransactions,
cairo.GetBlockWithTxs,
cairo.GetBlockWithTxs, cairo.GetBlockWithTxHashes,
)
cairo.SetInit(cairo)
cairo.SetTxnChecker(cairo)
Expand Down
10 changes: 10 additions & 0 deletions starknetrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ func (s *StarknetRPC) GetChainID() (*felt.Felt, *jsonrpc.Error) {
return s.network.ChainID(), nil
}

func (s *StarknetRPC) GetBlockWithTxHashes(id rpc.BlockID) (*rpc.BlockWithTxHashes, *jsonrpc.Error) {
req := &cairo.BlockWithTxHashesRequest{BlockID: cairo.NewFromJunoBlockID(id)}
resp, jsonErr := s.adaptChainRead(req, "GetBlockWithTxHashes")
if jsonErr != nil {
return nil, jsonErr
}
res := resp.DataInterface.(*cairo.BlockWithTxHashesResponse)
return res.BlockWithTxHashes, res.Err
}

func (s *StarknetRPC) GetBlockWithTxs(id rpc.BlockID) (*rpc.BlockWithTxs, *jsonrpc.Error) {
req := &cairo.BlockWithTxsRequest{BlockID: cairo.NewFromJunoBlockID(id)}
resp, jsonErr := s.adaptChainRead(req, "GetBlockWithTxs")
Expand Down
5 changes: 5 additions & 0 deletions starknetrpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func (s *StarknetRPC) Methods() ([]jsonrpc.Method, string) {
Name: "starknet_specVersion",
Handler: s.SpecVersion,
},
{
Name: "starknet_getBlockWithTxHashes",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: s.GetBlockWithTxHashes,
},
{
Name: "starknet_getBlockWithTxs",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Expand Down

0 comments on commit c8431d8

Please sign in to comment.