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

122 lines
4.1 KiB
Go
Raw Normal View History

package commands
import (
"context"
"fmt"
"math/big"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
"github.com/ledgerwatch/erigon/turbo/rpchelper"
"google.golang.org/grpc"
txpool_proto "github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
"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 {
2021-10-04 15:16:52 +00:00
return nil, fmt.Errorf("getBalance cannot open tx: %w", err1)
}
defer tx.Rollback()
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache, api.historyV2(tx), api._agg, api._txNums)
if err != nil {
return nil, err
}
acc, err := reader.ReadAccountData(address)
if err != nil {
return nil, fmt.Errorf("cant get a balance for account %x: %w", address.String(), err)
}
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) {
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)
if err1 != nil {
2021-10-04 15:16:52 +00:00
return nil, fmt.Errorf("getTransactionCount cannot open tx: %w", err1)
}
defer tx.Rollback()
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache, api.historyV2(tx), api._agg, api._txNums)
if err != nil {
return nil, err
}
nonce := hexutil.Uint64(0)
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 {
2021-10-04 15:16:52 +00:00
return nil, fmt.Errorf("getCode cannot open tx: %w", err1)
}
defer tx.Rollback()
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache, api.historyV2(tx), api._agg, api._txNums)
if err != nil {
return nil, err
}
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()
reader, err := rpchelper.CreateStateReader(ctx, tx, blockNrOrHash, api.filters, api.stateCache, api.historyV2(tx), api._agg, api._txNums)
if err != nil {
return hexutil.Encode(common.LeftPadBytes(empty, 32)), err
}
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
}