Skip to content

Commit de261b9

Browse files
committed
feat: added custom consensus mechanism
Signed-off-by: Soumya Ghosh Dastidar <[email protected]>
1 parent de23cf9 commit de261b9

File tree

13 files changed

+533
-347
lines changed

13 files changed

+533
-347
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
build/_workspace
44
build/_bin
55
tests/testdata
6+
genesis/

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ profile.cov
4747
/dashboard/assets/package-lock.json
4848

4949
**/yarn-error.log
50+
51+
genesis/
52+
geth/

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
GOBIN = ./build/bin
88
GO ?= latest
99
GORUN = env GO111MODULE=on go run
10+
IMAGE ?= aksara
11+
TAG ?= latest
1012

1113
geth:
1214
$(GORUN) build/ci.go install ./cmd/geth
@@ -48,3 +50,12 @@ devtools:
4850
env GOBIN= go install ./cmd/abigen
4951
@type "solc" 2> /dev/null || echo 'Please install solc'
5052
@type "protoc" 2> /dev/null || echo 'Please install protoc'
53+
54+
bootnode:
55+
$(GORUN) build/ci.go install ./cmd/bootnode
56+
@echo "Done building."
57+
@echo "Run \"$(GOBIN)/bootnode\" to launch bootnode."
58+
59+
image:
60+
@echo "Building image ${IMAGE}:${TAG}"
61+
docker build -t ${IMAGE}:${TAG} .

consensus/clique/api.go

+72-29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package clique
1818

1919
import (
20+
"bytes"
2021
"encoding/json"
2122
"fmt"
2223

@@ -93,40 +94,18 @@ func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
9394
return snap.signers(), nil
9495
}
9596

96-
// Proposals returns the current proposals the node tries to uphold and vote on.
97-
func (api *API) Proposals() map[common.Address]bool {
98-
api.clique.lock.RLock()
99-
defer api.clique.lock.RUnlock()
100-
101-
proposals := make(map[common.Address]bool)
102-
for address, auth := range api.clique.proposals {
103-
proposals[address] = auth
104-
}
105-
return proposals
106-
}
107-
108-
// Propose injects a new authorization proposal that the signer will attempt to
109-
// push through.
110-
func (api *API) Propose(address common.Address, auth bool) {
111-
api.clique.lock.Lock()
112-
defer api.clique.lock.Unlock()
113-
114-
api.clique.proposals[address] = auth
115-
}
116-
117-
// Discard drops a currently running proposal, stopping the signer from casting
118-
// further votes (either for or against).
119-
func (api *API) Discard(address common.Address) {
120-
api.clique.lock.Lock()
121-
defer api.clique.lock.Unlock()
122-
123-
delete(api.clique.proposals, address)
97+
type status struct {
98+
InturnPercent float64 `json:"inturnPercent"`
99+
SigningStatus map[common.Address]int `json:"sealerActivity"`
100+
NumBlocks uint64 `json:"numBlocks"`
124101
}
125102

126-
type status struct {
103+
type epochPerformance struct {
127104
InturnPercent float64 `json:"inturnPercent"`
128105
SigningStatus map[common.Address]int `json:"sealerActivity"`
129106
NumBlocks uint64 `json:"numBlocks"`
107+
NextEpoch uint64 `json:"nextEpoch"`
108+
StartBlock uint64 `json:"startBlock"`
130109
}
131110

132111
// Status returns the status of the last N blocks,
@@ -179,6 +158,70 @@ func (api *API) Status() (*status, error) {
179158
}, nil
180159
}
181160

161+
// EpochPerformance returns the performance of all the validators in an epoch,
162+
// - the signer activity,
163+
// - the number of blocks in epoch,
164+
// - start of epoch block,
165+
// - next epoch number if available else 0,
166+
// - the percentage of in-turn blocks
167+
func (api *API) EpochPerformance(epochNumber, epochBlockNumber uint64) (*epochPerformance, error) {
168+
var (
169+
numBlocks = uint64(0)
170+
header = api.chain.CurrentHeader()
171+
optimals = 0
172+
)
173+
epochBlock := api.chain.GetHeaderByNumber(epochBlockNumber)
174+
if epochBlock == nil {
175+
return nil, fmt.Errorf("missing epoch block %d", epochBlockNumber)
176+
}
177+
snap, err := api.clique.snapshot(api.chain, epochBlockNumber, epochBlock.Hash(), nil)
178+
if err != nil {
179+
return nil, err
180+
}
181+
if snap.EpochNumber != epochNumber {
182+
return nil, fmt.Errorf("epoch number mismatch, expected=%v got=%v", epochNumber, snap.EpochNumber)
183+
}
184+
var (
185+
signers = snap.signers()
186+
end = header.Number.Uint64()
187+
start = snap.Number + 1
188+
)
189+
signStatus := make(map[common.Address]int)
190+
for _, s := range signers {
191+
signStatus[s] = 0
192+
}
193+
nextEpoch := uint64(0)
194+
n := start
195+
for ; n <= end; n++ {
196+
h := api.chain.GetHeaderByNumber(n)
197+
if h == nil {
198+
return nil, fmt.Errorf("missing block %d", n)
199+
}
200+
numBlocks++
201+
202+
if h.Difficulty.Cmp(diffInTurn) == 0 {
203+
optimals++
204+
}
205+
sealer, err := api.clique.Author(h)
206+
if err != nil {
207+
return nil, err
208+
}
209+
signStatus[sealer]++
210+
211+
if !bytes.Equal(h.Nonce[:], nonceDropVote) {
212+
nextEpoch = h.Nonce.Uint64()
213+
break
214+
}
215+
}
216+
return &epochPerformance{
217+
InturnPercent: float64(100*optimals) / float64(numBlocks),
218+
SigningStatus: signStatus,
219+
NumBlocks: numBlocks,
220+
NextEpoch: nextEpoch,
221+
StartBlock: start,
222+
}, nil
223+
}
224+
182225
type blockNumberOrHashOrRLP struct {
183226
*rpc.BlockNumberOrHash
184227
RLP hexutil.Bytes `json:"rlp,omitempty"`

0 commit comments

Comments
 (0)