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

refactor: removed uncessary panics in zetaclientd process #2210

Merged
merged 9 commits into from
May 21, 2024
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* [2097](https://github.com/zeta-chain/node/pull/2097) - refactor lightclient verification flags to account for individual chains
* [2071](https://github.com/zeta-chain/node/pull/2071) - Modify chains struct to add all chain related information
* [2124](https://github.com/zeta-chain/node/pull/2124) - removed unused variables and method
* [2150](https://github.com/zeta-chain/node/pull/2150) - created `chains` `zetacore` `orchestrator` packages in zetaclient and reorganized source files accordingly.
* [2150](https://github.com/zeta-chain/node/pull/2150) - created `chains` `zetacore` `orchestrator` packages in zetaclient and reorganized source files accordingly
* [2210](https://github.com/zeta-chain/node/pull/2210) - removed uncessary panics in the zetaclientd process

### Tests

Expand Down
11 changes: 8 additions & 3 deletions cmd/zetaclientd-supervisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import (
)

func main() {
// load zetaclient config
cfg, err := config.Load(app.DefaultNodeHome)
if err != nil {
panic(fmt.Errorf("failed to load config: %w", err))
fmt.Println("failed to load config: ", err)
os.Exit(1)
}

// log outputs must be serialized since we are writing log messages in this process and
// also directly from the zetaclient process
serializedStdout := &serializedWriter{upstream: os.Stdout}
Expand All @@ -38,13 +41,15 @@ func main() {

hotkeyPassword, tssPassword, err := promptPasswords()
if err != nil {
panic(fmt.Errorf("unable to get passwords: %w", err))
logger.Error().Err(err).Msg("unable to get passwords")
os.Exit(1)
}

_, enableAutoDownload := os.LookupEnv("ZETACLIENTD_SUPERVISOR_ENABLE_AUTO_DOWNLOAD")
supervisor, err := newZetaclientdSupervisor(cfg.ZetaCoreURL, logger, enableAutoDownload)
if err != nil {
panic(fmt.Errorf("unable to get supervisor: %w", err))
logger.Error().Err(err).Msg("unable to get supervisor")
os.Exit(1)
}
supervisor.Start(ctx)

Expand Down
17 changes: 4 additions & 13 deletions cmd/zetaclientd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/config"
clientcontext "github.com/zeta-chain/zetacore/zetaclient/context"
"github.com/zeta-chain/zetacore/zetaclient/keys"
"github.com/zeta-chain/zetacore/zetaclient/metrics"
"github.com/zeta-chain/zetacore/zetaclient/zetacore"
)

Expand Down Expand Up @@ -62,22 +61,14 @@ func DebugCmd() *cobra.Command {
var ballotIdentifier string
chainLogger := zerolog.New(io.Discard).Level(zerolog.Disabled)

telemetryServer := metrics.NewTelemetryServer()
go func() {
err := telemetryServer.Start()
if err != nil {
panic("telemetryServer error")
}
}()

// create a new zetacore client
client, err := zetacore.NewClient(
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
debugArgs.zetaNode,
"",
debugArgs.zetaChainID,
false,
telemetryServer)

nil)
if err != nil {
return err
}
Expand All @@ -89,14 +80,13 @@ func DebugCmd() *cobra.Command {
if err != nil {
return err
}

chain := chains.GetChainFromChainID(chainID)
if chain == nil {
return fmt.Errorf("invalid chain id")
}

// get ballot identifier according to the chain type
if chains.IsEVMChain(chain.ChainId) {

evmObserver := evmobserver.Observer{
Mu: &sync.Mutex{},
}
Expand Down Expand Up @@ -204,6 +194,7 @@ func DebugCmd() *cobra.Command {
}
fmt.Println("BallotIdentifier : ", ballotIdentifier)

// query ballot
ballot, err := client.GetBallot(ballotIdentifier)
if err != nil {
return err
Expand Down
103 changes: 51 additions & 52 deletions cmd/zetaclientd/p2p_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,68 +151,67 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, hotkeyPk cry
// every 1min, print out the p2p diagnostic
ticker := time.NewTicker(time.Duration(cfg.P2PDiagnosticTicker) * time.Second)
round := 0
for {
select {
case <-ticker.C:
round++
// Now, look for others who have announced
// This is like your friend telling you the location to meet you.
startLogger.Info().Msgf("Searching for other peers...")
peerChan, err := routingDiscovery.FindPeers(context.Background(), "ZetaZetaOpenTheDoor")

for range ticker.C {
round++
// Now, look for others who have announced
// This is like your friend telling you the location to meet you.
startLogger.Info().Msgf("Searching for other peers...")
peerChan, err := routingDiscovery.FindPeers(context.Background(), "ZetaZetaOpenTheDoor")
if err != nil {
return err
}

peerCount := 0
okPingPongCount := 0
for peer := range peerChan {
peerCount++
if peer.ID == host.ID() {
startLogger.Info().Msgf("Found myself #(%d): %s", peerCount, peer)
continue
}
startLogger.Info().Msgf("Found peer #(%d): %s; pinging the peer...", peerCount, peer)
stream, err := host.NewStream(context.Background(), peer.ID, protocol.ID(ProtocolID))
if err != nil {
panic(err)
startLogger.Error().Err(err).Msgf("fail to create stream to peer %s", peer)
continue
}

peerCount := 0
okPingPongCount := 0
for peer := range peerChan {
peerCount++
if peer.ID == host.ID() {
startLogger.Info().Msgf("Found myself #(%d): %s", peerCount, peer)
continue
}
startLogger.Info().Msgf("Found peer #(%d): %s; pinging the peer...", peerCount, peer)
stream, err := host.NewStream(context.Background(), peer.ID, protocol.ID(ProtocolID))
if err != nil {
startLogger.Error().Err(err).Msgf("fail to create stream to peer %s", peer)
continue
}
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 {
startLogger.Error().Err(err).Msgf("fail to write to stream to peer %s", peer)
err = stream.Close()
if err != nil {
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
}
continue
}
//startLogger.Debug().Msgf("wrote %d bytes", nw)
buf := make([]byte, 1024)
nr, err := stream.Read(buf)
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 {
startLogger.Error().Err(err).Msgf("fail to write to stream to peer %s", peer)
err = stream.Close()
if err != nil {
startLogger.Error().Err(err).Msgf("fail to read from stream to peer %s", peer)
err = stream.Close()
if err != nil {
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
}
continue
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
}
//startLogger.Debug().Msgf("read %d bytes", nr)
startLogger.Debug().Msgf("echoed message: %s", string(buf[:nr]))
continue
}
//startLogger.Debug().Msgf("wrote %d bytes", nw)
buf := make([]byte, 1024)
nr, err := stream.Read(buf)
if err != nil {
startLogger.Error().Err(err).Msgf("fail to read from stream to peer %s", peer)
err = stream.Close()
if err != nil {
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
}
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)
}

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
}
startLogger.Info().Msgf("ping-pong success with peer #(%d): %s;", peerCount, peer)
okPingPongCount++
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
}
startLogger.Info().Msgf("Expect %d peers in total; successful pings (%d/%d)", peerCount, okPingPongCount, peerCount-1)
startLogger.Info().Msgf("ping-pong success with peer #(%d): %s;", peerCount, peer)
okPingPongCount++
}
startLogger.Info().Msgf("Expect %d peers in total; successful pings (%d/%d)", peerCount, okPingPongCount, peerCount-1)
}
return nil
}
26 changes: 19 additions & 7 deletions cmd/zetaclientd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,31 @@ func start(_ *cobra.Command, _ []string) error {
}
}()

// CreateZetaCoreClient: zetacore client is used for all communication to zetacore , which this client connects to.
// CreateZetacoreClient: zetacore client is used for all communication to zetacore , which this client connects to.
// Zetacore accumulates votes , and provides a centralized source of truth for all clients
zetacoreClient, err := CreateZetaCoreClient(cfg, telemetryServer, hotkeyPass)
zetacoreClient, err := CreateZetacoreClient(cfg, telemetryServer, hotkeyPass)
if err != nil {
panic(err)
startLogger.Error().Err(err).Msg("CreateZetacoreClient error")
return err
}

// Wait until zetacore is ready to create blocks
ready := zetacoreClient.WaitForZetacoreToCreateBlocks()
if !ready {
return errors.New("Zetacore is not ready")
}
zetacoreClient.WaitForCoreToCreateBlocks()
startLogger.Info().Msgf("Zetacore client is ready")
zetacoreClient.SetAccountNumber(authz.ZetaClientGranteeKey)

// cross-check chainid
res, err := zetacoreClient.GetNodeInfo()
if err != nil {
panic(err)
startLogger.Error().Err(err).Msg("GetNodeInfo error")
return err
}

if strings.Compare(res.GetDefaultNodeInfo().Network, cfg.ChainID) != 0 {
startLogger.Warn().Msgf("chain id mismatch, zeta-core chain id %s, zeta client chain id %s; reset zeta client chain id", res.GetDefaultNodeInfo().Network, cfg.ChainID)
startLogger.Warn().Msgf("chain id mismatch, zetacore chain id %s, zetaclient configured chain id %s; reset zetaclient chain id", res.GetDefaultNodeInfo().Network, cfg.ChainID)
cfg.ChainID = res.GetDefaultNodeInfo().Network
err := zetacoreClient.UpdateChainID(cfg.ChainID)
if err != nil {
Expand All @@ -118,7 +125,12 @@ func start(_ *cobra.Command, _ []string) error {

// CreateAuthzSigner : which is used to sign all authz messages . All votes broadcast to zetacore are wrapped in authz exec .
// This is to ensure that the user does not need to keep their operator key online , and can use a cold key to sign votes
CreateAuthzSigner(zetacoreClient.GetKeys().GetOperatorAddress().String(), zetacoreClient.GetKeys().GetAddress())
signerAddress, err := zetacoreClient.GetKeys().GetAddress()
if err != nil {
startLogger.Error().Err(err).Msg("error getting signer address")
return err
}
CreateAuthzSigner(zetacoreClient.GetKeys().GetOperatorAddress().String(), signerAddress)
startLogger.Debug().Msgf("CreateAuthzSigner is ready")

// Initialize core parameters from zetacore
Expand Down
2 changes: 1 addition & 1 deletion cmd/zetaclientd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func CreateAuthzSigner(granter string, grantee sdk.AccAddress) {
authz.SetupAuthZSignerList(granter, grantee)
}

func CreateZetaCoreClient(cfg config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetacore.Client, error) {
func CreateZetacoreClient(cfg config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetacore.Client, error) {
hotKey := cfg.AuthzHotkey
if cfg.HsmMode {
hotKey = cfg.HsmHotKey
Expand Down
Loading
Loading