Skip to content

Commit b0f30b0

Browse files
authored
Merge pull request #14545 from karalabe/clique-cache-signatures
consensus/clique: cache block signatures for fast checks
2 parents e96f298 + 309da54 commit b0f30b0

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

consensus/clique/clique.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
const (
4545
checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database
4646
inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory
47-
inmemorySignatures = 1024 // Number of recent blocks to keep in memory
47+
inmemorySignatures = 4096 // Number of recent block signatures to keep in memory
4848

4949
wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
5050
)
@@ -162,7 +162,12 @@ func sigHash(header *types.Header) (hash common.Hash) {
162162
}
163163

164164
// ecrecover extracts the Ethereum account address from a signed header.
165-
func ecrecover(header *types.Header) (common.Address, error) {
165+
func ecrecover(header *types.Header, sigcache *lru.ARCCache) (common.Address, error) {
166+
// If the signature's already cached, return that
167+
hash := header.Hash()
168+
if address, known := sigcache.Get(hash); known {
169+
return address.(common.Address), nil
170+
}
166171
// Retrieve the signature from the header extra-data
167172
if len(header.Extra) < extraSeal {
168173
return common.Address{}, errMissingSignature
@@ -177,6 +182,7 @@ func ecrecover(header *types.Header) (common.Address, error) {
177182
var signer common.Address
178183
copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
179184

185+
sigcache.Add(hash, signer)
180186
return signer, nil
181187
}
182188

@@ -223,7 +229,7 @@ func New(config *params.CliqueConfig, db ethdb.Database) *Clique {
223229
// Author implements consensus.Engine, returning the Ethereum address recovered
224230
// from the signature in the header's extra-data section.
225231
func (c *Clique) Author(header *types.Header) (common.Address, error) {
226-
return ecrecover(header)
232+
return ecrecover(header, c.signatures)
227233
}
228234

229235
// VerifyHeader checks whether a header conforms to the consensus rules.
@@ -369,7 +375,7 @@ func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash commo
369375
}
370376
// If an on-disk checkpoint snapshot can be found, use that
371377
if number%checkpointInterval == 0 {
372-
if s, err := loadSnapshot(c.config, c.db, hash); err == nil {
378+
if s, err := loadSnapshot(c.config, c.signatures, c.db, hash); err == nil {
373379
log.Trace("Loaded voting snapshot form disk", "number", number, "hash", hash)
374380
snap = s
375381
break
@@ -385,7 +391,7 @@ func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash commo
385391
for i := 0; i < len(signers); i++ {
386392
copy(signers[i][:], genesis.Extra[extraVanity+i*common.AddressLength:])
387393
}
388-
snap = newSnapshot(c.config, 0, genesis.Hash(), signers)
394+
snap = newSnapshot(c.config, c.signatures, 0, genesis.Hash(), signers)
389395
if err := snap.store(c.db); err != nil {
390396
return nil, err
391397
}
@@ -464,7 +470,7 @@ func (c *Clique) verifySeal(chain consensus.ChainReader, header *types.Header, p
464470
c.recents.Add(snap.Hash, snap)
465471

466472
// Resolve the authorization key and check against signers
467-
signer, err := ecrecover(header)
473+
signer, err := ecrecover(header, c.signatures)
468474
if err != nil {
469475
return err
470476
}

consensus/clique/snapshot.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/core/types"
2525
"github.com/ethereum/go-ethereum/ethdb"
2626
"github.com/ethereum/go-ethereum/params"
27+
lru "github.com/hashicorp/golang-lru"
2728
)
2829

2930
// Vote represents a single vote that an authorized signer made to modify the
@@ -44,7 +45,8 @@ type Tally struct {
4445

4546
// Snapshot is the state of the authorization voting at a given point in time.
4647
type Snapshot struct {
47-
config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
48+
config *params.CliqueConfig // Consensus engine parameters to fine tune behavior
49+
sigcache *lru.ARCCache // Cache of recent block signatures to speed up ecrecover
4850

4951
Number uint64 `json:"number"` // Block number where the snapshot was created
5052
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
@@ -57,14 +59,15 @@ type Snapshot struct {
5759
// newSnapshot create a new snapshot with the specified startup parameters. This
5860
// method does not initialize the set of recent signers, so only ever use if for
5961
// the genesis block.
60-
func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
62+
func newSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
6163
snap := &Snapshot{
62-
config: config,
63-
Number: number,
64-
Hash: hash,
65-
Signers: make(map[common.Address]struct{}),
66-
Recents: make(map[uint64]common.Address),
67-
Tally: make(map[common.Address]Tally),
64+
config: config,
65+
sigcache: sigcache,
66+
Number: number,
67+
Hash: hash,
68+
Signers: make(map[common.Address]struct{}),
69+
Recents: make(map[uint64]common.Address),
70+
Tally: make(map[common.Address]Tally),
6871
}
6972
for _, signer := range signers {
7073
snap.Signers[signer] = struct{}{}
@@ -73,7 +76,7 @@ func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, s
7376
}
7477

7578
// loadSnapshot loads an existing snapshot from the database.
76-
func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
79+
func loadSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
7780
blob, err := db.Get(append([]byte("clique-"), hash[:]...))
7881
if err != nil {
7982
return nil, err
@@ -83,6 +86,7 @@ func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Ha
8386
return nil, err
8487
}
8588
snap.config = config
89+
snap.sigcache = sigcache
8690

8791
return snap, nil
8892
}
@@ -99,13 +103,14 @@ func (s *Snapshot) store(db ethdb.Database) error {
99103
// copy creates a deep copy of the snapshot, though not the individual votes.
100104
func (s *Snapshot) copy() *Snapshot {
101105
cpy := &Snapshot{
102-
config: s.config,
103-
Number: s.Number,
104-
Hash: s.Hash,
105-
Signers: make(map[common.Address]struct{}),
106-
Recents: make(map[uint64]common.Address),
107-
Votes: make([]*Vote, len(s.Votes)),
108-
Tally: make(map[common.Address]Tally),
106+
config: s.config,
107+
sigcache: s.sigcache,
108+
Number: s.Number,
109+
Hash: s.Hash,
110+
Signers: make(map[common.Address]struct{}),
111+
Recents: make(map[uint64]common.Address),
112+
Votes: make([]*Vote, len(s.Votes)),
113+
Tally: make(map[common.Address]Tally),
109114
}
110115
for signer := range s.Signers {
111116
cpy.Signers[signer] = struct{}{}
@@ -190,7 +195,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
190195
delete(snap.Recents, number-limit)
191196
}
192197
// Resolve the authorization key and check against signers
193-
signer, err := ecrecover(header)
198+
signer, err := ecrecover(header, s.sigcache)
194199
if err != nil {
195200
return nil, err
196201
}

0 commit comments

Comments
 (0)