-
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
Support block overrides parameter for eth_call
& debug_traceCall
#691
base: main
Are you sure you want to change the base?
Changes from 2 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,124 @@ | ||
package requester | ||
|
||
import ( | ||
ethTypes "github.com/onflow/flow-evm-gateway/eth/types" | ||
"github.com/onflow/flow-evm-gateway/models" | ||
"github.com/onflow/flow-evm-gateway/storage" | ||
"github.com/onflow/flow-go/fvm/evm/offchain/blocks" | ||
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) { | ||
blockContext, err := blocks.NewBlockContext( | ||
bs.chainID, | ||
bs.block.Height, | ||
bs.block.Timestamp, | ||
func(n uint64) 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 | ||
}, | ||
bs.block.PrevRandao, | ||
bs.tracer, | ||
) | ||
if err != nil { | ||
return evmTypes.BlockContext{}, err | ||
} | ||
|
||
if bs.blockOverrides == nil { | ||
return blockContext, nil | ||
} | ||
|
||
if bs.blockOverrides.Number != nil { | ||
blockContext.BlockNumber = bs.blockOverrides.Number.ToInt().Uint64() | ||
} | ||
|
||
if bs.blockOverrides.Time != nil { | ||
blockContext.BlockTimestamp = uint64(*bs.blockOverrides.Time) | ||
} | ||
|
||
if bs.blockOverrides.Random != nil { | ||
blockContext.Random = *bs.blockOverrides.Random | ||
} | ||
|
||
if bs.blockOverrides.Coinbase != nil { | ||
blockContext.GasFeeCollector = evmTypes.NewAddress(*bs.blockOverrides.Coinbase) | ||
} | ||
|
||
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 { | ||
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. Can we rename it to 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. That's a good idea 👍 Renamed the type in 6566fba . |
||
blocks storage.BlockIndexer | ||
chainID flowGo.ChainID | ||
tracer *tracers.Tracer | ||
blockOverrides *ethTypes.BlockOverrides | ||
} | ||
|
||
var _ evmTypes.BlockSnapshotProvider = (*BlocksProvider)(nil) | ||
|
||
func NewBlocksProvider( | ||
blocks storage.BlockIndexer, | ||
chainID flowGo.ChainID, | ||
) *BlocksProvider { | ||
return &BlocksProvider{ | ||
blocks: blocks, | ||
chainID: chainID, | ||
} | ||
} | ||
|
||
func (bp *BlocksProvider) WithTracer(tracer *tracers.Tracer) *BlocksProvider { | ||
return &BlocksProvider{ | ||
blocks: bp.blocks, | ||
chainID: bp.chainID, | ||
tracer: tracer, | ||
blockOverrides: bp.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) ( | ||
evmTypes.BlockSnapshot, | ||
error, | ||
) { | ||
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 you add a comment when to use this one and when to use
replayer.BlocksProvider
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.
Good idea 👍 I added descriptions on both implementations in da79cd3 .