Skip to content

Commit

Permalink
Rianhughes/07 get block with receipts (#517)
Browse files Browse the repository at this point in the history
* implement starknet_getBlockWithReceipts
  • Loading branch information
rianhughes authored Mar 8, 2024
1 parent 4890312 commit 8775066
Show file tree
Hide file tree
Showing 13 changed files with 573 additions and 6 deletions.
4 changes: 4 additions & 0 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ func (account *Account) BlockWithTxs(ctx context.Context, blockID rpc.BlockID) (
return account.provider.BlockWithTxs(ctx, blockID)
}

func (account *Account) BlockWithReceipts(ctx context.Context, blockID rpc.BlockID) (interface{}, *rpc.RPCError) {
return account.provider.BlockWithReceipts(ctx, blockID)
}

// Call is a function that performs a function call on an Account.
//
// Parameters:
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type CasmClassEntryPoint struct {
}

// UnmarshalCasmClass is a function that unmarshals a CasmClass object from a file.
// CASM = Cairo instructions
// CASM = Cairo instructions
//
// It takes a file path as a parameter and returns a pointer to the unmarshaled CasmClass object and an error.
func UnmarshalCasmClass(filePath string) (*CasmClass, error) {
Expand Down
15 changes: 15 additions & 0 deletions mocks/mock_rpc_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
"encoding/json"
"errors"

"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -166,3 +167,32 @@ func (provider *Provider) BlockWithTxs(ctx context.Context, blockID BlockID) (in
}
return &result, nil
}

// Get block information with full transactions and receipts given the block id
func (provider *Provider) BlockWithReceipts(ctx context.Context, blockID BlockID) (interface{}, *RPCError) {
var result json.RawMessage
if err := do(ctx, provider.c, "starknet_getBlockWithReceipts", &result, blockID); err != nil {
return nil, tryUnwrapToRPCErr(err, ErrBlockNotFound)
}

var m map[string]interface{}
if err := json.Unmarshal(result, &m); err != nil {
return nil, Err(InternalError, err.Error())
}

// PendingBlockWithReceipts doesn't contain a "status" field
if _, ok := m["status"]; ok {
var block BlockWithReceipts
if err := json.Unmarshal(result, &block); err != nil {
return nil, Err(InternalError, err.Error())
}
return &block, nil
} else {
var pendingBlock PendingBlockWithReceipts
if err := json.Unmarshal(result, &pendingBlock); err != nil {
return nil, Err(InternalError, err.Error())
}
return &pendingBlock, nil
}

}
4 changes: 2 additions & 2 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func Err(code int, data any) *RPCError {
func tryUnwrapToRPCErr(err error, rpcErrors ...*RPCError) *RPCError {
errBytes, errIn := json.Marshal(err)
if errIn != nil {
return Err(InternalError, errIn)
return Err(InternalError, errIn.Error())
}

var nodeErr RPCError
errIn = json.Unmarshal(errBytes, &nodeErr)
if errIn != nil {
return Err(InternalError, errIn)
return Err(InternalError, errIn.Error())
}

for _, rpcErr := range rpcErrors {
Expand Down
38 changes: 38 additions & 0 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (r *rpcMock) CallContext(ctx context.Context, result interface{}, method st
return mock_starknet_getBlockTransactionCount(result, method, args...)
case "starknet_getBlockWithTxHashes":
return mock_starknet_getBlockWithTxHashes(result, method, args...)
case "starknet_getBlockWithReceipts":
return mock_starknet_getBlockWithReceipts(result, method, args...)
case "starknet_getClass":
return mock_starknet_getClass(result, method, args...)
case "starknet_getClassAt":
Expand Down Expand Up @@ -997,6 +999,42 @@ func mock_starknet_getBlockWithTxHashes(result interface{}, method string, args
return nil
}

func mock_starknet_getBlockWithReceipts(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
return errWrongType
}
if len(args) != 1 {
return errWrongArgs
}
_, ok = args[0].(BlockID)
if !ok {
fmt.Printf("args[0] should be BlockID, got %T\n", args[0])
return errWrongArgs
}

var blockWithReceipts struct {
Result BlockWithReceipts `json:"result"`
}
read, err := os.ReadFile("tests/blockWithReceipts/integration332275.json")

if err != nil {
return err
}

err = json.Unmarshal(read, &blockWithReceipts)
if err != nil {
return err
}

blockWithReceiptsJSON, err := json.Marshal(blockWithReceipts.Result)
if err != nil {
return err
}

return json.Unmarshal(blockWithReceiptsJSON, &r)
}

// mock_starknet_traceBlockTransactions is a function that traces the transactions of a block in the StarkNet network.
// The function first checks the type of the result parameter and returns an error if it is not of type *json.RawMessage.
// It then checks the length of the args parameter and returns an error if it is not equal to 1. Next, it checks the
Expand Down
1 change: 1 addition & 0 deletions rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type RpcProvider interface {
EstimateFee(ctx context.Context, requests []BroadcastTxn, simulationFlags []SimulationFlag, blockID BlockID) ([]FeeEstimate, *RPCError)
EstimateMessageFee(ctx context.Context, msg MsgFromL1, blockID BlockID) (*FeeEstimate, *RPCError)
Events(ctx context.Context, input EventsInput) (*EventChunk, *RPCError)
BlockWithReceipts(ctx context.Context, blockID BlockID) (interface{}, *RPCError)
GetTransactionStatus(ctx context.Context, transactionHash *felt.Felt) (*TxnStatusResp, *RPCError)
Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*felt.Felt, *RPCError)
SimulateTransactions(ctx context.Context, blockID BlockID, txns []Transaction, simulationFlags []SimulationFlag) ([]SimulatedTransaction, *RPCError)
Expand Down
Loading

0 comments on commit 8775066

Please sign in to comment.