From ddb3035283ab364be7155ce55577c6d500343c34 Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 12 Jan 2024 07:37:53 +0800 Subject: [PATCH] Problem: duplicated encoding when passing state overrides (#394) * Problem: duplicated encoding when passing state overrides use json.RawMessage to avoid decoding json unnesserarily * optional parameter --- rpc/backend/backend.go | 3 ++- rpc/backend/call_tx.go | 11 ++++------- rpc/namespaces/ethereum/eth/api.go | 5 +++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/rpc/backend/backend.go b/rpc/backend/backend.go index ac1108c077..a9d39d9256 100644 --- a/rpc/backend/backend.go +++ b/rpc/backend/backend.go @@ -17,6 +17,7 @@ package backend import ( "context" + "encoding/json" "math/big" "time" @@ -128,7 +129,7 @@ type EVMBackend interface { SendRawTransaction(data hexutil.Bytes) (common.Hash, error) SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.TransactionArgs, error) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error) - DoCall(args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber, overrides *rpctypes.StateOverride) (*evmtypes.MsgEthereumTxResponse, error) + DoCall(args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber, overrides *json.RawMessage) (*evmtypes.MsgEthereumTxResponse, error) GasPrice() (*hexutil.Big, error) // Filter API diff --git a/rpc/backend/call_tx.go b/rpc/backend/call_tx.go index c70f57a13b..01c3e71f14 100644 --- a/rpc/backend/call_tx.go +++ b/rpc/backend/call_tx.go @@ -360,7 +360,7 @@ func (b *Backend) EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rp // estimated gas used on the operation or an error if fails. func (b *Backend) DoCall( args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber, - overrides *rpctypes.StateOverride, + overrides *json.RawMessage, ) (*evmtypes.MsgEthereumTxResponse, error) { bz, err := json.Marshal(&args) if err != nil { @@ -372,12 +372,9 @@ func (b *Backend) DoCall( return nil, errors.New("header not found") } - var overridesJSON []byte + var bzOverrides []byte if overrides != nil { - overridesJSON, err = json.Marshal(overrides) - if err != nil { - return nil, err - } + bzOverrides = *overrides } req := evmtypes.EthCallRequest{ @@ -385,7 +382,7 @@ func (b *Backend) DoCall( GasCap: b.RPCGasCap(), ProposerAddress: sdk.ConsAddress(header.Block.ProposerAddress), ChainId: b.chainID.Int64(), - Overrides: overridesJSON, + Overrides: bzOverrides, } // From ContextWithHeight: if the provided height is 0, diff --git a/rpc/namespaces/ethereum/eth/api.go b/rpc/namespaces/ethereum/eth/api.go index 1eb2fa8588..4d998ba32d 100644 --- a/rpc/namespaces/ethereum/eth/api.go +++ b/rpc/namespaces/ethereum/eth/api.go @@ -17,6 +17,7 @@ package eth import ( "context" + "encoding/json" "github.com/ethereum/go-ethereum/signer/core/apitypes" @@ -85,7 +86,7 @@ type EthereumAPI interface { // // Allows developers to read data from the blockchain which includes executing // smart contracts. However, no data is published to the Ethereum network. - Call(args evmtypes.TransactionArgs, blockNrOrHash rpctypes.BlockNumberOrHash, overrides *rpctypes.StateOverride) (hexutil.Bytes, error) + Call(args evmtypes.TransactionArgs, blockNrOrHash rpctypes.BlockNumberOrHash, overrides *json.RawMessage) (hexutil.Bytes, error) // Chain Information // @@ -281,7 +282,7 @@ func (e *PublicAPI) GetProof(address common.Address, // Call performs a raw contract call. func (e *PublicAPI) Call(args evmtypes.TransactionArgs, blockNrOrHash rpctypes.BlockNumberOrHash, - overrides *rpctypes.StateOverride, + overrides *json.RawMessage, ) (hexutil.Bytes, error) { e.logger.Debug("eth_call", "args", args.String(), "block number or hash", blockNrOrHash)