Skip to content

Commit

Permalink
fix: lower the gas price multiplier for evm chains (#1881)
Browse files Browse the repository at this point in the history
* lower the gas price multiplier for evm chains

* moved constants into zetaclient

* created compliance package and move gas price multiplier function to zetabridge package
  • Loading branch information
ws4charlie authored Mar 20, 2024
1 parent 337e485 commit 80a42da
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 74 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
### Fixes

* [1861](https://github.com/zeta-chain/node/pull/1861) - fix `ObserverSlashAmount` invalid read
* [1880](https://github.com/zeta-chain/node/issues/1880) - lower the gas price multiplier for EVM chains.
* [1633](https://github.com/zeta-chain/node/issues/1633) - zetaclient should be able to pick up new connector and erc20Custody addresses

### Chores
Expand Down
3 changes: 0 additions & 3 deletions common/constant.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package common

const (
// DefaultGasPriceMultiplier is the default gas price multiplier for outbond txs
DefaultGasPriceMultiplier = 2

// DonationMessage is the message for donation transactions
// Transaction sent to the TSS or ERC20 Custody address containing this message are considered as a donation
DonationMessage = "I am rich!"
Expand Down
5 changes: 3 additions & 2 deletions zetaclient/bitcoin/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/compliance"
"github.com/zeta-chain/zetacore/zetaclient/config"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
Expand Down Expand Up @@ -712,7 +713,7 @@ func (ob *BTCChainClient) IsInTxRestricted(inTx *BTCInTxEvnet) bool {
receiver = parsedAddress.Hex()
}
if config.ContainRestrictedAddress(inTx.FromAddress, receiver) {
clientcommon.PrintComplianceLog(ob.logger.WatchInTx, ob.logger.Compliance,
compliance.PrintComplianceLog(ob.logger.WatchInTx, ob.logger.Compliance,
false, ob.chain.ChainId, inTx.TxHash, inTx.FromAddress, receiver, "BTC")
return true
}
Expand Down Expand Up @@ -1230,7 +1231,7 @@ func (ob *BTCChainClient) checkTssOutTxResult(cctx *types.CrossChainTx, hash *ch
}

// differentiate between normal and restricted cctx
if clientcommon.IsCctxRestricted(cctx) {
if compliance.IsCctxRestricted(cctx) {
err = ob.checkTSSVoutCancelled(params, rawResult.Vout)
if err != nil {
return errors.Wrapf(err, "checkTssOutTxResult: invalid TSS Vout in cancelled outTx %s nonce %d", hash, nonce)
Expand Down
6 changes: 5 additions & 1 deletion zetaclient/bitcoin/bitcoin_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/testutils"
"github.com/zeta-chain/zetacore/zetaclient/testutils/stub"
)
Expand Down Expand Up @@ -155,7 +156,10 @@ func TestCalcDepositorFee828440(t *testing.T) {
var blockVb btcjson.GetBlockVerboseTxResult
err := testutils.LoadObjectFromJSONFile(&blockVb, path.Join("../", testutils.TestDataPathBTC, "block_trimmed_8332_828440.json"))
require.NoError(t, err)
dynamicFee828440 := DepositorFee(32 * common.DefaultGasPriceMultiplier)
avgGasRate := float64(32.0)
// #nosec G701 test - always in range
gasRate := int64(avgGasRate * clientcommon.BTCOuttxGasPriceMultiplier)
dynamicFee828440 := DepositorFee(gasRate)

// should return default fee if it's a regtest block
fee := CalcDepositorFee(&blockVb, 18444, &chaincfg.RegressionNetParams, log.Logger)
Expand Down
5 changes: 3 additions & 2 deletions zetaclient/bitcoin/bitcoin_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

ethcommon "github.com/ethereum/go-ethereum/common"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/compliance"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/outtxprocessor"
Expand Down Expand Up @@ -345,9 +346,9 @@ func (signer *BTCSigner) TryProcessOutTx(
gasprice.Add(gasprice, satPerByte)

// compliance check
cancelTx := clientcommon.IsCctxRestricted(cctx)
cancelTx := compliance.IsCctxRestricted(cctx)
if cancelTx {
clientcommon.PrintComplianceLog(logger, signer.loggerCompliance,
compliance.PrintComplianceLog(logger, signer.loggerCompliance,
true, btcClient.chain.ChainId, cctx.Index, cctx.InboundTxParams.Sender, params.Receiver, "BTC")
amount = 0.0 // zero out the amount to cancel the tx
}
Expand Down
4 changes: 3 additions & 1 deletion zetaclient/bitcoin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/btcsuite/btcutil"
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"

"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -188,7 +189,8 @@ func CalcDepositorFee(blockVb *btcjson.GetBlockVerboseTxResult, chainID int64, n
feeRate = defaultDepositorFeeRate // use default fee rate if calculation fails, should not happen
logger.Error().Err(err).Msgf("cannot calculate fee rate for block %d", blockVb.Height)
}
feeRate = feeRate * common.DefaultGasPriceMultiplier
// #nosec G701 always in range
feeRate = int64(float64(feeRate) * clientcommon.BTCOuttxGasPriceMultiplier)
return DepositorFee(feeRate)
}

Expand Down
9 changes: 9 additions & 0 deletions zetaclient/common/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

const (
// EVMOuttxGasPriceMultiplier is the default gas price multiplier for EVM-chain outbond txs
EVMOuttxGasPriceMultiplier = 1.2

// BTCOuttxGasPriceMultiplier is the default gas price multiplier for BTC outbond txs
BTCOuttxGasPriceMultiplier = 2.0
)
18 changes: 18 additions & 0 deletions zetaclient/common/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common

import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

type ClientLogger struct {
Std zerolog.Logger
Compliance zerolog.Logger
}

func DefaultLoggers() ClientLogger {
return ClientLogger{
Std: log.Logger,
Compliance: log.Logger,
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package common
package compliance

import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
"github.com/zeta-chain/zetacore/zetaclient/config"
)

type ClientLogger struct {
Std zerolog.Logger
Compliance zerolog.Logger
}

func DefaultLoggers() ClientLogger {
return ClientLogger{
Std: log.Logger,
Compliance: log.Logger,
}
}

// IsCctxRestricted returns true if the cctx involves restricted addresses
func IsCctxRestricted(cctx *crosschaintypes.CrossChainTx) bool {
sender := cctx.InboundTxParams.Sender
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package compliance

import (
"path"
Expand Down
3 changes: 2 additions & 1 deletion zetaclient/evm/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/compliance"
"github.com/zeta-chain/zetacore/zetaclient/config"
clienttypes "github.com/zeta-chain/zetacore/zetaclient/types"
"gorm.io/driver/sqlite"
Expand Down Expand Up @@ -330,7 +331,7 @@ func (ob *ChainClient) IsSendOutTxProcessed(cctx *crosschaintypes.CrossChainTx,
logger = logger.With().Str("sendID", sendID).Logger()

// compliance check, special handling the cancelled cctx
if clientcommon.IsCctxRestricted(cctx) {
if compliance.IsCctxRestricted(cctx) {
recvStatus := common.ReceiveStatus_Failed
if receipt.Status == 1 {
recvStatus = common.ReceiveStatus_Success
Expand Down
5 changes: 3 additions & 2 deletions zetaclient/evm/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/compliance"
"github.com/zeta-chain/zetacore/zetaclient/interfaces"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/outtxprocessor"
Expand Down Expand Up @@ -349,8 +350,8 @@ func (signer *Signer) TryProcessOutTx(

var tx *ethtypes.Transaction
// compliance check goes first
if clientcommon.IsCctxRestricted(cctx) {
clientcommon.PrintComplianceLog(logger, signer.logger.Compliance,
if compliance.IsCctxRestricted(cctx) {
compliance.PrintComplianceLog(logger, signer.logger.Compliance,
true, evmClient.chain.ChainId, cctx.Index, cctx.InboundTxParams.Sender, txData.to.Hex(), cctx.GetCurrentOutTxParam().CoinType.String())
tx, err = signer.SignCancelTx(txData.nonce, txData.gasPrice, height) // cancel the tx
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions zetaclient/evm/inbounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/evm/erc20custody.sol"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/evm/zetaconnector.non-eth.sol"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"
"github.com/zeta-chain/zetacore/zetaclient/compliance"
"github.com/zeta-chain/zetacore/zetaclient/config"
clienttypes "github.com/zeta-chain/zetacore/zetaclient/types"

Expand Down Expand Up @@ -228,7 +228,7 @@ func (ob *ChainClient) BuildInboundVoteMsgForDepositedEvent(event *erc20custody.
maybeReceiver = parsedAddress.Hex()
}
if config.ContainRestrictedAddress(sender.Hex(), clienttypes.BytesToEthHex(event.Recipient), maybeReceiver) {
clientcommon.PrintComplianceLog(ob.logger.ExternalChainWatcher, ob.logger.Compliance,
compliance.PrintComplianceLog(ob.logger.ExternalChainWatcher, ob.logger.Compliance,
false, ob.chain.ChainId, event.Raw.TxHash.Hex(), sender.Hex(), clienttypes.BytesToEthHex(event.Recipient), "ERC20")
return nil
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func (ob *ChainClient) BuildInboundVoteMsgForZetaSentEvent(event *zetaconnector.
// compliance check
sender := event.ZetaTxSenderAddress.Hex()
if config.ContainRestrictedAddress(sender, destAddr, event.SourceTxOriginAddress.Hex()) {
clientcommon.PrintComplianceLog(ob.logger.ExternalChainWatcher, ob.logger.Compliance,
compliance.PrintComplianceLog(ob.logger.ExternalChainWatcher, ob.logger.Compliance,
false, ob.chain.ChainId, event.Raw.TxHash.Hex(), sender, destAddr, "Zeta")
return nil
}
Expand Down Expand Up @@ -322,7 +322,7 @@ func (ob *ChainClient) BuildInboundVoteMsgForTokenSentToTSS(tx *ethrpc.Transacti
maybeReceiver = parsedAddress.Hex()
}
if config.ContainRestrictedAddress(sender.Hex(), maybeReceiver) {
clientcommon.PrintComplianceLog(ob.logger.ExternalChainWatcher, ob.logger.Compliance,
compliance.PrintComplianceLog(ob.logger.ExternalChainWatcher, ob.logger.Compliance,
false, ob.chain.ChainId, tx.Hash, sender.Hex(), sender.Hex(), "Gas")
return nil
}
Expand Down
43 changes: 43 additions & 0 deletions zetaclient/zetabridge/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package zetabridge

const (
// DefaultGasLimit is the default gas limit used for broadcasting txs
DefaultGasLimit = 200_000

// PostGasPriceGasLimit is the gas limit for voting new gas price
PostGasPriceGasLimit = 1_500_000

// AddTxHashToOutTxTrackerGasLimit is the gas limit for adding tx hash to out tx tracker
AddTxHashToOutTxTrackerGasLimit = 200_000

// PostBlameDataGasLimit is the gas limit for voting on blames
PostBlameDataGasLimit = 200_000

// DefaultRetryCount is the number of retries for broadcasting a tx
DefaultRetryCount = 5

// ExtendedRetryCount is an extended number of retries for broadcasting a tx, used in keygen operations
ExtendedRetryCount = 15

// DefaultRetryInterval is the interval between retries in seconds
DefaultRetryInterval = 5

// MonitorVoteInboundTxResultInterval is the interval between retries for monitoring tx result in seconds
MonitorVoteInboundTxResultInterval = 5

// MonitorVoteInboundTxResultRetryCount is the number of retries to fetch monitoring tx result
MonitorVoteInboundTxResultRetryCount = 20

// PostVoteOutboundGasLimit is the gas limit for voting on observed outbound tx
PostVoteOutboundGasLimit = 400_000

// PostVoteOutboundRevertGasLimit is the gas limit for voting on observed outbound tx for revert (when outbound fails)
// The value needs to be higher because reverting implies interacting with the EVM to perform swaps for the gas token
PostVoteOutboundRevertGasLimit = 1_500_000

// MonitorVoteOutboundTxResultInterval is the interval between retries for monitoring tx result in seconds
MonitorVoteOutboundTxResultInterval = 5

// MonitorVoteOutboundTxResultRetryCount is the number of retries to fetch monitoring tx result
MonitorVoteOutboundTxResultRetryCount = 20
)
60 changes: 17 additions & 43 deletions zetaclient/zetabridge/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"cosmossdk.io/math"
appcontext "github.com/zeta-chain/zetacore/zetaclient/app_context"
authz2 "github.com/zeta-chain/zetacore/zetaclient/authz"
clientcommon "github.com/zeta-chain/zetacore/zetaclient/common"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
Expand All @@ -19,47 +20,15 @@ import (
observerTypes "github.com/zeta-chain/zetacore/x/observer/types"
)

const (
// DefaultGasLimit is the default gas limit used for broadcasting txs
DefaultGasLimit = 200_000

// PostGasPriceGasLimit is the gas limit for voting new gas price
PostGasPriceGasLimit = 1_500_000

// AddTxHashToOutTxTrackerGasLimit is the gas limit for adding tx hash to out tx tracker
AddTxHashToOutTxTrackerGasLimit = 200_000

// PostBlameDataGasLimit is the gas limit for voting on blames
PostBlameDataGasLimit = 200_000

// DefaultRetryCount is the number of retries for broadcasting a tx
DefaultRetryCount = 5

// ExtendedRetryCount is an extended number of retries for broadcasting a tx, used in keygen operations
ExtendedRetryCount = 15

// DefaultRetryInterval is the interval between retries in seconds
DefaultRetryInterval = 5

// MonitorVoteInboundTxResultInterval is the interval between retries for monitoring tx result in seconds
MonitorVoteInboundTxResultInterval = 5

// MonitorVoteInboundTxResultRetryCount is the number of retries to fetch monitoring tx result
MonitorVoteInboundTxResultRetryCount = 20

// PostVoteOutboundGasLimit is the gas limit for voting on observed outbound tx
PostVoteOutboundGasLimit = 400_000

// PostVoteOutboundRevertGasLimit is the gas limit for voting on observed outbound tx for revert (when outbound fails)
// The value needs to be higher because reverting implies interacting with the EVM to perform swaps for the gas token
PostVoteOutboundRevertGasLimit = 1_500_000

// MonitorVoteOutboundTxResultInterval is the interval between retries for monitoring tx result in seconds
MonitorVoteOutboundTxResultInterval = 5

// MonitorVoteOutboundTxResultRetryCount is the number of retries to fetch monitoring tx result
MonitorVoteOutboundTxResultRetryCount = 20
)
// GasPriceMultiplier returns the gas price multiplier for the given chain
func GasPriceMultiplier(chainID int64) (float64, error) {
if common.IsEVMChain(chainID) {
return clientcommon.EVMOuttxGasPriceMultiplier, nil
} else if common.IsBitcoinChain(chainID) {
return clientcommon.BTCOuttxGasPriceMultiplier, nil
}
return 0, fmt.Errorf("cannot get gas price multiplier for unknown chain %d", chainID)
}

func (b *ZetaCoreBridge) WrapMessageWithAuthz(msg sdk.Msg) (sdk.Msg, authz2.Signer, error) {
msgURL := sdk.MsgTypeURL(msg)
Expand All @@ -75,8 +44,13 @@ func (b *ZetaCoreBridge) WrapMessageWithAuthz(msg sdk.Msg) (sdk.Msg, authz2.Sign
}

func (b *ZetaCoreBridge) PostGasPrice(chain common.Chain, gasPrice uint64, supply string, blockNum uint64) (string, error) {
// double the gas price to avoid gas price spike
gasPrice = gasPrice * common.DefaultGasPriceMultiplier
// apply gas price multiplier for the chain
multiplier, err := GasPriceMultiplier(chain.ChainId)
if err != nil {
return "", err
}
// #nosec G701 always in range
gasPrice = uint64(float64(gasPrice) * multiplier)
signerAddress := b.keys.GetOperatorAddress().String()
msg := types.NewMsgGasPriceVoter(signerAddress, chain.ChainId, gasPrice, supply, blockNum)

Expand Down
Loading

0 comments on commit 80a42da

Please sign in to comment.