Skip to content

Commit

Permalink
feat: adjusted gas for large tx (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 authored Apr 24, 2024
1 parent 2951ad6 commit 23aab72
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
4 changes: 4 additions & 0 deletions executor/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const (
RelayerBytesLength = 48
UpdateCachedValidatorsInterval = 1 * time.Minute
ClaimRewardInterval = 1 * time.Minute
MaxTxSizeForFixGasLimit = 32768 // 32kb
EstimatedTxExtraMetaSize = 1024 // in bytes
GnfdGasPrice = int64(5000000000)
GasLimitRatio = int64(10)
)

var (
Expand Down
42 changes: 30 additions & 12 deletions executor/greenfield_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,32 @@ func (e *GreenfieldExecutor) GetNonceOnNextBlock() (uint64, error) {
}

func (e *GreenfieldExecutor) ClaimPackages(client *GreenfieldClient, payloadBts []byte, aggregatedSig []byte, voteAddressSet []uint64, claimTs int64, oracleSeq uint64, nonce uint64) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
defer cancel()
txRes, err := client.Claims(ctx,
msg := oracletypes.NewMsgClaim(
e.address,
e.getSrcChainId(),
e.getDestChainId(),
oracleSeq,
uint64(claimTs),
payloadBts,
voteAddressSet,
aggregatedSig,
gnfdsdktypes.TxOption{
NoSimulate: true,
GasLimit: uint64(e.config.GreenfieldConfig.GasLimit),
FeeAmount: sdk.NewCoins(sdk.NewCoin(gnfdsdktypes.Denom, sdk.NewInt(e.config.GreenfieldConfig.FeeAmount))),
Nonce: nonce,
},
)
aggregatedSig)
gasLimit, feeAmount, err := e.getGasLimitAndFeeAmount(msg)
if err != nil {
return "", err
}

txOpt := gnfdsdktypes.TxOption{
NoSimulate: true,
GasLimit: uint64(gasLimit),
FeeAmount: sdk.NewCoins(sdk.NewCoin(gnfdsdktypes.Denom, sdk.NewInt(feeAmount))),
Nonce: nonce,
}
ctx, cancel := context.WithTimeout(context.Background(), RPCTimeout)
defer cancel()
resp, err := client.BroadcastTx(ctx, []sdk.Msg{msg}, &txOpt)
if err != nil {
return "", err
}
txRes := resp.TxResponse
if txRes.Codespace == oracletypes.ModuleName && txRes.Code == oracletypes.ErrInvalidReceiveSequence.ABCICode() {
return "", oracletypes.ErrInvalidReceiveSequence
}
Expand Down Expand Up @@ -368,3 +373,16 @@ func (e *GreenfieldExecutor) getDestChainId() uint32 {
func (e *GreenfieldExecutor) getSrcChainId() uint32 {
return uint32(e.config.BSCConfig.ChainId)
}

func (e *GreenfieldExecutor) getGasLimitAndFeeAmount(msg *oracletypes.MsgClaim) (gasLimit int64, feeAmount int64, err error) {
bz, err := msg.Marshal()
if err != nil {
return
}
if len(bz)+EstimatedTxExtraMetaSize >= MaxTxSizeForFixGasLimit {
gasLimit = GasLimitRatio * int64(len(bz)+EstimatedTxExtraMetaSize)
feeAmount = gasLimit * GnfdGasPrice
return
}
return e.config.GreenfieldConfig.GasLimit, e.config.GreenfieldConfig.FeeAmount, nil
}
3 changes: 2 additions & 1 deletion executor/greenfield_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package executor
import (
"context"
"encoding/hex"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/cosmos/cosmos-sdk/x/oracle/types"
"testing"

cbfttypes "github.com/cometbft/cometbft/types"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down

0 comments on commit 23aab72

Please sign in to comment.