forked from cerc-io/plugeth
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
This commit is contained in:
parent
050ceff1ae
commit
6b7ae4e751
@ -31,7 +31,7 @@ type API struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetSnapshot retrieves the state snapshot at a given block.
|
// GetSnapshot retrieves the state snapshot at a given block.
|
||||||
func (api *API) GetSnapshot(number *rpc.BlockNumber) (interface{}, error) {
|
func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
|
||||||
// Retrieve the requested block number (or current if none requested)
|
// Retrieve the requested block number (or current if none requested)
|
||||||
var header *types.Header
|
var header *types.Header
|
||||||
if number == nil || *number == rpc.LatestBlockNumber {
|
if number == nil || *number == rpc.LatestBlockNumber {
|
||||||
@ -46,6 +46,15 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (interface{}, error) {
|
|||||||
return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
|
return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSnapshotAtHash retrieves the state snapshot at a given block.
|
||||||
|
func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
|
||||||
|
header := api.chain.GetHeaderByHash(hash)
|
||||||
|
if header == nil {
|
||||||
|
return nil, errUnknownBlock
|
||||||
|
}
|
||||||
|
return api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
|
||||||
|
}
|
||||||
|
|
||||||
// GetSigners retrieves the list of authorized signers at the specified block.
|
// GetSigners retrieves the list of authorized signers at the specified block.
|
||||||
func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
|
func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
|
||||||
// Retrieve the requested block number (or current if none requested)
|
// Retrieve the requested block number (or current if none requested)
|
||||||
@ -66,6 +75,19 @@ func (api *API) GetSigners(number *rpc.BlockNumber) ([]common.Address, error) {
|
|||||||
return snap.signers(), nil
|
return snap.signers(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSignersAtHash retrieves the state snapshot at a given block.
|
||||||
|
func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
|
||||||
|
header := api.chain.GetHeaderByHash(hash)
|
||||||
|
if header == nil {
|
||||||
|
return nil, errUnknownBlock
|
||||||
|
}
|
||||||
|
snap, err := api.clique.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return snap.signers(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Proposals returns the current proposals the node tries to uphold and vote on.
|
// Proposals returns the current proposals the node tries to uphold and vote on.
|
||||||
func (api *API) Proposals() map[common.Address]bool {
|
func (api *API) Proposals() map[common.Address]bool {
|
||||||
api.clique.lock.RLock()
|
api.clique.lock.RLock()
|
||||||
|
@ -349,16 +349,16 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type
|
|||||||
}
|
}
|
||||||
|
|
||||||
// snapshot retrieves the authorization snapshot at a given point in time.
|
// snapshot retrieves the authorization snapshot at a given point in time.
|
||||||
func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*snapshot, error) {
|
func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
|
||||||
// Search for a snapshot in memory or on disk for checkpoints
|
// Search for a snapshot in memory or on disk for checkpoints
|
||||||
var (
|
var (
|
||||||
headers []*types.Header
|
headers []*types.Header
|
||||||
snap *snapshot
|
snap *Snapshot
|
||||||
)
|
)
|
||||||
for snap == nil {
|
for snap == nil {
|
||||||
// If an in-memory snapshot was found, use that
|
// If an in-memory snapshot was found, use that
|
||||||
if s, ok := c.recents.Get(hash); ok {
|
if s, ok := c.recents.Get(hash); ok {
|
||||||
snap = s.(*snapshot)
|
snap = s.(*Snapshot)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// If an on-disk checkpoint snapshot can be found, use that
|
// If an on-disk checkpoint snapshot can be found, use that
|
||||||
|
@ -26,45 +26,45 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
// vote represents a single vote that an authorized signer made to modify the
|
// Vote represents a single vote that an authorized signer made to modify the
|
||||||
// list of authorizations.
|
// list of authorizations.
|
||||||
type vote struct {
|
type Vote struct {
|
||||||
Signer common.Address `json:"signer"` // Authorized signer that cast this vote
|
Signer common.Address `json:"signer"` // Authorized signer that cast this vote
|
||||||
Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
|
Block uint64 `json:"block"` // Block number the vote was cast in (expire old votes)
|
||||||
Address common.Address `json:"address"` // Account being voted on to change its authorization
|
Address common.Address `json:"address"` // Account being voted on to change its authorization
|
||||||
Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
|
Authorize bool `json:"authorize"` // Whether to authorize or deauthorize the voted account
|
||||||
}
|
}
|
||||||
|
|
||||||
// tally is a simple vote tally to keep the current score of votes. Votes that
|
// Tally is a simple vote tally to keep the current score of votes. Votes that
|
||||||
// go against the proposal aren't counted since it's equivalent to not voting.
|
// go against the proposal aren't counted since it's equivalent to not voting.
|
||||||
type tally struct {
|
type Tally struct {
|
||||||
Authorize bool `json:"authorize"` // Whether the vote it about authorizing or kicking someone
|
Authorize bool `json:"authorize"` // Whether the vote it about authorizing or kicking someone
|
||||||
Votes int `json:"votes"` // Number of votes until now wanting to pass the proposal
|
Votes int `json:"votes"` // Number of votes until now wanting to pass the proposal
|
||||||
}
|
}
|
||||||
|
|
||||||
// snapshot is the state of the authorization voting at a given point in time.
|
// Snapshot is the state of the authorization voting at a given point in time.
|
||||||
type snapshot struct {
|
type Snapshot struct {
|
||||||
config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
|
config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
|
||||||
|
|
||||||
Number uint64 `json:"number"` // Block number where the snapshot was created
|
Number uint64 `json:"number"` // Block number where the snapshot was created
|
||||||
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
|
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
|
||||||
Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
|
Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
|
||||||
Recents map[uint64]common.Address `json:"recents"` // Set of recent signers for spam protections
|
Recents map[uint64]common.Address `json:"recents"` // Set of recent signers for spam protections
|
||||||
Votes []*vote `json:"votes"` // List of votes cast in chronological order
|
Votes []*Vote `json:"votes"` // List of votes cast in chronological order
|
||||||
Tally map[common.Address]tally `json:"tally"` // Current vote tally to avoid recalculating
|
Tally map[common.Address]Tally `json:"tally"` // Current vote tally to avoid recalculating
|
||||||
}
|
}
|
||||||
|
|
||||||
// newSnapshot create a new snapshot with the specified startup parameters. This
|
// newSnapshot create a new snapshot with the specified startup parameters. This
|
||||||
// method does not initialize the set of recent signers, so only ever use if for
|
// method does not initialize the set of recent signers, so only ever use if for
|
||||||
// the genesis block.
|
// the genesis block.
|
||||||
func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, signers []common.Address) *snapshot {
|
func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
|
||||||
snap := &snapshot{
|
snap := &Snapshot{
|
||||||
config: config,
|
config: config,
|
||||||
Number: number,
|
Number: number,
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
Signers: make(map[common.Address]struct{}),
|
Signers: make(map[common.Address]struct{}),
|
||||||
Recents: make(map[uint64]common.Address),
|
Recents: make(map[uint64]common.Address),
|
||||||
Tally: make(map[common.Address]tally),
|
Tally: make(map[common.Address]Tally),
|
||||||
}
|
}
|
||||||
for _, signer := range signers {
|
for _, signer := range signers {
|
||||||
snap.Signers[signer] = struct{}{}
|
snap.Signers[signer] = struct{}{}
|
||||||
@ -73,12 +73,12 @@ func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// loadSnapshot loads an existing snapshot from the database.
|
// loadSnapshot loads an existing snapshot from the database.
|
||||||
func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Hash) (*snapshot, error) {
|
func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
|
||||||
blob, err := db.Get(append([]byte("clique-"), hash[:]...))
|
blob, err := db.Get(append([]byte("clique-"), hash[:]...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
snap := new(snapshot)
|
snap := new(Snapshot)
|
||||||
if err := json.Unmarshal(blob, snap); err != nil {
|
if err := json.Unmarshal(blob, snap); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Ha
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store inserts the snapshot into the database.
|
// store inserts the snapshot into the database.
|
||||||
func (s *snapshot) store(db ethdb.Database) error {
|
func (s *Snapshot) store(db ethdb.Database) error {
|
||||||
blob, err := json.Marshal(s)
|
blob, err := json.Marshal(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -97,15 +97,15 @@ func (s *snapshot) store(db ethdb.Database) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// copy creates a deep copy of the snapshot, though not the individual votes.
|
// copy creates a deep copy of the snapshot, though not the individual votes.
|
||||||
func (s *snapshot) copy() *snapshot {
|
func (s *Snapshot) copy() *Snapshot {
|
||||||
cpy := &snapshot{
|
cpy := &Snapshot{
|
||||||
config: s.config,
|
config: s.config,
|
||||||
Number: s.Number,
|
Number: s.Number,
|
||||||
Hash: s.Hash,
|
Hash: s.Hash,
|
||||||
Signers: make(map[common.Address]struct{}),
|
Signers: make(map[common.Address]struct{}),
|
||||||
Recents: make(map[uint64]common.Address),
|
Recents: make(map[uint64]common.Address),
|
||||||
Votes: make([]*vote, len(s.Votes)),
|
Votes: make([]*Vote, len(s.Votes)),
|
||||||
Tally: make(map[common.Address]tally),
|
Tally: make(map[common.Address]Tally),
|
||||||
}
|
}
|
||||||
for signer := range s.Signers {
|
for signer := range s.Signers {
|
||||||
cpy.Signers[signer] = struct{}{}
|
cpy.Signers[signer] = struct{}{}
|
||||||
@ -122,7 +122,7 @@ func (s *snapshot) copy() *snapshot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cast adds a new vote into the tally.
|
// cast adds a new vote into the tally.
|
||||||
func (s *snapshot) cast(address common.Address, authorize bool) bool {
|
func (s *Snapshot) cast(address common.Address, authorize bool) bool {
|
||||||
// Ensure the vote is meaningful
|
// Ensure the vote is meaningful
|
||||||
_, signer := s.Signers[address]
|
_, signer := s.Signers[address]
|
||||||
if (signer && authorize) || (!signer && !authorize) {
|
if (signer && authorize) || (!signer && !authorize) {
|
||||||
@ -133,13 +133,13 @@ func (s *snapshot) cast(address common.Address, authorize bool) bool {
|
|||||||
old.Votes++
|
old.Votes++
|
||||||
s.Tally[address] = old
|
s.Tally[address] = old
|
||||||
} else {
|
} else {
|
||||||
s.Tally[address] = tally{Authorize: authorize, Votes: 1}
|
s.Tally[address] = Tally{Authorize: authorize, Votes: 1}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// uncast removes a previously cast vote from the tally.
|
// uncast removes a previously cast vote from the tally.
|
||||||
func (s *snapshot) uncast(address common.Address, authorize bool) bool {
|
func (s *Snapshot) uncast(address common.Address, authorize bool) bool {
|
||||||
// If there's no tally, it's a dangling vote, just drop
|
// If there's no tally, it's a dangling vote, just drop
|
||||||
tally, ok := s.Tally[address]
|
tally, ok := s.Tally[address]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -161,7 +161,7 @@ func (s *snapshot) uncast(address common.Address, authorize bool) bool {
|
|||||||
|
|
||||||
// apply creates a new authorization snapshot by applying the given headers to
|
// apply creates a new authorization snapshot by applying the given headers to
|
||||||
// the original one.
|
// the original one.
|
||||||
func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
|
func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
|
||||||
// Allow passing in no headers for cleaner code
|
// Allow passing in no headers for cleaner code
|
||||||
if len(headers) == 0 {
|
if len(headers) == 0 {
|
||||||
return s, nil
|
return s, nil
|
||||||
@ -183,7 +183,7 @@ func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
|
|||||||
number := header.Number.Uint64()
|
number := header.Number.Uint64()
|
||||||
if number%s.config.Epoch == 0 {
|
if number%s.config.Epoch == 0 {
|
||||||
snap.Votes = nil
|
snap.Votes = nil
|
||||||
snap.Tally = make(map[common.Address]tally)
|
snap.Tally = make(map[common.Address]Tally)
|
||||||
}
|
}
|
||||||
// Delete the oldest signer from the recent list to allow it signing again
|
// Delete the oldest signer from the recent list to allow it signing again
|
||||||
if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
|
if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
|
||||||
@ -226,7 +226,7 @@ func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
|
|||||||
return nil, errInvalidVote
|
return nil, errInvalidVote
|
||||||
}
|
}
|
||||||
if snap.cast(header.Coinbase, authorize) {
|
if snap.cast(header.Coinbase, authorize) {
|
||||||
snap.Votes = append(snap.Votes, &vote{
|
snap.Votes = append(snap.Votes, &Vote{
|
||||||
Signer: signer,
|
Signer: signer,
|
||||||
Block: number,
|
Block: number,
|
||||||
Address: header.Coinbase,
|
Address: header.Coinbase,
|
||||||
@ -274,7 +274,7 @@ func (s *snapshot) apply(headers []*types.Header) (*snapshot, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// signers retrieves the list of authorized signers in ascending order.
|
// signers retrieves the list of authorized signers in ascending order.
|
||||||
func (s *snapshot) signers() []common.Address {
|
func (s *Snapshot) signers() []common.Address {
|
||||||
signers := make([]common.Address, 0, len(s.Signers))
|
signers := make([]common.Address, 0, len(s.Signers))
|
||||||
for signer := range s.Signers {
|
for signer := range s.Signers {
|
||||||
signers = append(signers, signer)
|
signers = append(signers, signer)
|
||||||
@ -290,7 +290,7 @@ func (s *snapshot) signers() []common.Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// inturn returns if a signer at a given block height is in-turn or not.
|
// inturn returns if a signer at a given block height is in-turn or not.
|
||||||
func (s *snapshot) inturn(number uint64, signer common.Address) bool {
|
func (s *Snapshot) inturn(number uint64, signer common.Address) bool {
|
||||||
signers, offset := s.signers(), 0
|
signers, offset := s.signers(), 0
|
||||||
for offset < len(signers) && signers[offset] != signer {
|
for offset < len(signers) && signers[offset] != signer {
|
||||||
offset++
|
offset++
|
||||||
|
@ -78,6 +78,7 @@ func (r *testerChainReader) Config() *params.ChainConfig { panic
|
|||||||
func (r *testerChainReader) CurrentHeader() *types.Header { panic("not supported") }
|
func (r *testerChainReader) CurrentHeader() *types.Header { panic("not supported") }
|
||||||
func (r *testerChainReader) GetHeader(common.Hash, uint64) *types.Header { panic("not supported") }
|
func (r *testerChainReader) GetHeader(common.Hash, uint64) *types.Header { panic("not supported") }
|
||||||
func (r *testerChainReader) GetBlock(common.Hash, uint64) *types.Block { panic("not supported") }
|
func (r *testerChainReader) GetBlock(common.Hash, uint64) *types.Block { panic("not supported") }
|
||||||
|
func (r *testerChainReader) GetHeaderByHash(common.Hash) *types.Header { panic("not supported") }
|
||||||
func (r *testerChainReader) GetHeaderByNumber(number uint64) *types.Header {
|
func (r *testerChainReader) GetHeaderByNumber(number uint64) *types.Header {
|
||||||
if number == 0 {
|
if number == 0 {
|
||||||
return core.GetHeader(r.db, core.GetCanonicalHash(r.db, 0), 0)
|
return core.GetHeader(r.db, core.GetCanonicalHash(r.db, 0), 0)
|
||||||
|
@ -40,6 +40,9 @@ type ChainReader interface {
|
|||||||
// GetHeaderByNumber retrieves a block header from the database by number.
|
// GetHeaderByNumber retrieves a block header from the database by number.
|
||||||
GetHeaderByNumber(number uint64) *types.Header
|
GetHeaderByNumber(number uint64) *types.Header
|
||||||
|
|
||||||
|
// GetHeaderByHash retrieves a block header from the database by its hash.
|
||||||
|
GetHeaderByHash(hash common.Hash) *types.Header
|
||||||
|
|
||||||
// GetBlock retrieves a block from the database by hash and number.
|
// GetBlock retrieves a block from the database by hash and number.
|
||||||
GetBlock(hash common.Hash, number uint64) *types.Block
|
GetBlock(hash common.Hash, number uint64) *types.Block
|
||||||
}
|
}
|
||||||
|
@ -30,10 +30,8 @@ var Modules = map[string]string{
|
|||||||
"shh": Shh_JS,
|
"shh": Shh_JS,
|
||||||
"swarmfs": SWARMFS_JS,
|
"swarmfs": SWARMFS_JS,
|
||||||
"txpool": TxPool_JS,
|
"txpool": TxPool_JS,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const Chequebook_JS = `
|
const Chequebook_JS = `
|
||||||
web3._extend({
|
web3._extend({
|
||||||
property: 'chequebook',
|
property: 'chequebook',
|
||||||
@ -77,12 +75,22 @@ web3._extend({
|
|||||||
params: 1,
|
params: 1,
|
||||||
inputFormatter: [null]
|
inputFormatter: [null]
|
||||||
}),
|
}),
|
||||||
|
new web3._extend.Method({
|
||||||
|
name: 'getSnapshotAtHash',
|
||||||
|
call: 'clique_getSnapshotAtHash',
|
||||||
|
params: 1
|
||||||
|
}),
|
||||||
new web3._extend.Method({
|
new web3._extend.Method({
|
||||||
name: 'getSigners',
|
name: 'getSigners',
|
||||||
call: 'clique_getSigners',
|
call: 'clique_getSigners',
|
||||||
params: 1,
|
params: 1,
|
||||||
inputFormatter: [null]
|
inputFormatter: [null]
|
||||||
}),
|
}),
|
||||||
|
new web3._extend.Method({
|
||||||
|
name: 'getSignersAtHash',
|
||||||
|
call: 'clique_getSignersAtHash',
|
||||||
|
params: 1
|
||||||
|
}),
|
||||||
new web3._extend.Method({
|
new web3._extend.Method({
|
||||||
name: 'propose',
|
name: 'propose',
|
||||||
call: 'clique_propose',
|
call: 'clique_propose',
|
||||||
|
Loading…
Reference in New Issue
Block a user