Skip to content

Commit 6b7ae4e

Browse files
authored
consensus/clique, internal/web3ext: support hash based API queries (#14321)
* consensus/clique, internal/web3ext: support hash based API queries * consensus/clique: make RPC return types public
1 parent 050ceff commit 6b7ae4e

6 files changed

Lines changed: 66 additions & 32 deletions

File tree

‎consensus/clique/api.go‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type API struct {
3131
}
3232

3333
// GetSnapshot retrieves the state snapshot at a given block.
34-
func (api *API) GetSnapshot(number *rpc.BlockNumber) (interface{}, error) {
34+
func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
3535
// Retrieve the requested block number (or current if none requested)
3636
var header *types.Header
3737
if number == nil || *number == rpc.LatestBlockNumber {
@@ -46,6 +46,15 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (interface{}, error) {
4646
return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
4747
}
4848

49+
// GetSnapshotAtHash retrieves the state snapshot at a given block.
50+
func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
51+
header := api.chain.GetHeaderByHash(hash)
52+
if header == nil {
53+
return nil, errUnknownBlock
54+
}
55+
return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
56+
}
57+
4958
// GetSigners retrieves the list of authorized signers at the specified block.
5059
func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
5160
// Retrieve the requested block number (or current if none requested)
@@ -66,6 +75,19 @@ func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
6675
return snap.signers(), nil
6776
}
6877

78+
// GetSignersAtHash retrieves the state snapshot at a given block.
79+
func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
80+
header := api.chain.GetHeaderByHash(hash)
81+
if header == nil {
82+
return nil, errUnknownBlock
83+
}
84+
snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
85+
if err != nil {
86+
return nil, err
87+
}
88+
return snap.signers(), nil
89+
}
90+
6991
// Proposals returns the current proposals the node tries to uphold and vote on.
7092
func (api *API) Proposals() map[common.Address]bool {
7193
api.clique.lock.RLock()

‎consensus/clique/clique.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,16 +349,16 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
349349
}
350350

351351
// snapshot retrieves the authorization snapshot at a given point in time.
352-
func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*snapshot, error) {
352+
func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
353353
// Search for a snapshot in memory or on disk for checkpoints
354354
var (
355355
headers []*types.Header
356-
snap *snapshot
356+
snap *Snapshot
357357
)
358358
for snap == nil {
359359
// If an in-memory snapshot was found, use that
360360
if s, ok := c.recents.Get(hash); ok {
361-
snap = s.(*snapshot)
361+
snap = s.(*Snapshot)
362362
break
363363
}
364364
// If an on-disk checkpoint snapshot can be found, use that

‎consensus/clique/snapshot.go‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,45 @@ import (
2626
"github.com/ethereum/go-ethereum/params"
2727
)
2828

29-
// vote represents a single vote that an authorized signer made to modify the
29+
// Vote represents a single vote that an authorized signer made to modify the
3030
// list of authorizations.
31-
type vote struct {
31+
type Vote struct {
3232
Signer common.Address `json:"signer"` // Authorized signer that cast this vote
3333
Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
3434
Address common.Address `json:"address"` // Account being voted on to change its authorization
3535
Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
3636
}
3737

38-
// tally is a simple vote tally to keep the current score of votes. Votes that
38+
// Tally is a simple vote tally to keep the current score of votes. Votes that
3939
// go against the proposal aren't counted since it's equivalent to not voting.
40-
type tally struct {
40+
type Tally struct {
4141
Authorize bool `json:"authorize"` // Whether the vote it about authorizing or kicking someone
4242
Votes int `json:"votes"` // Number of votes until now wanting to pass the proposal
4343
}
4444

45-
// snapshot is the state of the authorization voting at a given point in time.
46-
type snapshot struct {
45+
// Snapshot is the state of the authorization voting at a given point in time.
46+
type Snapshot struct {
4747
config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
4848

4949
Number uint64 `json:"number"` // Block number where the snapshot was created
5050
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
5151
Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
5252
Recents map[uint64]common.Address `json:"recents"` // Set of recent signers for spam protections
53-
Votes []*vote `json:"votes"` // List of votes cast in chronological order
54-
Tally map[common.Address]tally `json:"tally"` // Current vote tally to avoid recalculating
53+
Votes []*Vote `json:"votes"` // List of votes cast in chronological order
54+
Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
5555
}
5656

5757
// newSnapshot create a new snapshot with the specified startup parameters. This
5858
// method does not initialize the set of recent signers, so only ever use if for
5959
// the genesis block.
60-
func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, signers []common.Address) *snapshot {
61-
snap := &snapshot{
60+
func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
61+
snap := &Snapshot{
6262
config: config,
6363
Number: number,
6464
Hash: hash,
6565
Signers: make(map[common.Address]struct{}),
6666
Recents: make(map[uint64]common.Address),
67-
Tally: make(map[common.Address]tally),
67+
Tally: make(map[common.Address]Tally),
6868
}
6969
for _, signer := range signers {
7070
snap.Signers[signer] = struct{}{}
@@ -73,12 +73,12 @@ func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, s
7373
}
7474

7575
// loadSnapshot loads an existing snapshot from the database.
76-
func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Hash) (*snapshot, error) {
76+
func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
7777
blob, err := db.Get(append([]byte("clique-"), hash[:]...))
7878
if err != nil {
7979
return nil, err
8080
}
81-
snap := new(snapshot)
81+
snap := new(Snapshot)
8282
if err := json.Unmarshal(blob, snap); err != nil {
8383
return nil, err
8484
}
@@ -88,7 +88,7 @@ func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Ha
8888
}
8989

9090
// store inserts the snapshot into the database.
91-
func (s *snapshot) store(db ethdb.Database) error {
91+
func (s *Snapshot) store(db ethdb.Database) error {
9292
blob, err := json.Marshal(s)
9393
if err != nil {
9494
return err
@@ -97,15 +97,15 @@ func (s *snapshot) store(db ethdb.Database) error {
9797
}
9898

9999
// copy creates a deep copy of the snapshot, though not the individual votes.
100-
func (s *snapshot) copy() *snapshot {
101-
cpy := &snapshot{
100+
func (s *Snapshot) copy() *Snapshot {
101+
cpy := &Snapshot{
102102
config: s.config,
103103
Number: s.Number,
104104
Hash: s.Hash,
105105
Signers: make(map[common.Address]struct{}),
106106
Recents: make(map[uint64]common.Address),
107-
Votes: make([]*vote, len(s.Votes)),
108-
Tally: make(map[common.Address]tally),
107+
Votes: make([]*Vote, len(s.Votes)),
108+
Tally: make(map[common.Address]Tally),
109109
}
110110
for signer := range s.Signers {
111111
cpy.Signers[signer] = struct{}{}
@@ -122,7 +122,7 @@ func (s *snapshot) copy() *snapshot {
122122
}
123123

124124
// cast adds a new vote into the tally.
125-
func (s *snapshot) cast(address common.Address, authorize bool) bool {
125+
func (s *Snapshot) cast(address common.Address, authorize bool) bool {
126126
// Ensure the vote is meaningful
127127
_, signer := s.Signers[address]
128128
if (signer && authorize) || (!signer && !authorize) {
@@ -133,13 +133,13 @@ func (s *snapshot) cast(address common.Address, authorize bool) bool {
133133
old.Votes++
134134
s.Tally[address] = old
135135
} else {
136-
s.Tally[address] = tally{Authorize: authorize, Votes: 1}
136+
s.Tally[address] = Tally{Authorize: authorize, Votes: 1}
137137
}
138138
return true
139139
}
140140

141141
// uncast removes a previously cast vote from the tally.
142-
func (s *snapshot) uncast(address common.Address, authorize bool) bool {
142+
func (s *Snapshot) uncast(address common.Address, authorize bool) bool {
143143
// If there's no tally, it's a dangling vote, just drop
144144
tally, ok := s.Tally[address]
145145
if !ok {
@@ -161,7 +161,7 @@ func (s *snapshot) uncast(address common.Address, authorize bool) bool {
161161

162162
// apply creates a new authorization snapshot by applying the given headers to
163163
// the original one.
164-
func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
164+
func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
165165
// Allow passing in no headers for cleaner code
166166
if len(headers) == 0 {
167167
return s, nil
@@ -183,7 +183,7 @@ func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
183183
number := header.Number.Uint64()
184184
if number%s.config.Epoch == 0 {
185185
snap.Votes = nil
186-
snap.Tally = make(map[common.Address]tally)
186+
snap.Tally = make(map[common.Address]Tally)
187187
}
188188
// Delete the oldest signer from the recent list to allow it signing again
189189
if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
@@ -226,7 +226,7 @@ func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
226226
return nil, errInvalidVote
227227
}
228228
if snap.cast(header.Coinbase, authorize) {
229-
snap.Votes = append(snap.Votes, &vote{
229+
snap.Votes = append(snap.Votes, &Vote{
230230
Signer: signer,
231231
Block: number,
232232
Address: header.Coinbase,
@@ -274,7 +274,7 @@ func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
274274
}
275275

276276
// signers retrieves the list of authorized signers in ascending order.
277-
func (s *snapshot) signers() []common.Address {
277+
func (s *Snapshot) signers() []common.Address {
278278
signers := make([]common.Address, 0, len(s.Signers))
279279
for signer := range s.Signers {
280280
signers = append(signers, signer)
@@ -290,7 +290,7 @@ func (s *snapshot) signers() []common.Address {
290290
}
291291

292292
// inturn returns if a signer at a given block height is in-turn or not.
293-
func (s *snapshot) inturn(number uint64, signer common.Address) bool {
293+
func (s *Snapshot) inturn(number uint64, signer common.Address) bool {
294294
signers, offset := s.signers(), 0
295295
for offset < len(signers) && signers[offset] != signer {
296296
offset++

‎consensus/clique/snapshot_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (r *testerChainReader) Config() *params.ChainConfig { panic
7878
func (r *testerChainReader) CurrentHeader() *types.Header { panic("not supported") }
7979
func (r *testerChainReader) GetHeader(common.Hash, uint64) *types.Header { panic("not supported") }
8080
func (r *testerChainReader) GetBlock(common.Hash, uint64) *types.Block { panic("not supported") }
81+
func (r *testerChainReader) GetHeaderByHash(common.Hash) *types.Header { panic("not supported") }
8182
func (r *testerChainReader) GetHeaderByNumber(number uint64) *types.Header {
8283
if number == 0 {
8384
return core.GetHeader(r.db, core.GetCanonicalHash(r.db, 0), 0)

‎consensus/consensus.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type ChainReader interface {
4040
// GetHeaderByNumber retrieves a block header from the database by number.
4141
GetHeaderByNumber(number uint64) *types.Header
4242

43+
// GetHeaderByHash retrieves a block header from the database by its hash.
44+
GetHeaderByHash(hash common.Hash) *types.Header
45+
4346
// GetBlock retrieves a block from the database by hash and number.
4447
GetBlock(hash common.Hash, number uint64) *types.Block
4548
}

‎internal/web3ext/web3ext.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ var Modules = map[string]string{
3030
"shh": Shh_JS,
3131
"swarmfs": SWARMFS_JS,
3232
"txpool": TxPool_JS,
33-
3433
}
3534

36-
3735
const Chequebook_JS = `
3836
web3._extend({
3937
property: 'chequebook',
@@ -77,12 +75,22 @@ web3._extend({
7775
params: 1,
7876
inputFormatter: [null]
7977
}),
78+
new web3._extend.Method({
79+
name: 'getSnapshotAtHash',
80+
call: 'clique_getSnapshotAtHash',
81+
params: 1
82+
}),
8083
new web3._extend.Method({
8184
name: 'getSigners',
8285
call: 'clique_getSigners',
8386
params: 1,
8487
inputFormatter: [null]
8588
}),
89+
new web3._extend.Method({
90+
name: 'getSignersAtHash',
91+
call: 'clique_getSignersAtHash',
92+
params: 1
93+
}),
8694
new web3._extend.Method({
8795
name: 'propose',
8896
call: 'clique_propose',

0 commit comments

Comments
 (0)