2020-07-21 14:31:02 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-09-23 18:18:23 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-10-14 15:59:42 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/adapter"
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/rpchelper"
|
2020-07-21 14:31:02 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/hexutil"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("getBalance cannot open tx: %v", err1)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2021-05-17 12:15:19 +00:00
|
|
|
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:39:04 +00:00
|
|
|
acc, err := rpchelper.GetAccount(tx, blockNumber, address)
|
2020-07-29 16:21:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cant get a balance for account %q for block %v", address.String(), blockNumber)
|
|
|
|
}
|
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-04-03 06:26:00 +00:00
|
|
|
tx, err1 := api.db.BeginRo(ctx)
|
2020-10-14 15:59:42 +00:00
|
|
|
if err1 != nil {
|
|
|
|
return nil, fmt.Errorf("getTransactionCount cannot open tx: %v", err1)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2021-05-17 12:15:19 +00:00
|
|
|
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nonce := hexutil.Uint64(0)
|
2021-03-30 09:53:54 +00:00
|
|
|
reader := adapter.NewStateReader(tx, blockNumber)
|
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 {
|
|
|
|
return nil, fmt.Errorf("getCode cannot open tx: %v", err1)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2021-05-17 12:15:19 +00:00
|
|
|
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
reader := adapter.NewStateReader(tx, blockNumber)
|
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-01-02 19:28:22 +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
|
|
|
|
2021-05-17 12:15:19 +00:00
|
|
|
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
|
2021-01-02 19:28:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return hexutil.Encode(common.LeftPadBytes(empty[:], 32)), err
|
|
|
|
}
|
2021-03-30 09:53:54 +00:00
|
|
|
reader := adapter.NewStateReader(tx, blockNumber)
|
2020-10-14 15:59:42 +00:00
|
|
|
acc, err := reader.ReadAccountData(address)
|
|
|
|
if acc == nil || err != nil {
|
|
|
|
return hexutil.Encode(common.LeftPadBytes(empty[:], 32)), err
|
|
|
|
}
|
|
|
|
|
|
|
|
location := common.HexToHash(index)
|
|
|
|
res, err := reader.ReadAccountStorage(address, acc.Incarnation, &location)
|
|
|
|
if err != nil {
|
|
|
|
res = empty
|
|
|
|
}
|
|
|
|
return hexutil.Encode(common.LeftPadBytes(res[:], 32)), err
|
|
|
|
}
|