From 820ddd6fed869ff834c8ccc43d4aab20b673849d Mon Sep 17 00:00:00 2001 From: rianhughes Date: Thu, 12 Oct 2023 16:24:16 +0300 Subject: [PATCH 1/5] rpcv05 Update Block header and pending block header --- rpc/types_block.go | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/rpc/types_block.go b/rpc/types_block.go index 143ee34a..3e30a92c 100644 --- a/rpc/types_block.go +++ b/rpc/types_block.go @@ -89,14 +89,8 @@ type Block struct { } type PendingBlock 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 transactions in this block - Transactions BlockTransactions `json:"transactions"` + PendingBlockHeader PendingBlockHeader + BlockTransactions BlockTransactions } type BlockTxHashes struct { @@ -107,14 +101,8 @@ type BlockTxHashes struct { } 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"` + PendingBlockHeader PendingBlockHeader + BlockTxHashes BlockTxHashes } type BlockHeader struct { @@ -130,4 +118,28 @@ type BlockHeader struct { Timestamp uint64 `json:"timestamp"` // SequencerAddress the StarkNet identity of the sequencer submitting this block SequencerAddress *felt.Felt `json:"sequencer_address"` + // The price of l1 gas in the block + L1GasPrice ResourcePrice `json:"l1_gas_price"` + // Semver of the current Starknet protocol + StarknetVersion string `json:"starknet_version"` +} + +type PendingBlockHeader 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"` + // The price of l1 gas in the block + L1GasPrice ResourcePrice `json:"l1_gas_price"` + // Semver of the current Starknet protocol + StarknetVersion string `json:"starknet_version"` +} + +type ResourcePrice struct { + // The price of one unit of the given resource, denominated in strk + PriceInStrk NumAsHex `json:"price_in_strk,omitempty"` + // The price of one unit of the given resource, denominated in wei + PriceInWei string `json:"price_in_wei"` } From d14829171e894c98e9ed8224d2c47c71dcb88ff4 Mon Sep 17 00:00:00 2001 From: rianhughes Date: Fri, 13 Oct 2023 11:06:15 +0300 Subject: [PATCH 2/5] wip --- rpc/block.go | 18 ++++++++++-------- rpc/block_test.go | 13 +++++++------ rpc/mock_test.go | 16 +++++++++------- rpc/types_block.go | 8 ++++---- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/rpc/block.go b/rpc/block.go index 7625d357..72b4c571 100644 --- a/rpc/block.go +++ b/rpc/block.go @@ -62,10 +62,11 @@ func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID // if header.Hash == nil it's a pending block if result.BlockHeader.BlockHash == nil { return &PendingBlockTxHashes{ - ParentHash: result.ParentHash, - Timestamp: result.Timestamp, - SequencerAddress: result.SequencerAddress, - Transactions: result.Transactions, + PendingBlockHeader{ + ParentHash: result.ParentHash, + Timestamp: result.Timestamp, + SequencerAddress: result.SequencerAddress}, + BlockTxHashes{Transactions: result.Transactions}, }, nil } @@ -108,10 +109,11 @@ func (provider *Provider) BlockWithTxs(ctx context.Context, blockID BlockID) (in // if header.Hash == nil it's a pending block if result.BlockHeader.BlockHash == nil { return &PendingBlock{ - ParentHash: result.ParentHash, - Timestamp: result.Timestamp, - SequencerAddress: result.SequencerAddress, - Transactions: result.Transactions, + PendingBlockHeader{ + ParentHash: result.ParentHash, + Timestamp: result.Timestamp, + SequencerAddress: result.SequencerAddress}, + result.Transactions, }, nil } return &result, nil diff --git a/rpc/block_test.go b/rpc/block_test.go index 58beccd2..3c0a4d08 100644 --- a/rpc/block_test.go +++ b/rpc/block_test.go @@ -129,10 +129,11 @@ func TestBlockWithTxHashes(t *testing.T) { { BlockID: BlockID{Tag: "latest"}, ExpectedPendingBlockWithTxHashes: &PendingBlockTxHashes{ - ParentHash: &felt.Zero, - Timestamp: 123, - SequencerAddress: &felt.Zero, - Transactions: txHashes, + PendingBlockHeader{ + ParentHash: &felt.Zero, + Timestamp: 123, + SequencerAddress: &felt.Zero}, + BlockTxHashes{Transactions: txHashes}, }, }, { @@ -166,7 +167,7 @@ func TestBlockWithTxHashes(t *testing.T) { }, "mainnet": {}, }[testEnv] - + for _, test := range testSet { spy := NewSpy(testConfig.provider.c) testConfig.provider.c = spy @@ -207,7 +208,7 @@ func TestBlockWithTxHashes(t *testing.T) { if !ok { t.Fatalf("should return *PendingBlockTxHashes, instead: %T\n", result) } - + require.Equal(t, pBlock.ParentHash, test.ExpectedPendingBlockWithTxHashes.ParentHash, "Error in PendingBlockTxHashes ParentHash") require.Equal(t, pBlock.SequencerAddress, test.ExpectedPendingBlockWithTxHashes.SequencerAddress, "Error in PendingBlockTxHashes SequencerAddress") require.Equal(t, pBlock.Timestamp, test.ExpectedPendingBlockWithTxHashes.Timestamp, "Error in PendingBlockTxHashes Timestamp") diff --git a/rpc/mock_test.go b/rpc/mock_test.go index f96492b5..4b1becb1 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -629,17 +629,19 @@ func mock_starknet_getBlockWithTxHashes(result interface{}, method string, args "0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99", "0x28981b14353a28bc46758dff412ac544d16f2ffc8dde31867855592ea054ab1", }) - if(err != nil){ + if err != nil { return err } if blockId.Tag == "latest" { - pBlock, err := json.Marshal(PendingBlockTxHashes{ - ParentHash: &felt.Zero, - Timestamp: 123, - SequencerAddress: &felt.Zero, - Transactions: txHashes, - }) + pBlock, err := json.Marshal( + PendingBlockTxHashes{ + PendingBlockHeader{ + ParentHash: &felt.Zero, + Timestamp: 123, + SequencerAddress: &felt.Zero}, + BlockTxHashes{Transactions: txHashes}, + }) if err != nil { return err } diff --git a/rpc/types_block.go b/rpc/types_block.go index 3e30a92c..537d8d38 100644 --- a/rpc/types_block.go +++ b/rpc/types_block.go @@ -89,8 +89,8 @@ type Block struct { } type PendingBlock struct { - PendingBlockHeader PendingBlockHeader - BlockTransactions BlockTransactions + PendingBlockHeader + BlockTransactions } type BlockTxHashes struct { @@ -101,8 +101,8 @@ type BlockTxHashes struct { } type PendingBlockTxHashes struct { - PendingBlockHeader PendingBlockHeader - BlockTxHashes BlockTxHashes + PendingBlockHeader + BlockTxHashes } type BlockHeader struct { From fa46b8a12d1425206a8578e8cbcf07f539649c7a Mon Sep 17 00:00:00 2001 From: rianhughes Date: Fri, 13 Oct 2023 11:24:15 +0300 Subject: [PATCH 3/5] update type --- rpc/block.go | 6 ++++-- rpc/block_test.go | 9 ++++----- rpc/client.go | 2 ++ rpc/mock_test.go | 7 +++++-- rpc/types_block.go | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/rpc/block.go b/rpc/block.go index 72b4c571..18b26c86 100644 --- a/rpc/block.go +++ b/rpc/block.go @@ -3,6 +3,7 @@ package rpc import ( "context" "errors" + "fmt" "github.com/NethermindEth/juno/core/felt" ) @@ -53,7 +54,8 @@ func WithBlockTag(tag string) BlockID { func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID) (interface{}, error) { var result BlockTxHashes if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil { - if errors.Is(err, errNotFound) { + fmt.Println("===", err) + if errors.Is(err, ErrBlockNotFound) { return nil, ErrBlockNotFound } return nil, err @@ -66,7 +68,7 @@ func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID ParentHash: result.ParentHash, Timestamp: result.Timestamp, SequencerAddress: result.SequencerAddress}, - BlockTxHashes{Transactions: result.Transactions}, + result.Transactions, }, nil } diff --git a/rpc/block_test.go b/rpc/block_test.go index 3c0a4d08..c9c2d390 100644 --- a/rpc/block_test.go +++ b/rpc/block_test.go @@ -127,13 +127,14 @@ func TestBlockWithTxHashes(t *testing.T) { testSet := map[string][]testSetType{ "mock": { { - BlockID: BlockID{Tag: "latest"}, + BlockID: BlockID{Tag: "latest"}, + ExpectedError: nil, ExpectedPendingBlockWithTxHashes: &PendingBlockTxHashes{ PendingBlockHeader{ ParentHash: &felt.Zero, Timestamp: 123, SequencerAddress: &felt.Zero}, - BlockTxHashes{Transactions: txHashes}, + txHashes, }, }, { @@ -172,9 +173,7 @@ func TestBlockWithTxHashes(t *testing.T) { spy := NewSpy(testConfig.provider.c) testConfig.provider.c = spy result, err := testConfig.provider.BlockWithTxHashes(context.Background(), test.BlockID) - if err != test.ExpectedError { - t.Fatal("BlockWithTxHashes match the expected error:", err) - } + require.Equal(t, test.ExpectedError, err, "Error in BlockWithTxHashes") switch resultType := result.(type) { case *BlockTxHashes: block, ok := result.(*BlockTxHashes) diff --git a/rpc/client.go b/rpc/client.go index 4a49d334..a275ec3f 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -3,6 +3,7 @@ package rpc import ( "context" "encoding/json" + "fmt" ethrpc "github.com/ethereum/go-ethereum/rpc" ) @@ -21,6 +22,7 @@ func do(ctx context.Context, call callCloser, method string, data interface{}, a if len(raw) == 0 { return errNotFound } + fmt.Println(string(raw)) if err := json.Unmarshal(raw, &data); err != nil { return err } diff --git a/rpc/mock_test.go b/rpc/mock_test.go index 4b1becb1..fc19ef95 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -640,12 +640,15 @@ func mock_starknet_getBlockWithTxHashes(result interface{}, method string, args ParentHash: &felt.Zero, Timestamp: 123, SequencerAddress: &felt.Zero}, - BlockTxHashes{Transactions: txHashes}, + txHashes, }) if err != nil { return err } - json.Unmarshal(pBlock, &r) + err = json.Unmarshal(pBlock, &r) + if err != nil { + return err + } } else { blockHash, err := utils.HexToFelt("0xbeef") if err != nil { diff --git a/rpc/types_block.go b/rpc/types_block.go index 537d8d38..277e0097 100644 --- a/rpc/types_block.go +++ b/rpc/types_block.go @@ -102,7 +102,7 @@ type BlockTxHashes struct { type PendingBlockTxHashes struct { PendingBlockHeader - BlockTxHashes + Transactions []*felt.Felt `json:"transactions"` } type BlockHeader struct { From 50a0dcc3c3d5c77711c45a26256c6918117a7e42 Mon Sep 17 00:00:00 2001 From: Carmen Cabrera Date: Wed, 25 Oct 2023 15:52:17 -0400 Subject: [PATCH 4/5] remove unnecessary prints --- rpc/block.go | 7 +------ rpc/client.go | 2 -- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/rpc/block.go b/rpc/block.go index 18b26c86..2479b227 100644 --- a/rpc/block.go +++ b/rpc/block.go @@ -3,7 +3,6 @@ package rpc import ( "context" "errors" - "fmt" "github.com/NethermindEth/juno/core/felt" ) @@ -54,11 +53,7 @@ func WithBlockTag(tag string) BlockID { func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID) (interface{}, error) { var result BlockTxHashes if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil { - fmt.Println("===", err) - if errors.Is(err, ErrBlockNotFound) { - return nil, ErrBlockNotFound - } - return nil, err + return nil, tryUnwrapToRPCErr(err, ErrBlockNotFound) } // if header.Hash == nil it's a pending block diff --git a/rpc/client.go b/rpc/client.go index a275ec3f..4a49d334 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -3,7 +3,6 @@ package rpc import ( "context" "encoding/json" - "fmt" ethrpc "github.com/ethereum/go-ethereum/rpc" ) @@ -22,7 +21,6 @@ func do(ctx context.Context, call callCloser, method string, data interface{}, a if len(raw) == 0 { return errNotFound } - fmt.Println(string(raw)) if err := json.Unmarshal(raw, &data); err != nil { return err } From 23a8933ea19ea9a3a46d54d7bc42fe1c94646a42 Mon Sep 17 00:00:00 2001 From: Carmen Cabrera Date: Wed, 25 Oct 2023 15:52:58 -0400 Subject: [PATCH 5/5] fix PriceInWei property type --- rpc/types_block.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/types_block.go b/rpc/types_block.go index 277e0097..ec2b007c 100644 --- a/rpc/types_block.go +++ b/rpc/types_block.go @@ -141,5 +141,5 @@ type ResourcePrice struct { // The price of one unit of the given resource, denominated in strk PriceInStrk NumAsHex `json:"price_in_strk,omitempty"` // The price of one unit of the given resource, denominated in wei - PriceInWei string `json:"price_in_wei"` + PriceInWei NumAsHex `json:"price_in_wei"` }