From 3878c8f9e56ad7668a796eb90a5c05f28e0cbb32 Mon Sep 17 00:00:00 2001 From: kevinssgh <79858682+kevinssgh@users.noreply.github.com> Date: Mon, 19 Jun 2023 14:46:40 -0400 Subject: [PATCH] initial commit (#710) Co-authored-by: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> --- zetaclient/bitcoin_client.go | 10 +++++++--- zetaclient/btc_signer.go | 2 +- zetaclient/evm_client.go | 33 ++++++++++++++++++-------------- zetaclient/evm_client_db_test.go | 25 ++++++++++++++---------- zetaclient/types/sql_evm.go | 16 ++++++++-------- 5 files changed, 50 insertions(+), 36 deletions(-) diff --git a/zetaclient/bitcoin_client.go b/zetaclient/bitcoin_client.go index b89429bb9d..40ad7a92ad 100644 --- a/zetaclient/bitcoin_client.go +++ b/zetaclient/bitcoin_client.go @@ -309,8 +309,7 @@ func (ob *BitcoinChainClient) ConfirmationsThreshold(amount *big.Int) int64 { // returns isIncluded, isConfirmed, Error func (ob *BitcoinChainClient) IsSendOutTxProcessed(sendHash string, nonce int, _ common.CoinType, logger zerolog.Logger) (bool, bool, error) { - chain := ob.chain.ChainId - outTxID := fmt.Sprintf("%d-%d", chain, nonce) + outTxID := ob.GetTxID(uint64(nonce)) logger.Info().Msgf("IsSendOutTxProcessed %s", outTxID) ob.mu.Lock() @@ -653,7 +652,7 @@ func (ob *BitcoinChainClient) observeOutTx() { continue } for _, tracker := range trackers { - outTxID := fmt.Sprintf("%d-%d", tracker.ChainId, tracker.Nonce) + outTxID := ob.GetTxID(tracker.Nonce) ob.logger.ObserveOutTx.Info().Msgf("tracker outTxID: %s", outTxID) for _, txHash := range tracker.HashList { hash, err := chainhash.NewHashFromStr(txHash.TxHash) @@ -775,3 +774,8 @@ func (ob *BitcoinChainClient) loadDB(dbpath string) error { return err } + +func (ob *BitcoinChainClient) GetTxID(nonce uint64) string { + tssAddr := ob.Tss.BTCAddress() + return fmt.Sprintf("%d-%s-%d", ob.chain.ChainId, tssAddr, nonce) +} diff --git a/zetaclient/btc_signer.go b/zetaclient/btc_signer.go index e2bb1c6001..b91330d530 100644 --- a/zetaclient/btc_signer.go +++ b/zetaclient/btc_signer.go @@ -269,7 +269,7 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out logger.Err(err).Msgf("Unable to add to tracker on ZetaCore: nonce %d chain %s outTxHash %s", send.GetCurrentOutTxParam().OutboundTxTssNonce, btcClient.chain.ChainName, outTxHash) } logger.Info().Msgf("Broadcast to core successful %s", zetaHash) - outTxID := fmt.Sprintf("%d-%d", btcClient.chain.ChainId, send.GetCurrentOutTxParam().OutboundTxTssNonce) + outTxID := btcClient.GetTxID(send.GetCurrentOutTxParam().OutboundTxTssNonce) // Save successfully broadcasted transaction to btc chain client btcClient.mu.Lock() diff --git a/zetaclient/evm_client.go b/zetaclient/evm_client.go index 5c922a1bc6..836ac074fa 100644 --- a/zetaclient/evm_client.go +++ b/zetaclient/evm_client.go @@ -72,8 +72,8 @@ type EVMChainClient struct { txWatchList map[ethcommon.Hash]string mu *sync.Mutex db *gorm.DB - outTXConfirmedReceipts map[int]*ethtypes.Receipt - outTXConfirmedTransaction map[int]*ethtypes.Transaction + outTXConfirmedReceipts map[string]*ethtypes.Receipt + outTXConfirmedTransaction map[string]*ethtypes.Transaction MinNonce int64 MaxNonce int64 OutTxChan chan OutTx // send to this channel if you want something back! @@ -106,8 +106,8 @@ func NewEVMChainClient(bridge *ZetaCoreBridge, tss TSSSigner, dbpath string, met ob.zetaClient = bridge ob.txWatchList = make(map[ethcommon.Hash]string) ob.Tss = tss - ob.outTXConfirmedReceipts = make(map[int]*ethtypes.Receipt) - ob.outTXConfirmedTransaction = make(map[int]*ethtypes.Transaction) + ob.outTXConfirmedReceipts = make(map[string]*ethtypes.Receipt) + ob.outTXConfirmedTransaction = make(map[string]*ethtypes.Transaction) ob.OutTxChan = make(chan OutTx, 100) logFile, err := os.OpenFile(ob.chain.ChainName.String()+"_debug.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) @@ -211,7 +211,7 @@ func (ob *EVMChainClient) Stop() { } err = dbInst.Close() if err != nil { - ob.logger.ChainLogger.Error().Err(err).Msg("error closing pendingUtxos") + ob.logger.ChainLogger.Error().Err(err).Msg("error closing database") } ob.logger.ChainLogger.Info().Msgf("%s observer stopped", ob.chain.String()) @@ -221,8 +221,8 @@ func (ob *EVMChainClient) Stop() { // If isConfirmed, it also post to ZetaCore func (ob *EVMChainClient) IsSendOutTxProcessed(sendHash string, nonce int, cointype common.CoinType, logger zerolog.Logger) (bool, bool, error) { ob.mu.Lock() - receipt, found1 := ob.outTXConfirmedReceipts[nonce] - transaction, found2 := ob.outTXConfirmedTransaction[nonce] + receipt, found1 := ob.outTXConfirmedReceipts[ob.GetIndex(nonce)] + transaction, found2 := ob.outTXConfirmedTransaction[ob.GetIndex(nonce)] ob.mu.Unlock() found := found1 && found2 if !found { @@ -417,17 +417,17 @@ func (ob *EVMChainClient) observeOutTx() { receipt, transaction, err := ob.queryTxByHash(txHash.TxHash, int64(nonceInt)) if err == nil && receipt != nil { // confirmed ob.mu.Lock() - ob.outTXConfirmedReceipts[int(nonceInt)] = receipt - ob.outTXConfirmedTransaction[int(nonceInt)] = transaction + ob.outTXConfirmedReceipts[ob.GetIndex(int(nonceInt))] = receipt + ob.outTXConfirmedTransaction[ob.GetIndex(int(nonceInt))] = transaction ob.mu.Unlock() // Convert to DB types - rec, err := clienttypes.ToReceiptSQLType(receipt, int(nonceInt)) + rec, err := clienttypes.ToReceiptSQLType(receipt, ob.GetIndex(int(nonceInt))) if err != nil { ob.logger.ObserveOutTx.Error().Err(err).Msgf("error converting receipt to db type") continue } - trans, err := clienttypes.ToTransactionSQLType(transaction, int(nonceInt)) + trans, err := clienttypes.ToTransactionSQLType(transaction, ob.GetIndex(int(nonceInt))) if err != nil { ob.logger.ObserveOutTx.Err(err).Msgf("error converting transaction to db type") continue @@ -460,7 +460,7 @@ func (ob *EVMChainClient) observeOutTx() { // receipt non-nil, err nil: txHash confirmed func (ob *EVMChainClient) queryTxByHash(txHash string, nonce int64) (*ethtypes.Receipt, *ethtypes.Transaction, error) { logger := ob.logger.ObserveOutTx.With().Str("txHash", txHash).Int64("nonce", nonce).Logger() - if ob.outTXConfirmedReceipts[int(nonce)] != nil && ob.outTXConfirmedTransaction[int(nonce)] != nil { + if ob.outTXConfirmedReceipts[ob.GetIndex(int(nonce))] != nil && ob.outTXConfirmedTransaction[ob.GetIndex(int(nonce))] != nil { return nil, nil, fmt.Errorf("queryTxByHash: txHash %s receipts already recorded", txHash) } ctxt, cancel := context.WithTimeout(context.Background(), 3*time.Second) @@ -933,7 +933,7 @@ func (ob *EVMChainClient) BuildReceiptsMap() error { if err != nil { return err } - ob.outTXConfirmedReceipts[receipt.Nonce] = r + ob.outTXConfirmedReceipts[receipt.Identifier] = r } return nil @@ -951,7 +951,7 @@ func (ob *EVMChainClient) BuildTransactionsMap() error { if err != nil { return err } - ob.outTXConfirmedTransaction[transaction.Nonce] = trans + ob.outTXConfirmedTransaction[transaction.Identifier] = trans } return nil } @@ -1021,3 +1021,8 @@ func (ob *EVMChainClient) SetMinAndMaxNonce(trackers []cctxtypes.OutTxTracker) e } return nil } + +func (ob *EVMChainClient) GetIndex(nonce int) string { + tssAddr := ob.Tss.EVMAddress().String() + return fmt.Sprintf("%d-%s-%d", ob.chain.ChainId, tssAddr, nonce) +} diff --git a/zetaclient/evm_client_db_test.go b/zetaclient/evm_client_db_test.go index 23492d5277..2c0dd4a203 100644 --- a/zetaclient/evm_client_db_test.go +++ b/zetaclient/evm_client_db_test.go @@ -9,6 +9,7 @@ import ( clienttypes "github.com/zeta-chain/zetacore/zetaclient/types" "gorm.io/driver/sqlite" "gorm.io/gorm" + "strconv" "testing" ) @@ -18,8 +19,8 @@ const NumOfEntries = 2 type EVMClientTestSuite struct { suite.Suite db *gorm.DB - outTXConfirmedReceipts map[int]*ethtypes.Receipt - outTXConfirmedTransaction map[int]*ethtypes.Transaction + outTXConfirmedReceipts map[string]*ethtypes.Receipt + outTXConfirmedTransaction map[string]*ethtypes.Transaction } func TestEVMClient(t *testing.T) { @@ -27,8 +28,8 @@ func TestEVMClient(t *testing.T) { } func (suite *EVMClientTestSuite) SetupTest() { - suite.outTXConfirmedReceipts = map[int]*ethtypes.Receipt{} - suite.outTXConfirmedTransaction = map[int]*ethtypes.Transaction{} + suite.outTXConfirmedReceipts = map[string]*ethtypes.Receipt{} + suite.outTXConfirmedTransaction = map[string]*ethtypes.Transaction{} db, err := gorm.Open(sqlite.Open(TempSQLiteDbPath), &gorm.Config{}) suite.NoError(err) @@ -56,29 +57,33 @@ func (suite *EVMClientTestSuite) SetupTest() { BlockNumber: nil, TransactionIndex: uint(i), } - r, _ := clienttypes.ToReceiptSQLType(receipt, i) + r, _ := clienttypes.ToReceiptSQLType(receipt, strconv.Itoa(i)) dbc := suite.db.Create(r) suite.NoError(dbc.Error) - suite.outTXConfirmedReceipts[i] = receipt + suite.outTXConfirmedReceipts[strconv.Itoa(i)] = receipt } //Create some transaction entries in the DB for i := 0; i < NumOfEntries; i++ { transaction := legacyTx(i) - trans, _ := clienttypes.ToTransactionSQLType(transaction, i) + trans, _ := clienttypes.ToTransactionSQLType(transaction, strconv.Itoa(i)) dbc := suite.db.Create(trans) suite.NoError(dbc.Error) - suite.outTXConfirmedTransaction[i] = transaction + suite.outTXConfirmedTransaction[strconv.Itoa(i)] = transaction } } func (suite *EVMClientTestSuite) TearDownSuite() { + dbInst, err := suite.db.DB() + suite.NoError(err) + err = dbInst.Close() + suite.NoError(err) } func (suite *EVMClientTestSuite) TestEVMReceipts() { for key, value := range suite.outTXConfirmedReceipts { var receipt clienttypes.ReceiptSQLType - suite.db.Where("Nonce = ?", key).First(&receipt) + suite.db.Where("Identifier = ?", key).First(&receipt) r, _ := clienttypes.FromReceiptDBType(receipt.Receipt) suite.Equal(*r, *value) @@ -88,7 +93,7 @@ func (suite *EVMClientTestSuite) TestEVMReceipts() { func (suite *EVMClientTestSuite) TestEVMTransactions() { for key, value := range suite.outTXConfirmedTransaction { var transaction clienttypes.TransactionSQLType - suite.db.Where("Nonce = ?", key).First(&transaction) + suite.db.Where("Identifier = ?", key).First(&transaction) trans, _ := clienttypes.FromTransactionDBType(transaction.Transaction) diff --git a/zetaclient/types/sql_evm.go b/zetaclient/types/sql_evm.go index 390fa8eb38..d5fe5bfd3f 100644 --- a/zetaclient/types/sql_evm.go +++ b/zetaclient/types/sql_evm.go @@ -53,13 +53,13 @@ type TransactionDB struct { type ReceiptSQLType struct { gorm.Model - Nonce int - Receipt ReceiptDB `gorm:"embedded"` + Identifier string + Receipt ReceiptDB `gorm:"embedded"` } type TransactionSQLType struct { gorm.Model - Nonce int + Identifier string Transaction TransactionDB `gorm:"embedded"` } @@ -110,14 +110,14 @@ func FromReceiptDBType(receipt ReceiptDB) (*ethtypes.Receipt, error) { return res, err } -func ToReceiptSQLType(receipt *ethtypes.Receipt, nonce int) (*ReceiptSQLType, error) { +func ToReceiptSQLType(receipt *ethtypes.Receipt, index string) (*ReceiptSQLType, error) { r, err := ToReceiptDBType(receipt) if err != nil { return nil, err } return &ReceiptSQLType{ - Nonce: nonce, - Receipt: r, + Identifier: index, + Receipt: r, }, nil } @@ -142,13 +142,13 @@ func FromTransactionDBType(transaction TransactionDB) (*ethtypes.Transaction, er return res, err } -func ToTransactionSQLType(transaction *ethtypes.Transaction, nonce int) (*TransactionSQLType, error) { +func ToTransactionSQLType(transaction *ethtypes.Transaction, index string) (*TransactionSQLType, error) { trans, err := ToTransactionDBType(transaction) if err != nil { return nil, err } return &TransactionSQLType{ - Nonce: nonce, + Identifier: index, Transaction: trans, }, nil }