-
Notifications
You must be signed in to change notification settings - Fork 20k
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
internal/ethapi: add block overrides to eth_call #26414
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,28 @@ func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockN | |
return hex, err | ||
} | ||
|
||
// CallContractWithBlockOverrides executes a message call transaction, which is directly executed | ||
// in the VM of the node, but never mined into the blockchain. | ||
// | ||
// blockNumber selects the block height at which the call runs. It can be nil, in which | ||
// case the code is taken from the latest known block. Note that state from very old | ||
// blocks might not be available. | ||
// | ||
// overrides specifies a map of contract states that should be overwritten before executing | ||
// the message call. | ||
// | ||
// blockOverrides specifies block fields exposed to the EVM that can be overridden for the call. | ||
// | ||
// Please use ethclient.CallContract instead if you don't need the override functionality. | ||
func (ec *Client) CallContractWithBlockOverrides(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int, overrides *map[common.Address]OverrideAccount, blockOverrides BlockOverrides) ([]byte, error) { | ||
var hex hexutil.Bytes | ||
err := ec.c.CallContext( | ||
ctx, &hex, "eth_call", toCallArg(msg), | ||
toBlockNumArg(blockNumber), overrides, blockOverrides, | ||
) | ||
return hex, err | ||
} | ||
|
||
// GCStats retrieves the current garbage collection stats from a geth node. | ||
func (ec *Client) GCStats(ctx context.Context) (*debug.GCStats, error) { | ||
var result debug.GCStats | ||
|
@@ -265,3 +287,52 @@ func (a OverrideAccount) MarshalJSON() ([]byte, error) { | |
} | ||
return json.Marshal(output) | ||
} | ||
|
||
// BlockOverrides specifies the set of header fields to override. | ||
type BlockOverrides struct { | ||
// Number overrides the block number. | ||
Number *big.Int | ||
// Difficulty overrides the block difficulty. | ||
Difficulty *big.Int | ||
// Time overrides the block timestamp. Time is applied only when | ||
// it is non-zero. | ||
Time uint64 | ||
// GasLimit overrides the block gas limit. GasLimit is applied only when | ||
// it is non-zero. | ||
GasLimit uint64 | ||
// Coinbase overrides the block coinbase. Coinbase is applied only when | ||
// it is different from the zero address. | ||
Coinbase common.Address | ||
// Random overrides the block extra data which feeds into the RANDOM opcode. | ||
// Random is applied only when it is a non-zero hash. | ||
Random common.Hash | ||
// BaseFee overrides the block base fee. | ||
BaseFee *big.Int | ||
} | ||
|
||
func (o BlockOverrides) MarshalJSON() ([]byte, error) { | ||
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. Did you "generate" this manually? 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. Yes it's handmade quality :) |
||
type override struct { | ||
Number *hexutil.Big `json:"number,omitempty"` | ||
Difficulty *hexutil.Big `json:"difficulty,omitempty"` | ||
Time hexutil.Uint64 `json:"time,omitempty"` | ||
GasLimit hexutil.Uint64 `json:"gasLimit,omitempty"` | ||
Coinbase *common.Address `json:"coinbase,omitempty"` | ||
Random *common.Hash `json:"random,omitempty"` | ||
BaseFee *hexutil.Big `json:"baseFee,omitempty"` | ||
} | ||
|
||
output := override{ | ||
Number: (*hexutil.Big)(o.Number), | ||
Difficulty: (*hexutil.Big)(o.Difficulty), | ||
Time: hexutil.Uint64(o.Time), | ||
GasLimit: hexutil.Uint64(o.GasLimit), | ||
BaseFee: (*hexutil.Big)(o.BaseFee), | ||
} | ||
if o.Coinbase != (common.Address{}) { | ||
output.Coinbase = &o.Coinbase | ||
} | ||
if o.Random != (common.Hash{}) { | ||
output.Random = &o.Random | ||
} | ||
return json.Marshal(output) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Perhaps add a comment that this struct is a direct "replica" of the
BlockOverrides
defined ininterna/ethapi/api.go
.Would it make sense to have it defined in only one place (thus remove it from internal)?
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.
We kinda need both. The API needs fields with suitable types for input (i.e.
hexutil
), and gethclient needs standard types which will then be serialized to be sent to the API. That's the reason why I copied the structure and changed the types.