forked from cerc-io/laconicd-deprecated
impr: support batch eth txs (#901)
* support batch eth tx Closes: 896 Allow multiple MsgEthereumTx in single tx * fix transaction receipt api * fix tx receipt api and accumulate tx gas used * fix lint * fix test * fix rpc test * cleanup * fix cumulativeGasUsed and gasUsed * fix lint * Update app/ante/eth.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update app/ante/eth.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * Update rpc/ethereum/backend/utils.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * pr suggestions * typo * fix lint Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
co-authored by
Federico Kunze Küllmer
parent
aeb6aeb715
commit
7d8664043e
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/store/prefix"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
@@ -301,3 +302,36 @@ func (k Keeper) BaseFee(ctx sdk.Context, ethCfg *params.ChainConfig) *big.Int {
|
||||
}
|
||||
return baseFee
|
||||
}
|
||||
|
||||
// ResetTransientGasUsed reset gas used to prepare for execution of current cosmos tx, called in ante handler.
|
||||
func (k Keeper) ResetTransientGasUsed(ctx sdk.Context) {
|
||||
store := ctx.TransientStore(k.transientKey)
|
||||
store.Delete(types.KeyPrefixTransientGasUsed)
|
||||
}
|
||||
|
||||
// GetTransientGasUsed returns the gas used by current cosmos tx.
|
||||
func (k Keeper) GetTransientGasUsed(ctx sdk.Context) uint64 {
|
||||
store := ctx.TransientStore(k.transientKey)
|
||||
bz := store.Get(types.KeyPrefixTransientGasUsed)
|
||||
if len(bz) == 0 {
|
||||
return 0
|
||||
}
|
||||
return sdk.BigEndianToUint64(bz)
|
||||
}
|
||||
|
||||
// SetTransientGasUsed sets the gas used by current cosmos tx.
|
||||
func (k Keeper) SetTransientGasUsed(ctx sdk.Context, gasUsed uint64) {
|
||||
store := ctx.TransientStore(k.transientKey)
|
||||
bz := sdk.Uint64ToBigEndian(gasUsed)
|
||||
store.Set(types.KeyPrefixTransientGasUsed, bz)
|
||||
}
|
||||
|
||||
// AddTransientGasUsed accumulate gas used by each eth msgs included in current cosmos tx.
|
||||
func (k Keeper) AddTransientGasUsed(ctx sdk.Context, gasUsed uint64) (uint64, error) {
|
||||
result := k.GetTransientGasUsed(ctx) + gasUsed
|
||||
if result < gasUsed {
|
||||
return 0, sdkerrors.Wrap(types.ErrGasOverflow, "transient gas used")
|
||||
}
|
||||
k.SetTransientGasUsed(ctx, result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -38,7 +38,9 @@ func (k *Keeper) EthereumTx(goCtx context.Context, msg *types.MsgEthereumTx) (*t
|
||||
// add event for ethereum transaction hash format
|
||||
sdk.NewAttribute(types.AttributeKeyEthereumTxHash, response.Hash),
|
||||
// add event for index of valid ethereum tx
|
||||
sdk.NewAttribute(types.AttributeKeyTxIndex, strconv.FormatInt(int64(txIndex), 10)),
|
||||
sdk.NewAttribute(types.AttributeKeyTxIndex, strconv.FormatUint(txIndex, 10)),
|
||||
// add event for eth tx gas used, we can't get it from cosmos tx result when it contains multiple eth tx msgs.
|
||||
sdk.NewAttribute(types.AttributeKeyTxGasUsed, strconv.FormatUint(response.GasUsed, 10)),
|
||||
}
|
||||
|
||||
if len(ctx.TxBytes()) > 0 {
|
||||
|
||||
@@ -287,8 +287,13 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
|
||||
|
||||
k.SetTxIndexTransient(ctx, uint64(txConfig.TxIndex)+1)
|
||||
|
||||
// update the gas used after refund
|
||||
k.ResetGasMeterAndConsumeGas(ctx, res.GasUsed)
|
||||
totalGasUsed, err := k.AddTransientGasUsed(ctx, res.GasUsed)
|
||||
if err != nil {
|
||||
return nil, sdkerrors.Wrap(err, "failed to add transient gas used")
|
||||
}
|
||||
|
||||
// reset the gas meter for current cosmos transaction
|
||||
k.ResetGasMeterAndConsumeGas(ctx, totalGasUsed)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user