|
17 | 17 | package clique |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "fmt" |
| 21 | + |
20 | 22 | "github.com/ethereum/go-ethereum/common" |
21 | 23 | "github.com/ethereum/go-ethereum/consensus" |
22 | 24 | "github.com/ethereum/go-ethereum/core/types" |
@@ -117,3 +119,60 @@ func (api *API) Discard(address common.Address) { |
117 | 119 |
|
118 | 120 | delete(api.clique.proposals, address) |
119 | 121 | } |
| 122 | + |
| 123 | +type CliqueStatus struct { |
| 124 | + InturnPercent float64 `json:"inturnPercent"` |
| 125 | + SigningStatus map[common.Address]int `json:"sealerActivity""` |
| 126 | + NumBlocks uint64 `json:"numBlocks"` |
| 127 | +} |
| 128 | + |
| 129 | +// Status returns the status of the last N blocks, |
| 130 | +// - the number of active signers, |
| 131 | +// - the number of signers, |
| 132 | +// - the percentage of in-turn blocks |
| 133 | +func (api *API) Status() (*CliqueStatus, error) { |
| 134 | + var ( |
| 135 | + numBlocks = uint64(64) |
| 136 | + header = api.chain.CurrentHeader() |
| 137 | + diff = uint64(0) |
| 138 | + optimals = 0 |
| 139 | + ) |
| 140 | + snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + var ( |
| 145 | + signers = snap.signers() |
| 146 | + end = header.Number.Uint64() |
| 147 | + start = end - numBlocks |
| 148 | + ) |
| 149 | + if numBlocks > end { |
| 150 | + start = 1 |
| 151 | + numBlocks = end - start |
| 152 | + } |
| 153 | + signStatus := make(map[common.Address]int) |
| 154 | + for _, s := range signers { |
| 155 | + signStatus[s] = 0 |
| 156 | + } |
| 157 | + for n := start; n < end; n++ { |
| 158 | + h := api.chain.GetHeaderByNumber(n) |
| 159 | + if h != nil { |
| 160 | + if h.Difficulty.Cmp(diffInTurn) == 0 { |
| 161 | + optimals += 1 |
| 162 | + } |
| 163 | + diff += h.Difficulty.Uint64() |
| 164 | + if sealer, err := api.clique.Author(h); err != nil { |
| 165 | + return nil, err |
| 166 | + } else { |
| 167 | + signStatus[sealer] += 1 |
| 168 | + } |
| 169 | + } else { |
| 170 | + return nil, fmt.Errorf("missing block %d", n) |
| 171 | + } |
| 172 | + } |
| 173 | + return &CliqueStatus{ |
| 174 | + InturnPercent: float64((100 * optimals)) / float64(numBlocks), |
| 175 | + SigningStatus: signStatus, |
| 176 | + NumBlocks: numBlocks, |
| 177 | + }, nil |
| 178 | +} |
0 commit comments