Skip to content

Commit

Permalink
initial commit (zeta-chain#710)
Browse files Browse the repository at this point in the history
Co-authored-by: brewmaster012 <[email protected]>
  • Loading branch information
kevinssgh and brewmaster012 authored Jun 19, 2023
1 parent 94bee69 commit 3878c8f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
10 changes: 7 additions & 3 deletions zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion zetaclient/btc_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 19 additions & 14 deletions zetaclient/evm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
25 changes: 15 additions & 10 deletions zetaclient/evm_client_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
clienttypes "github.com/zeta-chain/zetacore/zetaclient/types"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"strconv"
"testing"
)

Expand All @@ -18,17 +19,17 @@ 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) {
suite.Run(t, new(EVMClientTestSuite))
}

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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
16 changes: 8 additions & 8 deletions zetaclient/types/sql_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit 3878c8f

Please sign in to comment.