Skip to content

Commit f2f37f0

Browse files
authored
Merge pull request #77 from vulcanize/genesis-state-diff
Include genesis block state diff.
2 parents 300d43e + a79dfda commit f2f37f0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

statediff/indexer/ipfs/ipld/eth_header_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ func TestEthBlockResolveLinksBadLink(t *testing.T) {
393393
if rest != nil {
394394
t.Fatal("Expected rest to be nil")
395395
}
396+
396397
if err != ErrInvalidLink {
397398
t.Fatalf("Expected error\r\nexpected %s\r\ngot %s", ErrInvalidLink, err.Error())
398399
}

statediff/service.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
)
4747

4848
const chainEventChanSize = 20000
49+
const genesisBlockNumber = 0
4950

5051
var writeLoopParams = Params{
5152
IntermediateStateNodes: true,
@@ -259,6 +260,18 @@ func (sds *Service) WriteLoop(chainEventCh chan core.ChainEvent) {
259260
wg.Wait()
260261
}
261262

263+
func (sds *Service) writeGenesisStateDiff(currBlock *types.Block, workerId uint) {
264+
// For genesis block we need to return the entire state trie hence we diff it with an empty trie.
265+
log.Info("Writing state diff", "block height", genesisBlockNumber, "worker", workerId)
266+
err := sds.writeStateDiff(currBlock, common.Hash{}, writeLoopParams)
267+
if err != nil {
268+
log.Error("statediff.Service.WriteLoop: processing error", "block height",
269+
genesisBlockNumber, "error", err.Error(), "worker", workerId)
270+
return
271+
}
272+
statediffMetrics.lastStatediffHeight.Update(genesisBlockNumber)
273+
}
274+
262275
func (sds *Service) writeLoopWorker(params workerParams) {
263276
defer params.wg.Done()
264277
for {
@@ -272,6 +285,12 @@ func (sds *Service) writeLoopWorker(params workerParams) {
272285
log.Error("Parent block is nil, skipping this block", "block height", currentBlock.Number())
273286
continue
274287
}
288+
289+
// chainEvent streams block from block 1, but we also need to include data from the genesis block.
290+
if parentBlock.Number().Uint64() == genesisBlockNumber {
291+
sds.writeGenesisStateDiff(parentBlock, params.id)
292+
}
293+
275294
log.Info("Writing state diff", "block height", currentBlock.Number().Uint64(), "worker", params.id)
276295
err := sds.writeStateDiff(currentBlock, parentBlock.Root(), writeLoopParams)
277296
if err != nil {
@@ -310,10 +329,18 @@ func (sds *Service) Loop(chainEventCh chan core.ChainEvent) {
310329
}
311330
currentBlock := chainEvent.Block
312331
parentBlock := sds.BlockCache.getParentBlock(currentBlock, sds.BlockChain)
332+
313333
if parentBlock == nil {
314334
log.Error("Parent block is nil, skipping this block", "block height", currentBlock.Number())
315335
continue
316336
}
337+
338+
// chainEvent streams block from block 1, but we also need to include data from the genesis block.
339+
if parentBlock.Number().Uint64() == genesisBlockNumber {
340+
// For genesis block we need to return the entire state trie hence we diff it with an empty trie.
341+
sds.streamStateDiff(parentBlock, common.Hash{})
342+
}
343+
317344
sds.streamStateDiff(currentBlock, parentBlock.Root())
318345
case err := <-errCh:
319346
log.Warn("Error from chain event subscription", "error", err)

0 commit comments

Comments
 (0)