-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement BlockSnapshotProvider
& BlockSnapshot
interfaces
#632
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,112 @@ | ||||||||||||
package replayer | ||||||||||||
|
||||||||||||
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" | ||||||||||||
) | ||||||||||||
|
||||||||||||
type blockSnapshot struct { | ||||||||||||
*BlocksProvider | ||||||||||||
block models.Block | ||||||||||||
} | ||||||||||||
|
||||||||||||
var _ evmTypes.BlockSnapshot = (*blockSnapshot)(nil) | ||||||||||||
|
||||||||||||
func (bs *blockSnapshot) BlockContext() (evmTypes.BlockContext, error) { | ||||||||||||
return evmTypes.BlockContext{ | ||||||||||||
ChainID: evmTypes.EVMChainIDFromFlowChainID(bs.chainID), | ||||||||||||
BlockNumber: bs.block.Height, | ||||||||||||
BlockTimestamp: bs.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 >= bs.block.Height { | ||||||||||||
return gethCommon.Hash{} | ||||||||||||
} | ||||||||||||
// If the given block height, is more than 256 blocks | ||||||||||||
// in the past, return an empty block hash. | ||||||||||||
if bs.block.Height-n > 256 { | ||||||||||||
return gethCommon.Hash{} | ||||||||||||
} | ||||||||||||
Comment on lines
+35
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remove this line, then the relevant test case fails with: Not equal:
expected: 0x0000000000000000000000000000000000000000000000000000000000000000
actual : 0x5bc86e647028e549fd4e95bd845fee4ea79f4e32eef71ebe2d0aa5cf3dc64985 That's because
An EVM transaction, however, when it is being replayed by the EVM Gateway, it should have access only to the last 256 blocks. Hence this condition in required in the |
||||||||||||
|
||||||||||||
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: bs.block.PrevRandao, | ||||||||||||
Tracer: bs.tracer, | ||||||||||||
}, nil | ||||||||||||
} | ||||||||||||
|
||||||||||||
type BlocksProvider struct { | ||||||||||||
blocks storage.BlockIndexer | ||||||||||||
chainID flowGo.ChainID | ||||||||||||
tracer *tracers.Tracer | ||||||||||||
latestBlock *models.Block | ||||||||||||
} | ||||||||||||
|
||||||||||||
var _ evmTypes.BlockSnapshotProvider = (*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 bp.latestBlock != nil && bp.latestBlock.Height != (block.Height-1) { | ||||||||||||
return fmt.Errorf( | ||||||||||||
"received new block: %d, non-sequential of latest block: %d", | ||||||||||||
block.Height, | ||||||||||||
bp.latestBlock.Height, | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
bp.latestBlock = block | ||||||||||||
|
||||||||||||
return nil | ||||||||||||
} | ||||||||||||
|
||||||||||||
func (bp *BlocksProvider) GetSnapshotAt(height uint64) ( | ||||||||||||
evmTypes.BlockSnapshot, | ||||||||||||
error, | ||||||||||||
) { | ||||||||||||
if bp.latestBlock != nil && bp.latestBlock.Height == height { | ||||||||||||
return &blockSnapshot{ | ||||||||||||
BlocksProvider: bp, | ||||||||||||
block: *bp.latestBlock, | ||||||||||||
}, nil | ||||||||||||
} | ||||||||||||
|
||||||||||||
block, err := bp.blocks.GetByHeight(height) | ||||||||||||
if err != nil { | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
|
||||||||||||
return &blockSnapshot{ | ||||||||||||
BlocksProvider: bp, | ||||||||||||
block: *block, | ||||||||||||
}, nil | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we read the latest block and initialize the blocks provider with it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #632 (comment).
The
latestBlock
field ofBlocksProvider
is not the latest indexed block on the EVM Gateway's DB.It is the latest block the event ingestion engine has received, and the one that should be replayed. If no errors or state mismatches occur, this block will be saved in the EVM Gateway's DB.