diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-05-30 03:05:30 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-05-30 03:07:02 +0800 |
commit | 309da541dec34a51d38a8c4f7f76b7e0d3c8eb60 (patch) | |
tree | e439af10c00364060f4b4099a1643372086d2010 /consensus/clique/snapshot.go | |
parent | dd06c8584368316c8fb388384b0723d8c7e543f0 (diff) | |
download | dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.tar dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.tar.gz dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.tar.bz2 dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.tar.lz dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.tar.xz dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.tar.zst dexon-309da541dec34a51d38a8c4f7f76b7e0d3c8eb60.zip |
consensus/clique: cache block signatures for fast checks
Diffstat (limited to 'consensus/clique/snapshot.go')
-rw-r--r-- | consensus/clique/snapshot.go | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/consensus/clique/snapshot.go b/consensus/clique/snapshot.go index 46b32ca5f..fb86bc5e6 100644 --- a/consensus/clique/snapshot.go +++ b/consensus/clique/snapshot.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/params" + lru "github.com/hashicorp/golang-lru" ) // Vote represents a single vote that an authorized signer made to modify the @@ -44,7 +45,8 @@ type Tally struct { // Snapshot is the state of the authorization voting at a given point in time. type Snapshot struct { - config *params.CliqueConfig // Consensus engine parameters to fine tune behavior + config *params.CliqueConfig // Consensus engine parameters to fine tune behavior + sigcache *lru.ARCCache // Cache of recent block signatures to speed up ecrecover Number uint64 `json:"number"` // Block number where the snapshot was created Hash common.Hash `json:"hash"` // Block hash where the snapshot was created @@ -57,14 +59,15 @@ type Snapshot struct { // 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 // the genesis block. -func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, signers []common.Address) *Snapshot { +func newSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, number uint64, hash common.Hash, signers []common.Address) *Snapshot { snap := &Snapshot{ - config: config, - Number: number, - Hash: hash, - Signers: make(map[common.Address]struct{}), - Recents: make(map[uint64]common.Address), - Tally: make(map[common.Address]Tally), + config: config, + sigcache: sigcache, + Number: number, + Hash: hash, + Signers: make(map[common.Address]struct{}), + Recents: make(map[uint64]common.Address), + Tally: make(map[common.Address]Tally), } for _, signer := range signers { snap.Signers[signer] = struct{}{} @@ -73,7 +76,7 @@ func newSnapshot(config *params.CliqueConfig, number uint64, hash common.Hash, s } // 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, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash) (*Snapshot, error) { blob, err := db.Get(append([]byte("clique-"), hash[:]...)) if err != nil { return nil, err @@ -83,6 +86,7 @@ func loadSnapshot(config *params.CliqueConfig, db ethdb.Database, hash common.Ha return nil, err } snap.config = config + snap.sigcache = sigcache return snap, nil } @@ -99,13 +103,14 @@ func (s *Snapshot) store(db ethdb.Database) error { // copy creates a deep copy of the snapshot, though not the individual votes. func (s *Snapshot) copy() *Snapshot { cpy := &Snapshot{ - config: s.config, - Number: s.Number, - Hash: s.Hash, - Signers: make(map[common.Address]struct{}), - Recents: make(map[uint64]common.Address), - Votes: make([]*Vote, len(s.Votes)), - Tally: make(map[common.Address]Tally), + config: s.config, + sigcache: s.sigcache, + Number: s.Number, + Hash: s.Hash, + Signers: make(map[common.Address]struct{}), + Recents: make(map[uint64]common.Address), + Votes: make([]*Vote, len(s.Votes)), + Tally: make(map[common.Address]Tally), } for signer := range s.Signers { cpy.Signers[signer] = struct{}{} @@ -190,7 +195,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { delete(snap.Recents, number-limit) } // Resolve the authorization key and check against signers - signer, err := ecrecover(header) + signer, err := ecrecover(header, s.sigcache) if err != nil { return nil, err } |