From 177dc821ac09d0a1056990bf036cf930d4156eb0 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 20 Sep 2019 19:17:45 +0200 Subject: [PATCH 1/2] consensus/clique: clique status api call --- consensus/clique/api.go | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/consensus/clique/api.go b/consensus/clique/api.go index 6bcf987af55e..c40bed3f522a 100644 --- a/consensus/clique/api.go +++ b/consensus/clique/api.go @@ -17,6 +17,8 @@ package clique import ( + "fmt" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core/types" @@ -117,3 +119,60 @@ func (api *API) Discard(address common.Address) { delete(api.clique.proposals, address) } + +type CliqueStatus struct { + InturnPercent float64 `json:"inturnPercent"` + SigningStatus map[common.Address]int `json:"sealerActivity""` + NumBlocks uint64 `json:"numBlocks"` +} + +// Status returns the status of the last N blocks, +// - the number of active signers, +// - the number of signers, +// - the percentage of in-turn blocks +func (api *API) Status() (*CliqueStatus, error) { + var ( + numBlocks = uint64(64) + header = api.chain.CurrentHeader() + diff = uint64(0) + optimals = 0 + ) + snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) + if err != nil { + return nil, err + } + var ( + signers = snap.signers() + end = header.Number.Uint64() + start = end - numBlocks + ) + if numBlocks > end { + start = 1 + numBlocks = end - start + } + signStatus := make(map[common.Address]int) + for _, s := range signers { + signStatus[s] = 0 + } + for n := start; n < end; n++ { + h := api.chain.GetHeaderByNumber(n) + if h != nil { + if h.Difficulty.Cmp(diffInTurn) == 0 { + optimals += 1 + } + diff += h.Difficulty.Uint64() + if sealer, err := api.clique.Author(h); err != nil { + return nil, err + } else { + signStatus[sealer] += 1 + } + } else { + return nil, fmt.Errorf("missing block %d", n) + } + } + return &CliqueStatus{ + InturnPercent: float64((100 * optimals)) / float64(numBlocks), + SigningStatus: signStatus, + NumBlocks: numBlocks, + }, nil +} From 4b2ae2a036d742e635704af81b0efe80e8ad8887 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Wed, 2 Oct 2019 10:36:56 +0200 Subject: [PATCH 2/2] clique/api: fix some minor review concerns --- consensus/clique/api.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/consensus/clique/api.go b/consensus/clique/api.go index c40bed3f522a..fdb7b7a07904 100644 --- a/consensus/clique/api.go +++ b/consensus/clique/api.go @@ -120,7 +120,7 @@ func (api *API) Discard(address common.Address) { delete(api.clique.proposals, address) } -type CliqueStatus struct { +type Status struct { InturnPercent float64 `json:"inturnPercent"` SigningStatus map[common.Address]int `json:"sealerActivity""` NumBlocks uint64 `json:"numBlocks"` @@ -130,7 +130,7 @@ type CliqueStatus struct { // - the number of active signers, // - the number of signers, // - the percentage of in-turn blocks -func (api *API) Status() (*CliqueStatus, error) { +func (api *API) Status() (*Status, error) { var ( numBlocks = uint64(64) header = api.chain.CurrentHeader() @@ -156,21 +156,20 @@ func (api *API) Status() (*CliqueStatus, error) { } for n := start; n < end; n++ { h := api.chain.GetHeaderByNumber(n) - if h != nil { - if h.Difficulty.Cmp(diffInTurn) == 0 { - optimals += 1 - } - diff += h.Difficulty.Uint64() - if sealer, err := api.clique.Author(h); err != nil { - return nil, err - } else { - signStatus[sealer] += 1 - } - } else { + if h == nil { return nil, fmt.Errorf("missing block %d", n) } + if h.Difficulty.Cmp(diffInTurn) == 0 { + optimals++ + } + diff += h.Difficulty.Uint64() + sealer, err := api.clique.Author(h) + if err != nil { + return nil, err + } + signStatus[sealer]++ } - return &CliqueStatus{ + return &Status{ InturnPercent: float64((100 * optimals)) / float64(numBlocks), SigningStatus: signStatus, NumBlocks: numBlocks,