From 09ab8f0c54f8948b13652998b3c3d2299aabb31a Mon Sep 17 00:00:00 2001 From: pingke Date: Wed, 30 Oct 2024 18:46:05 +0800 Subject: [PATCH 1/2] add checkState before report it --- cmd/priv-dashboard/main.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd/priv-dashboard/main.go b/cmd/priv-dashboard/main.go index f7fdf56b..6fb9ed5c 100644 --- a/cmd/priv-dashboard/main.go +++ b/cmd/priv-dashboard/main.go @@ -6,6 +6,7 @@ package main import ( "context" "encoding/json" + "errors" "flag" "fmt" "io" @@ -85,6 +86,11 @@ func (d *dashboard) ReportStateHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf(`{"status":"error", "err message":"%s"}`, err.Error()))) return } + if err = d.checkState(&state); err != nil { + log.Warn("check node state failed", "error", err.Error()) + w.Write([]byte(fmt.Sprintf(`{"status":"error", "err message":"%s"}`, err.Error()))) + return + } log.Info("Get state from peer", "peer id", state.Id, "state", string(body)) d.lock.Lock() @@ -99,9 +105,26 @@ func (d *dashboard) ReportStateHandler(w http.ResponseWriter, r *http.Request) { d.m.SetSubmissionState(state.Id, state.Version, state.Address, shard.ShardId, shard.Miner, submission.Succeeded, submission.Failed, submission.Dropped, submission.LastSucceededTime) } + w.Write([]byte(`{"status":"ok"}`)) } +func (d *dashboard) checkState(state *node.NodeState) error { + if state == nil { + return errors.New("state is nil") + } + if len(state.Shards) == 0 { + return errors.New("no shard exist in the node state") + } + for _, shard := range state.Shards { + if shard.SyncState == nil || shard.MiningState == nil || shard.SubmissionState == nil { + return errors.New("invalid shard state") + } + } + + return nil +} + func (d *dashboard) Report() { var ( minerOfShards = make(map[uint64]map[common.Address]struct{}) From 1ec2b13078a83b77aa390bbc7727325ff98057fa Mon Sep 17 00:00:00 2001 From: pingke Date: Wed, 30 Oct 2024 23:53:34 +0800 Subject: [PATCH 2/2] add error info --- cmd/priv-dashboard/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/priv-dashboard/main.go b/cmd/priv-dashboard/main.go index 6fb9ed5c..762b57b3 100644 --- a/cmd/priv-dashboard/main.go +++ b/cmd/priv-dashboard/main.go @@ -105,7 +105,7 @@ func (d *dashboard) ReportStateHandler(w http.ResponseWriter, r *http.Request) { d.m.SetSubmissionState(state.Id, state.Version, state.Address, shard.ShardId, shard.Miner, submission.Succeeded, submission.Failed, submission.Dropped, submission.LastSucceededTime) } - + w.Write([]byte(`{"status":"ok"}`)) } @@ -114,11 +114,11 @@ func (d *dashboard) checkState(state *node.NodeState) error { return errors.New("state is nil") } if len(state.Shards) == 0 { - return errors.New("no shard exist in the node state") + return fmt.Errorf("no shard exist in the node state %s", state.Id) } for _, shard := range state.Shards { if shard.SyncState == nil || shard.MiningState == nil || shard.SubmissionState == nil { - return errors.New("invalid shard state") + return fmt.Errorf("invalid shard state in the node state %s", state.Id) } }