Skip to content

Commit

Permalink
mock test
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Feb 7, 2024
1 parent b762cae commit c8bc183
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 65 deletions.
35 changes: 14 additions & 21 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,36 +171,29 @@ func (provider *Provider) BlockWithTxs(ctx context.Context, blockID BlockID) (in

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

resultBytes, err := json.Marshal(result)
if err != nil {
return nil, err
}
fmt.Println(result)
fmt.Println(string(result))

resultMap, ok := result.(map[string]interface{})
if !ok {
return nil, errors.New("result is not a map[string]interface{}")
var block BlockWithReceipts
err := json.Unmarshal(result, &block)
fmt.Println(block)
if err == nil && block.BlockStatus != "" {
// If unmarshalling was successful and the block status is not empty, return the block
return &block, nil
}

// if result.Status == nil it's a pending block
if resultMap["BlockStatus"] == nil {
var pendingBlock PendingBlockWithReceipts
err := json.Unmarshal(resultBytes, &pendingBlock)
if err != nil {
return nil, err
}
return &pendingBlock, nil
}

var block BlockWithReceipts
err = json.Unmarshal(resultBytes, &block)
var pendingBlock PendingBlockWithReceipts
err = json.Unmarshal(result, &pendingBlock)
if err != nil {
// If unmarshalling failed, return the error
return nil, err
}
return &block, nil

// If unmarshalling was successful, return the pending block
return &pendingBlock, nil
}
69 changes: 28 additions & 41 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,51 +981,38 @@ func mock_starknet_getBlockWithReceipts(result interface{}, method string, args
}

if blockId.Tag == "pending" {
fmt.Println("MOCK IN PENDING")
pBlock, err := json.Marshal(
PendingBlockWithReceipts{
PendingBlockHeader{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
},
BlockBodyWithReceipts{},
pBlock := `{
"parent_hash": "1234567890abcdef",
"timestamp": 1633020442,
"sequencer_address": "0123456789abcdef",
"l1_gas_price": {
"price": 0,
"currency": "WEI"
},
)
if err != nil {
return err
}

return json.Unmarshal(pBlock, &r)
"starknet_version": "1.0.0",
"transactions": [ ]
}`
*r = json.RawMessage(pBlock)
} else {
fmt.Println("MOCK IN REAL")
block, err := json.Marshal(
BlockWithReceipts{
BlockStatus: BlockStatus_AcceptedOnL1,
BlockHeader: BlockHeader{
BlockHash: new(felt.Felt).SetUint64(1),
ParentHash: new(felt.Felt).SetUint64(0),
Timestamp: 124,
SequencerAddress: new(felt.Felt).SetUint64(42)},
BlockBodyWithReceipts: BlockBodyWithReceipts{
Transactions: []TransactionWithReceipt{
{
Transaction: InvokeTxnV0{
Type: TransactionType_Invoke,
},
Receipt: InvokeTransactionReceipt{
TransactionHash: new(felt.Felt).SetUint64(1),
},
},
},
},
block := `{
"status": "ACCEPTED_ON_L1",
"block_hash": "1234567890abcdef",
"parent_hash": "abcdef1234567890",
"block_number": 42,
"new_root": "fedcba0987654321",
"timestamp": 1633020442,
"sequencer_address": "0123456789abcdef",
"l1_gas_price": {
"price": 0,
"currency": "WEI"
},
)
if err != nil {
return err
}
"starknet_version": "1.0.0",
"transactions": [
]
}`

return json.Unmarshal(block, &r)
*r = json.RawMessage(block)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion rpc/types_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ type PendingBlock struct {
BlockTransactions
}

// encoding/json doesn't support inlining fields
type BlockWithReceipts struct {
BlockStatus `json:"status"`
BlockStatus string `json:"status"`
BlockHeader
BlockBodyWithReceipts
}
Expand Down
9 changes: 7 additions & 2 deletions rpc/types_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,19 @@ func TestBlockWithReceipts(t *testing.T) {
blockID := BlockID{Tag: "greatest block"}
block, err := testConfig.provider.BlockWithReceipts(ctx, blockID)
require.Nil(t, err)
require.NotNil(t, block)
blockCasted := block.(*BlockWithReceipts)
require.NotNil(t, blockCasted.BlockStatus)
require.NotNil(t, blockCasted.BlockNumber)
require.Equal(t, len(blockCasted.Transactions), 0)
})

t.Run("BlockWithReceipts - pending", func(t *testing.T) {
blockID := BlockID{Tag: "pending"}
block, err := testConfig.provider.BlockWithReceipts(ctx, blockID)
require.Nil(t, err)
require.NotNil(t, block)
blockCasted := block.(*PendingBlockWithReceipts)
require.NotNil(t, blockCasted.ParentHash)
require.Equal(t, len(blockCasted.Transactions), 0)
})

}

0 comments on commit c8bc183

Please sign in to comment.