Skip to content

Commit 177dc82

Browse files
committed
consensus/clique: clique status api call
1 parent 24ef835 commit 177dc82

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

consensus/clique/api.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package clique
1818

1919
import (
20+
"fmt"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/consensus"
2224
"github.com/ethereum/go-ethereum/core/types"
@@ -117,3 +119,60 @@ func (api *API) Discard(address common.Address) {
117119

118120
delete(api.clique.proposals, address)
119121
}
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

Comments
 (0)