2020-08-19 11:46:20 +00:00
|
|
|
package rpchelper
|
|
|
|
|
|
|
|
import (
|
2022-02-23 09:44:55 +00:00
|
|
|
"context"
|
2020-08-19 11:46:20 +00:00
|
|
|
"fmt"
|
2020-08-29 21:28:09 +00:00
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2022-02-23 09:44:55 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
|
2022-09-05 14:31:00 +00:00
|
|
|
state2 "github.com/ledgerwatch/erigon-lib/state"
|
2022-11-08 21:48:11 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
2022-02-23 09:44:55 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
2022-12-10 22:41:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/systemcontracts"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
|
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:18:43 +00:00
|
|
|
func GetBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (uint64, common.Hash, bool, error) {
|
2021-09-28 02:25:56 +00:00
|
|
|
return _GetBlockNumber(blockNrOrHash.RequireCanonical, blockNrOrHash, tx, filters)
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:18:43 +00:00
|
|
|
func GetCanonicalBlockNumber(blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (uint64, common.Hash, bool, error) {
|
2021-09-28 02:25:56 +00:00
|
|
|
return _GetBlockNumber(true, blockNrOrHash, tx, filters)
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:18:43 +00:00
|
|
|
func _GetBlockNumber(requireCanonical bool, blockNrOrHash rpc.BlockNumberOrHash, tx kv.Tx, filters *Filters) (blockNumber uint64, hash common.Hash, latest bool, err error) {
|
2022-06-14 08:07:46 +00:00
|
|
|
// Due to changed semantics of `lastest` block in RPC request, it is now distinct
|
|
|
|
// from the block block number corresponding to the plain state
|
|
|
|
var plainStateBlockNumber uint64
|
|
|
|
if plainStateBlockNumber, err = stages.GetStageProgress(tx, stages.Execution); err != nil {
|
|
|
|
return 0, common.Hash{}, false, fmt.Errorf("getting plain state block number: %w", err)
|
2022-02-23 23:42:14 +00:00
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
hash, ok = blockNrOrHash.Hash()
|
2020-08-19 11:46:20 +00:00
|
|
|
if !ok {
|
2020-08-29 21:28:09 +00:00
|
|
|
number := *blockNrOrHash.BlockNumber
|
2022-06-14 08:07:46 +00:00
|
|
|
switch number {
|
|
|
|
case rpc.LatestBlockNumber:
|
|
|
|
if blockNumber, err = GetLatestBlockNumber(tx); err != nil {
|
|
|
|
return 0, common.Hash{}, false, err
|
|
|
|
}
|
|
|
|
case rpc.EarliestBlockNumber:
|
2020-09-12 05:33:07 +00:00
|
|
|
blockNumber = 0
|
2022-06-23 17:37:39 +00:00
|
|
|
case rpc.FinalizedBlockNumber:
|
2022-06-14 08:07:46 +00:00
|
|
|
blockNumber, err = GetFinalizedBlockNumber(tx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, false, err
|
|
|
|
}
|
|
|
|
case rpc.SafeBlockNumber:
|
|
|
|
blockNumber, err = GetSafeBlockNumber(tx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, common.Hash{}, false, err
|
|
|
|
}
|
|
|
|
case rpc.PendingBlockNumber:
|
2021-05-17 12:15:19 +00:00
|
|
|
pendingBlock := filters.LastPendingBlock()
|
2021-03-30 07:09:00 +00:00
|
|
|
if pendingBlock == nil {
|
2022-06-14 08:07:46 +00:00
|
|
|
blockNumber = plainStateBlockNumber
|
2021-03-30 07:09:00 +00:00
|
|
|
} else {
|
2022-02-23 23:42:14 +00:00
|
|
|
return pendingBlock.NumberU64(), pendingBlock.Hash(), false, nil
|
2021-03-30 07:09:00 +00:00
|
|
|
}
|
2022-08-08 12:07:24 +00:00
|
|
|
case rpc.LatestExecutedBlockNumber:
|
|
|
|
blockNumber = plainStateBlockNumber
|
2022-06-14 08:07:46 +00:00
|
|
|
default:
|
2020-08-29 21:28:09 +00:00
|
|
|
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 {
|
2022-02-23 23:42:14 +00:00
|
|
|
return 0, common.Hash{}, false, err
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2022-02-23 23:42:14 +00:00
|
|
|
return 0, common.Hash{}, false, fmt.Errorf("block %x not found", hash)
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
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 {
|
2022-02-23 23:42:14 +00:00
|
|
|
return 0, common.Hash{}, false, err
|
2020-10-10 06:05:56 +00:00
|
|
|
}
|
2021-09-28 02:25:56 +00:00
|
|
|
if requireCanonical && ch != hash {
|
2022-02-23 23:42:14 +00:00
|
|
|
return 0, common.Hash{}, false, nonCanonocalHashError{hash}
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-14 08:07:46 +00:00
|
|
|
return blockNumber, hash, blockNumber == plainStateBlockNumber, nil
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:53:42 +00:00
|
|
|
func CreateStateReader(ctx context.Context, tx kv.Tx, blockNrOrHash rpc.BlockNumberOrHash, txnIndex uint64, filters *Filters, stateCache kvcache.Cache, historyV3 bool, agg *state2.AggregatorV3, chainName string) (state.StateReader, error) {
|
2022-02-23 23:42:14 +00:00
|
|
|
blockNumber, _, latest, err := _GetBlockNumber(true, blockNrOrHash, tx, filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-10 22:41:04 +00:00
|
|
|
return CreateStateReaderFromBlockNumber(ctx, tx, blockNumber, latest, txnIndex, stateCache, historyV3, agg, chainName)
|
2022-11-08 21:48:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:53:42 +00:00
|
|
|
func CreateStateReaderFromBlockNumber(ctx context.Context, tx kv.Tx, blockNumber uint64, latest bool, txnIndex uint64, stateCache kvcache.Cache, historyV3 bool, agg *state2.AggregatorV3, chainName string) (state.StateReader, error) {
|
2022-02-23 23:42:14 +00:00
|
|
|
if latest {
|
2022-02-23 09:44:55 +00:00
|
|
|
cacheView, err := stateCache.View(ctx, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-31 05:31:38 +00:00
|
|
|
return state.NewCachedReader2(cacheView, tx), nil
|
|
|
|
}
|
2022-12-10 22:41:04 +00:00
|
|
|
return CreateHistoryStateReader(tx, blockNumber+1, txnIndex, agg, historyV3, chainName)
|
2022-10-31 05:31:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 14:53:42 +00:00
|
|
|
func CreateHistoryStateReader(tx kv.Tx, blockNumber, txnIndex uint64, agg *state2.AggregatorV3, historyV3 bool, chainName string) (state.StateReader, error) {
|
2022-10-31 05:31:38 +00:00
|
|
|
if !historyV3 {
|
2022-12-10 22:41:04 +00:00
|
|
|
return state.NewPlainState(tx, blockNumber, systemcontracts.SystemContractCodeLookup[chainName]), nil
|
2022-10-31 05:31:38 +00:00
|
|
|
}
|
|
|
|
aggCtx := agg.MakeContext()
|
|
|
|
aggCtx.SetTx(tx)
|
2022-12-24 06:11:15 +00:00
|
|
|
r := state.NewHistoryReaderV3()
|
2022-10-31 05:31:38 +00:00
|
|
|
r.SetTx(tx)
|
2022-12-24 06:11:15 +00:00
|
|
|
r.SetAc(aggCtx)
|
2022-10-31 05:31:38 +00:00
|
|
|
minTxNum, err := rawdb.TxNums.Min(tx, blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-23 09:44:55 +00:00
|
|
|
}
|
2022-10-31 05:31:38 +00:00
|
|
|
r.SetTxNum(minTxNum + txnIndex)
|
|
|
|
return r, nil
|
2022-02-23 09:44:55 +00:00
|
|
|
}
|