2020-08-19 11:46:20 +00:00
|
|
|
package rpchelper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-29 21:28:09 +00:00
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/filters"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"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"
|
2020-08-19 11:46:20 +00:00
|
|
|
)
|
|
|
|
|
2021-06-11 12:19:10 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *filters.Filters) (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 {
|
2021-04-15 05:23:10 +00:00
|
|
|
blockNumber, err = stages.GetStageProgress(tx, stages.Execution)
|
2020-08-29 21:28:09 +00:00
|
|
|
if err != nil {
|
2021-03-30 07:09:00 +00:00
|
|
|
return 0, common.Hash{}, fmt.Errorf("getting latest block number: %w", err)
|
2020-08-29 21:28:09 +00:00
|
|
|
}
|
2020-09-12 05:33:07 +00:00
|
|
|
} else if number == rpc.EarliestBlockNumber {
|
|
|
|
blockNumber = 0
|
|
|
|
} else if number == rpc.PendingBlockNumber {
|
2021-05-17 12:15:19 +00:00
|
|
|
pendingBlock := filters.LastPendingBlock()
|
2021-03-30 07:09:00 +00:00
|
|
|
if pendingBlock == nil {
|
2021-04-15 05:23:10 +00:00
|
|
|
blockNumber, err = stages.GetStageProgress(tx, stages.Execution)
|
2021-03-30 07:09:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, fmt.Errorf("getting latest block number: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return pendingBlock.NumberU64(), pendingBlock.Hash(), nil
|
|
|
|
}
|
2020-08-29 21:28:09 +00:00
|
|
|
} else {
|
|
|
|
blockNumber = uint64(number.Int64())
|
|
|
|
}
|
2021-04-15 05:23:10 +00:00
|
|
|
hash, err = rawdb.ReadCanonicalHash(tx, blockNumber)
|
2020-08-19 11:46:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, err
|
|
|
|
}
|
|
|
|
} else {
|
2021-04-15 05:23:10 +00:00
|
|
|
number := rawdb.ReadHeaderNumber(tx, hash)
|
2021-01-02 19:28:22 +00:00
|
|
|
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
|
|
|
|
2021-04-15 05:23:10 +00:00
|
|
|
ch, err := rawdb.ReadCanonicalHash(tx, blockNumber)
|
2020-10-10 06:05:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, err
|
|
|
|
}
|
|
|
|
if blockNrOrHash.RequireCanonical && ch != hash {
|
2021-06-11 12:19:10 +00:00
|
|
|
return 0, common.Hash{}, nonCanonocalHashError{hash}
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return blockNumber, hash, nil
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
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)
|
2020-08-19 11:46:20 +00:00
|
|
|
return reader.ReadAccountData(address)
|
|
|
|
}
|