Skip to content

Commit

Permalink
fix: some necessary code refactor for banned address and bug fix duri…
Browse files Browse the repository at this point in the history
…ng refactor (#1603)
  • Loading branch information
ws4charlie authored and lumtis committed Jan 24, 2024
1 parent 47c22b7 commit 60f1a5b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 94 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
### Features

* [1591](https://github.com/zeta-chain/node/pull/1591) - support lower gas limit for voting on inbound and outbound transactions
* [1592](https://github.com/zeta-chain/node/issues/1592) - check inbound tracker tx hash against Tss address and some refactor on inTx observation

## Version: v12.0.0

Expand Down
4 changes: 2 additions & 2 deletions zetaclient/btc_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ func (signer *BTCSigner) SignWithdrawTx(
signer.logger.Info().Msgf("sizeLimit %d is less than txSize %d for nonce %d", sizeLimit, txSize, nonce)
}
if txSize < outTxBytesMin { // outbound shouldn't be blocked a low sizeLimit
signer.logger.Warn().Msgf("sizeLimit %d is less than outTxBytesMin %d; use outTxBytesMin", sizeLimit, outTxBytesMin)
signer.logger.Warn().Msgf("txSize %d is less than outTxBytesMin %d; use outTxBytesMin", txSize, outTxBytesMin)
txSize = outTxBytesMin
}
if txSize > outTxBytesMax { // in case of accident
signer.logger.Warn().Msgf("sizeLimit %d is greater than outTxBytesMax %d; use outTxBytesMax", sizeLimit, outTxBytesMax)
signer.logger.Warn().Msgf("txSize %d is greater than outTxBytesMax %d; use outTxBytesMax", txSize, outTxBytesMax)
txSize = outTxBytesMax
}

Expand Down
56 changes: 27 additions & 29 deletions zetaclient/evm_client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zetaclient

import (
"bytes"
"context"
"fmt"
"math"
Expand Down Expand Up @@ -980,13 +979,11 @@ func (ob *EVMChainClient) observeZetaSent(startBlock, toBlock uint64) uint64 {
if event.Raw.BlockNumber > beingScanned {
beingScanned = event.Raw.BlockNumber
}
msg, err := ob.GetInboundVoteMsgForZetaSentEvent(event)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msgf(
"observeZetaSent: error getting inbound vote msg for tx %s chain %d", event.Raw.TxHash.Hex(), ob.chain.ChainId)
msg := ob.GetInboundVoteMsgForZetaSentEvent(event)
if msg == nil {
continue
}
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundMessagePassingExecutionGasLimit, &msg)
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundMessagePassingExecutionGasLimit, msg)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msgf(
"observeZetaSent: error posting event to zeta core for tx %s at height %d for chain %d",
Expand Down Expand Up @@ -1059,13 +1056,24 @@ func (ob *EVMChainClient) observeERC20Deposited(startBlock, toBlock uint64) uint
if event.Raw.BlockNumber > beingScanned {
beingScanned = event.Raw.BlockNumber
}
msg, err := ob.GetInboundVoteMsgForDepositedEvent(event)
tx, _, err := ob.evmClient.TransactionByHash(context.Background(), event.Raw.TxHash)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msgf(
"observeERC20Deposited: error getting inbound vote msg for tx %s chain %d", event.Raw.TxHash.Hex(), ob.chain.ChainId)
ob.logger.ExternalChainWatcher.Err(err).Msgf(
"observeERC20Deposited: TransactionByHash error for tx %s chain %d", tx.Hash().Hex(), ob.chain.ChainId)
return beingScanned - 1 // we have to re-scan from this block next time
}
sender, err := ob.GetTransactionSender(tx, event.Raw.BlockHash, event.Raw.TxIndex)
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msgf(
"observeERC20Deposited: GetTransactionSender error for tx %s chain %d", tx.Hash().Hex(), ob.chain.ChainId)
return beingScanned - 1 // we have to re-scan from this block next time
}

msg := ob.GetInboundVoteMsgForDepositedEvent(event, sender)
if msg == nil {
continue
}
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundExecutionGasLimit, &msg)
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundExecutionGasLimit, msg)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msgf(
"observeERC20Deposited: error posting event to zeta core for tx %s at height %d for chain %d",
Expand Down Expand Up @@ -1108,51 +1116,41 @@ func (ob *EVMChainClient) observeTssRecvd(startBlock, toBlock uint64, flags obse
block, err := ob.GetBlockByNumberCached(bn)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msgf("observeTssRecvd: error getting block %d for chain %d", bn, ob.chain.ChainId)
return startBlock - 1 // we have to re-scan from this block next time
return bn - 1 // we have to re-scan from this block next time
}
for _, tx := range block.Transactions() {
if tx.To() == nil {
continue
}
if bytes.Equal(tx.Data(), []byte(DonationMessage)) {
ob.logger.ExternalChainWatcher.Info().Msgf(
"observeTssRecvd: thank you rich folk for your donation!: %s chain %d", tx.Hash().Hex(), ob.chain.ChainId)
continue
}

if *tx.To() == tssAddress {
receipt, err := ob.evmClient.TransactionReceipt(context.Background(), tx.Hash())
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msgf(
"observeTssRecvd: TransactionReceipt error for tx %s chain %d", tx.Hash().Hex(), ob.chain.ChainId)
return startBlock - 1 // we have to re-scan this block next time
return bn - 1 // we have to re-scan this block next time
}
if receipt.Status != 1 { // 1: successful, 0: failed
ob.logger.ExternalChainWatcher.Info().Msgf("observeTssRecvd: tx %s chain %d failed; don't act", tx.Hash().Hex(), ob.chain.ChainId)
continue
}

from, err := ob.evmClient.TransactionSender(context.Background(), tx, block.Hash(), receipt.TransactionIndex)
sender, err := ob.GetTransactionSender(tx, block.Hash(), receipt.TransactionIndex)
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msgf("observeTssRecvd: TransactionSender error for tx %s", tx.Hash().Hex())
// trying local recovery (assuming LondonSigner dynamic fee tx type) of sender address
signer := ethtypes.NewLondonSigner(big.NewInt(ob.chain.ChainId))
from, err = signer.Sender(tx)
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msgf(
"observeTssRecvd: local recovery of sender address failed for tx %s chain %d", tx.Hash().Hex(), ob.chain.ChainId)
continue
}
ob.logger.ExternalChainWatcher.Err(err).Msgf(
"observeTssRecvd: GetTransactionSender error for tx %s chain %d", tx.Hash().Hex(), ob.chain.ChainId)
return bn - 1 // we have to re-scan this block next time
}
msg := ob.GetInboundVoteMsgForTokenSentToTSS(tx.Hash(), tx.Value(), receipt, from, tx.Data())

msg := ob.GetInboundVoteMsgForTokenSentToTSS(tx, sender, receipt.BlockNumber.Uint64())
if msg == nil {
continue
}
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundExecutionGasLimit, msg)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msgf(
"observeTssRecvd: error posting to zeta core for tx %s at height %d for chain %d", tx.Hash().Hex(), bn, ob.chain.ChainId)
return startBlock - 1 // we have to re-scan this block next time
return bn - 1 // we have to re-scan this block next time
} else if zetaHash != "" {
ob.logger.ExternalChainWatcher.Info().Msgf(
"observeTssRecvd: gas asset deposit detected in tx %s at height %d for chain %d, PostVoteInbound zeta tx: %s ballot %s",
Expand Down
69 changes: 43 additions & 26 deletions zetaclient/inbound_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package zetaclient
import (
"errors"
"fmt"
"math/big"

"github.com/btcsuite/btcd/chaincfg/chainhash"
ethcommon "github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
"golang.org/x/net/context"
Expand Down Expand Up @@ -172,27 +170,30 @@ func (ob *EVMChainClient) CheckReceiptForCoinTypeZeta(txHash string, vote bool)
return "", fmt.Errorf("txHash %s has not been confirmed yet: receipt block %d current block %d", txHash, receipt.BlockNumber, lastHeight)
}

var msg types.MsgVoteOnObservedInboundTx
var msg *types.MsgVoteOnObservedInboundTx
for _, log := range receipt.Logs {
event, err := connector.ParseZetaSent(*log)
if err == nil && event != nil {
// sanity check tx event
err = ob.CheckEvmTxLog(&event.Raw, addrConnector, txHash, TopicsZetaSent)
if err == nil {
msg, err = ob.GetInboundVoteMsgForZetaSentEvent(event)
if err == nil {
msg = ob.GetInboundVoteMsgForZetaSentEvent(event)
if msg != nil {
break
}
} else {
ob.logger.ExternalChainWatcher.Error().Err(err).Msg("CheckEvmTxLog error on ZetaSent event")
}
}
}
if msg == nil {
return "", errors.New("no ZetaSent event found")
}
if !vote {
return msg.Digest(), nil
}

zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundMessagePassingExecutionGasLimit, &msg)
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundMessagePassingExecutionGasLimit, msg)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msg("error posting to zeta core")
return "", err
Expand All @@ -208,39 +209,51 @@ func (ob *EVMChainClient) CheckReceiptForCoinTypeERC20(txHash string, vote bool)
if err != nil {
return "", err
}
// get transaction, receipt and sender
hash := ethcommon.HexToHash(txHash)
tx, _, err := ob.evmClient.TransactionByHash(context.Background(), hash)
if err != nil {
return "", err
}
receipt, err := ob.evmClient.TransactionReceipt(context.Background(), hash)
if err != nil {
return "", err
}
sender, err := ob.evmClient.TransactionSender(context.Background(), tx, receipt.BlockHash, receipt.TransactionIndex)
if err != nil {
return "", err
}

// check if the tx is confirmed
lastHeight := ob.GetLastBlockHeight()
if !ob.HasEnoughConfirmations(receipt, lastHeight) {
return "", fmt.Errorf("txHash %s has not been confirmed yet: receipt block %d current block %d", txHash, receipt.BlockNumber, lastHeight)
}

var msg types.MsgVoteOnObservedInboundTx
var msg *types.MsgVoteOnObservedInboundTx
for _, log := range receipt.Logs {
zetaDeposited, err := custody.ParseDeposited(*log)
if err == nil && zetaDeposited != nil {
// sanity check tx event
err = ob.CheckEvmTxLog(&zetaDeposited.Raw, addrCustory, txHash, TopicsDeposited)
if err == nil {
msg, err = ob.GetInboundVoteMsgForDepositedEvent(zetaDeposited)
msg = ob.GetInboundVoteMsgForDepositedEvent(zetaDeposited, sender)
if err == nil {
break
}
} else {
ob.logger.ExternalChainWatcher.Error().Err(err).Msg("CheckEvmTxLog error on Deposited event")
ob.logger.ExternalChainWatcher.Error().Err(err).Msg("CheckEvmTxLog error on ERC20CustodyDeposited event")
}
}
}
if msg == nil {
return "", errors.New("no ERC20CustodyDeposited event found")
}
if !vote {
return msg.Digest(), nil
}

zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundExecutionGasLimit, &msg)
zetaHash, ballot, err := ob.zetaClient.PostVoteInbound(PostVoteInboundGasLimit, PostVoteInboundExecutionGasLimit, msg)
if err != nil {
ob.logger.ExternalChainWatcher.Error().Err(err).Msg("error posting to zeta core")
return "", err
Expand All @@ -252,6 +265,13 @@ func (ob *EVMChainClient) CheckReceiptForCoinTypeERC20(txHash string, vote bool)
}

func (ob *EVMChainClient) CheckReceiptForCoinTypeGas(txHash string, vote bool) (string, error) {
// TSS address should be set
tssAddress := ob.Tss.EVMAddress()
if tssAddress == (ethcommon.Address{}) {
return "", errors.New("TSS address not set")
}

// check transaction and receipt
hash := ethcommon.HexToHash(txHash)
tx, isPending, err := ob.evmClient.TransactionByHash(context.Background(), hash)
if err != nil {
Expand All @@ -260,6 +280,12 @@ func (ob *EVMChainClient) CheckReceiptForCoinTypeGas(txHash string, vote bool) (
if isPending {
return "", errors.New("tx is still pending")
}
if tx.To() == nil {
return "", errors.New("tx.To() is nil")
}
if *tx.To() != tssAddress {
return "", fmt.Errorf("tx.To() %s is not TSS address", tssAddress.Hex())
}

receipt, err := ob.evmClient.TransactionReceipt(context.Background(), hash)
if err != nil {
Expand All @@ -270,29 +296,20 @@ func (ob *EVMChainClient) CheckReceiptForCoinTypeGas(txHash string, vote bool) (
ob.logger.ExternalChainWatcher.Info().Msgf("tx %s failed; don't act", tx.Hash().Hex())
return "", errors.New("tx not successful yet")
}
sender, err := ob.evmClient.TransactionSender(context.Background(), tx, receipt.BlockHash, receipt.TransactionIndex)
if err != nil {
return "", err
}

// check if the tx is confirmed
lastHeight := ob.GetLastBlockHeight()
if !ob.HasEnoughConfirmations(receipt, lastHeight) {
return "", fmt.Errorf("txHash %s has not been confirmed yet: receipt block %d current block %d", txHash, receipt.BlockNumber, lastHeight)
}

block, err := ob.evmClient.BlockByNumber(context.Background(), receipt.BlockNumber)
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msg("BlockByNumber error")
return "", err
}
from, err := ob.evmClient.TransactionSender(context.Background(), tx, block.Hash(), receipt.TransactionIndex)
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msg("TransactionSender error; trying local recovery (assuming LondonSigner dynamic fee tx type) of sender address")
signer := ethtypes.NewLondonSigner(big.NewInt(ob.chain.ChainId))
from, err = signer.Sender(tx)
if err != nil {
ob.logger.ExternalChainWatcher.Err(err).Msg("local recovery of sender address failed")
return "", err
}
msg := ob.GetInboundVoteMsgForTokenSentToTSS(tx, sender, receipt.BlockNumber.Uint64())
if msg == nil {
return "", errors.New("no message built for token sent to TSS")
}
msg := ob.GetInboundVoteMsgForTokenSentToTSS(tx.Hash(), tx.Value(), receipt, from, tx.Data())
if !vote {
return msg.Digest(), nil
}
Expand Down
Loading

0 comments on commit 60f1a5b

Please sign in to comment.