2020-07-21 14:31:02 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-23 18:18:23 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-11-15 10:54:31 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
2021-11-15 10:54:31 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-07-21 14:31:02 +00:00
|
|
|
|
2021-11-15 10:54:31 +00:00
|
|
|
txpool_proto "github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2020-07-21 14:31:02 +00:00
|
|
|
)
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// GetBalance implements eth_getBalance. Returns the balance of an account for a given address.
|
2020-10-12 08:39:04 +00:00
|
|
|
func (api *APIImpl) GetBalance(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Big, error) {
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err1 := api.db.BeginRo(ctx)
|
2020-10-12 08:39:04 +00:00
|
|
|
if err1 != nil {
|
2021-10-04 15:16:52 +00:00
|
|
|
return nil, fmt.Errorf("getBalance cannot open tx: %w", err1)
|
2020-10-12 08:39:04 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2022-02-23 23:42:14 +00:00
|
|
|
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-23 23:42:14 +00:00
|
|
|
acc, err := reader.ReadAccountData(address)
|
2020-07-29 16:21:34 +00:00
|
|
|
if err != nil {
|
2022-02-23 23:42:14 +00:00
|
|
|
return nil, fmt.Errorf("cant get a balance for account %x: %w", address.String(), err)
|
2020-07-29 16:21:34 +00:00
|
|
|
}
|
2020-09-23 18:18:23 +00:00
|
|
|
if acc == nil {
|
|
|
|
// Special case - non-existent account is assumed to have zero balance
|
|
|
|
return (*hexutil.Big)(big.NewInt(0)), nil
|
|
|
|
}
|
2020-07-29 16:21:34 +00:00
|
|
|
|
|
|
|
return (*hexutil.Big)(acc.Balance.ToBig()), nil
|
|
|
|
}
|
2020-10-14 15:59:42 +00:00
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// GetTransactionCount implements eth_getTransactionCount. Returns the number of transactions sent from an address (the nonce).
|
2020-10-14 15:59:42 +00:00
|
|
|
func (api *APIImpl) GetTransactionCount(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (*hexutil.Uint64, error) {
|
2021-11-15 10:54:31 +00:00
|
|
|
if blockNrOrHash.BlockNumber != nil && *blockNrOrHash.BlockNumber == rpc.PendingBlockNumber {
|
|
|
|
reply, err := api.txPool.Nonce(ctx, &txpool_proto.NonceRequest{
|
|
|
|
Address: gointerfaces.ConvertAddressToH160(address),
|
|
|
|
}, &grpc.EmptyCallOption{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if reply.Found {
|
|
|
|
reply.Nonce++
|
|
|
|
return (*hexutil.Uint64)(&reply.Nonce), nil
|
|
|
|
}
|
|
|
|
}
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err1 := api.db.BeginRo(ctx)
|
2020-10-14 15:59:42 +00:00
|
|
|
if err1 != nil {
|
2021-10-04 15:16:52 +00:00
|
|
|
return nil, fmt.Errorf("getTransactionCount cannot open tx: %w", err1)
|
2020-10-14 15:59:42 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2022-02-23 23:42:14 +00:00
|
|
|
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nonce := hexutil.Uint64(0)
|
2020-10-14 15:59:42 +00:00
|
|
|
acc, err := reader.ReadAccountData(address)
|
|
|
|
if acc == nil || err != nil {
|
|
|
|
return &nonce, err
|
|
|
|
}
|
|
|
|
return (*hexutil.Uint64)(&acc.Nonce), err
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// GetCode implements eth_getCode. Returns the byte code at a given address (if it's a smart contract).
|
2020-10-14 15:59:42 +00:00
|
|
|
func (api *APIImpl) GetCode(ctx context.Context, address common.Address, blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err1 := api.db.BeginRo(ctx)
|
2020-10-14 15:59:42 +00:00
|
|
|
if err1 != nil {
|
2021-10-04 15:16:52 +00:00
|
|
|
return nil, fmt.Errorf("getCode cannot open tx: %w", err1)
|
2020-10-14 15:59:42 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2022-02-23 23:42:14 +00:00
|
|
|
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
acc, err := reader.ReadAccountData(address)
|
|
|
|
if acc == nil || err != nil {
|
|
|
|
return hexutil.Bytes(""), nil
|
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
res, _ := reader.ReadAccountCode(address, acc.Incarnation, acc.CodeHash)
|
2020-10-14 15:59:42 +00:00
|
|
|
if res == nil {
|
|
|
|
return hexutil.Bytes(""), nil
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// GetStorageAt implements eth_getStorageAt. Returns the value from a storage position at a given address.
|
2020-10-14 15:59:42 +00:00
|
|
|
func (api *APIImpl) GetStorageAt(ctx context.Context, address common.Address, index string, blockNrOrHash rpc.BlockNumberOrHash) (string, error) {
|
|
|
|
var empty []byte
|
|
|
|
|
2021-04-03 06:26:00 +00:00
|
|
|
tx, err1 := api.db.BeginRo(ctx)
|
2020-10-14 15:59:42 +00:00
|
|
|
if err1 != nil {
|
2021-07-17 02:09:56 +00:00
|
|
|
return hexutil.Encode(common.LeftPadBytes(empty, 32)), err1
|
2020-10-14 15:59:42 +00:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2021-01-02 19:28:22 +00:00
|
|
|
|
2022-02-23 23:42:14 +00:00
|
|
|
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache)
|
2022-02-23 09:44:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return hexutil.Encode(common.LeftPadBytes(empty, 32)), err
|
|
|
|
}
|
2020-10-14 15:59:42 +00:00
|
|
|
acc, err := reader.ReadAccountData(address)
|
|
|
|
if acc == nil || err != nil {
|
2021-07-17 02:09:56 +00:00
|
|
|
return hexutil.Encode(common.LeftPadBytes(empty, 32)), err
|
2020-10-14 15:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location := common.HexToHash(index)
|
|
|
|
res, err := reader.ReadAccountStorage(address, acc.Incarnation, &location)
|
|
|
|
if err != nil {
|
|
|
|
res = empty
|
|
|
|
}
|
2021-07-17 02:09:56 +00:00
|
|
|
return hexutil.Encode(common.LeftPadBytes(res, 32)), err
|
2020-10-14 15:59:42 +00:00
|
|
|
}
|