diff --git a/README.md b/README.md index b052a031ac65757723d147b693afb6fc01a4e945..83bf73dce2ebcece989be6b345c3ad9bce816f30 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ eth_getBlockByHash eth_getBlockTransactionCountByHash eth_getBlockTransactionCountByNumber eth_getBalance +eth_getCode eth_getLogs eth_getStorageAt eth_getTransactionReceipt diff --git a/cmd/rpcdaemon/commands/eth_api.go b/cmd/rpcdaemon/commands/eth_api.go index 0c7d97cc166c48b1f1a5a638057b2cbbdac51222..bf969ac146944e53af396934ff29772ac2fd6e6f 100644 --- a/cmd/rpcdaemon/commands/eth_api.go +++ b/cmd/rpcdaemon/commands/eth_api.go @@ -39,6 +39,7 @@ type EthAPI interface { GetTransactionByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, txIndex hexutil.Uint64) (*RPCTransaction, error) GetTransactionByBlockNumberAndIndex(ctx context.Context, blockNr rpc.BlockNumber, txIndex hexutil.Uint) (*RPCTransaction, error) GetStorageAt(ctx context.Context, address common.Address, index string, blockNrOrHash rpc.BlockNumberOrHash) (string, error) + GetCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) } // APIImpl is implementation of the EthAPI interface based on remote Db access @@ -235,3 +236,28 @@ func (api *APIImpl) GetStorageAt(ctx context.Context, address common.Address, in return hexutil.Encode(res), nil } + +// GetCode returns the code stored at the given address in the state for the given block number. +func (api *APIImpl) GetCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) { + blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, api.dbReader) + if err != nil { + return nil, err + } + + reader := adapter.NewStateReader(api.db, blockNumber) + acc, err := reader.ReadAccountData(address) + if err != nil { + return nil, err + } + + if acc == nil { + return nil, fmt.Errorf("account %s not found", address) + } + + res, err := reader.ReadAccountCode(address, acc.CodeHash) + if err != nil { + return nil, err + } + + return res, nil +}