mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
consensus/clique: clique status api call
This commit is contained in:
parent
24ef83518c
commit
177dc821ac
1 changed files with 59 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue