2020-08-19 11:46:20 +00:00
|
|
|
package rpchelper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-29 21:28:09 +00:00
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
2020-08-29 21:28:09 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/adapter"
|
|
|
|
)
|
|
|
|
|
2020-11-22 21:25:26 +00:00
|
|
|
func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, dbReader ethdb.Database) (uint64, common.Hash, error) {
|
2020-08-19 11:46:20 +00:00
|
|
|
var blockNumber uint64
|
|
|
|
var err error
|
|
|
|
hash, ok := blockNrOrHash.Hash()
|
|
|
|
if !ok {
|
2020-08-29 21:28:09 +00:00
|
|
|
number := *blockNrOrHash.BlockNumber
|
|
|
|
if number == rpc.LatestBlockNumber {
|
2020-12-21 11:01:37 +00:00
|
|
|
blockNumber, err = stages.GetStageProgress(dbReader, stages.Execution)
|
2020-08-29 21:28:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, fmt.Errorf("getting latest block number: %v", err)
|
|
|
|
}
|
2020-09-12 05:33:07 +00:00
|
|
|
|
|
|
|
} else if number == rpc.EarliestBlockNumber {
|
|
|
|
blockNumber = 0
|
|
|
|
|
|
|
|
} else if number == rpc.PendingBlockNumber {
|
|
|
|
return 0, common.Hash{}, fmt.Errorf("pending blocks are not supported")
|
|
|
|
|
2020-08-29 21:28:09 +00:00
|
|
|
} else {
|
|
|
|
blockNumber = uint64(number.Int64())
|
|
|
|
}
|
2021-01-02 19:28:22 +00:00
|
|
|
hash, err = rawdb.ReadCanonicalHash(dbReader, blockNumber)
|
2020-08-19 11:46:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, err
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-02 19:28:22 +00:00
|
|
|
number := rawdb.ReadHeaderNumber(dbReader, hash)
|
|
|
|
if number == nil {
|
2020-08-19 11:46:20 +00:00
|
|
|
return 0, common.Hash{}, fmt.Errorf("block %x not found", hash)
|
|
|
|
}
|
2021-01-02 19:28:22 +00:00
|
|
|
blockNumber = *number
|
2020-08-19 11:46:20 +00:00
|
|
|
|
2020-10-10 06:05:56 +00:00
|
|
|
ch, err := rawdb.ReadCanonicalHash(dbReader, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, err
|
|
|
|
}
|
|
|
|
if blockNrOrHash.RequireCanonical && ch != hash {
|
2020-08-19 11:46:20 +00:00
|
|
|
return 0, common.Hash{}, fmt.Errorf("hash %q is not currently canonical", hash.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return blockNumber, hash, nil
|
|
|
|
}
|
|
|
|
|
2021-01-02 19:28:22 +00:00
|
|
|
func GetAccount(tx ethdb.Database, blockNumber uint64, address common.Address) (*accounts.Account, error) {
|
|
|
|
reader := adapter.NewStateReader(tx.(ethdb.HasTx).Tx(), blockNumber)
|
2020-08-19 11:46:20 +00:00
|
|
|
return reader.ReadAccountData(address)
|
|
|
|
}
|