erigon-pulse/cmd/rpcdaemon/commands/eth_accounts.go

111 lines
3.6 KiB
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"math/big"
"github.com/ledgerwatch/erigon/turbo/adapter"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/rpc"
)
// GetBalance implements eth_getBalance. Returns the balance of an account for a given address.
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)
if err1 != nil {
return nil, fmt.Errorf("getBalance cannot open tx: %v", err1)
}
defer tx.Rollback()
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
if err != nil {
return nil, err
}
acc, err := rpchelper.GetAccount(tx, blockNumber, address)
if err != nil {
return nil, fmt.Errorf("cant get a balance for account %q for block %v", address.String(), blockNumber)
}
if acc == nil {
// Special case - non-existent account is assumed to have zero balance
return (*hexutil.Big)(big.NewInt(0)), nil
}
return (*hexutil.Big)(acc.Balance.ToBig()), nil
}
// GetTransactionCount implements eth_getTransactionCount. Returns the number of transactions sent from an address (the nonce).
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)
if err1 != nil {
return nil, fmt.Errorf("getTransactionCount cannot open tx: %v", err1)
}
defer tx.Rollback()
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
if err != nil {
return nil, err
}
nonce := hexutil.Uint64(0)
2021-03-30 09:53:54 +00:00
reader := adapter.NewStateReader(tx, blockNumber)
acc, err := reader.ReadAccountData(address)
if acc == nil || err != nil {
return &nonce, err
}
return (*hexutil.Uint64)(&acc.Nonce), err
}
// GetCode implements eth_getCode. Returns the byte code at a given address (if it's a smart contract).
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)
if err1 != nil {
return nil, fmt.Errorf("getCode cannot open tx: %v", err1)
}
defer tx.Rollback()
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
if err != nil {
return nil, err
}
2021-03-30 09:53:54 +00:00
reader := adapter.NewStateReader(tx, blockNumber)
acc, err := reader.ReadAccountData(address)
if acc == nil || err != nil {
return hexutil.Bytes(""), nil
}
State cache switching writes to reads during commit (#1368) * State cache init * More code * Fix lint * More tests * More tests * More tests * Fix test * Transformations * remove writeQueue, before fixing the tests * Fix tests * Add more tests, incarnation to the code items * Fix lint * Fix lint * Remove shards prototype, add incarnation to the state reader code * Clean up and replace cache in call_traces stage * fix flaky test * Save changes * Readers to use addrHash, writes - addresses * Fix lint * Fix lint * More accurate tracking of size * Optimise for smaller write batches * Attempt to integrate state cache into Execution stage * cacheSize to default flags * Print correct cache sizes and batch sizes * cacheSize in the integration * Fix tests * Fix lint * Remove print * Fix exec stage * Fix test * Refresh sequence on write * No double increment * heap.Remove * Try to fix alignment * Refactoring, adding hashItems * More changes * Fix compile errors * Fix lint * Wrapping cached reader * Wrap writer into cached writer * Turn state cache off by default * Fix plain state writer * Fix for code/storage mixup * Fix tests * Fix clique test * Better fix for the tests * Add test and fix some more * Fix compile error| * More functions * Fixes * Fix for the tests * sepatate DeletedFlag and AbsentFlag * Minor fixes * Test refactoring * More changes * Fix some tests * More test fixes * More test fixes * Fix lint * Move blockchain_test to be able to use stagedsync * More fixes * Fixes and cleanup * Fix tests in turbo/stages * Fix lint * Fix lint * Intemediate * Fix tests * Intemediate * More fixes * Compilation fixes * More fixes * Fix compile errors * More test fixes * More fixes * More test fixes * Fix compile error * Fixes * Fix * Fix * More fixes * Fixes * More fixes and cleanup * Further fix * Check gas used and bloom with header Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2020-12-08 09:44:29 +00:00
res, _ := reader.ReadAccountCode(address, acc.Incarnation, acc.CodeHash)
if res == nil {
return hexutil.Bytes(""), nil
}
return res, nil
}
// GetStorageAt implements eth_getStorageAt. Returns the value from a storage position at a given address.
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)
if err1 != nil {
2021-07-17 02:09:56 +00:00
return hexutil.Encode(common.LeftPadBytes(empty, 32)), err1
}
defer tx.Rollback()
blockNumber, _, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, api.filters)
if err != nil {
2021-07-17 02:09:56 +00:00
return hexutil.Encode(common.LeftPadBytes(empty, 32)), err
}
2021-03-30 09:53:54 +00:00
reader := adapter.NewStateReader(tx, blockNumber)
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
}
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
}