Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions consensus/clique/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard coding 64 seems overly arbitrary. We should probably convert it into an optional parameter, defaulting to 64 if unspecified perhaps. Though even then 64 blocks is 16 minutes, not sure if that's relevant enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be cleaner to make this a mandatory argument (and also drop the numBlocks from the result)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that 64 is a bit arbitrary, the reason I didn't set an optional parameter is that it would be too easy to send in a very large number and basically DoS the server. If we want larger span of time, then we'd probably have to do the work during normal operation, and have this method just deliver the data we've accumulated over time.

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
}