From 4286a09db49991d18ec84be9b0a5fb73d1bdafac Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Mon, 20 May 2024 10:29:23 -0500 Subject: [PATCH] return error from WaitForZetacoreToCreateBlocks() and SetAccountNumber() and handle them in start.go --- cmd/zetaclientd/p2p_diagnostics.go | 7 ++++-- cmd/zetaclientd/start.go | 15 ++++++++---- .../chains/bitcoin/observer/observer.go | 8 +++---- zetaclient/context/zetacore_context_test.go | 15 ------------ zetaclient/zetacore/client.go | 24 +++++++++---------- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/cmd/zetaclientd/p2p_diagnostics.go b/cmd/zetaclientd/p2p_diagnostics.go index 59c27f4fc7..0c112861e1 100644 --- a/cmd/zetaclientd/p2p_diagnostics.go +++ b/cmd/zetaclientd/p2p_diagnostics.go @@ -176,6 +176,8 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, hotkeyPk cry startLogger.Error().Err(err).Msgf("fail to create stream to peer %s", peer) continue } + + // write a message to the stream message := fmt.Sprintf("round %d %s => %s", round, host.ID().String()[len(host.ID().String())-5:], peer.ID.String()[len(peer.ID.String())-5:]) _, err = stream.Write([]byte(message)) if err != nil { @@ -186,7 +188,8 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, hotkeyPk cry } continue } - //startLogger.Debug().Msgf("wrote %d bytes", nw) + + // read the echoed message buf := make([]byte, 1024) nr, err := stream.Read(buf) if err != nil { @@ -197,13 +200,13 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, hotkeyPk cry } continue } - //startLogger.Debug().Msgf("read %d bytes", nr) startLogger.Debug().Msgf("echoed message: %s", string(buf[:nr])) err = stream.Close() if err != nil { startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer) } + // check if the message is echoed correctly if string(buf[:nr]) != message { startLogger.Error().Msgf("ping-pong failed with peer #(%d): %s; want %s got %s", peerCount, peer, message, string(buf[:nr])) continue diff --git a/cmd/zetaclientd/start.go b/cmd/zetaclientd/start.go index 0f9c4e7b14..3448db3ed3 100644 --- a/cmd/zetaclientd/start.go +++ b/cmd/zetaclientd/start.go @@ -100,12 +100,19 @@ func start(_ *cobra.Command, _ []string) error { } // Wait until zetacore is ready to create blocks - ready := zetacoreClient.WaitForZetacoreToCreateBlocks() - if !ready { - return errors.New("Zetacore is not ready") + err = zetacoreClient.WaitForZetacoreToCreateBlocks() + if err != nil { + startLogger.Error().Err(err).Msg("WaitForZetacoreToCreateBlocks error") + return err } startLogger.Info().Msgf("Zetacore client is ready") - zetacoreClient.SetAccountNumber(authz.ZetaClientGranteeKey) + + // Set grantee account number and sequence number + err = zetacoreClient.SetAccountNumber(authz.ZetaClientGranteeKey) + if err != nil { + startLogger.Error().Err(err).Msg("SetAccountNumber error") + return err + } // cross-check chainid res, err := zetacoreClient.GetNodeInfo() diff --git a/zetaclient/chains/bitcoin/observer/observer.go b/zetaclient/chains/bitcoin/observer/observer.go index 12b0b471da..7162ef5396 100644 --- a/zetaclient/chains/bitcoin/observer/observer.go +++ b/zetaclient/chains/bitcoin/observer/observer.go @@ -352,10 +352,6 @@ func (ob *Observer) GetLastBlockHeight() int64 { } func (ob *Observer) SetLastBlockHeightScanned(height int64) { - if height < 0 { - ob.logger.Chain.Error().Msgf("lastBlockScanned for chain %d is negative: %d", ob.chain.ChainId, height) - return - } atomic.StoreInt64(&ob.lastBlockScanned, height) metrics.LastScannedBlockNumber.WithLabelValues(ob.chain.ChainName.String()).Set(float64(height)) } @@ -670,10 +666,14 @@ func (ob *Observer) BuildBroadcastedTxMap() error { } func (ob *Observer) LoadLastBlock() error { + // Get the latest block number from node bn, err := ob.rpcClient.GetBlockCount() if err != nil { return err } + if bn < 0 { + return fmt.Errorf("LoadLastBlock: negative block number %d", bn) + } //Load persisted block number var lastBlockNum clienttypes.LastBlockSQLType diff --git a/zetaclient/context/zetacore_context_test.go b/zetaclient/context/zetacore_context_test.go index 060ffb8355..409af6154a 100644 --- a/zetaclient/context/zetacore_context_test.go +++ b/zetaclient/context/zetacore_context_test.go @@ -14,16 +14,6 @@ import ( context "github.com/zeta-chain/zetacore/zetaclient/context" ) -func assertPanic(t *testing.T, f func(), errorLog string) { - defer func() { - r := recover() - if r != nil { - require.Contains(t, r, errorLog) - } - }() - f() -} - func getTestCoreContext( evmChain chains.Chain, evmChainParams *observertypes.ChainParams, @@ -149,11 +139,6 @@ func TestNewZetaCoreContext(t *testing.T) { } zetaContext := context.NewZetacoreContext(testCfg) require.NotNil(t, zetaContext) - - // assert btc chain params panic because chain params are not yet updated - assertPanic(t, func() { - zetaContext.GetBTCChainParams() - }, "BTCChain is missing for chainID 0") }) } diff --git a/zetaclient/zetacore/client.go b/zetaclient/zetacore/client.go index 2af72f407c..05a54a4a38 100644 --- a/zetaclient/zetacore/client.go +++ b/zetaclient/zetacore/client.go @@ -7,6 +7,7 @@ import ( "cosmossdk.io/simapp/params" rpcclient "github.com/cometbft/cometbft/rpc/client" + "github.com/pkg/errors" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/zeta-chain/zetacore/app" @@ -150,28 +151,28 @@ func (c *Client) GetAccountNumberAndSequenceNumber(_ authz.KeyType) (uint64, uin return ctx.AccountRetriever.GetAccountNumberSequence(ctx, address) } -func (c *Client) SetAccountNumber(keyType authz.KeyType) { +// SetAccountNumber sets the account number and sequence number for the given keyType +func (c *Client) SetAccountNumber(keyType authz.KeyType) error { ctx, err := c.GetContext() if err != nil { - c.logger.Error().Err(err).Msg("fail to get context") - return + return errors.Wrap(err, "fail to get context") } address, err := c.keys.GetAddress() if err != nil { - c.logger.Error().Err(err).Msg("fail to get address") - return + return errors.Wrap(err, "fail to get address") } accN, seq, err := ctx.AccountRetriever.GetAccountNumberSequence(ctx, address) if err != nil { - c.logger.Error().Err(err).Msg("fail to get account number and sequence number") - return + return errors.Wrap(err, "fail to get account number and sequence number") } c.accountNumber[keyType] = accN c.seqNumber[keyType] = seq + + return nil } -// WaitForZetacoreToCreateBlocks returns true if zetacore is ready to create blocks -func (c *Client) WaitForZetacoreToCreateBlocks() bool { +// WaitForZetacoreToCreateBlocks waits for zetacore to create blocks +func (c *Client) WaitForZetacoreToCreateBlocks() error { retryCount := 0 for { block, err := c.GetLatestZetaBlock() @@ -182,12 +183,11 @@ func (c *Client) WaitForZetacoreToCreateBlocks() bool { retryCount++ c.logger.Debug().Msgf("Failed to get latest Block , Retry : %d/%d", retryCount, DefaultRetryCount) if retryCount > ExtendedRetryCount { - c.logger.Error().Msgf("Zetacore is not ready, waited for %d seconds", DefaultRetryCount*DefaultRetryInterval) - return false + return fmt.Errorf("zetacore is not ready, waited for %d seconds", DefaultRetryCount*DefaultRetryInterval) } time.Sleep(DefaultRetryInterval * time.Second) } - return true + return nil } // UpdateZetacoreContext updates zetacore context