Skip to content

Commit 297baea

Browse files
ws4charlielumtis
andauthored
refactor: removed uncessary panics in zetaclientd process (#2210)
* removed uncessary panics in zetaclientd process * added changelog entry * fix unit test and renamed as * throw and handle error from MonitorCore() * resolved conflict in ObserveInTx() * return error from WaitForZetacoreToCreateBlocks() and SetAccountNumber() and handle them in start.go --------- Co-authored-by: Lucas Bertrand <[email protected]>
1 parent ca15dc2 commit 297baea

39 files changed

+445
-393
lines changed

changelog.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
### Refactor
2020

2121
* [2094](https://github.com/zeta-chain/node/pull/2094) - upgrade go-tss to use cosmos v0.47
22-
* [2110](https://github.com/zeta-chain/node/pull/2110) - move non-query rate limiter logic to zetaclient side and code refactor.
22+
* [2110](https://github.com/zeta-chain/node/pull/2110) - move non-query rate limiter logic to zetaclient side and code refactor
2323
* [2032](https://github.com/zeta-chain/node/pull/2032) - improve some general structure of the ZetaClient codebase
2424
* [2097](https://github.com/zeta-chain/node/pull/2097) - refactor lightclient verification flags to account for individual chains
2525
* [2071](https://github.com/zeta-chain/node/pull/2071) - Modify chains struct to add all chain related information
2626
* [2118](https://github.com/zeta-chain/node/pull/2118) - consolidate inbound and outbound naming
2727
* [2124](https://github.com/zeta-chain/node/pull/2124) - removed unused variables and method
28-
* [2150](https://github.com/zeta-chain/node/pull/2150) - created `chains` `zetacore` `orchestrator` packages in zetaclient and reorganized source files accordingly.
28+
* [2150](https://github.com/zeta-chain/node/pull/2150) - created `chains` `zetacore` `orchestrator` packages in zetaclient and reorganized source files accordingly
29+
* [2210](https://github.com/zeta-chain/node/pull/2210) - removed uncessary panics in the zetaclientd process
2930
* [2205](https://github.com/zeta-chain/node/pull/2205) - remove deprecated variables pre-v17
3031

3132
### Tests

cmd/zetaclientd-supervisor/main.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ import (
1616
)
1717

1818
func main() {
19+
// load zetaclient config
1920
cfg, err := config.Load(app.DefaultNodeHome)
2021
if err != nil {
21-
panic(fmt.Errorf("failed to load config: %w", err))
22+
fmt.Println("failed to load config: ", err)
23+
os.Exit(1)
2224
}
25+
2326
// log outputs must be serialized since we are writing log messages in this process and
2427
// also directly from the zetaclient process
2528
serializedStdout := &serializedWriter{upstream: os.Stdout}
@@ -38,13 +41,15 @@ func main() {
3841

3942
hotkeyPassword, tssPassword, err := promptPasswords()
4043
if err != nil {
41-
panic(fmt.Errorf("unable to get passwords: %w", err))
44+
logger.Error().Err(err).Msg("unable to get passwords")
45+
os.Exit(1)
4246
}
4347

4448
_, enableAutoDownload := os.LookupEnv("ZETACLIENTD_SUPERVISOR_ENABLE_AUTO_DOWNLOAD")
4549
supervisor, err := newZetaclientdSupervisor(cfg.ZetaCoreURL, logger, enableAutoDownload)
4650
if err != nil {
47-
panic(fmt.Errorf("unable to get supervisor: %w", err))
51+
logger.Error().Err(err).Msg("unable to get supervisor")
52+
os.Exit(1)
4853
}
4954
supervisor.Start(ctx)
5055

cmd/zetaclientd/debug.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/zeta-chain/zetacore/zetaclient/config"
2525
clientcontext "github.com/zeta-chain/zetacore/zetaclient/context"
2626
"github.com/zeta-chain/zetacore/zetaclient/keys"
27-
"github.com/zeta-chain/zetacore/zetaclient/metrics"
2827
"github.com/zeta-chain/zetacore/zetaclient/zetacore"
2928
)
3029

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

65-
telemetryServer := metrics.NewTelemetryServer()
66-
go func() {
67-
err := telemetryServer.Start()
68-
if err != nil {
69-
panic("telemetryServer error")
70-
}
71-
}()
72-
64+
// create a new zetacore client
7365
client, err := zetacore.NewClient(
7466
&keys.Keys{OperatorAddress: sdk.MustAccAddressFromBech32(sample.AccAddress())},
7567
debugArgs.zetaNode,
7668
"",
7769
debugArgs.zetaChainID,
7870
false,
79-
telemetryServer)
80-
71+
nil)
8172
if err != nil {
8273
return err
8374
}
@@ -89,14 +80,13 @@ func DebugCmd() *cobra.Command {
8980
if err != nil {
9081
return err
9182
}
92-
9383
chain := chains.GetChainFromChainID(chainID)
9484
if chain == nil {
9585
return fmt.Errorf("invalid chain id")
9686
}
9787

88+
// get ballot identifier according to the chain type
9889
if chains.IsEVMChain(chain.ChainId) {
99-
10090
evmObserver := evmobserver.Observer{
10191
Mu: &sync.Mutex{},
10292
}
@@ -204,6 +194,7 @@ func DebugCmd() *cobra.Command {
204194
}
205195
fmt.Println("BallotIdentifier : ", ballotIdentifier)
206196

197+
// query ballot
207198
ballot, err := client.GetBallot(ballotIdentifier)
208199
if err != nil {
209200
return err

cmd/zetaclientd/p2p_diagnostics.go

+53-51
Original file line numberDiff line numberDiff line change
@@ -151,68 +151,70 @@ func RunDiagnostics(startLogger zerolog.Logger, peers p2p.AddrList, hotkeyPk cry
151151
// every 1min, print out the p2p diagnostic
152152
ticker := time.NewTicker(time.Duration(cfg.P2PDiagnosticTicker) * time.Second)
153153
round := 0
154-
for {
155-
select {
156-
case <-ticker.C:
157-
round++
158-
// Now, look for others who have announced
159-
// This is like your friend telling you the location to meet you.
160-
startLogger.Info().Msgf("Searching for other peers...")
161-
peerChan, err := routingDiscovery.FindPeers(context.Background(), "ZetaZetaOpenTheDoor")
154+
155+
for range ticker.C {
156+
round++
157+
// Now, look for others who have announced
158+
// This is like your friend telling you the location to meet you.
159+
startLogger.Info().Msgf("Searching for other peers...")
160+
peerChan, err := routingDiscovery.FindPeers(context.Background(), "ZetaZetaOpenTheDoor")
161+
if err != nil {
162+
return err
163+
}
164+
165+
peerCount := 0
166+
okPingPongCount := 0
167+
for peer := range peerChan {
168+
peerCount++
169+
if peer.ID == host.ID() {
170+
startLogger.Info().Msgf("Found myself #(%d): %s", peerCount, peer)
171+
continue
172+
}
173+
startLogger.Info().Msgf("Found peer #(%d): %s; pinging the peer...", peerCount, peer)
174+
stream, err := host.NewStream(context.Background(), peer.ID, protocol.ID(ProtocolID))
162175
if err != nil {
163-
panic(err)
176+
startLogger.Error().Err(err).Msgf("fail to create stream to peer %s", peer)
177+
continue
164178
}
165179

166-
peerCount := 0
167-
okPingPongCount := 0
168-
for peer := range peerChan {
169-
peerCount++
170-
if peer.ID == host.ID() {
171-
startLogger.Info().Msgf("Found myself #(%d): %s", peerCount, peer)
172-
continue
173-
}
174-
startLogger.Info().Msgf("Found peer #(%d): %s; pinging the peer...", peerCount, peer)
175-
stream, err := host.NewStream(context.Background(), peer.ID, protocol.ID(ProtocolID))
176-
if err != nil {
177-
startLogger.Error().Err(err).Msgf("fail to create stream to peer %s", peer)
178-
continue
179-
}
180-
message := fmt.Sprintf("round %d %s => %s", round, host.ID().String()[len(host.ID().String())-5:], peer.ID.String()[len(peer.ID.String())-5:])
181-
_, err = stream.Write([]byte(message))
182-
if err != nil {
183-
startLogger.Error().Err(err).Msgf("fail to write to stream to peer %s", peer)
184-
err = stream.Close()
185-
if err != nil {
186-
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
187-
}
188-
continue
189-
}
190-
//startLogger.Debug().Msgf("wrote %d bytes", nw)
191-
buf := make([]byte, 1024)
192-
nr, err := stream.Read(buf)
180+
// write a message to the stream
181+
message := fmt.Sprintf("round %d %s => %s", round, host.ID().String()[len(host.ID().String())-5:], peer.ID.String()[len(peer.ID.String())-5:])
182+
_, err = stream.Write([]byte(message))
183+
if err != nil {
184+
startLogger.Error().Err(err).Msgf("fail to write to stream to peer %s", peer)
185+
err = stream.Close()
193186
if err != nil {
194-
startLogger.Error().Err(err).Msgf("fail to read from stream to peer %s", peer)
195-
err = stream.Close()
196-
if err != nil {
197-
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
198-
}
199-
continue
187+
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
200188
}
201-
//startLogger.Debug().Msgf("read %d bytes", nr)
202-
startLogger.Debug().Msgf("echoed message: %s", string(buf[:nr]))
189+
continue
190+
}
191+
192+
// read the echoed message
193+
buf := make([]byte, 1024)
194+
nr, err := stream.Read(buf)
195+
if err != nil {
196+
startLogger.Error().Err(err).Msgf("fail to read from stream to peer %s", peer)
203197
err = stream.Close()
204198
if err != nil {
205199
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
206200
}
201+
continue
202+
}
203+
startLogger.Debug().Msgf("echoed message: %s", string(buf[:nr]))
204+
err = stream.Close()
205+
if err != nil {
206+
startLogger.Warn().Err(err).Msgf("fail to close stream to peer %s", peer)
207+
}
207208

208-
if string(buf[:nr]) != message {
209-
startLogger.Error().Msgf("ping-pong failed with peer #(%d): %s; want %s got %s", peerCount, peer, message, string(buf[:nr]))
210-
continue
211-
}
212-
startLogger.Info().Msgf("ping-pong success with peer #(%d): %s;", peerCount, peer)
213-
okPingPongCount++
209+
// check if the message is echoed correctly
210+
if string(buf[:nr]) != message {
211+
startLogger.Error().Msgf("ping-pong failed with peer #(%d): %s; want %s got %s", peerCount, peer, message, string(buf[:nr]))
212+
continue
214213
}
215-
startLogger.Info().Msgf("Expect %d peers in total; successful pings (%d/%d)", peerCount, okPingPongCount, peerCount-1)
214+
startLogger.Info().Msgf("ping-pong success with peer #(%d): %s;", peerCount, peer)
215+
okPingPongCount++
216216
}
217+
startLogger.Info().Msgf("Expect %d peers in total; successful pings (%d/%d)", peerCount, okPingPongCount, peerCount-1)
217218
}
219+
return nil
218220
}

cmd/zetaclientd/start.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,38 @@ func start(_ *cobra.Command, _ []string) error {
9191
}
9292
}()
9393

94-
// CreateZetaCoreClient: zetacore client is used for all communication to zetacore , which this client connects to.
94+
// CreateZetacoreClient: zetacore client is used for all communication to zetacore , which this client connects to.
9595
// Zetacore accumulates votes , and provides a centralized source of truth for all clients
96-
zetacoreClient, err := CreateZetaCoreClient(cfg, telemetryServer, hotkeyPass)
96+
zetacoreClient, err := CreateZetacoreClient(cfg, telemetryServer, hotkeyPass)
9797
if err != nil {
98-
panic(err)
98+
startLogger.Error().Err(err).Msg("CreateZetacoreClient error")
99+
return err
100+
}
101+
102+
// Wait until zetacore is ready to create blocks
103+
err = zetacoreClient.WaitForZetacoreToCreateBlocks()
104+
if err != nil {
105+
startLogger.Error().Err(err).Msg("WaitForZetacoreToCreateBlocks error")
106+
return err
99107
}
100-
zetacoreClient.WaitForCoreToCreateBlocks()
101108
startLogger.Info().Msgf("Zetacore client is ready")
102-
zetacoreClient.SetAccountNumber(authz.ZetaClientGranteeKey)
109+
110+
// Set grantee account number and sequence number
111+
err = zetacoreClient.SetAccountNumber(authz.ZetaClientGranteeKey)
112+
if err != nil {
113+
startLogger.Error().Err(err).Msg("SetAccountNumber error")
114+
return err
115+
}
103116

104117
// cross-check chainid
105118
res, err := zetacoreClient.GetNodeInfo()
106119
if err != nil {
107-
panic(err)
120+
startLogger.Error().Err(err).Msg("GetNodeInfo error")
121+
return err
108122
}
109123

110124
if strings.Compare(res.GetDefaultNodeInfo().Network, cfg.ChainID) != 0 {
111-
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)
125+
startLogger.Warn().Msgf("chain id mismatch, zetacore chain id %s, zetaclient configured chain id %s; reset zetaclient chain id", res.GetDefaultNodeInfo().Network, cfg.ChainID)
112126
cfg.ChainID = res.GetDefaultNodeInfo().Network
113127
err := zetacoreClient.UpdateChainID(cfg.ChainID)
114128
if err != nil {
@@ -118,7 +132,12 @@ func start(_ *cobra.Command, _ []string) error {
118132

119133
// CreateAuthzSigner : which is used to sign all authz messages . All votes broadcast to zetacore are wrapped in authz exec .
120134
// 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
121-
CreateAuthzSigner(zetacoreClient.GetKeys().GetOperatorAddress().String(), zetacoreClient.GetKeys().GetAddress())
135+
signerAddress, err := zetacoreClient.GetKeys().GetAddress()
136+
if err != nil {
137+
startLogger.Error().Err(err).Msg("error getting signer address")
138+
return err
139+
}
140+
CreateAuthzSigner(zetacoreClient.GetKeys().GetOperatorAddress().String(), signerAddress)
122141
startLogger.Debug().Msgf("CreateAuthzSigner is ready")
123142

124143
// Initialize core parameters from zetacore
@@ -265,8 +284,12 @@ func start(_ *cobra.Command, _ []string) error {
265284
}
266285

267286
// Orchestrator wraps the zetacore client and adds the observers and signer maps to it . This is the high level object used for CCTX interactions
268-
mo1 := orchestrator.NewOrchestrator(zetacoreClient, signerMap, observerMap, masterLogger, telemetryServer)
269-
mo1.MonitorCore(appContext)
287+
orchestrator := orchestrator.NewOrchestrator(zetacoreClient, signerMap, observerMap, masterLogger, telemetryServer)
288+
err = orchestrator.MonitorCore(appContext)
289+
if err != nil {
290+
startLogger.Error().Err(err).Msg("Orchestrator failed to start")
291+
return err
292+
}
270293

271294
// start zeta supply checker
272295
// TODO: enable

cmd/zetaclientd/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func CreateAuthzSigner(granter string, grantee sdk.AccAddress) {
2121
authz.SetupAuthZSignerList(granter, grantee)
2222
}
2323

24-
func CreateZetaCoreClient(cfg config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetacore.Client, error) {
24+
func CreateZetacoreClient(cfg config.Config, telemetry *metrics.TelemetryServer, hotkeyPassword string) (*zetacore.Client, error) {
2525
hotKey := cfg.AuthzHotkey
2626
if cfg.HsmMode {
2727
hotKey = cfg.HsmHotKey

0 commit comments

Comments
 (0)