Skip to content

Commit

Permalink
create type for block with tx hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
cicr99 committed Sep 22, 2023
1 parent 9d83cc2 commit 72cf46a
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 133 deletions.
4 changes: 2 additions & 2 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func WithBlockTag(tag string) BlockID {

// BlockWithTxHashes gets block information given the block id.
func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID) (interface{}, error) {
var result Block
var result BlockTxHashes
if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil {
if errors.Is(err, errNotFound) {
return nil, ErrBlockNotFound
Expand All @@ -61,7 +61,7 @@ func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID

// if header.Hash == nil it's a pending block
if result.BlockHeader.BlockHash == nil {
return PendingBlock{
return PendingBlockTxHashes{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress,
Expand Down
105 changes: 59 additions & 46 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func TestBlockWithTxHashes(t *testing.T) {
type testSetType struct {
BlockID BlockID
ExpectedError error
ExpectedBlockWithTxHashes *Block
ExpectedPendingBlockWithTxHashes *PendingBlock
ExpectedBlockWithTxHashes *BlockTxHashes
ExpectedPendingBlockWithTxHashes *PendingBlockTxHashes
}

var blockGoerli310370 = Block{
var blockGoerli310370 = BlockTxHashes{
BlockHeader: BlockHeader{
BlockHash: utils.TestHexToFelt(t, "0x6c2fe3db009a2e008c2d65fca14204f3405cb74742fcf685f02473acaf70c72"),
ParentHash: utils.TestHexToFelt(t, "0x1ce6fa8ef59dfa1ad8f7ce7c3a4e6752e2d8ae6274f8257345f680e6ae0b5b5"),
Expand All @@ -93,50 +93,60 @@ func TestBlockWithTxHashes(t *testing.T) {
Timestamp: 1661450764,
},
Status: "ACCEPTED_ON_L1",
Transactions: []BlockTransaction{
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x28981b14353a28bc46758dff412ac544d16f2ffc8dde31867855592ea054ab1")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x41176c650076712f1618a141fc1cf9a8c39f0d9548a3458f29cf363310a1e72")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x43cd66f3ddbfbf681ab99bb57bf9d94c83d6e9b586bdbde78ab2deb0328ebd5")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x7602cfebe4f3cb3ef4c8b8c6d7dda2efaf4a500723020066f5db50acd5095cd")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x2612f3f870ee7e7617d4f9efdc41fa8fd571f9720b059b1aa14c1bf15d3a92a")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x1a7810a6c68adf0621ed384d915409c936efa0c9d436683ea0cf7ea171719b")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x26683aeef3e9d9bcc1f0d45a5f0b67d0aa1919726524b2a8dc59504dacfd1f4")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x1d374aa073435cdde1ec1caf972f7c175fd23438bb220848e71720e00fd7474")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0xfc13eabaa2f38981e68bb010370cad7a7d0b65a59101ec816042adca0d6841")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x672d007224128b99bcc145cd3dbd8930a944b6a5fff5c27e3b158a6ff701509")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x24795cbca6d2eba941082cea3f686bc86ef27dd46fdf84b32f9ba25bbeddb28")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x69281a4dd58c260a06b3266554c0cf1a4f19b79d8488efef2a1f003d67506ed")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x62211cc3c94d612b580eb729410e52277f838f962d91af91fb2b0526704c04d")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x5e4128b7680db32de4dff7bc57cb11c9f222752b1f875e84b29785b4c284e2a")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0xdb8ad2b7d008fd2ad7fba4315b193032dee85e17346c80276a2e08c7f09f80")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x67b9541ca879abc29fa24a0fa070285d1899fc044159521c827f6b6aa09bbd6")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x5d9c0ab1d4ed6e9376c8ab45ee02b25dd0adced12941aafe8ce37369d19d9c2")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x4e52da53e23d92d9818908aeb104b007ea24d3cd4a5aa43144d2db1011e314f")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x6cc05f5ab469a3675acb5885c274d5143dca75dd9835c582f59e85ab0642d39")},
TransactionHash{TransactionHash: utils.TestHexToFelt(t, "0x561ed983d1d9c37c964a96f80ccaf3de772e2b73106d6f49dd7c3f7ed8483d9")},
},
Transactions: utils.TestHexArrToFelt(t, []string{
"0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99",
"0x28981b14353a28bc46758dff412ac544d16f2ffc8dde31867855592ea054ab1",
"0x41176c650076712f1618a141fc1cf9a8c39f0d9548a3458f29cf363310a1e72",
"0x43cd66f3ddbfbf681ab99bb57bf9d94c83d6e9b586bdbde78ab2deb0328ebd5",
"0x7602cfebe4f3cb3ef4c8b8c6d7dda2efaf4a500723020066f5db50acd5095cd",
"0x2612f3f870ee7e7617d4f9efdc41fa8fd571f9720b059b1aa14c1bf15d3a92a",
"0x1a7810a6c68adf0621ed384d915409c936efa0c9d436683ea0cf7ea171719b",
"0x26683aeef3e9d9bcc1f0d45a5f0b67d0aa1919726524b2a8dc59504dacfd1f4",
"0x1d374aa073435cdde1ec1caf972f7c175fd23438bb220848e71720e00fd7474",
"0xfc13eabaa2f38981e68bb010370cad7a7d0b65a59101ec816042adca0d6841",
"0x672d007224128b99bcc145cd3dbd8930a944b6a5fff5c27e3b158a6ff701509",
"0x24795cbca6d2eba941082cea3f686bc86ef27dd46fdf84b32f9ba25bbeddb28",
"0x69281a4dd58c260a06b3266554c0cf1a4f19b79d8488efef2a1f003d67506ed",
"0x62211cc3c94d612b580eb729410e52277f838f962d91af91fb2b0526704c04d",
"0x5e4128b7680db32de4dff7bc57cb11c9f222752b1f875e84b29785b4c284e2a",
"0xdb8ad2b7d008fd2ad7fba4315b193032dee85e17346c80276a2e08c7f09f80",
"0x67b9541ca879abc29fa24a0fa070285d1899fc044159521c827f6b6aa09bbd6",
"0x5d9c0ab1d4ed6e9376c8ab45ee02b25dd0adced12941aafe8ce37369d19d9c2",
"0x4e52da53e23d92d9818908aeb104b007ea24d3cd4a5aa43144d2db1011e314f",
"0x6cc05f5ab469a3675acb5885c274d5143dca75dd9835c582f59e85ab0642d39",
"0x561ed983d1d9c37c964a96f80ccaf3de772e2b73106d6f49dd7c3f7ed8483d9",
}),
}

txHashes := utils.TestHexArrToFelt(t, []string{
"0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99",
"0x28981b14353a28bc46758dff412ac544d16f2ffc8dde31867855592ea054ab1",
})

testSet := map[string][]testSetType{
"mock": {{
BlockID: BlockID{Tag: "latest"},
ExpectedPendingBlockWithTxHashes: &PendingBlock{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
"mock": {
{
BlockID: BlockID{Tag: "latest"},
ExpectedPendingBlockWithTxHashes: &PendingBlockTxHashes{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
Transactions: txHashes,
},
},
},
{
BlockID: BlockID{Hash: &felt.Zero},
ExpectedBlockWithTxHashes: &Block{
ExpectedBlockWithTxHashes: &BlockTxHashes{
BlockHeader: BlockHeader{
BlockHash: &felt.Zero,
ParentHash: &felt.Zero,
Timestamp: 123,
Timestamp: 124,
SequencerAddress: &felt.Zero},
Status: BlockStatus_AcceptedOnL1,
Status: BlockStatus_AcceptedOnL1,
Transactions: txHashes,
},
}},
},
},
"testnet": {
{
BlockID: WithBlockTag("latest"),
Expand All @@ -163,11 +173,11 @@ func TestBlockWithTxHashes(t *testing.T) {
if err != test.ExpectedError {
t.Fatal("BlockWithTxHashes match the expected error:", err)
}
switch resultBlock := result.(type) {
case Block:
block, ok := result.(*Block)
switch result.(type) {
case BlockTxHashes:
block, ok := result.(*BlockTxHashes)
if !ok {
t.Fatalf("should return *Block, instead: %T\n", result)
t.Fatalf("should return *BlockTxHashes, instead: %T\n", result)
}
if test.ExpectedError != nil {
continue
Expand All @@ -189,12 +199,15 @@ func TestBlockWithTxHashes(t *testing.T) {
t.Fatalf("the expected transaction blocks to match, instead: %s", cmp.Diff(test.ExpectedBlockWithTxHashes, block))
}
}
case PendingBlock:
require.Equal(t, resultBlock.ParentHash, test.ExpectedPendingBlockWithTxHashes.ParentHash, "Error in PendingBlock ParentHash")
require.Equal(t, resultBlock.SequencerAddress, test.ExpectedPendingBlockWithTxHashes.SequencerAddress, "Error in PendingBlock SequencerAddress")
require.Equal(t, resultBlock.Timestamp, test.ExpectedPendingBlockWithTxHashes.Timestamp, "Error in PendingBlock Timestamp")
case PendingBlockTxHashes:
pBlock, ok := result.(PendingBlockTxHashes)
if !ok {
t.Fatalf("should return *PendingBlockTxHashes, instead: %T\n", result)
}
if !cmp.Equal(*test.ExpectedPendingBlockWithTxHashes, pBlock) {
t.Fatalf("the expected transaction pending blocks to match, instead: %s", cmp.Diff(test.ExpectedPendingBlockWithTxHashes, pBlock))
}
}

}
}

Expand Down
86 changes: 49 additions & 37 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,46 @@ func (r *rpcMock) Close() {

func (r *rpcMock) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
switch method {
case "starknet_addDeclareTransaction":
return mock_starknet_addDeclareTransaction(result, method, args...)
case "starknet_addInvokeTransaction":
return mock_starknet_addInvokeTransaction(result, method, args...)
case "starknet_blockNumber":
return mock_starknet_blockNumber(result, method, args...)
case "starknet_call":
return mock_starknet_call(result, method, args...)
case "starknet_chainId":
return mock_starknet_chainId(result, method, args...)
case "starknet_syncing":
return mock_starknet_syncing(result, method, args...)
case "starknet_getTransactionByHash":
return mock_starknet_getTransactionByHash(result, method, args...)
case "starknet_getTransactionByBlockIdAndIndex":
return mock_starknet_getTransactionByBlockIdAndIndex(result, method, args...)
case "starknet_estimateFee":
return mock_starknet_estimateFee(result, method, args...)
case "starknet_estimateMessageFee":
return mock_starknet_estimateMessageFee(result, method, args...)
case "starknet_getBlockTransactionCount":
return mock_starknet_getBlockTransactionCount(result, method, args...)
case "starknet_getTransactionReceipt":
return mock_starknet_getTransactionReceipt(result, method, args...)
case "starknet_getBlockWithTxHashes":
return mock_starknet_getBlockWithTxHashes(result, method, args...)
case "starknet_getClass":
return mock_starknet_getClass(result, method, args...)
case "starknet_getClassAt":
return mock_starknet_getClassAt(result, method, args...)
case "starknet_getClassHashAt":
return mock_starknet_getClassHashAt(result, method, args...)
case "starknet_getClass":
return mock_starknet_getClass(result, method, args...)
case "starknet_getEvents":
return mock_starknet_getEvents(result, method, args...)
case "starknet_getNonce":
return mock_starknet_getNonce(result, method, args...)
case "starknet_getStorageAt":
return mock_starknet_getStorageAt(result, method, args...)
case "starknet_getStateUpdate":
return mock_starknet_getStateUpdate(result, method, args...)
case "starknet_call":
return mock_starknet_call(result, method, args...)
case "starknet_addDeclareTransaction":
return mock_starknet_addDeclareTransaction(result, method, args...)
case "starknet_addInvokeTransaction":
return mock_starknet_addInvokeTransaction(result, method, args...)
case "starknet_estimateFee":
return mock_starknet_estimateFee(result, method, args...)
case "starknet_estimateMessageFee":
return mock_starknet_estimateMessageFee(result, method, args...)
case "starknet_getBlockWithTxHashes":
return mock_starknet_getBlockWithTxHashes(result, method, args...)
case "starknet_getStorageAt":
return mock_starknet_getStorageAt(result, method, args...)
case "starknet_getTransactionByBlockIdAndIndex":
return mock_starknet_getTransactionByBlockIdAndIndex(result, method, args...)
case "starknet_getTransactionByHash":
return mock_starknet_getTransactionByHash(result, method, args...)
case "starknet_getTransactionReceipt":
return mock_starknet_getTransactionReceipt(result, method, args...)
case "starknet_syncing":
return mock_starknet_syncing(result, method, args...)
case "starknet_traceBlockTransactions":
return mock_starknet_traceBlockTransactions(result, method, args...)
case "starknet_traceTransaction":
Expand Down Expand Up @@ -626,29 +626,41 @@ func mock_starknet_getBlockWithTxHashes(result interface{}, method string, args
fmt.Printf("args[0] should be BlockID, got %T\n", args[0])
return errWrongArgs
}

txHashes, err := utils.HexArrToFelt([]string{
"0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99",
"0x28981b14353a28bc46758dff412ac544d16f2ffc8dde31867855592ea054ab1",
})
if(err != nil){
return err
}

if blockId.Tag == "latest" {
pBlock, err := json.Marshal(PendingBlock{
pBlock, err := json.Marshal(PendingBlockTxHashes{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
Transactions: txHashes,
})
if err != nil {
return err
}
json.Unmarshal(pBlock, &r)
} else {
block, err := json.Marshal(BlockTxHashes{
BlockHeader: BlockHeader{
BlockHash: &felt.Zero,
ParentHash: &felt.Zero,
Timestamp: 124,
SequencerAddress: &felt.Zero},
Status: BlockStatus_AcceptedOnL1,
Transactions: txHashes,
})
if err != nil {
return err
}
json.Unmarshal(block, &r)
}
block, err := json.Marshal(Block{
BlockHeader: BlockHeader{
BlockHash: &felt.Zero,
ParentHash: &felt.Zero,
Timestamp: 124,
SequencerAddress: &felt.Zero},
Status: BlockStatus_AcceptedOnL1,
})
if err != nil {
return err
}
json.Unmarshal(block, &r)

return nil
}
Expand Down
39 changes: 19 additions & 20 deletions rpc/type_block_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var _ BlockTransaction = BlockDeclareTxnV2{}
var _ BlockTransaction = BlockDeployTxn{}
var _ BlockTransaction = BlockDeployAccountTxn{}
var _ BlockTransaction = BlockL1HandlerTxn{}
var _ BlockTransaction = TransactionHash{}

func (tx BlockInvokeTxnV0) Hash() *felt.Felt {
return tx.TransactionHash
Expand Down Expand Up @@ -91,29 +90,29 @@ type BlockDeployAccountTxn struct {
DeployAccountTxn
}

type TransactionHash struct {
TransactionHash *felt.Felt `json:"transaction_hash"`
}
// type TransactionHash struct {
// TransactionHash *felt.Felt `json:"transaction_hash"`
// }

func (t TransactionHash) Hash() *felt.Felt {
return t.TransactionHash
}
// func (t TransactionHash) Hash() *felt.Felt {
// return t.TransactionHash
// }

func (t *TransactionHash) UnmarshalJSON(input []byte) error {
return t.TransactionHash.UnmarshalJSON(input)
}
// func (t *TransactionHash) UnmarshalJSON(input []byte) error {
// return t.TransactionHash.UnmarshalJSON(input)
// }

func (t TransactionHash) MarshalJSON() ([]byte, error) {
return t.TransactionHash.MarshalJSON()
}
// func (t TransactionHash) MarshalJSON() ([]byte, error) {
// return t.TransactionHash.MarshalJSON()
// }

func (t TransactionHash) MarshalText() ([]byte, error) {
return t.TransactionHash.MarshalJSON()
}
// func (t TransactionHash) MarshalText() ([]byte, error) {
// return t.TransactionHash.MarshalJSON()
// }

func (t *TransactionHash) UnmarshalText(input []byte) error {
return t.TransactionHash.UnmarshalJSON(input)
}
// func (t *TransactionHash) UnmarshalText(input []byte) error {
// return t.TransactionHash.UnmarshalJSON(input)
// }

func (txns *BlockTransactions) UnmarshalJSON(data []byte) error {
var dec []interface{}
Expand Down Expand Up @@ -182,4 +181,4 @@ func unmarshalBlockTxn(t interface{}) (BlockTransaction, error) {
}

return nil, fmt.Errorf("unknown transaction type: %v", t)
}
}
22 changes: 20 additions & 2 deletions rpc/types_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (bs BlockStatus) MarshalJSON() ([]byte, error) {
type Block struct {
BlockHeader
Status BlockStatus `json:"status"`
// Transactions The hashes of the transactions included in this block
// Transactions The transactions in this block
Transactions BlockTransactions `json:"transactions"`
}

Expand All @@ -95,10 +95,28 @@ type PendingBlock struct {
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress *felt.Felt `json:"sequencer_address"`
// Transactions The hashes of the transactions included in this block
// Transactions The transactions in this block
Transactions BlockTransactions `json:"transactions"`
}

type BlockTxHashes struct {
BlockHeader
Status BlockStatus `json:"status"`
// Transactions The hashes of the transactions included in this block
Transactions []*felt.Felt `json:"transactions"`
}

type PendingBlockTxHashes struct {
// ParentHash The hash of this block's parent
ParentHash *felt.Felt `json:"parent_hash"`
// Timestamp the time in which the block was created, encoded in Unix time
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress *felt.Felt `json:"sequencer_address"`
// Transactions The hashes of the transactions included in this block
Transactions []*felt.Felt `json:"transactions"`
}

type BlockHeader struct {
// BlockHash The hash of this block
BlockHash *felt.Felt `json:"block_hash"`
Expand Down
Loading

0 comments on commit 72cf46a

Please sign in to comment.