erigon-pulse/turbo/rpchelper/helper.go

106 lines
3.6 KiB
Go
Raw Normal View History

package rpchelper
import (
"context"
"fmt"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/filters"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/adapter"
)
// unable to decode supplied params, or an invalid number of parameters
type nonCanonocalHashError struct{ hash common.Hash }
func (e nonCanonocalHashError) ErrorCode() int { return -32603 }
func (e nonCanonocalHashError) Error() string {
return fmt.Sprintf("hash %x is not currently canonical", e.hash)
}
func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *filters.Filters) (uint64, common.Hash, bool, error) {
2021-09-28 02:25:56 +00:00
return _GetBlockNumber(blockNrOrHash.RequireCanonical, blockNrOrHash, tx, filters)
}
func GetCanonicalBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *filters.Filters) (uint64, common.Hash, bool, error) {
2021-09-28 02:25:56 +00:00
return _GetBlockNumber(true, blockNrOrHash, tx, filters)
}
func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *filters.Filters) (blockNumber uint64, hash common.Hash, latest bool, err error) {
var latestBlockNumber uint64
if latestBlockNumber, err = stages.GetStageProgress(tx, stages.Execution); err != nil {
return 0, common.Hash{}, false, fmt.Errorf("getting latest block number: %w", err)
}
var ok bool
hash, ok = blockNrOrHash.Hash()
if !ok {
number := *blockNrOrHash.BlockNumber
if number == rpc.LatestBlockNumber {
blockNumber = latestBlockNumber
2020-09-12 05:33:07 +00:00
} else if number == rpc.EarliestBlockNumber {
blockNumber = 0
} else if number == rpc.PendingBlockNumber {
pendingBlock := filters.LastPendingBlock()
if pendingBlock == nil {
2021-04-15 05:23:10 +00:00
blockNumber, err = stages.GetStageProgress(tx, stages.Execution)
if err != nil {
return 0, common.Hash{}, false, fmt.Errorf("getting latest block number: %w", err)
}
} else {
return pendingBlock.NumberU64(), pendingBlock.Hash(), false, nil
}
} else {
blockNumber = uint64(number.Int64())
}
2021-04-15 05:23:10 +00:00
hash, err = rawdb.ReadCanonicalHash(tx, blockNumber)
if err != nil {
return 0, common.Hash{}, false, err
}
} else {
2021-04-15 05:23:10 +00:00
number := rawdb.ReadHeaderNumber(tx, hash)
if number == nil {
return 0, common.Hash{}, false, fmt.Errorf("block %x not found", hash)
}
blockNumber = *number
2021-04-15 05:23:10 +00:00
ch, err := rawdb.ReadCanonicalHash(tx, blockNumber)
if err != nil {
return 0, common.Hash{}, false, err
}
2021-09-28 02:25:56 +00:00
if requireCanonical && ch != hash {
return 0, common.Hash{}, false, nonCanonocalHashError{hash}
}
}
return blockNumber, hash, blockNumber == latestBlockNumber, nil
}
func GetAccount(tx kv.Tx, blockNumber uint64, address common.Address) (*accounts.Account, error) {
2021-03-30 09:53:54 +00:00
reader := adapter.NewStateReader(tx, blockNumber)
return reader.ReadAccountData(address)
}
func CreateStateReader(ctx context.Context, tx kv.Tx, blockNrOrHash rpc.BlockNumberOrHash, filters *filters.Filters, stateCache kvcache.Cache) (state.StateReader, error) {
blockNumber, _, latest, err := _GetBlockNumber(true, blockNrOrHash, tx, filters)
if err != nil {
return nil, err
}
var stateReader state.StateReader
if latest {
cacheView, err := stateCache.View(ctx, tx)
if err != nil {
return nil, err
}
stateReader = state.NewCachedReader2(cacheView, tx)
} else {
2022-03-19 02:36:35 +00:00
stateReader = state.NewPlainState(tx, blockNumber+1)
}
return stateReader, nil
}