-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BlockProvider for block snapshotting
- Loading branch information
Showing
6 changed files
with
559 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package pebble | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/onflow/flow-evm-gateway/models" | ||
"github.com/onflow/flow-evm-gateway/storage" | ||
evmTypes "github.com/onflow/flow-go/fvm/evm/types" | ||
flowGo "github.com/onflow/flow-go/model/flow" | ||
gethCommon "github.com/onflow/go-ethereum/common" | ||
"github.com/onflow/go-ethereum/eth/tracers" | ||
) | ||
|
||
// TODO(m-Peter): Remove ResultsCollector when local state index comes in | ||
type ResultsCollector struct{} | ||
|
||
func (s *ResultsCollector) StorageRegisterUpdates() map[flowGo.RegisterID]flowGo.RegisterValue { | ||
return map[flowGo.RegisterID]flowGo.RegisterValue{} | ||
} | ||
|
||
var _ evmTypes.ReplayResultCollector = (*ResultsCollector)(nil) | ||
|
||
type BlocksProvider struct { | ||
blocks storage.BlockIndexer | ||
chainID flowGo.ChainID | ||
tracer *tracers.Tracer | ||
latestBlock *models.Block | ||
} | ||
|
||
var _ evmTypes.BlockSnapshotProvider = (*BlocksProvider)(nil) | ||
var _ evmTypes.BlockSnapshot = (*BlocksProvider)(nil) | ||
|
||
func NewBlocksProvider( | ||
blocks storage.BlockIndexer, | ||
chainID flowGo.ChainID, | ||
tracer *tracers.Tracer, | ||
) *BlocksProvider { | ||
return &BlocksProvider{ | ||
blocks: blocks, | ||
chainID: chainID, | ||
tracer: tracer, | ||
} | ||
} | ||
|
||
func (bp *BlocksProvider) OnBlockReceived(block *models.Block) error { | ||
if block == nil { | ||
return fmt.Errorf("received nil block") | ||
} | ||
|
||
if bp.latestBlock != nil { | ||
return fmt.Errorf( | ||
"received new block: %d, while still processing latest block: %d", | ||
block.Height, | ||
bp.latestBlock.Height, | ||
) | ||
} | ||
|
||
bp.latestBlock = block | ||
|
||
return nil | ||
} | ||
|
||
func (bp *BlocksProvider) OnBlockExecuted( | ||
height uint64, | ||
resCol evmTypes.ReplayResultCollector, | ||
) error { | ||
if bp.latestBlock == nil { | ||
return fmt.Errorf( | ||
"received block execution for: %d without latest block", | ||
height, | ||
) | ||
} | ||
|
||
if bp.latestBlock.Height != height { | ||
return fmt.Errorf( | ||
"latest block height doesn't match expected: %d, got: %d", | ||
bp.latestBlock.Height, | ||
height, | ||
) | ||
} | ||
|
||
bp.latestBlock = nil | ||
|
||
return nil | ||
} | ||
|
||
func (bp *BlocksProvider) GetSnapshotAt(height uint64) ( | ||
evmTypes.BlockSnapshot, | ||
error, | ||
) { | ||
if bp.latestBlock != nil && bp.latestBlock.Height == height { | ||
return bp, nil | ||
} | ||
|
||
block, err := bp.blocks.GetByHeight(height) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BlocksProvider{ | ||
blocks: bp.blocks, | ||
chainID: bp.chainID, | ||
tracer: bp.tracer, | ||
latestBlock: block, | ||
}, nil | ||
} | ||
|
||
func (bs *BlocksProvider) BlockContext() (evmTypes.BlockContext, error) { | ||
if bs.latestBlock == nil { | ||
return evmTypes.BlockContext{}, fmt.Errorf( | ||
"cannot create block context without latest block", | ||
) | ||
} | ||
|
||
block := bs.latestBlock | ||
return evmTypes.BlockContext{ | ||
ChainID: evmTypes.EVMChainIDFromFlowChainID(bs.chainID), | ||
BlockNumber: block.Height, | ||
BlockTimestamp: block.Timestamp, | ||
DirectCallBaseGasUsage: evmTypes.DefaultDirectCallBaseGasUsage, | ||
DirectCallGasPrice: evmTypes.DefaultDirectCallGasPrice, | ||
GasFeeCollector: evmTypes.CoinbaseAddress, | ||
GetHashFunc: func(n uint64) gethCommon.Hash { | ||
// For block heights greater than or equal to the current, | ||
// return an empty block hash. | ||
if n >= block.Height { | ||
return gethCommon.Hash{} | ||
} | ||
// If the given block height, is more than 256 blocks | ||
// in the past, return an empty block hash. | ||
if block.Height-n > 256 { | ||
return gethCommon.Hash{} | ||
} | ||
|
||
block, err := bs.blocks.GetByHeight(n) | ||
if err != nil { | ||
return gethCommon.Hash{} | ||
} | ||
blockHash, err := block.Hash() | ||
if err != nil { | ||
return gethCommon.Hash{} | ||
} | ||
|
||
return blockHash | ||
}, | ||
Random: block.PrevRandao, | ||
Tracer: bs.tracer, | ||
}, nil | ||
} |
Oops, something went wrong.