@@ -46,6 +46,7 @@ import (
4646)
4747
4848const chainEventChanSize = 20000
49+ const genesisBlockNumber = 0
4950
5051var 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+
262275func (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