Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cleaned up nonce and persistence of includedTx #891

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
cctx := k.CreateNewCCTX(ctx, msg, index, types.CctxStatus_PendingInbound, observationChain, receiverChain)
defer func() {
EmitEventInboundFinalized(ctx, &cctx)
cctx.InboundTxParams.InboundTxFinalizedZetaHeight = uint64(ctx.BlockHeight())
k.SetCctxAndNonceToCctxAndInTxHashToCctx(ctx, cctx)
}()
// FinalizeInbound updates CCTX Prices and Nonce
Expand Down Expand Up @@ -134,7 +135,6 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
} else { // Cross Chain SWAP
tmpCtx, commit := ctx.CacheContext()
err = func() error {
cctx.InboundTxParams.InboundTxFinalizedZetaHeight = uint64(ctx.BlockHeader().Height)
err := k.PayGasInZetaAndUpdateCctx(tmpCtx, receiverChain.ChainId, &cctx)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions x/observer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
genesisObservers := genState.Observers
types.VerifyObserverMapper(genesisObservers)
observerCount := uint64(0)
for _, mapper := range genesisObservers {
k.SetObserverMapper(ctx, mapper)
observerCount += uint64(len(mapper.ObserverList))
}
k.SetCoreParams(ctx, types.GetCoreParams())
// Set all the nodeAccount
Expand All @@ -30,6 +32,10 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
if genState.Keygen != nil {
k.SetKeygen(ctx, *genState.Keygen)
}
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: observerCount,
LastChangeHeight: 0,
})

}

Expand Down
108 changes: 15 additions & 93 deletions zetaclient/bitcoin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ type BTCLog struct {
type BitcoinChainClient struct {
*ChainMetrics

chain common.Chain
rpcClient *rpcclient.Client
zetaClient *ZetaCoreBridge
Tss TSSSigner
lastBlock int64
BlockTime uint64 // block time in seconds
includedTx map[string]btcjson.GetTransactionResult // key: chain-nonce
broadcastedTx map[string]chainhash.Hash
nextTxNonce2Broadcast int
mu *sync.Mutex
utxos []btcjson.ListUnspentResult
db *gorm.DB
stop chan struct{}
logger BTCLog
cfg *config.Config
ts *TelemetryServer
chain common.Chain
rpcClient *rpcclient.Client
zetaClient *ZetaCoreBridge
Tss TSSSigner
lastBlock int64
BlockTime uint64 // block time in seconds
includedTx map[string]btcjson.GetTransactionResult // key: chain-nonce
broadcastedTx map[string]chainhash.Hash
mu *sync.Mutex
utxos []btcjson.ListUnspentResult
db *gorm.DB
stop chan struct{}
logger BTCLog
cfg *config.Config
ts *TelemetryServer
}

const (
Expand Down Expand Up @@ -132,12 +131,6 @@ func NewBitcoinClient(chain common.Chain, bridge *ZetaCoreBridge, tss TSSSigner,
return nil, err
}

//Set Next Nonce for broadcasted tx
err = ob.SetNextNonceBroadcast()
if err != nil {
return nil, err
}

return &ob, nil
}

Expand Down Expand Up @@ -669,8 +662,6 @@ func (ob *BitcoinChainClient) SaveBroadcastedTx(txHash chainhash.Hash, nonce uin
outTxID := ob.GetTxID(nonce)
ob.mu.Lock()
ob.broadcastedTx[outTxID] = txHash
ob.nextTxNonce2Broadcast++
ob.ts.SetNextNonce(ob.nextTxNonce2Broadcast)
ob.mu.Unlock()

broadcastEntry := clienttypes.ToTransactionHashSQLType(txHash, outTxID)
Expand Down Expand Up @@ -715,16 +706,6 @@ func (ob *BitcoinChainClient) observeOutTx() {
ob.mu.Lock()
ob.includedTx[outTxID] = *getTxResult
ob.mu.Unlock()

//Save to db
tx, err := clienttypes.ToTransactionResultSQLType(*getTxResult, outTxID)
if err != nil {
ob.logger.ObserveOutTx.Error().Err(err).Msg("observeOutTx: error converting to TransactionResultSQLType")
continue
}
if err := ob.db.Create(&tx).Error; err != nil {
ob.logger.ObserveOutTx.Error().Err(err).Msg("observeOutTx: error saving submitted tx")
}
}
}
}
Expand Down Expand Up @@ -787,22 +768,6 @@ func (ob *BitcoinChainClient) isValidTSSVin(vins []btcjson.Vin) bool {
return true
}

func (ob *BitcoinChainClient) BuildIncludedTxMap() error {
var submittedTransactions []clienttypes.TransactionResultSQLType
if err := ob.db.Find(&submittedTransactions).Error; err != nil {
ob.logger.ChainLogger.Error().Err(err).Msg("error iterating over db")
return err
}
for _, txResult := range submittedTransactions {
r, err := clienttypes.FromTransactionResultSQLType(txResult)
if err != nil {
return err
}
ob.includedTx[txResult.Key] = r
}
return nil
}

func (ob *BitcoinChainClient) BuildBroadcastedTxMap() error {
var broadcastedTransactions []clienttypes.TransactionHashSQLType
if err := ob.db.Find(&broadcastedTransactions).Error; err != nil {
Expand All @@ -815,43 +780,6 @@ func (ob *BitcoinChainClient) BuildBroadcastedTxMap() error {
return nil
}

func (ob *BitcoinChainClient) SetNextNonceBroadcast() error {
nonces, err := ob.zetaClient.GetPendingNonces()
if err != nil {
return err
}

found := false
for _, nonce := range nonces.PendingNonces {
if len(nonce.Tss) == 0 {
continue
}
tssKey, err := NewTSSKey(nonce.Tss)
if err != nil {
continue
}
if ob.chain.ChainId == nonce.ChainId && bytes.Equal(tssKey.PubkeyInBytes, ob.Tss.Pubkey()) {
// find lowest nonce that had not been broadcasted
next2Broadcast := int(nonce.NonceLow)
for nc := nonce.NonceLow; nc < nonce.NonceHigh; nc++ {
if _, exist := ob.broadcastedTx[ob.GetTxID(uint64(nc))]; exist {
next2Broadcast++
continue
}
break
}
ob.nextTxNonce2Broadcast = next2Broadcast
ob.ts.SetNextNonce(ob.nextTxNonce2Broadcast)
found = true
}
}
if !found {
ob.logger.ChainLogger.Error().Msgf("initial nonce for Chain ID: %d not found", ob.chain.ChainId)
}

return nil
}

func (ob *BitcoinChainClient) LoadLastBlock() error {
bn, err := ob.rpcClient.GetBlockCount()
if err != nil {
Expand Down Expand Up @@ -902,12 +830,6 @@ func (ob *BitcoinChainClient) loadDB(dbpath string) error {
return err
}

//Load included transactions
err = ob.BuildIncludedTxMap()
if err != nil {
return err
}

//Load last block
err = ob.LoadLastBlock()
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions zetaclient/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ func (t *TelemetryServer) GetCoreBlockNumber() int64 {
return t.lastCoreBlockNumber
}

func (t *TelemetryServer) SetNextNonce(nextNonce int) {
t.mu.Lock()
t.status.BTCNextNonce = nextNonce
t.mu.Unlock()
}

func (t *TelemetryServer) GetNextNonce() int {
t.mu.Lock()
defer t.mu.Unlock()
return t.status.BTCNextNonce
}

func (t *TelemetryServer) SetNumberOfUTXOs(numberOfUTXOs int) {
t.mu.Lock()
t.status.BTCNumberOfUTXOs = numberOfUTXOs
Expand Down
1 change: 0 additions & 1 deletion zetaclient/types/telemetry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ package types

// Status type for telemetry. More fields can be added as needed
type Status struct {
BTCNextNonce int `json:"btc_next_nonce"`
BTCNumberOfUTXOs int `json:"btc_number_of_utxos"`
}
Loading