Skip to content

Commit

Permalink
Update API of requester.BlocksProvider type to avoid concurrency issues
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Jan 8, 2025
1 parent 0d68945 commit da79cd3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
5 changes: 2 additions & 3 deletions api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ func (d *DebugAPI) TraceCall(
blocksProvider := requester.NewBlocksProvider(
d.blocks,
d.config.FlowNetworkID,
)
blocksProvider.SetTracer(tracer)
).WithTracer(tracer)
if config.BlockOverrides != nil {
blocksProvider.SetBlockOverrides(&ethTypes.BlockOverrides{
blocksProvider = blocksProvider.WithBlockOverrides(&ethTypes.BlockOverrides{
Number: config.BlockOverrides.Number,
Time: config.BlockOverrides.Time,
Coinbase: config.BlockOverrides.Coinbase,
Expand Down
6 changes: 0 additions & 6 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {
b.config,
)

blocksProvider := requester.NewBlocksProvider(
b.storages.Blocks,
b.config.FlowNetworkID,
)

accountKeys := make([]*requester.AccountKey, 0)
if !b.config.IndexOnly {
account, err := b.client.GetAccount(ctx, b.config.COAAddress)
Expand Down Expand Up @@ -230,7 +225,6 @@ func (b *Bootstrap) StartAPIServer(ctx context.Context) error {

evm, err := requester.NewEVM(
b.storages.Registers,
blocksProvider,
b.client,
b.config,
b.logger,
Expand Down
7 changes: 7 additions & 0 deletions services/replayer/blocks_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func (bs *blockSnapshot) BlockContext() (evmTypes.BlockContext, error) {
)
}

// This BlocksProvider implementation is used in the EVM events ingestion pipeline.
// The ingestion module notifies the BlocksProvider of incoming EVM blocks, by
// calling the `OnBlockReceived` method. This method guarantees that blocks are
// processed sequentially, and keeps track of the latest block, which is used
// for generating the proper `BlockContext`. This is necessary for replaying
// EVM blocks/transactions locally, and verifying that there are no state
// mismatches.
type BlocksProvider struct {
blocks storage.BlockIndexer
chainID flowGo.ChainID
Expand Down
24 changes: 20 additions & 4 deletions services/requester/blocks_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (bs *blockSnapshot) BlockContext() (evmTypes.BlockContext, error) {
return blockContext, nil
}

// This BlocksProvider implementation is only used for the `eth_call` &
// `debug_traceCall` JSON-RPC endpoints. It accepts optional `Tracer` &
// `BlockOverrides` objects, which are used when constructing the
// `BlockContext` object.
type BlocksProvider struct {
blocks storage.BlockIndexer
chainID flowGo.ChainID
Expand All @@ -84,12 +88,24 @@ func NewBlocksProvider(
}
}

func (bp *BlocksProvider) SetTracer(tracer *tracers.Tracer) {
bp.tracer = tracer
func (bp *BlocksProvider) WithTracer(tracer *tracers.Tracer) *BlocksProvider {
return &BlocksProvider{
blocks: bp.blocks,
chainID: bp.chainID,
tracer: tracer,
blockOverrides: bp.blockOverrides,
}
}

func (bp *BlocksProvider) SetBlockOverrides(blockOverrides *ethTypes.BlockOverrides) {
bp.blockOverrides = blockOverrides
func (bp *BlocksProvider) WithBlockOverrides(
blockOverrides *ethTypes.BlockOverrides,
) *BlocksProvider {
return &BlocksProvider{
blocks: bp.blocks,
chainID: bp.chainID,
tracer: bp.tracer,
blockOverrides: blockOverrides,
}
}

func (bp *BlocksProvider) GetSnapshotAt(height uint64) (
Expand Down
25 changes: 12 additions & 13 deletions services/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,14 @@ type Requester interface {
var _ Requester = &EVM{}

type EVM struct {
registerStore *pebble.RegisterStorage
blocksProvider *BlocksProvider
client *CrossSporkClient
config config.Config
txPool *TxPool
logger zerolog.Logger
blocks storage.BlockIndexer
mux sync.Mutex
keystore *KeyStore
registerStore *pebble.RegisterStorage
client *CrossSporkClient
config config.Config
txPool *TxPool
logger zerolog.Logger
blocks storage.BlockIndexer
mux sync.Mutex
keystore *KeyStore

head *types.Header
evmSigner types.Signer
Expand All @@ -113,7 +112,6 @@ type EVM struct {

func NewEVM(
registerStore *pebble.RegisterStorage,
blocksProvider *BlocksProvider,
client *CrossSporkClient,
config config.Config,
logger zerolog.Logger,
Expand Down Expand Up @@ -168,7 +166,6 @@ func NewEVM(

evm := &EVM{
registerStore: registerStore,
blocksProvider: blocksProvider,
client: client,
config: config,
logger: logger,
Expand Down Expand Up @@ -443,15 +440,17 @@ func (e *EVM) getBlockView(
height uint64,
blockOverrides *ethTypes.BlockOverrides,
) (*query.View, error) {
blocksProvider := NewBlocksProvider(e.blocks, e.config.FlowNetworkID)

if blockOverrides != nil {
e.blocksProvider.SetBlockOverrides(blockOverrides)
blocksProvider = blocksProvider.WithBlockOverrides(blockOverrides)
}

viewProvider := query.NewViewProvider(
e.config.FlowNetworkID,
evm.StorageAccountAddress(e.config.FlowNetworkID),
e.registerStore,
e.blocksProvider,
blocksProvider,
blockGasLimit,
)

Expand Down

0 comments on commit da79cd3

Please sign in to comment.