From 54db8f23e07488805d416406c93934d4eeba9b72 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Wed, 22 Sep 2021 16:14:35 +0530 Subject: [PATCH] Refactor the code --- pkg/eth/api.go | 3 --- pkg/eth/eth_state_test.go | 8 ++++---- pkg/eth/ipld_retriever.go | 10 ++++++---- pkg/graphql/graphql.go | 7 +++++-- test/integration_test.go | 8 ++++---- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/pkg/eth/api.go b/pkg/eth/api.go index 2c0ca3ab..e9e9c922 100644 --- a/pkg/eth/api.go +++ b/pkg/eth/api.go @@ -709,9 +709,6 @@ func (pea *PublicEthAPI) localGetBalance(ctx context.Context, address common.Add func (pea *PublicEthAPI) GetStorageAt(ctx context.Context, address common.Address, key string, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) { storageVal, err := pea.B.GetStorageByNumberOrHash(ctx, address, common.HexToHash(key), blockNrOrHash) if storageVal != nil && err == nil { - if len(storageVal) == 0 { - return make([]byte, 32), nil - } var value common.Hash _, content, _, err := rlp.Split(storageVal) if err == io.ErrUnexpectedEOF { diff --git a/pkg/eth/eth_state_test.go b/pkg/eth/eth_state_test.go index 89099c5a..6ff70c25 100644 --- a/pkg/eth/eth_state_test.go +++ b/pkg/eth/eth_state_test.go @@ -476,16 +476,16 @@ var _ = Describe("eth state reading tests", func() { It("Returns empty slice if it tries to access a contract which does not exist", func() { storage, err := api.GetStorageAt(ctx, test_helpers.ContractAddr, test_helpers.ContractSlotKeyHash.Hex(), rpc.BlockNumberOrHashWithNumber(0)) Expect(err).NotTo(HaveOccurred()) - Expect(storage).To(Equal(hexutil.Bytes(make([]byte, 32)))) + Expect(storage).To(Equal(hexutil.Bytes(eth.EmptyNodeValue))) storage, err = api.GetStorageAt(ctx, test_helpers.ContractAddr, test_helpers.ContractSlotKeyHash.Hex(), rpc.BlockNumberOrHashWithNumber(1)) Expect(err).NotTo(HaveOccurred()) - Expect(storage).To(Equal(hexutil.Bytes(make([]byte, 32)))) + Expect(storage).To(Equal(hexutil.Bytes(eth.EmptyNodeValue))) }) It("Returns empty slice if it tries to access a contract slot which does not exist", func() { storage, err := api.GetStorageAt(ctx, test_helpers.ContractAddr, randomHash.Hex(), rpc.BlockNumberOrHashWithNumber(2)) Expect(err).NotTo(HaveOccurred()) - Expect(storage).To(Equal(hexutil.Bytes(make([]byte, 32)))) + Expect(storage).To(Equal(hexutil.Bytes(eth.EmptyNodeValue))) }) It("Retrieves the storage value at the provided contract address and storage leaf key at the block with the provided hash or number", func() { // After deployment @@ -506,7 +506,7 @@ var _ = Describe("eth state reading tests", func() { val, err = api.GetStorageAt(ctx, test_helpers.ContractAddr, test_helpers.IndexOne, rpc.BlockNumberOrHashWithNumber(5)) Expect(err).ToNot(HaveOccurred()) - Expect(val).To(Equal(hexutil.Bytes(make([]byte, 32)))) + Expect(val).To(Equal(hexutil.Bytes(eth.EmptyNodeValue))) }) It("Throws an error for a non-existing block hash", func() { _, err := api.GetStorageAt(ctx, test_helpers.ContractAddr, test_helpers.IndexOne, rpc.BlockNumberOrHashWithHash(randomHash, true)) diff --git a/pkg/eth/ipld_retriever.go b/pkg/eth/ipld_retriever.go index 03be27c1..b28fb9dc 100644 --- a/pkg/eth/ipld_retriever.go +++ b/pkg/eth/ipld_retriever.go @@ -152,6 +152,8 @@ const ( LIMIT 1` ) +var EmptyNodeValue = make([]byte, common.HashLength) + type rctIpldResult struct { LeafCID string `db:"leaf_cid"` Data []byte `db:"data"` @@ -440,7 +442,7 @@ func (r *IPLDRetriever) RetrieveAccountByAddressAndBlockHash(address common.Addr } if accountResult.NodeType == removedNode { - return "", []byte{}, nil + return "", EmptyNodeValue, nil } var i []interface{} @@ -463,7 +465,7 @@ func (r *IPLDRetriever) RetrieveAccountByAddressAndBlockNumber(address common.Ad } if accountResult.NodeType == removedNode { - return "", []byte{}, nil + return "", EmptyNodeValue, nil } var i []interface{} @@ -485,7 +487,7 @@ func (r *IPLDRetriever) RetrieveStorageAtByAddressAndStorageSlotAndBlockHash(add return "", nil, nil, err } if storageResult.NodeType == removedNode { - return "", []byte{}, []byte{}, nil + return "", EmptyNodeValue, EmptyNodeValue, nil } var i []interface{} if err := rlp.DecodeBytes(storageResult.Data, &i); err != nil { @@ -508,7 +510,7 @@ func (r *IPLDRetriever) RetrieveStorageAtByAddressAndStorageKeyAndBlockNumber(ad } if storageResult.NodeType == removedNode { - return "", []byte{}, nil + return "", EmptyNodeValue, nil } var i []interface{} if err := rlp.DecodeBytes(storageResult.Data, &i); err != nil { diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 561abc29..1f36ef33 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -18,6 +18,7 @@ package graphql import ( + "bytes" "context" "database/sql" "errors" @@ -31,6 +32,7 @@ import ( "github.com/ethereum/go-ethereum/eth/filters" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + "github.com/vulcanize/ipld-eth-server/pkg/eth" ) @@ -1010,8 +1012,9 @@ func (r *Resolver) GetStorageAt(ctx context.Context, args struct { return nil, err } - if len(rlpValue) == 0 { - return &StorageResult{value: make([]byte, 32), cid: cid, ipldBlock: ipldBlock}, nil + + if bytes.Compare(rlpValue, eth.EmptyNodeValue) == 0 { + return &StorageResult{value: eth.EmptyNodeValue, cid: cid, ipldBlock: ipldBlock}, nil } var value interface{} diff --git a/test/integration_test.go b/test/integration_test.go index 38f1d73f..f2663568 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -9,12 +9,13 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rlp" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - integration "github.com/vulcanize/ipld-eth-server/test" - "github.com/ethereum/go-ethereum/ethclient" + "github.com/vulcanize/ipld-eth-server/pkg/eth" + integration "github.com/vulcanize/ipld-eth-server/test" ) const nonExistingBlockHash = "0x111111111111111111111111111111111111111111111111111111111111111" @@ -394,7 +395,6 @@ var _ = Describe("Integration test", func() { It("get storage after selfdestruct", func() { totalSupplyIndex := "0x2" - zeroHash := make([]byte, 32) tx, err := integration.DestoyContract(contract.Address) Expect(err).ToNot(HaveOccurred()) @@ -405,7 +405,7 @@ var _ = Describe("Integration test", func() { Expect(err).ToNot(HaveOccurred()) Expect(gethStorage1).NotTo(Equal(gethStorage2)) - Expect(gethStorage2).To(Equal(zeroHash)) + Expect(gethStorage2).To(Equal(eth.EmptyNodeValue)) ipldStorage1, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract.Address), common.HexToHash(totalSupplyIndex), big.NewInt(tx.BlockNumber-1)) Expect(err).ToNot(HaveOccurred())