Skip to content

Commit

Permalink
return error from WaitForZetacoreToCreateBlocks() and SetAccountNumbe…
Browse files Browse the repository at this point in the history
…r() and handle them in start.go
  • Loading branch information
ws4charlie committed May 20, 2024
1 parent 4bc58ca commit 4286a09
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
7 changes: 5 additions & 2 deletions cmd/zetaclientd/p2p_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
15 changes: 11 additions & 4 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)

Check warning on line 675 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L674-L675

Added lines #L674 - L675 were not covered by tests
}

//Load persisted block number
var lastBlockNum clienttypes.LastBlockSQLType
Expand Down
15 changes: 0 additions & 15 deletions zetaclient/context/zetacore_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
})
}

Expand Down
24 changes: 12 additions & 12 deletions zetaclient/zetacore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {

Check warning on line 155 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L155

Added line #L155 was not covered by tests
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")

Check warning on line 158 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L158

Added line #L158 was not covered by tests
}
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")

Check warning on line 162 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L160-L162

Added lines #L160 - L162 were not covered by tests
}
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")

Check warning on line 166 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L166

Added line #L166 was not covered by tests
}
c.accountNumber[keyType] = accN
c.seqNumber[keyType] = seq

return nil

Check warning on line 171 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L171

Added line #L171 was not covered by tests
}

// 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 {

Check warning on line 175 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L175

Added line #L175 was not covered by tests
retryCount := 0
for {
block, err := c.GetLatestZetaBlock()
Expand All @@ -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)

Check warning on line 186 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L186

Added line #L186 was not covered by tests
}
time.Sleep(DefaultRetryInterval * time.Second)
}
return true
return nil

Check warning on line 190 in zetaclient/zetacore/client.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/zetacore/client.go#L190

Added line #L190 was not covered by tests
}

// UpdateZetacoreContext updates zetacore context
Expand Down

0 comments on commit 4286a09

Please sign in to comment.