2020-10-22 20:39:51 +00:00
|
|
|
package types
|
2019-09-24 21:38:58 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"context"
|
2021-08-25 01:21:57 +00:00
|
|
|
"encoding/json"
|
2021-09-05 11:03:06 +00:00
|
|
|
"errors"
|
2019-09-24 21:38:58 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-04-13 19:18:50 +00:00
|
|
|
"math/big"
|
2019-09-24 21:38:58 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
"google.golang.org/grpc/metadata"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-09-28 10:07:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-09-24 21:38:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-09-28 10:07:18 +00:00
|
|
|
|
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
ethermint "github.com/cerc-io/laconicd/types"
|
2019-09-24 21:38:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockNumber represents decoding hex string to block values
|
|
|
|
type BlockNumber int64
|
|
|
|
|
|
|
|
const (
|
2021-04-18 16:39:15 +00:00
|
|
|
EthPendingBlockNumber = BlockNumber(-2)
|
|
|
|
EthLatestBlockNumber = BlockNumber(-1)
|
|
|
|
EthEarliestBlockNumber = BlockNumber(0)
|
2019-09-24 21:38:58 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 01:21:57 +00:00
|
|
|
const (
|
2022-10-10 10:38:33 +00:00
|
|
|
BlockParamEarliest = "earliest"
|
|
|
|
BlockParamLatest = "latest"
|
|
|
|
BlockParamFinalized = "finalized"
|
|
|
|
BlockParamPending = "pending"
|
2021-08-25 01:21:57 +00:00
|
|
|
)
|
|
|
|
|
2020-07-03 15:40:00 +00:00
|
|
|
// NewBlockNumber creates a new BlockNumber instance.
|
2020-04-13 19:18:50 +00:00
|
|
|
func NewBlockNumber(n *big.Int) BlockNumber {
|
2021-09-28 10:07:18 +00:00
|
|
|
if !n.IsInt64() {
|
|
|
|
// default to latest block if it overflows
|
|
|
|
return EthLatestBlockNumber
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:18:50 +00:00
|
|
|
return BlockNumber(n.Int64())
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// ContextWithHeight wraps a context with the a gRPC block height header. If the provided height is
|
2021-04-18 16:39:15 +00:00
|
|
|
// 0, it will return an empty context and the gRPC query will use the latest block height for querying.
|
2021-07-20 15:16:02 +00:00
|
|
|
// Note that all metadata are processed and removed by tendermint layer, so it wont be accessible at gRPC server level.
|
2021-04-17 10:00:07 +00:00
|
|
|
func ContextWithHeight(height int64) context.Context {
|
2021-04-18 16:39:15 +00:00
|
|
|
if height == 0 {
|
2021-04-17 10:00:07 +00:00
|
|
|
return context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, fmt.Sprintf("%d", height))
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:38:58 +00:00
|
|
|
// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
|
2022-10-10 10:38:33 +00:00
|
|
|
// - "latest", "finalized", "earliest" or "pending" as string arguments
|
2019-09-24 21:38:58 +00:00
|
|
|
// - the block number
|
|
|
|
// Returned errors:
|
|
|
|
// - an invalid block number error when the given argument isn't a known strings
|
|
|
|
// - an out of range error when the given block number is either too little or too large
|
|
|
|
func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
|
|
|
|
input := strings.TrimSpace(string(data))
|
|
|
|
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
|
|
|
|
input = input[1 : len(input)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch input {
|
2021-08-25 01:21:57 +00:00
|
|
|
case BlockParamEarliest:
|
2021-04-18 16:39:15 +00:00
|
|
|
*bn = EthEarliestBlockNumber
|
2019-09-24 21:38:58 +00:00
|
|
|
return nil
|
2022-10-10 10:38:33 +00:00
|
|
|
case BlockParamLatest, BlockParamFinalized:
|
2021-04-18 16:39:15 +00:00
|
|
|
*bn = EthLatestBlockNumber
|
2019-09-24 21:38:58 +00:00
|
|
|
return nil
|
2021-08-25 01:21:57 +00:00
|
|
|
case BlockParamPending:
|
2021-04-18 16:39:15 +00:00
|
|
|
*bn = EthPendingBlockNumber
|
2020-05-15 02:08:13 +00:00
|
|
|
return nil
|
2019-09-24 21:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blckNum, err := hexutil.DecodeUint64(input)
|
2021-09-05 11:03:06 +00:00
|
|
|
if errors.Is(err, hexutil.ErrMissingPrefix) {
|
2021-04-18 16:39:15 +00:00
|
|
|
blckNum = cast.ToUint64(input)
|
|
|
|
} else if err != nil {
|
2019-09-24 21:38:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2019-09-24 21:38:58 +00:00
|
|
|
if blckNum > math.MaxInt64 {
|
2021-04-18 16:39:15 +00:00
|
|
|
return fmt.Errorf("block number larger than int64")
|
2019-09-24 21:38:58 +00:00
|
|
|
}
|
|
|
|
*bn = BlockNumber(blckNum)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2019-09-24 21:38:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64 converts block number to primitive type
|
|
|
|
func (bn BlockNumber) Int64() int64 {
|
2021-04-18 16:39:15 +00:00
|
|
|
if bn < 0 {
|
|
|
|
return 0
|
|
|
|
} else if bn == 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
return int64(bn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TmHeight is a util function used for the Tendermint RPC client. It returns
|
|
|
|
// nil if the block number is "latest". Otherwise, it returns the pointer of the
|
|
|
|
// int64 value of the height.
|
|
|
|
func (bn BlockNumber) TmHeight() *int64 {
|
2021-04-18 16:39:15 +00:00
|
|
|
if bn < 0 {
|
2020-10-22 20:39:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
height := bn.Int64()
|
|
|
|
return &height
|
2019-09-24 21:38:58 +00:00
|
|
|
}
|
2021-08-25 01:21:57 +00:00
|
|
|
|
|
|
|
// BlockNumberOrHash represents a block number or a block hash.
|
|
|
|
type BlockNumberOrHash struct {
|
|
|
|
BlockNumber *BlockNumber `json:"blockNumber,omitempty"`
|
|
|
|
BlockHash *common.Hash `json:"blockHash,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error {
|
|
|
|
type erased BlockNumberOrHash
|
|
|
|
e := erased{}
|
|
|
|
err := json.Unmarshal(data, &e)
|
|
|
|
if err == nil {
|
|
|
|
return bnh.checkUnmarshal(BlockNumberOrHash(e))
|
|
|
|
}
|
|
|
|
var input string
|
|
|
|
err = json.Unmarshal(data, &input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bnh.decodeFromString(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bnh *BlockNumberOrHash) checkUnmarshal(e BlockNumberOrHash) error {
|
|
|
|
if e.BlockNumber != nil && e.BlockHash != nil {
|
|
|
|
return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other")
|
|
|
|
}
|
|
|
|
bnh.BlockNumber = e.BlockNumber
|
|
|
|
bnh.BlockHash = e.BlockHash
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bnh *BlockNumberOrHash) decodeFromString(input string) error {
|
|
|
|
switch input {
|
|
|
|
case BlockParamEarliest:
|
|
|
|
bn := EthEarliestBlockNumber
|
|
|
|
bnh.BlockNumber = &bn
|
|
|
|
case BlockParamLatest:
|
|
|
|
bn := EthLatestBlockNumber
|
|
|
|
bnh.BlockNumber = &bn
|
|
|
|
case BlockParamPending:
|
|
|
|
bn := EthPendingBlockNumber
|
|
|
|
bnh.BlockNumber = &bn
|
|
|
|
default:
|
|
|
|
// check if the input is a block hash
|
|
|
|
if len(input) == 66 {
|
|
|
|
hash := common.Hash{}
|
|
|
|
err := hash.UnmarshalText([]byte(input))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bnh.BlockHash = &hash
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// otherwise take the hex string has int64 value
|
|
|
|
blockNumber, err := hexutil.DecodeUint64(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-28 10:07:18 +00:00
|
|
|
|
|
|
|
bnInt, err := ethermint.SafeInt64(blockNumber)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-08-25 01:21:57 +00:00
|
|
|
}
|
2021-09-28 10:07:18 +00:00
|
|
|
|
|
|
|
bn := BlockNumber(bnInt)
|
2021-08-25 01:21:57 +00:00
|
|
|
bnh.BlockNumber = &bn
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|