Skip to content

Commit feeccdf

Browse files
karalabefjl
authored andcommitted
consensus/clique: Proof of Authority (bnb-chain#3753)
This PR is a prototype implementation of plugable consensus engines and the Clique PoA protocol ethereum/EIPs#225
1 parent bfe5eb7 commit feeccdf

12 files changed

Lines changed: 1575 additions & 123 deletions

File tree

‎consensus/clique/api.go‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package clique
18+
19+
import (
20+
"github.com/ethereum/go-ethereum/common"
21+
"github.com/ethereum/go-ethereum/consensus"
22+
"github.com/ethereum/go-ethereum/core/types"
23+
"github.com/ethereum/go-ethereum/rpc"
24+
)
25+
26+
// API is a user facing RPC API to allow controlling the signer and voting
27+
// mechanisms of the proof-of-authority scheme.
28+
type API struct {
29+
chain consensus.ChainReader
30+
clique *Clique
31+
}
32+
33+
// GetSnapshot retrieves the state snapshot at a given block.
34+
func (api *API) GetSnapshot(number *rpc.BlockNumber) (interface{}, error) {
35+
// Retrieve the requested block number (or current if none requested)
36+
var header *types.Header
37+
if number == nil || *number == rpc.LatestBlockNumber {
38+
header = api.chain.CurrentHeader()
39+
} else {
40+
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
41+
}
42+
// Ensure we have an actually valid block and return its snapshot
43+
if header == nil {
44+
return nil, errUnknownBlock
45+
}
46+
return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
47+
}
48+
49+
// GetSigners retrieves the list of authorized signers at the specified block.
50+
func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
51+
// Retrieve the requested block number (or current if none requested)
52+
var header *types.Header
53+
if number == nil || *number == rpc.LatestBlockNumber {
54+
header = api.chain.CurrentHeader()
55+
} else {
56+
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
57+
}
58+
// Ensure we have an actually valid block and return the signers from its snapshot
59+
if header == nil {
60+
return nil, errUnknownBlock
61+
}
62+
snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
63+
if err != nil {
64+
return nil, err
65+
}
66+
return snap.signers(), nil
67+
}
68+
69+
// Proposals returns the current proposals the node tries to uphold and vote on.
70+
func (api *API) Proposals() map[common.Address]bool {
71+
api.clique.lock.RLock()
72+
defer api.clique.lock.RUnlock()
73+
74+
proposals := make(map[common.Address]bool)
75+
for address, auth := range api.clique.proposals {
76+
proposals[address] = auth
77+
}
78+
return proposals
79+
}
80+
81+
// Propose injects a new authorization proposal that the signer will attempt to
82+
// push through.
83+
func (api *API) Propose(address common.Address, auth bool) {
84+
api.clique.lock.Lock()
85+
defer api.clique.lock.Unlock()
86+
87+
api.clique.proposals[address] = auth
88+
}
89+
90+
// Discard drops a currently running proposal, stopping the signer from casting
91+
// further votes (either for or against).
92+
func (api *API) Discard(address common.Address) {
93+
api.clique.lock.Lock()
94+
defer api.clique.lock.Unlock()
95+
96+
delete(api.clique.proposals, address)
97+
}

0 commit comments

Comments
 (0)