internal/ethapi: add block overrides to eth_call (#26414)
Adds an optional config parameter to eth_call which allows users to override block context fields (same functionality that was added to traceCall in #24871) --------- Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
co-authored by
Martin Holst Swende
parent
52c246fac3
commit
ae66009640
@@ -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) {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -127,6 +127,9 @@ func TestGethClient(t *testing.T) {
|
||||
}, {
|
||||
"TestCallContract",
|
||||
func(t *testing.T) { testCallContract(t, client) },
|
||||
}, {
|
||||
"TestCallContractWithBlockOverrides",
|
||||
func(t *testing.T) { testCallContractWithBlockOverrides(t, client) },
|
||||
},
|
||||
// The testaccesslist is a bit time-sensitive: the newTestBackend imports
|
||||
// one block. The `testAcessList` fails if the miner has not yet created a
|
||||
@@ -413,3 +416,75 @@ func TestOverrideAccountMarshal(t *testing.T) {
|
||||
t.Error("want:", expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlockOverridesMarshal(t *testing.T) {
|
||||
for i, tt := range []struct {
|
||||
bo BlockOverrides
|
||||
want string
|
||||
}{
|
||||
{
|
||||
bo: BlockOverrides{},
|
||||
want: `{}`,
|
||||
},
|
||||
{
|
||||
bo: BlockOverrides{
|
||||
Coinbase: common.HexToAddress("0x1111111111111111111111111111111111111111"),
|
||||
},
|
||||
want: `{"coinbase":"0x1111111111111111111111111111111111111111"}`,
|
||||
},
|
||||
{
|
||||
bo: BlockOverrides{
|
||||
Number: big.NewInt(1),
|
||||
Difficulty: big.NewInt(2),
|
||||
Time: 3,
|
||||
GasLimit: 4,
|
||||
BaseFee: big.NewInt(5),
|
||||
},
|
||||
want: `{"number":"0x1","difficulty":"0x2","time":"0x3","gasLimit":"0x4","baseFee":"0x5"}`,
|
||||
},
|
||||
} {
|
||||
marshalled, err := json.Marshal(&tt.bo)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if string(marshalled) != tt.want {
|
||||
t.Errorf("Testcase #%d failed. expected\n%s\ngot\n%s", i, tt.want, string(marshalled))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCallContractWithBlockOverrides(t *testing.T, client *rpc.Client) {
|
||||
ec := New(client)
|
||||
msg := ethereum.CallMsg{
|
||||
From: testAddr,
|
||||
To: &common.Address{},
|
||||
Gas: 50000,
|
||||
GasPrice: big.NewInt(1000000000),
|
||||
Value: big.NewInt(1),
|
||||
}
|
||||
override := OverrideAccount{
|
||||
// Returns coinbase address.
|
||||
Code: common.FromHex("0x41806000526014600cf3"),
|
||||
}
|
||||
mapAcc := make(map[common.Address]OverrideAccount)
|
||||
mapAcc[common.Address{}] = override
|
||||
res, err := ec.CallContract(context.Background(), msg, big.NewInt(0), &mapAcc)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !bytes.Equal(res, common.FromHex("0x0000000000000000000000000000000000000000")) {
|
||||
t.Fatalf("unexpected result: %x", res)
|
||||
}
|
||||
|
||||
// Now test with block overrides
|
||||
bo := BlockOverrides{
|
||||
Coinbase: common.HexToAddress("0x1111111111111111111111111111111111111111"),
|
||||
}
|
||||
res, err = ec.CallContractWithBlockOverrides(context.Background(), msg, big.NewInt(0), &mapAcc, bo)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !bytes.Equal(res, common.FromHex("0x1111111111111111111111111111111111111111")) {
|
||||
t.Fatalf("unexpected result: %x", res)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user