@@ -3,13 +3,15 @@ package ingestion
33import (
44 "context"
55 "fmt"
6+ "math/big"
67 "time"
78
89 flowGo "github.com/onflow/flow-go/model/flow"
910
1011 pebbleDB "github.com/cockroachdb/pebble"
1112 gethTypes "github.com/ethereum/go-ethereum/core/types"
1213 gethBAL "github.com/ethereum/go-ethereum/core/types/bal"
14+ gethParams "github.com/ethereum/go-ethereum/params"
1315 "github.com/onflow/flow-go-sdk"
1416 "github.com/rs/zerolog"
1517
@@ -19,6 +21,7 @@ import (
1921 "github.com/onflow/flow-evm-gateway/storage"
2022 "github.com/onflow/flow-evm-gateway/storage/pebble"
2123
24+ "github.com/onflow/flow-go/fvm/evm/emulator"
2225 "github.com/onflow/flow-go/fvm/evm/offchain/sync"
2326)
2427
@@ -54,6 +57,7 @@ type Engine struct {
5457 logsPublisher * models.Publisher [[]* gethTypes.Log ]
5558 collector metrics.Collector
5659 replayerConfig replayer.Config
60+ chainConfig * gethParams.ChainConfig
5761}
5862
5963func NewEventIngestionEngine (
@@ -69,6 +73,7 @@ func NewEventIngestionEngine(
6973 log zerolog.Logger ,
7074 collector metrics.Collector ,
7175 replayerConfig replayer.Config ,
76+ chainID * big.Int ,
7277) * Engine {
7378 log = log .With ().Str ("component" , "ingestion" ).Logger ()
7479
@@ -87,6 +92,7 @@ func NewEventIngestionEngine(
8792 logsPublisher : logsPublisher ,
8893 collector : collector ,
8994 replayerConfig : replayerConfig ,
95+ chainConfig : emulator .MakeChainConfig (chainID ),
9096 }
9197}
9298
@@ -232,9 +238,10 @@ func (e *Engine) indexEvents(events *models.CadenceEvents, batch *pebbleDB.Batch
232238 }
233239
234240 // Step 1: Re-execute all transactions on the latest EVM block
241+ block := events .Block ()
235242
236243 // Step 1.1: Notify the `BlocksProvider` of the newly received EVM block
237- if err := e .blocksProvider .OnBlockReceived (events . Block () ); err != nil {
244+ if err := e .blocksProvider .OnBlockReceived (block ); err != nil {
238245 return err
239246 }
240247
@@ -253,16 +260,20 @@ func (e *Engine) indexEvents(events *models.CadenceEvents, batch *pebbleDB.Batch
253260 blockEvents := events .BlockEventPayload ()
254261 replayResult , txResults , err := replayer .ReplayBlockEvents (events .TxEventPayloads (), blockEvents )
255262 if err != nil {
256- return fmt .Errorf ("failed to replay block on height: %d, with: %w" , events . Block () .Height , err )
263+ return fmt .Errorf ("failed to replay block on height: %d, with: %w" , block .Height , err )
257264 }
258- stateAccessList := gethBAL .NewConstructionBlockAccessList ()
259- for _ , txRes := range txResults {
260- if txRes .StateAccessList != nil {
261- stateAccessList .Merge (txRes .StateAccessList )
265+
266+ // compute the `BlockAccessListHash` field when Amsterdam is activated
267+ if e .chainConfig .IsAmsterdam (new (big.Int ).SetUint64 (block .Height ), block .Timestamp ) {
268+ stateAccessList := gethBAL .NewConstructionBlockAccessList ()
269+ for _ , txRes := range txResults {
270+ if txRes .StateAccessList != nil {
271+ stateAccessList .Merge (txRes .StateAccessList )
272+ }
262273 }
274+ accessListHash := stateAccessList .ToEncodingObj ().Hash ()
275+ block .AccessListHash = & accessListHash
263276 }
264- accessListHash := stateAccessList .ToEncodingObj ().Hash ()
265- events .Block ().AccessListHash = & accessListHash
266277
267278 // Step 2: Write all the necessary changes to each storage
268279
@@ -273,7 +284,7 @@ func (e *Engine) indexEvents(events *models.CadenceEvents, batch *pebbleDB.Batch
273284 batch ,
274285 )
275286 if err != nil {
276- return fmt .Errorf ("failed to store state changes on block: %d" , events . Block () .Height )
287+ return fmt .Errorf ("failed to store state changes on block: %d" , block .Height )
277288 }
278289
279290 // Step 2.2: Write the latest EVM block to `Blocks` storage
@@ -282,11 +293,11 @@ func (e *Engine) indexEvents(events *models.CadenceEvents, batch *pebbleDB.Batch
282293 err = e .indexBlock (
283294 events .CadenceHeight (),
284295 events .CadenceBlockID (),
285- events . Block () ,
296+ block ,
286297 batch ,
287298 )
288299 if err != nil {
289- return fmt .Errorf ("failed to index block %d event: %w" , events . Block () .Height , err )
300+ return fmt .Errorf ("failed to index block %d event: %w" , block .Height , err )
290301 }
291302
292303 // Step 2.3: Write all EVM transactions of the current block,
@@ -304,10 +315,10 @@ func (e *Engine) indexEvents(events *models.CadenceEvents, batch *pebbleDB.Batch
304315 // to `Receipts` storage
305316 err = e .indexReceipts (events .Receipts (), batch )
306317 if err != nil {
307- return fmt .Errorf ("failed to index receipts for block %d event: %w" , events . Block () .Height , err )
318+ return fmt .Errorf ("failed to index receipts for block %d event: %w" , block .Height , err )
308319 }
309320
310- blockCreation := time .Unix (int64 (events . Block () .Timestamp ), 0 )
321+ blockCreation := time .Unix (int64 (block .Timestamp ), 0 )
311322 e .collector .BlockIngestionTime (blockCreation )
312323
313324 return nil
0 commit comments