Skip to content

Commit b55eb23

Browse files
committed
Calculate BlockAccessListHash only when Amsterdam is activated
1 parent 5f1b67f commit b55eb23

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ jobs:
3535
uses: golangci/golangci-lint-action@v8
3636
with:
3737
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
38-
version: v2.4
38+
version: v2.9.0
3939
args: --timeout=10m

bootstrap/bootstrap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (b *Bootstrap) StartEventIngestion(ctx context.Context) error {
192192
b.logger,
193193
b.collector,
194194
replayerConfig,
195+
b.config.EVMNetworkID,
195196
)
196197

197198
StartEngine(ctx, b.events, l)

services/ingestion/engine.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package ingestion
33
import (
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

5963
func 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

services/ingestion/engine_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func TestSerialBlockIngestion(t *testing.T) {
7474
zerolog.Nop(),
7575
metrics.NopCollector,
7676
defaultReplayerConfig(),
77+
types.FlowEVMPreviewNetChainID,
7778
)
7879

7980
done := make(chan struct{})
@@ -153,6 +154,7 @@ func TestSerialBlockIngestion(t *testing.T) {
153154
zerolog.Nop(),
154155
metrics.NopCollector,
155156
defaultReplayerConfig(),
157+
types.FlowEVMPreviewNetChainID,
156158
)
157159

158160
waitErr := make(chan struct{})
@@ -265,6 +267,7 @@ func TestBlockAndTransactionIngestion(t *testing.T) {
265267
zerolog.Nop(),
266268
metrics.NopCollector,
267269
defaultReplayerConfig(),
270+
types.FlowEVMPreviewNetChainID,
268271
)
269272

270273
done := make(chan struct{})
@@ -364,6 +367,7 @@ func TestBlockAndTransactionIngestion(t *testing.T) {
364367
zerolog.Nop(),
365368
metrics.NopCollector,
366369
defaultReplayerConfig(),
370+
types.FlowEVMPreviewNetChainID,
367371
)
368372

369373
done := make(chan struct{})
@@ -455,6 +459,7 @@ func TestBlockAndTransactionIngestion(t *testing.T) {
455459
zerolog.Nop(),
456460
metrics.NopCollector,
457461
defaultReplayerConfig(),
462+
types.FlowEVMPreviewNetChainID,
458463
)
459464

460465
done := make(chan struct{})

0 commit comments

Comments
 (0)