@@ -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.
4647type 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.
100104func (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