From a1c13a66a8ed305f25ddf7a5c1efcc3182f31939 Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Fri, 28 May 2021 17:04:07 +0530 Subject: [PATCH 1/2] Include genesis block state diff. --- statediff/service.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/statediff/service.go b/statediff/service.go index 12e6bf9e6..7935c4887 100644 --- a/statediff/service.go +++ b/statediff/service.go @@ -46,6 +46,7 @@ import ( ) const chainEventChanSize = 20000 +const genesisBlockNumber = 0 var writeLoopParams = Params{ IntermediateStateNodes: true, @@ -259,6 +260,18 @@ func (sds *Service) WriteLoop(chainEventCh chan core.ChainEvent) { wg.Wait() } +func (sds *Service) writeGenesisStateDiff(currBlock *types.Block, workerId uint) { + // For genesis block we need to return the entire state trie hence we diff it with an empty trie. + log.Info("Writing state diff", "block height", genesisBlockNumber, "worker", workerId) + err := sds.writeStateDiff(currBlock, common.Hash{}, writeLoopParams) + if err != nil { + log.Error("statediff.Service.WriteLoop: processing error", "block height", + genesisBlockNumber, "error", err.Error(), "worker", workerId) + return + } + statediffMetrics.lastStatediffHeight.Update(genesisBlockNumber) +} + func (sds *Service) writeLoopWorker(params workerParams) { defer params.wg.Done() for { @@ -272,6 +285,12 @@ func (sds *Service) writeLoopWorker(params workerParams) { log.Error("Parent block is nil, skipping this block", "block height", currentBlock.Number()) continue } + + // chainEvent streams block from block 1, but we also need to include data from the genesis block. + if parentBlock.Number().Uint64() == genesisBlockNumber { + sds.writeGenesisStateDiff(parentBlock, params.id) + } + log.Info("Writing state diff", "block height", currentBlock.Number().Uint64(), "worker", params.id) err := sds.writeStateDiff(currentBlock, parentBlock.Root(), writeLoopParams) if err != nil { @@ -310,10 +329,18 @@ func (sds *Service) Loop(chainEventCh chan core.ChainEvent) { } currentBlock := chainEvent.Block parentBlock := sds.BlockCache.getParentBlock(currentBlock, sds.BlockChain) + if parentBlock == nil { log.Error("Parent block is nil, skipping this block", "block height", currentBlock.Number()) continue } + + // chainEvent streams block from block 1, but we also need to include data from the genesis block. + if parentBlock.Number().Uint64() == genesisBlockNumber { + // For genesis block we need to return the entire state trie hence we diff it with an empty trie. + sds.streamStateDiff(parentBlock, common.Hash{}) + } + sds.streamStateDiff(currentBlock, parentBlock.Root()) case err := <-errCh: log.Warn("Error from chain event subscription", "error", err) From ac7fa08255465276483f418ed814ff46366c1cfd Mon Sep 17 00:00:00 2001 From: Arijit Das Date: Tue, 1 Jun 2021 09:31:10 +0530 Subject: [PATCH 2/2] Fix linting issue. --- statediff/indexer/ipfs/ipld/eth_account.go | 2 +- statediff/indexer/ipfs/ipld/eth_account_test.go | 4 ++-- statediff/indexer/ipfs/ipld/eth_header.go | 2 +- statediff/indexer/ipfs/ipld/eth_header_test.go | 8 ++++---- statediff/indexer/ipfs/ipld/eth_receipt.go | 2 +- statediff/indexer/ipfs/ipld/eth_tx.go | 2 +- statediff/indexer/ipfs/ipld/eth_tx_test.go | 8 ++++---- statediff/indexer/ipfs/ipld/eth_tx_trie_test.go | 2 +- statediff/indexer/ipfs/ipld/shared.go | 4 +++- tests/fuzzers/bls12381/bls12381_fuzz.go | 2 +- 10 files changed, 19 insertions(+), 17 deletions(-) diff --git a/statediff/indexer/ipfs/ipld/eth_account.go b/statediff/indexer/ipfs/ipld/eth_account.go index 08e95f81f..bd68968b8 100644 --- a/statediff/indexer/ipfs/ipld/eth_account.go +++ b/statediff/indexer/ipfs/ipld/eth_account.go @@ -111,7 +111,7 @@ func (as *EthAccountSnapshot) Resolve(p []string) (interface{}, []string, error) case "root": return &node.Link{Cid: keccak256ToCid(MEthStorageTrie, as.Root)}, nil, nil default: - return nil, nil, fmt.Errorf("no such link") + return nil, nil, ErrInvalidLink } } diff --git a/statediff/indexer/ipfs/ipld/eth_account_test.go b/statediff/indexer/ipfs/ipld/eth_account_test.go index 77efd8dbe..7a605eb3c 100644 --- a/statediff/indexer/ipfs/ipld/eth_account_test.go +++ b/statediff/indexer/ipfs/ipld/eth_account_test.go @@ -105,7 +105,7 @@ func TestAccountSnapshotResolve(t *testing.T) { if rest != nil { t.Fatal("rest should be nil") } - if err.Error() != fmt.Sprintf("no such link") { + if err != ErrInvalidLink { t.Fatal("wrong error") } } @@ -175,7 +175,7 @@ func TestAccountSnapshotResolveLink(t *testing.T) { if rest != nil { t.Fatal("Expected rest to be nil") } - if err.Error() != "no such link" { + if err != ErrInvalidLink { t.Fatal("Wrong error") } diff --git a/statediff/indexer/ipfs/ipld/eth_header.go b/statediff/indexer/ipfs/ipld/eth_header.go index c5db67efd..5905bdd7e 100644 --- a/statediff/indexer/ipfs/ipld/eth_header.go +++ b/statediff/indexer/ipfs/ipld/eth_header.go @@ -158,7 +158,7 @@ func (b *EthHeader) Resolve(p []string) (interface{}, []string, error) { case "time": return b.Time, nil, nil default: - return nil, nil, fmt.Errorf("no such link") + return nil, nil, ErrInvalidLink } } diff --git a/statediff/indexer/ipfs/ipld/eth_header_test.go b/statediff/indexer/ipfs/ipld/eth_header_test.go index 191c02254..c23b44c71 100644 --- a/statediff/indexer/ipfs/ipld/eth_header_test.go +++ b/statediff/indexer/ipfs/ipld/eth_header_test.go @@ -192,8 +192,8 @@ func TestEthBlockResolveNoSuchLink(t *testing.T) { t.Fatal("Should have failed with unknown field") } - if err.Error() != "no such link" { - t.Fatalf("Wrong error message\r\nexpected %s\r\ngot %s", "no such link", err.Error()) + if err != ErrInvalidLink { + t.Fatalf("Wrong error message\r\nexpected %s\r\ngot %s", ErrInvalidLink, err.Error()) } } @@ -393,8 +393,8 @@ func TestEthBlockResolveLinksBadLink(t *testing.T) { if rest != nil { t.Fatal("Expected rest to be nil") } - if err.Error() != "no such link" { - t.Fatalf("Expected error\r\nexpected %s\r\ngot %s", "no such link", err.Error()) + if err != ErrInvalidLink { + t.Fatalf("Expected error\r\nexpected %s\r\ngot %s", ErrInvalidLink, err) } } diff --git a/statediff/indexer/ipfs/ipld/eth_receipt.go b/statediff/indexer/ipfs/ipld/eth_receipt.go index ae1a43465..822f47345 100644 --- a/statediff/indexer/ipfs/ipld/eth_receipt.go +++ b/statediff/indexer/ipfs/ipld/eth_receipt.go @@ -130,7 +130,7 @@ func (r *EthReceipt) Resolve(p []string) (interface{}, []string, error) { case "gasUsed": return r.GasUsed, nil, nil default: - return nil, nil, fmt.Errorf("no such link") + return nil, nil, ErrInvalidLink } } diff --git a/statediff/indexer/ipfs/ipld/eth_tx.go b/statediff/indexer/ipfs/ipld/eth_tx.go index c4357988e..deb6158bb 100644 --- a/statediff/indexer/ipfs/ipld/eth_tx.go +++ b/statediff/indexer/ipfs/ipld/eth_tx.go @@ -144,7 +144,7 @@ func (t *EthTx) Resolve(p []string) (interface{}, []string, error) { case "value": return hexutil.EncodeBig(t.Value()), nil, nil default: - return nil, nil, fmt.Errorf("no such link") + return nil, nil, ErrInvalidLink } } diff --git a/statediff/indexer/ipfs/ipld/eth_tx_test.go b/statediff/indexer/ipfs/ipld/eth_tx_test.go index 15f01892d..d908c7acf 100644 --- a/statediff/indexer/ipfs/ipld/eth_tx_test.go +++ b/statediff/indexer/ipfs/ipld/eth_tx_test.go @@ -164,8 +164,8 @@ func TestEthTxResolve(t *testing.T) { if rest != nil { t.Fatal("rest should be nil") } - if err.Error() != "no such link" { - t.Fatalf("wrong error\r\nexpected %s\r\ngot %s", "no such link", err.Error()) + if err != ErrInvalidLink { + t.Fatalf("wrong error\r\nexpected %s\r\ngot %s", ErrInvalidLink, err.Error()) } } @@ -245,8 +245,8 @@ func TestEthTxResolveLink(t *testing.T) { if rest != nil { t.Fatal("Expected rest to be nil") } - if err.Error() != "no such link" { - t.Fatalf("Wrong error\r\nexpected %s\r\ngot %s", "no such link", err.Error()) + if err != ErrInvalidLink { + t.Fatalf("Wrong error\r\nexpected %s\r\ngot %s", ErrInvalidLink, err.Error()) } // good case diff --git a/statediff/indexer/ipfs/ipld/eth_tx_trie_test.go b/statediff/indexer/ipfs/ipld/eth_tx_trie_test.go index 97cfd2a4a..e05f4c705 100644 --- a/statediff/indexer/ipfs/ipld/eth_tx_trie_test.go +++ b/statediff/indexer/ipfs/ipld/eth_tx_trie_test.go @@ -216,7 +216,7 @@ func TestTxTrieResolveBranch(t *testing.T) { t.Fatalf("Returned object is not a link") } - for j, _ := range expectedRest { + for j := range expectedRest { if rest[j] != expectedRest[j] { t.Fatalf("Wrong rest of the path returned\r\nexpected %s\r\ngot %s", expectedRest[j], rest[j]) } diff --git a/statediff/indexer/ipfs/ipld/shared.go b/statediff/indexer/ipfs/ipld/shared.go index 37aaf2063..95fbc71c7 100644 --- a/statediff/indexer/ipfs/ipld/shared.go +++ b/statediff/indexer/ipfs/ipld/shared.go @@ -18,6 +18,7 @@ package ipld import ( "bytes" + "errors" "github.com/ipfs/go-cid" mh "github.com/multiformats/go-multihash" @@ -46,7 +47,8 @@ const ( ) var ( - nullHashBytes = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000") + nullHashBytes = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000") + ErrInvalidLink = errors.New("no such link") ) // RawdataToCid takes the desired codec and a slice of bytes diff --git a/tests/fuzzers/bls12381/bls12381_fuzz.go b/tests/fuzzers/bls12381/bls12381_fuzz.go index 298050ad3..c0f452f3e 100644 --- a/tests/fuzzers/bls12381/bls12381_fuzz.go +++ b/tests/fuzzers/bls12381/bls12381_fuzz.go @@ -159,7 +159,7 @@ func FuzzCrossG1MultiExp(data []byte) int { gethPoints = append(gethPoints, new(bls12381.PointG1).Set(kp1)) gnarkPoints = append(gnarkPoints, *cp1) } - if len(gethScalars) == 0{ + if len(gethScalars) == 0 { return 0 } // compute multi exponentiation