Skip to content

Commit

Permalink
rename crosschainTX to cctx
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Jan 25, 2024
1 parent 1a24f73 commit ec6b0d8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 76 deletions.
3 changes: 3 additions & 0 deletions x/crosschain/keeper/msg_server_migrate_tss_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func (k Keeper) MigrateTSSFundsForChain(ctx sdk.Context, chainID int64, amount s
cctx.GetCurrentOutTxParam().OutboundTxGasLimit = common.EVMSend
// Multiple current gas price with standard multiplier to add some buffer
multipliedGasPrice, err := common.MultiplyGasPrice(medianGasPrice, types.TssMigrationGasMultiplierEVM)
if err != nil {
return err
}
cctx.GetCurrentOutTxParam().OutboundTxGasPrice = multipliedGasPrice.String()
evmFee := sdkmath.NewUint(cctx.GetCurrentOutTxParam().OutboundTxGasLimit).Mul(multipliedGasPrice)
if evmFee.GT(amount) {
Expand Down
152 changes: 76 additions & 76 deletions zetaclient/evm_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (signer *EVMSigner) SignCommandTx(
}

func (signer *EVMSigner) TryProcessOutTx(
crossChainTx *types.CrossChainTx,
cctx *types.CrossChainTx,
outTxMan *OutTxProcessorManager,
outTxID string,
chainclient ChainClient,
Expand All @@ -327,10 +327,10 @@ func (signer *EVMSigner) TryProcessOutTx(
) {
logger := signer.logger.With().
Str("outTxID", outTxID).
Str("SendHash", crossChainTx.Index).
Str("SendHash", cctx.Index).
Logger()
logger.Info().Msgf("start processing outTxID %s", outTxID)
logger.Info().Msgf("EVM Chain TryProcessOutTx: %s, value %d to %s", crossChainTx.Index, crossChainTx.GetCurrentOutTxParam().Amount.BigInt(), crossChainTx.GetCurrentOutTxParam().Receiver)
logger.Info().Msgf("EVM Chain TryProcessOutTx: %s, value %d to %s", cctx.Index, cctx.GetCurrentOutTxParam().Amount.BigInt(), cctx.GetCurrentOutTxParam().Receiver)

defer func() {
outTxMan.EndTryProcess(outTxID)
Expand All @@ -339,23 +339,23 @@ func (signer *EVMSigner) TryProcessOutTx(

var to ethcommon.Address
var toChain *common.Chain
if crossChainTx.CctxStatus.Status == types.CctxStatus_PendingRevert {
to = ethcommon.HexToAddress(crossChainTx.InboundTxParams.Sender)
toChain = common.GetChainFromChainID(crossChainTx.InboundTxParams.SenderChainId)
if cctx.CctxStatus.Status == types.CctxStatus_PendingRevert {
to = ethcommon.HexToAddress(cctx.InboundTxParams.Sender)
toChain = common.GetChainFromChainID(cctx.InboundTxParams.SenderChainId)
if toChain == nil {
logger.Error().Msgf("Unknown chain: %d", crossChainTx.InboundTxParams.SenderChainId)
logger.Error().Msgf("Unknown chain: %d", cctx.InboundTxParams.SenderChainId)
return
}
logger.Info().Msgf("Abort: reverting inbound")
} else if crossChainTx.CctxStatus.Status == types.CctxStatus_PendingOutbound {
to = ethcommon.HexToAddress(crossChainTx.GetCurrentOutTxParam().Receiver)
toChain = common.GetChainFromChainID(crossChainTx.GetCurrentOutTxParam().ReceiverChainId)
} else if cctx.CctxStatus.Status == types.CctxStatus_PendingOutbound {
to = ethcommon.HexToAddress(cctx.GetCurrentOutTxParam().Receiver)
toChain = common.GetChainFromChainID(cctx.GetCurrentOutTxParam().ReceiverChainId)
if toChain == nil {
logger.Error().Msgf("Unknown chain: %d", crossChainTx.GetCurrentOutTxParam().ReceiverChainId)
logger.Error().Msgf("Unknown chain: %d", cctx.GetCurrentOutTxParam().ReceiverChainId)
return
}
} else {
logger.Info().Msgf("Transaction doesn't need to be processed status: %d", crossChainTx.CctxStatus.Status)
logger.Info().Msgf("Transaction doesn't need to be processed status: %d", cctx.CctxStatus.Status)
return
}
evmClient, ok := chainclient.(*EVMChainClient)
Expand All @@ -365,8 +365,8 @@ func (signer *EVMSigner) TryProcessOutTx(
}

// Early return if the cctx is already processed
nonce := crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce
included, confirmed, err := evmClient.IsSendOutTxProcessed(crossChainTx.Index, nonce, crossChainTx.GetCurrentOutTxParam().CoinType, logger)
nonce := cctx.GetCurrentOutTxParam().OutboundTxTssNonce
included, confirmed, err := evmClient.IsSendOutTxProcessed(cctx.Index, nonce, cctx.GetCurrentOutTxParam().CoinType, logger)
if err != nil {
logger.Error().Err(err).Msg("IsSendOutTxProcessed failed")
}
Expand All @@ -376,27 +376,27 @@ func (signer *EVMSigner) TryProcessOutTx(
}

var message []byte
if crossChainTx.GetCurrentOutTxParam().CoinType != common.CoinType_Cmd {
message, err = base64.StdEncoding.DecodeString(crossChainTx.RelayedMessage)
if cctx.GetCurrentOutTxParam().CoinType != common.CoinType_Cmd {
message, err = base64.StdEncoding.DecodeString(cctx.RelayedMessage)
if err != nil {
logger.Err(err).Msgf("decode CCTX.Message %s error", crossChainTx.RelayedMessage)
logger.Err(err).Msgf("decode CCTX.Message %s error", cctx.RelayedMessage)
}
}

gasLimit := crossChainTx.GetCurrentOutTxParam().OutboundTxGasLimit
gasLimit := cctx.GetCurrentOutTxParam().OutboundTxGasLimit
if gasLimit < 100_000 {
gasLimit = 100_000
logger.Warn().Msgf("gasLimit %d is too low; set to %d", crossChainTx.GetCurrentOutTxParam().OutboundTxGasLimit, gasLimit)
logger.Warn().Msgf("gasLimit %d is too low; set to %d", cctx.GetCurrentOutTxParam().OutboundTxGasLimit, gasLimit)
}
if gasLimit > 1_000_000 {
gasLimit = 1_000_000
logger.Warn().Msgf("gasLimit %d is too high; set to %d", crossChainTx.GetCurrentOutTxParam().OutboundTxGasLimit, gasLimit)
logger.Warn().Msgf("gasLimit %d is too high; set to %d", cctx.GetCurrentOutTxParam().OutboundTxGasLimit, gasLimit)
}

logger.Info().Msgf("chain %s minting %d to %s, nonce %d, finalized zeta bn %d", toChain, crossChainTx.InboundTxParams.Amount, to.Hex(), nonce, crossChainTx.InboundTxParams.InboundTxFinalizedZetaHeight)
sendHash, err := hex.DecodeString(crossChainTx.Index[2:]) // remove the leading 0x
logger.Info().Msgf("chain %s minting %d to %s, nonce %d, finalized zeta bn %d", toChain, cctx.InboundTxParams.Amount, to.Hex(), nonce, cctx.InboundTxParams.InboundTxFinalizedZetaHeight)
sendHash, err := hex.DecodeString(cctx.Index[2:]) // remove the leading 0x
if err != nil || len(sendHash) != 32 {
logger.Error().Err(err).Msgf("decode CCTX %s error", crossChainTx.Index)
logger.Error().Err(err).Msgf("decode CCTX %s error", cctx.Index)
return
}
var sendhash [32]byte
Expand All @@ -408,7 +408,7 @@ func (signer *EVMSigner) TryProcessOutTx(
// The code below is a fix for https://github.com/zeta-chain/node/issues/1085
// doesn't close directly the issue because we should determine if we want to keep using SuggestGasPrice if no OutboundTxGasPrice
// we should possibly remove it completely and return an error if no OutboundTxGasPrice is provided because it means no fee is processed on ZetaChain
specified, ok := new(big.Int).SetString(crossChainTx.GetCurrentOutTxParam().OutboundTxGasPrice, 10)
specified, ok := new(big.Int).SetString(cctx.GetCurrentOutTxParam().OutboundTxGasPrice, 10)
if !ok {
if common.IsEthereumChain(toChain.ChainId) {
suggested, err := signer.client.SuggestGasPrice(context.Background())
Expand All @@ -418,7 +418,7 @@ func (signer *EVMSigner) TryProcessOutTx(
}
gasprice = roundUpToNearestGwei(suggested)
} else {
logger.Error().Err(err).Msgf("cannot convert gas price %s ", crossChainTx.GetCurrentOutTxParam().OutboundTxGasPrice)
logger.Error().Err(err).Msgf("cannot convert gas price %s ", cctx.GetCurrentOutTxParam().OutboundTxGasPrice)
return
}
} else {
Expand All @@ -444,138 +444,138 @@ func (signer *EVMSigner) TryProcessOutTx(

var tx *ethtypes.Transaction

if crossChainTx.GetCurrentOutTxParam().CoinType == common.CoinType_Cmd { // admin command
to := ethcommon.HexToAddress(crossChainTx.GetCurrentOutTxParam().Receiver)
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_Cmd { // admin command
to := ethcommon.HexToAddress(cctx.GetCurrentOutTxParam().Receiver)
if to == (ethcommon.Address{}) {
logger.Error().Msgf("invalid receiver %s", crossChainTx.GetCurrentOutTxParam().Receiver)
logger.Error().Msgf("invalid receiver %s", cctx.GetCurrentOutTxParam().Receiver)
return
}

msg := strings.Split(crossChainTx.RelayedMessage, ":")
msg := strings.Split(cctx.RelayedMessage, ":")
if len(msg) != 2 {
logger.Error().Msgf("invalid message %s", msg)
return
}
tx, err = signer.SignCommandTx(msg[0], msg[1], to, crossChainTx.GetCurrentOutTxParam(), gasLimit, gasprice, height)
} else if crossChainTx.InboundTxParams.SenderChainId == zetaBridge.ZetaChain().ChainId && crossChainTx.CctxStatus.Status == types.CctxStatus_PendingOutbound && flags.IsOutboundEnabled {
if crossChainTx.GetCurrentOutTxParam().CoinType == common.CoinType_Gas {
logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignCommandTx(msg[0], msg[1], to, cctx.GetCurrentOutTxParam(), gasLimit, gasprice, height)
} else if cctx.InboundTxParams.SenderChainId == zetaBridge.ZetaChain().ChainId && cctx.CctxStatus.Status == types.CctxStatus_PendingOutbound && flags.IsOutboundEnabled {
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_Gas {
logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignWithdrawTx(
to,
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().Amount.BigInt(),
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
}
if crossChainTx.GetCurrentOutTxParam().CoinType == common.CoinType_ERC20 {
asset := ethcommon.HexToAddress(crossChainTx.InboundTxParams.Asset)
logger.Info().Msgf("SignERC20WithdrawTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_ERC20 {
asset := ethcommon.HexToAddress(cctx.InboundTxParams.Asset)
logger.Info().Msgf("SignERC20WithdrawTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignERC20WithdrawTx(
to,
asset,
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
cctx.GetCurrentOutTxParam().Amount.BigInt(),
gasLimit,
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
}
if crossChainTx.GetCurrentOutTxParam().CoinType == common.CoinType_Zeta {
logger.Info().Msgf("SignOutboundTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_Zeta {
logger.Info().Msgf("SignOutboundTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignOutboundTx(
ethcommon.HexToAddress(crossChainTx.InboundTxParams.Sender),
big.NewInt(crossChainTx.InboundTxParams.SenderChainId),
ethcommon.HexToAddress(cctx.InboundTxParams.Sender),
big.NewInt(cctx.InboundTxParams.SenderChainId),
to,
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
cctx.GetCurrentOutTxParam().Amount.BigInt(),
gasLimit,
message,
sendhash,
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
}
} else if crossChainTx.CctxStatus.Status == types.CctxStatus_PendingRevert && crossChainTx.OutboundTxParams[0].ReceiverChainId == zetaBridge.ZetaChain().ChainId {
if crossChainTx.GetCurrentOutTxParam().CoinType == common.CoinType_Gas {
logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
} else if cctx.CctxStatus.Status == types.CctxStatus_PendingRevert && cctx.OutboundTxParams[0].ReceiverChainId == zetaBridge.ZetaChain().ChainId {
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_Gas {
logger.Info().Msgf("SignWithdrawTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignWithdrawTx(
to,
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().Amount.BigInt(),
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
}
if crossChainTx.GetCurrentOutTxParam().CoinType == common.CoinType_ERC20 {
asset := ethcommon.HexToAddress(crossChainTx.InboundTxParams.Asset)
logger.Info().Msgf("SignERC20WithdrawTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
if cctx.GetCurrentOutTxParam().CoinType == common.CoinType_ERC20 {
asset := ethcommon.HexToAddress(cctx.InboundTxParams.Asset)
logger.Info().Msgf("SignERC20WithdrawTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignERC20WithdrawTx(
to,
asset,
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
cctx.GetCurrentOutTxParam().Amount.BigInt(),
gasLimit,
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
}
} else if crossChainTx.CctxStatus.Status == types.CctxStatus_PendingRevert {
logger.Info().Msgf("SignRevertTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
} else if cctx.CctxStatus.Status == types.CctxStatus_PendingRevert {
logger.Info().Msgf("SignRevertTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignRevertTx(
ethcommon.HexToAddress(crossChainTx.InboundTxParams.Sender),
big.NewInt(crossChainTx.OutboundTxParams[0].ReceiverChainId),
ethcommon.HexToAddress(cctx.InboundTxParams.Sender),
big.NewInt(cctx.OutboundTxParams[0].ReceiverChainId),
to.Bytes(),
big.NewInt(crossChainTx.GetCurrentOutTxParam().ReceiverChainId),
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
big.NewInt(cctx.GetCurrentOutTxParam().ReceiverChainId),
cctx.GetCurrentOutTxParam().Amount.BigInt(),
gasLimit,
message,
sendhash,
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
} else if crossChainTx.CctxStatus.Status == types.CctxStatus_PendingOutbound {
logger.Info().Msgf("SignOutboundTx: %d => %s, nonce %d, gasprice %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
} else if cctx.CctxStatus.Status == types.CctxStatus_PendingOutbound {
logger.Info().Msgf("SignOutboundTx: %d => %s, nonce %d, gasprice %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, gasprice)
tx, err = signer.SignOutboundTx(
ethcommon.HexToAddress(crossChainTx.InboundTxParams.Sender),
big.NewInt(crossChainTx.InboundTxParams.SenderChainId),
ethcommon.HexToAddress(cctx.InboundTxParams.Sender),
big.NewInt(cctx.InboundTxParams.SenderChainId),
to,
crossChainTx.GetCurrentOutTxParam().Amount.BigInt(),
cctx.GetCurrentOutTxParam().Amount.BigInt(),
gasLimit,
message,
sendhash,
crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce,
cctx.GetCurrentOutTxParam().OutboundTxTssNonce,
gasprice,
height,
)
}

if err != nil {
logger.Warn().Err(err).Msgf("signer SignOutbound error: nonce %d chain %d", crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, crossChainTx.GetCurrentOutTxParam().ReceiverChainId)
logger.Warn().Err(err).Msgf("signer SignOutbound error: nonce %d chain %d", cctx.GetCurrentOutTxParam().OutboundTxTssNonce, cctx.GetCurrentOutTxParam().ReceiverChainId)
return
}
logger.Info().Msgf("Key-sign success: %d => %s, nonce %d", crossChainTx.InboundTxParams.SenderChainId, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce)
logger.Info().Msgf("Key-sign success: %d => %s, nonce %d", cctx.InboundTxParams.SenderChainId, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce)

_, err = zetaBridge.GetObserverList()
if err != nil {
logger.Warn().Err(err).Msgf("unable to get observer list: chain %d observation %s", crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, observertypes.ObservationType_OutBoundTx.String())
logger.Warn().Err(err).Msgf("unable to get observer list: chain %d observation %s", cctx.GetCurrentOutTxParam().OutboundTxTssNonce, observertypes.ObservationType_OutBoundTx.String())

}
if tx != nil {
outTxHash := tx.Hash().Hex()
logger.Info().Msgf("on chain %s nonce %d, outTxHash %s signer %s", signer.chain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, outTxHash, myID)
logger.Info().Msgf("on chain %s nonce %d, outTxHash %s signer %s", signer.chain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, outTxHash, myID)
//if len(signers) == 0 || myid == signers[send.OutboundTxParams.Broadcaster] || myid == signers[int(send.OutboundTxParams.Broadcaster+1)%len(signers)] {
backOff := 1000 * time.Millisecond
// retry loop: 1s, 2s, 4s, 8s, 16s in case of RPC error
for i := 0; i < 5; i++ {
logger.Info().Msgf("broadcasting tx %s to chain %s: nonce %d, retry %d", outTxHash, toChain, crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, i)
logger.Info().Msgf("broadcasting tx %s to chain %s: nonce %d, retry %d", outTxHash, toChain, cctx.GetCurrentOutTxParam().OutboundTxTssNonce, i)
// #nosec G404 randomness is not a security issue here
time.Sleep(time.Duration(rand.Intn(1500)) * time.Millisecond) // FIXME: use backoff
err := signer.Broadcast(tx)
if err != nil {
log.Warn().Err(err).Msgf("OutTx Broadcast error")
retry, report := HandleBroadcastError(err, strconv.FormatUint(crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, 10), toChain.String(), outTxHash)
retry, report := HandleBroadcastError(err, strconv.FormatUint(cctx.GetCurrentOutTxParam().OutboundTxTssNonce, 10), toChain.String(), outTxHash)
if report {
signer.reportToOutTxTracker(zetaBridge, toChain.ChainId, tx.Nonce(), outTxHash, logger)
}
Expand All @@ -585,7 +585,7 @@ func (signer *EVMSigner) TryProcessOutTx(
backOff *= 2
continue
}
logger.Info().Msgf("Broadcast success: nonce %d to chain %s outTxHash %s", crossChainTx.GetCurrentOutTxParam().OutboundTxTssNonce, toChain, outTxHash)
logger.Info().Msgf("Broadcast success: nonce %d to chain %s outTxHash %s", cctx.GetCurrentOutTxParam().OutboundTxTssNonce, toChain, outTxHash)
signer.reportToOutTxTracker(zetaBridge, toChain.ChainId, tx.Nonce(), outTxHash, logger)
break // successful broadcast; no need to retry
}
Expand Down

0 comments on commit ec6b0d8

Please sign in to comment.