2020-09-03 07:51:19 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
|
2021-07-28 02:47:38 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2020-09-03 07:51:19 +00:00
|
|
|
)
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func getBlockNumber(number rpc.BlockNumber, tx kv.Tx) (uint64, error) {
|
2020-09-03 07:51:19 +00:00
|
|
|
var blockNum uint64
|
|
|
|
var err error
|
|
|
|
if number == rpc.LatestBlockNumber || number == rpc.PendingBlockNumber {
|
2021-03-30 09:53:54 +00:00
|
|
|
blockNum, err = getLatestBlockNumber(tx)
|
2020-09-03 07:51:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else if number == rpc.EarliestBlockNumber {
|
|
|
|
blockNum = 0
|
|
|
|
} else {
|
|
|
|
blockNum = uint64(number.Int64())
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockNum, nil
|
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func getLatestBlockNumber(tx kv.Tx) (uint64, error) {
|
2021-04-08 11:03:45 +00:00
|
|
|
blockNum, err := stages.GetStageProgress(tx, stages.Execution)
|
2020-09-03 07:51:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("getting latest block number: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockNum, nil
|
|
|
|
}
|