From a784f4c119c91599d638f6454f977d15e01473b6 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 25 Aug 2022 15:31:59 +0530 Subject: [PATCH] Add a spec to test out of order uncles --- go.mod | 2 +- go.sum | 4 ++-- pkg/eth/api_test.go | 24 ++++++++++++++++++++++++ pkg/eth/ipld_retriever.go | 3 ++- pkg/eth/test_helpers/test_data.go | 24 +++++++++++++++++++++++- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 85fb1fbc..6c761ecd 100644 --- a/go.mod +++ b/go.mod @@ -274,7 +274,7 @@ require ( go.uber.org/multierr v1.8.0 // indirect go.uber.org/zap v1.21.0 // indirect go4.org v0.0.0-20200411211856-f5505b9728dd // indirect - golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect + golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect diff --git a/go.sum b/go.sum index 5307c14d..ff0c4da4 100644 --- a/go.sum +++ b/go.sum @@ -1953,8 +1953,8 @@ golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 h1:GIAS/yBem/gq2MUqgNIzUHW7cJMmx3TGZOrnyYaNQ6c= -golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 h1:vJ2V3lFLg+bBhgroYuRfyN583UzVveQmIXjc8T/y3to= +golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= diff --git a/pkg/eth/api_test.go b/pkg/eth/api_test.go index 305c8e52..1dcea155 100644 --- a/pkg/eth/api_test.go +++ b/pkg/eth/api_test.go @@ -334,6 +334,18 @@ var _ = Describe("API", func() { Expect(err).ToNot(HaveOccurred()) Expect(block["baseFee"].(*big.Int)).To(Equal(baseFee)) }) + It("Retrieves a block by number with uncles in correct order", func() { + block, err := api.GetBlockByNumber(ctx, londonBlockNum, false) + Expect(err).ToNot(HaveOccurred()) + + expectedUncles := []common.Hash{ + test_helpers.MockLondonUncles[0].Hash(), + test_helpers.MockLondonUncles[1].Hash(), + } + Expect(block["uncles"]).To(Equal(expectedUncles)) + Expect(block["sha3Uncles"]).To(Equal(test_helpers.MockLondonBlock.UncleHash())) + Expect(block["hash"]).To(Equal(test_helpers.MockLondonBlock.Hash())) + }) }) Describe("eth_getBlockByHash", func() { @@ -375,6 +387,18 @@ var _ = Describe("API", func() { Expect(err).ToNot(HaveOccurred()) Expect(block["baseFee"].(*big.Int)).To(Equal(baseFee)) }) + It("Retrieves a block by hash with uncles in correct order", func() { + block, err := api.GetBlockByHash(ctx, test_helpers.MockLondonBlock.Hash(), false) + Expect(err).ToNot(HaveOccurred()) + + expectedUncles := []common.Hash{ + test_helpers.MockLondonUncles[0].Hash(), + test_helpers.MockLondonUncles[1].Hash(), + } + Expect(block["uncles"]).To(Equal(expectedUncles)) + Expect(block["sha3Uncles"]).To(Equal(test_helpers.MockLondonBlock.UncleHash())) + Expect(block["hash"]).To(Equal(test_helpers.MockLondonBlock.Hash())) + }) }) /* diff --git a/pkg/eth/ipld_retriever.go b/pkg/eth/ipld_retriever.go index 5eaf6144..5786ae0c 100644 --- a/pkg/eth/ipld_retriever.go +++ b/pkg/eth/ipld_retriever.go @@ -74,7 +74,8 @@ const ( uncle_cids.mh_key = blocks.key AND uncle_cids.block_number = blocks.block_number ) - WHERE header_cids.block_hash = $1` + WHERE header_cids.block_hash = $1 + ORDER BY uncle_cids.parent_hash` RetrieveUnclesByBlockNumberPgStr = `SELECT uncle_cids.cid, data FROM eth.uncle_cids INNER JOIN eth.header_cids ON ( diff --git a/pkg/eth/test_helpers/test_data.go b/pkg/eth/test_helpers/test_data.go index d4f7118d..df6200f7 100644 --- a/pkg/eth/test_helpers/test_data.go +++ b/pkg/eth/test_helpers/test_data.go @@ -594,7 +594,29 @@ var ( } MockLondonTransactions, MockLondonReceipts, _ = createDynamicTransactionsAndReceipts(LondonBlockNum) - MockLondonBlock = createNewBlock(&MockLondonHeader, MockLondonTransactions, nil, MockLondonReceipts, new(trie.Trie)) + MockLondonUncles = []*types.Header{ + { + Time: 1, + Number: new(big.Int).Add(BlockNumber, big.NewInt(1)), + ParentHash: common.HexToHash("0x2"), + Root: common.HexToHash("0x1"), + TxHash: common.HexToHash("0x1"), + ReceiptHash: common.HexToHash("0x1"), + Difficulty: big.NewInt(500001), + Extra: []byte{}, + }, + { + Time: 2, + Number: new(big.Int).Add(BlockNumber, big.NewInt(1)), + ParentHash: common.HexToHash("0x1"), + Root: common.HexToHash("0x2"), + TxHash: common.HexToHash("0x2"), + ReceiptHash: common.HexToHash("0x2"), + Difficulty: big.NewInt(500002), + Extra: []byte{}, + }, + } + MockLondonBlock = createNewBlock(&MockLondonHeader, MockLondonTransactions, MockLondonUncles, MockLondonReceipts, new(trie.Trie)) ) func createNewBlock(header *types.Header, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, hasher types.TrieHasher) *types.Block {