2020-10-22 20:39:51 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2021-10-15 08:59:26 +00:00
|
|
|
"bytes"
|
2020-10-22 20:39:51 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2022-08-11 20:49:05 +00:00
|
|
|
"strings"
|
2020-10-22 20:39:51 +00:00
|
|
|
|
2021-10-15 08:59:26 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-10-22 20:39:51 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2022-11-14 19:40:14 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2022-11-14 19:40:14 +00:00
|
|
|
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-10-22 20:39:51 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
|
|
|
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"
|
2020-10-22 20:39:51 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2021-10-06 11:22:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
2020-10-22 20:39:51 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2022-07-29 10:35:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2020-10-22 20:39:51 +00:00
|
|
|
)
|
|
|
|
|
2022-08-11 20:49:05 +00:00
|
|
|
// ExceedBlockGasLimitError defines the error message when tx execution exceeds the block gas limit.
|
|
|
|
// The tx fee is deducted in ante handler, so it shouldn't be ignored in JSON-RPC API.
|
|
|
|
const ExceedBlockGasLimitError = "out of gas in location: block gas meter; gasWanted:"
|
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
// RawTxToEthTx returns a evm MsgEthereum transaction from raw tx bytes.
|
2022-01-14 09:37:33 +00:00
|
|
|
func RawTxToEthTx(clientCtx client.Context, txBz tmtypes.Tx) ([]*evmtypes.MsgEthereumTx, error) {
|
2021-06-08 11:11:37 +00:00
|
|
|
tx, err := clientCtx.TxConfig.TxDecoder()(txBz)
|
2020-10-22 20:39:51 +00:00
|
|
|
if err != nil {
|
2022-11-14 19:40:14 +00:00
|
|
|
return nil, errorsmod.Wrap(errortypes.ErrJSONUnmarshal, err.Error())
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
ethTxs := make([]*evmtypes.MsgEthereumTx, len(tx.GetMsgs()))
|
|
|
|
for i, msg := range tx.GetMsgs() {
|
|
|
|
ethTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid message type %T, expected %T", msg, &evmtypes.MsgEthereumTx{})
|
|
|
|
}
|
2022-07-19 15:12:48 +00:00
|
|
|
ethTx.Hash = ethTx.AsTransaction().Hash().Hex()
|
2022-01-14 09:37:33 +00:00
|
|
|
ethTxs[i] = ethTx
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
2022-01-14 09:37:33 +00:00
|
|
|
return ethTxs, nil
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EthHeaderFromTendermint is an util function that returns an Ethereum Header
|
|
|
|
// from a tendermint Header.
|
2021-11-14 13:34:10 +00:00
|
|
|
func EthHeaderFromTendermint(header tmtypes.Header, bloom ethtypes.Bloom, baseFee *big.Int) *ethtypes.Header {
|
2021-05-26 10:33:24 +00:00
|
|
|
txHash := ethtypes.EmptyRootHash
|
|
|
|
if len(header.DataHash) == 0 {
|
|
|
|
txHash = common.BytesToHash(header.DataHash)
|
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
|
2020-10-22 20:39:51 +00:00
|
|
|
return ðtypes.Header{
|
|
|
|
ParentHash: common.BytesToHash(header.LastBlockID.Hash.Bytes()),
|
2021-05-26 10:33:24 +00:00
|
|
|
UncleHash: ethtypes.EmptyUncleHash,
|
2021-11-14 13:34:10 +00:00
|
|
|
Coinbase: common.BytesToAddress(header.ProposerAddress),
|
2020-10-22 20:39:51 +00:00
|
|
|
Root: common.BytesToHash(header.AppHash),
|
2021-05-26 10:33:24 +00:00
|
|
|
TxHash: txHash,
|
|
|
|
ReceiptHash: ethtypes.EmptyRootHash,
|
2021-11-14 13:34:10 +00:00
|
|
|
Bloom: bloom,
|
2021-05-26 10:33:24 +00:00
|
|
|
Difficulty: big.NewInt(0),
|
2020-10-22 20:39:51 +00:00
|
|
|
Number: big.NewInt(header.Height),
|
2021-05-26 10:33:24 +00:00
|
|
|
GasLimit: 0,
|
|
|
|
GasUsed: 0,
|
2021-11-14 13:34:10 +00:00
|
|
|
Time: uint64(header.Time.UTC().Unix()),
|
|
|
|
Extra: []byte{},
|
2020-10-22 20:39:51 +00:00
|
|
|
MixDigest: common.Hash{},
|
|
|
|
Nonce: ethtypes.BlockNonce{},
|
2021-10-06 11:22:32 +00:00
|
|
|
BaseFee: baseFee,
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:30:03 +00:00
|
|
|
// BlockMaxGasFromConsensusParams returns the gas limit for the current block from the chain consensus params.
|
|
|
|
func BlockMaxGasFromConsensusParams(goCtx context.Context, clientCtx client.Context, blockHeight int64) (int64, error) {
|
|
|
|
resConsParams, err := clientCtx.Client.ConsensusParams(goCtx, &blockHeight)
|
2020-10-22 20:39:51 +00:00
|
|
|
if err != nil {
|
2021-07-19 01:52:44 +00:00
|
|
|
return int64(^uint32(0)), err
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gasLimit := resConsParams.ConsensusParams.Block.MaxGas
|
|
|
|
if gasLimit == -1 {
|
|
|
|
// Sets gas limit to max uint32 to not error with javascript dev tooling
|
|
|
|
// This -1 value indicating no block gas limit is set to max uint64 with geth hexutils
|
|
|
|
// which errors certain javascript dev tooling which only supports up to 53 bits
|
|
|
|
gasLimit = int64(^uint32(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
return gasLimit, nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 19:52:09 +00:00
|
|
|
// FormatBlock creates an ethereum block from a tendermint header and ethereum-formatted
|
|
|
|
// transactions.
|
|
|
|
func FormatBlock(
|
2021-04-18 16:39:15 +00:00
|
|
|
header tmtypes.Header, size int, gasLimit int64,
|
2021-11-14 13:34:10 +00:00
|
|
|
gasUsed *big.Int, transactions []interface{}, bloom ethtypes.Bloom,
|
2021-10-05 15:38:20 +00:00
|
|
|
validatorAddr common.Address, baseFee *big.Int,
|
2020-10-22 20:39:51 +00:00
|
|
|
) map[string]interface{} {
|
2021-11-14 13:34:10 +00:00
|
|
|
var transactionsRoot common.Hash
|
|
|
|
if len(transactions) == 0 {
|
|
|
|
transactionsRoot = ethtypes.EmptyRootHash
|
|
|
|
} else {
|
|
|
|
transactionsRoot = common.BytesToHash(header.DataHash)
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 16:12:05 +00:00
|
|
|
result := map[string]interface{}{
|
2020-10-22 20:39:51 +00:00
|
|
|
"number": hexutil.Uint64(header.Height),
|
2021-04-18 16:39:15 +00:00
|
|
|
"hash": hexutil.Bytes(header.Hash()),
|
2021-07-03 10:21:17 +00:00
|
|
|
"parentHash": common.BytesToHash(header.LastBlockID.Hash.Bytes()),
|
2021-06-22 07:45:10 +00:00
|
|
|
"nonce": ethtypes.BlockNonce{}, // PoW specific
|
2021-05-26 10:33:24 +00:00
|
|
|
"sha3Uncles": ethtypes.EmptyUncleHash, // No uncles in Tendermint
|
2020-10-22 20:39:51 +00:00
|
|
|
"logsBloom": bloom,
|
|
|
|
"stateRoot": hexutil.Bytes(header.AppHash),
|
2021-07-14 14:40:32 +00:00
|
|
|
"miner": validatorAddr,
|
2020-10-22 20:39:51 +00:00
|
|
|
"mixHash": common.Hash{},
|
2021-05-26 10:33:24 +00:00
|
|
|
"difficulty": (*hexutil.Big)(big.NewInt(0)),
|
2021-07-29 17:40:59 +00:00
|
|
|
"extraData": "0x",
|
2020-10-22 20:39:51 +00:00
|
|
|
"size": hexutil.Uint64(size),
|
|
|
|
"gasLimit": hexutil.Uint64(gasLimit), // Static gas limit
|
|
|
|
"gasUsed": (*hexutil.Big)(gasUsed),
|
|
|
|
"timestamp": hexutil.Uint64(header.Time.Unix()),
|
2021-11-14 13:34:10 +00:00
|
|
|
"transactionsRoot": transactionsRoot,
|
2021-05-26 10:33:24 +00:00
|
|
|
"receiptsRoot": ethtypes.EmptyRootHash,
|
|
|
|
|
|
|
|
"uncles": []common.Hash{},
|
2021-06-07 11:02:52 +00:00
|
|
|
"transactions": transactions,
|
2021-05-26 10:33:24 +00:00
|
|
|
"totalDifficulty": (*hexutil.Big)(big.NewInt(0)),
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
2021-12-23 16:12:05 +00:00
|
|
|
|
|
|
|
if baseFee != nil {
|
|
|
|
result["baseFeePerGas"] = (*hexutil.Big)(baseFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2020-10-22 20:39:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:42:53 +00:00
|
|
|
// NewTransactionFromMsg returns a transaction that will serialize to the RPC
|
|
|
|
// representation, with the given location metadata set (if available).
|
|
|
|
func NewTransactionFromMsg(
|
|
|
|
msg *evmtypes.MsgEthereumTx,
|
|
|
|
blockHash common.Hash,
|
|
|
|
blockNumber, index uint64,
|
2021-10-06 11:22:32 +00:00
|
|
|
baseFee *big.Int,
|
2022-11-16 17:59:12 +00:00
|
|
|
chainID *big.Int,
|
2021-07-12 17:42:53 +00:00
|
|
|
) (*RPCTransaction, error) {
|
2021-10-06 11:22:32 +00:00
|
|
|
tx := msg.AsTransaction()
|
2022-11-16 17:59:12 +00:00
|
|
|
return NewRPCTransaction(tx, blockHash, blockNumber, index, baseFee, chainID)
|
2021-07-12 17:42:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
// NewTransactionFromData returns a transaction that will serialize to the RPC
|
|
|
|
// representation, with the given location metadata set (if available).
|
2021-10-06 11:22:32 +00:00
|
|
|
func NewRPCTransaction(
|
|
|
|
tx *ethtypes.Transaction, blockHash common.Hash, blockNumber, index uint64, baseFee *big.Int,
|
2022-11-16 17:59:12 +00:00
|
|
|
chainID *big.Int,
|
2021-05-10 16:34:00 +00:00
|
|
|
) (*RPCTransaction, error) {
|
2021-10-06 11:22:32 +00:00
|
|
|
// Determine the signer. For replay-protected transactions, use the most permissive
|
|
|
|
// signer, because we assume that signers are backwards-compatible with old
|
|
|
|
// transactions. For non-protected transactions, the homestead signer signer is used
|
|
|
|
// because the return value of ChainId is zero for those transactions.
|
|
|
|
var signer ethtypes.Signer
|
|
|
|
if tx.Protected() {
|
|
|
|
signer = ethtypes.LatestSignerForChainID(tx.ChainId())
|
|
|
|
} else {
|
|
|
|
signer = ethtypes.HomesteadSigner{}
|
2021-05-26 10:33:24 +00:00
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
from, _ := ethtypes.Sender(signer, tx)
|
|
|
|
v, r, s := tx.RawSignatureValues()
|
|
|
|
result := &RPCTransaction{
|
|
|
|
Type: hexutil.Uint64(tx.Type()),
|
2021-05-10 16:34:00 +00:00
|
|
|
From: from,
|
2021-10-06 11:22:32 +00:00
|
|
|
Gas: hexutil.Uint64(tx.Gas()),
|
|
|
|
GasPrice: (*hexutil.Big)(tx.GasPrice()),
|
|
|
|
Hash: tx.Hash(),
|
|
|
|
Input: hexutil.Bytes(tx.Data()),
|
|
|
|
Nonce: hexutil.Uint64(tx.Nonce()),
|
|
|
|
To: tx.To(),
|
|
|
|
Value: (*hexutil.Big)(tx.Value()),
|
2021-07-05 16:39:08 +00:00
|
|
|
V: (*hexutil.Big)(v),
|
|
|
|
R: (*hexutil.Big)(r),
|
|
|
|
S: (*hexutil.Big)(s),
|
2022-11-16 17:59:12 +00:00
|
|
|
ChainID: (*hexutil.Big)(chainID),
|
2021-05-10 16:34:00 +00:00
|
|
|
}
|
|
|
|
if blockHash != (common.Hash{}) {
|
2021-10-06 11:22:32 +00:00
|
|
|
result.BlockHash = &blockHash
|
|
|
|
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
|
|
|
|
result.TransactionIndex = (*hexutil.Uint64)(&index)
|
2021-05-10 16:34:00 +00:00
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
switch tx.Type() {
|
|
|
|
case ethtypes.AccessListTxType:
|
|
|
|
al := tx.AccessList()
|
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
|
|
|
case ethtypes.DynamicFeeTxType:
|
|
|
|
al := tx.AccessList()
|
|
|
|
result.Accesses = &al
|
|
|
|
result.ChainID = (*hexutil.Big)(tx.ChainId())
|
|
|
|
result.GasFeeCap = (*hexutil.Big)(tx.GasFeeCap())
|
|
|
|
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
|
|
|
|
// if the transaction has been mined, compute the effective gas price
|
|
|
|
if baseFee != nil && blockHash != (common.Hash{}) {
|
|
|
|
// price = min(tip, gasFeeCap - baseFee) + baseFee
|
|
|
|
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
|
|
|
|
result.GasPrice = (*hexutil.Big)(price)
|
|
|
|
} else {
|
|
|
|
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
|
|
|
|
}
|
2021-06-30 15:28:38 +00:00
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
return result, nil
|
2021-05-10 16:34:00 +00:00
|
|
|
}
|
2021-10-15 08:59:26 +00:00
|
|
|
|
|
|
|
// BaseFeeFromEvents parses the feemarket basefee from cosmos events
|
|
|
|
func BaseFeeFromEvents(events []abci.Event) *big.Int {
|
|
|
|
for _, event := range events {
|
|
|
|
if event.Type != feemarkettypes.EventTypeFeeMarket {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, attr := range event.Attributes {
|
|
|
|
if bytes.Equal(attr.Key, []byte(feemarkettypes.AttributeKeyBaseFee)) {
|
|
|
|
result, success := new(big.Int).SetString(string(attr.Value), 10)
|
|
|
|
if success {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-29 10:35:07 +00:00
|
|
|
|
|
|
|
// CheckTxFee is an internal function used to check whether the fee of
|
|
|
|
// the given transaction is _reasonable_(under the cap).
|
|
|
|
func CheckTxFee(gasPrice *big.Int, gas uint64, cap float64) error {
|
|
|
|
// Short circuit if there is no cap for transaction fee at all.
|
|
|
|
if cap == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
totalfee := new(big.Float).SetInt(new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(gas)))
|
|
|
|
// 1 photon in 10^18 aphoton
|
|
|
|
oneToken := new(big.Float).SetInt(big.NewInt(params.Ether))
|
|
|
|
// quo = rounded(x/y)
|
|
|
|
feeEth := new(big.Float).Quo(totalfee, oneToken)
|
|
|
|
// no need to check error from parsing
|
|
|
|
feeFloat, _ := feeEth.Float64()
|
|
|
|
if feeFloat > cap {
|
|
|
|
return fmt.Errorf("tx fee (%.2f ether) exceeds the configured cap (%.2f ether)", feeFloat, cap)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-11 20:49:05 +00:00
|
|
|
|
|
|
|
// TxExceedBlockGasLimit returns true if the tx exceeds block gas limit.
|
|
|
|
func TxExceedBlockGasLimit(res *abci.ResponseDeliverTx) bool {
|
|
|
|
return strings.Contains(res.Log, ExceedBlockGasLimitError)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxSuccessOrExceedsBlockGasLimit returnsrue if the transaction was successful
|
|
|
|
// or if it failed with an ExceedBlockGasLimit error
|
|
|
|
func TxSuccessOrExceedsBlockGasLimit(res *abci.ResponseDeliverTx) bool {
|
|
|
|
return res.Code == 0 || TxExceedBlockGasLimit(res)
|
|
|
|
}
|