erigon-pulse/turbo/rpchelper/helper.go

67 lines
2.0 KiB
Go
Raw Normal View History

package rpchelper
import (
"fmt"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/rpc"
"github.com/ledgerwatch/turbo-geth/turbo/adapter"
)
2021-03-30 09:53:54 +00:00
func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx ethdb.Tx, pending *Pending) (uint64, common.Hash, error) {
var blockNumber uint64
var err error
hash, ok := blockNrOrHash.Hash()
if !ok {
number := *blockNrOrHash.BlockNumber
if number == rpc.LatestBlockNumber {
2021-04-15 05:23:10 +00:00
blockNumber, err = stages.GetStageProgress(tx, stages.Execution)
if err != nil {
return 0, common.Hash{}, fmt.Errorf("getting latest block number: %w", err)
}
2020-09-12 05:33:07 +00:00
} else if number == rpc.EarliestBlockNumber {
blockNumber = 0
} else if number == rpc.PendingBlockNumber {
pendingBlock := pending.Block()
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{}, fmt.Errorf("getting latest block number: %w", err)
}
} else {
return pendingBlock.NumberU64(), pendingBlock.Hash(), 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{}, err
}
} else {
2021-04-15 05:23:10 +00:00
number := rawdb.ReadHeaderNumber(tx, hash)
if number == nil {
return 0, common.Hash{}, 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{}, err
}
if blockNrOrHash.RequireCanonical && ch != hash {
return 0, common.Hash{}, fmt.Errorf("hash %q is not currently canonical", hash.String())
}
}
return blockNumber, hash, nil
}
2021-03-30 09:53:54 +00:00
func GetAccount(tx ethdb.Tx, blockNumber uint64, address common.Address) (*accounts.Account, error) {
reader := adapter.NewStateReader(tx, blockNumber)
return reader.ReadAccountData(address)
}