Skip to content

Commit

Permalink
add getBlockWithTxs RPC (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawliet-Chan authored Apr 16, 2024
1 parent fb014fa commit acf4b07
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 5 deletions.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,36 @@ The genesis configs of Itachi chain is same as Madara. You can learn more detail


## Starknet RPC
#### Compatible Versions:
- 0.5.1
- 0.6.0
#### Compatible RPC methods
### 0.5.1
- [x] addDeclareTransaction
- [x] addDeployAccountTransaction
- [x] addInvokeTransaction
- [x] call
- [x] estimateFee
- [x] getTransactionReceipt
- [x] getTransactionByHash
- [x] getNonce
- [x] getTransactionStatus
- [x] getClass
- [x] getClassAt
- [x] getClassHashAt
- [ ] blockHashAndNumber
- [ ] getBlockWithTxs
- [x] chainId
- [ ] syncing
- [ ] getTransactionByBlockIdAndIndex
- [ ] getBlockTransactionCount
- [ ] estimateMessageFee
- [ ] blockNumber
- [x] specVersion
- [ ] traceTransaction
- [x] simulateTransactions
- [ ] traceBlockTransactions
- [x] getStorageAt
- [ ] getStateUpdate


### 0.6.0
- [x] addDeclareTransaction
- [x] addDeployAccountTransaction
- [x] addInvokeTransaction
Expand All @@ -67,6 +93,7 @@ The genesis configs of Itachi chain is same as Madara. You can learn more detail
- [x] getClassAt
- [x] getClassHashAt
- [ ] blockHashAndNumber
- [x] getBlockWithTxs
- [x] chainId
- [ ] syncing
- [ ] getTransactionByBlockIdAndIndex
Expand Down
67 changes: 67 additions & 0 deletions cairo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/NethermindEth/juno/rpc"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/vm"
"github.com/yu-org/yu/common"
"github.com/yu-org/yu/core/context"
"github.com/yu-org/yu/core/types"
"net/http"
"slices"
)
Expand Down Expand Up @@ -99,6 +101,71 @@ func (c *Cairo) getReceipt(hash felt.Felt) (*rpc.TransactionReceipt, error) {
return starkReceipt, err
}

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

type BlockWithTxsResponse struct {
BlockWithTxs *rpc.BlockWithTxs `json:"block_with_txs"`
Err *jsonrpc.Error `json:"err"`
}

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

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))
}
if err != nil {
ctx.Json(http.StatusInternalServerError, &BlockWithTxsResponse{Err: jsonrpc.Err(jsonrpc.InternalError, err.Error())})
return
}

starkTxs := make([]*rpc.Transaction, 0)
for _, txHash := range compactBlock.TxnsHashes {
var yuTxn *types.SignedTxn
yuTxn, err = c.TxDB.GetTxn(txHash)
if err != nil {
ctx.Json(http.StatusInternalServerError, &BlockWithTxsResponse{Err: jsonrpc.Err(jsonrpc.InternalError, err.Error())})
return
}
txReq := new(TxRequest)
err = yuTxn.BindJson(txReq)
if err != nil {
ctx.Json(http.StatusInternalServerError, &BlockWithTxsResponse{Err: jsonrpc.Err(jsonrpc.InternalError, err.Error())})
return
}
starkTxs = append(starkTxs, &txReq.Tx.Transaction)
}

num := uint64(compactBlock.Height)
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
},
Transactions: starkTxs,
}

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

type TransactionStatusRequest struct {
Hash felt.Felt `json:"hash"`
}
Expand Down
1 change: 1 addition & 0 deletions cairo/cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +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.SetInit(cairo)
cairo.SetTxnChecker(cairo)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/sourcegraph/conc v0.2.0
github.com/stretchr/testify v1.8.4
github.com/yu-org/yu v0.0.0-20240415152650-db2903655297
github.com/yu-org/yu v0.0.0-20240416111537-b95d87718178
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,8 @@ github.com/yu-org/yu v0.0.0-20240320055213-cb9a4b6cf999 h1:S+phH6kSI3Opbhr2AvJMu
github.com/yu-org/yu v0.0.0-20240320055213-cb9a4b6cf999/go.mod h1:W2qfPLdMEmzOymqCFxb3vN+fjZT94CoWPCVPtpgxwdo=
github.com/yu-org/yu v0.0.0-20240415152650-db2903655297 h1:ynCpNLkORUPnIMkfxlAzofOMy0a05G+7FiYlSk1juJQ=
github.com/yu-org/yu v0.0.0-20240415152650-db2903655297/go.mod h1:W2qfPLdMEmzOymqCFxb3vN+fjZT94CoWPCVPtpgxwdo=
github.com/yu-org/yu v0.0.0-20240416111537-b95d87718178 h1:/4JuKSLlegvz48ZUGi2dCrG32zDUT/qFQvutcFiUS34=
github.com/yu-org/yu v0.0.0-20240416111537-b95d87718178/go.mod h1:W2qfPLdMEmzOymqCFxb3vN+fjZT94CoWPCVPtpgxwdo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
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) GetBlockWithTxs(id rpc.BlockID) (*rpc.BlockWithTxs, *jsonrpc.Error) {
req := &cairo.BlockWithTxsRequest{BlockID: cairo.NewFromJunoBlockID(id)}
resp, jsonErr := s.adaptChainRead(req, "GetBlockWithTxs")
if jsonErr != nil {
return nil, jsonErr
}
res := resp.DataInterface.(*cairo.BlockWithTxsResponse)
return res.BlockWithTxs, res.Err
}

func (s *StarknetRPC) AddTransaction(tx rpc.BroadcastedTransaction) (*rpc.AddTxResponse, *jsonrpc.Error) {
return s.addTransaction(tx, false)
}
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_getBlockWithTxs",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: s.GetBlockWithTxs,
},
{
Name: "starknet_addDeployAccountTransaction",
Params: []jsonrpc.Parameter{{Name: "deploy_account_transaction"}},
Expand Down

0 comments on commit acf4b07

Please sign in to comment.