Skip to content

Commit

Permalink
Support snap sync for consortium consensus (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
ducthotran2010 authored and DNK90 committed Dec 10, 2021
1 parent 40e3e80 commit 604c0d0
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 19 deletions.
118 changes: 99 additions & 19 deletions consensus/consortium/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
)

const (
checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database
inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory
inmemorySignatures = 4096 // Number of recent block signatures to keep in memory

wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers
Expand Down Expand Up @@ -165,6 +167,7 @@ type Consortium struct {
config *params.ConsortiumConfig // Consensus engine configuration parameters
db ethdb.Database // Database to store and retrieve snapshot checkpoints

recents *lru.ARCCache // Snapshots for recent block to speed up reorgs
signatures *lru.ARCCache // Signatures of recent blocks to speed up mining

proposals map[common.Address]bool // Current list of proposals we are pushing
Expand All @@ -185,11 +188,13 @@ func New(config *params.ConsortiumConfig, db ethdb.Database) *Consortium {
conf.Epoch = epochLength
}
// Allocate the snapshot caches and create the engine
recents, _ := lru.NewARC(inmemorySnapshots)
signatures, _ := lru.NewARC(inmemorySignatures)

return &Consortium{
config: &conf,
db: db,
recents: recents,
signatures: signatures,
proposals: make(map[common.Address]bool),
}
Expand Down Expand Up @@ -311,23 +316,93 @@ func (c *Consortium) verifyCascadingFields(chain consensus.ChainHeaderReader, he
if parent.Time+c.config.Period > header.Time {
return ErrInvalidTimestamp
}
// If the block is a checkpoint block, verify the signer list
if number%c.config.Epoch == 0 {
validators, err := c.getValidatorsFromContract()
if err != nil {
return err

// Todo(Thor): If the block is a checkpoint block, verify the signer list

// All basic checks passed, verify the seal and return
return c.verifySeal(chain, header, parents)
}

// snapshot retrieves the authorization snapshot at a given point in time.
func (c *Consortium) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
// Search for a snapshot in memory or on disk for checkpoints
var (
headers []*types.Header
snap *Snapshot
)
for snap == nil {
// If an in-memory snapshot was found, use that
if s, ok := c.recents.Get(hash); ok {
snap = s.(*Snapshot)
break
}
// If an on-disk checkpoint snapshot can be found, use that
if number%checkpointInterval == 0 {
if s, err := loadSnapshot(c.config, c.signatures, c.db, hash); err == nil {
log.Trace("Loaded snapshot from disk", "number", number, "hash", hash)
snap = s
break
}
}
signers := make([]byte, len(validators)*common.AddressLength)
for i, signer := range validators {
copy(signers[i*common.AddressLength:], signer[:])
// If we're at the genesis, snapshot the initial state. Alternatively if we're
// at a checkpoint block without a parent (light client CHT), or we have piled
// up more headers than allowed to be reorged (chain reinit from a freezer),
// consider the checkpoint trusted and snapshot it.
if number == 0 || (number%c.config.Epoch == 0 && (len(headers) > params.FullImmutabilityThreshold || chain.GetHeaderByNumber(number-1) == nil)) {
cpHeader := chain.GetHeaderByNumber(number)
if cpHeader != nil {
hash := cpHeader.Hash()

validators, err := c.getValidatorsFromContract()
if err != nil {
return nil, err
}

snap = newSnapshot(c.config, c.signatures, number, hash, validators)
if err := snap.store(c.db); err != nil {
return nil, err
}
log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash)
break
}
}
extraSuffix := len(header.Extra) - extraSeal
if !bytes.Equal(header.Extra[extraVanity:extraSuffix], signers) {
return errMismatchingCheckpointSigners
// No snapshot for this header, gather the header and move backward
var header *types.Header
if len(parents) > 0 {
// If we have explicit parents, pick from there (enforced)
header = parents[len(parents)-1]
if header.Hash() != hash || header.Number.Uint64() != number {
return nil, consensus.ErrUnknownAncestor
}
parents = parents[:len(parents)-1]
} else {
// No explicit parents (or no more left), reach out to the database
header = chain.GetHeader(hash, number)
if header == nil {
return nil, consensus.ErrUnknownAncestor
}
}
headers = append(headers, header)
number, hash = number-1, header.ParentHash
}
// All basic checks passed, verify the seal and return
return c.verifySeal(chain, header, parents)
// Previous snapshot found, apply any pending headers on top of it
for i := 0; i < len(headers)/2; i++ {
headers[i], headers[len(headers)-1-i] = headers[len(headers)-1-i], headers[i]
}
snap, err := snap.apply(chain, c, headers, parents)
if err != nil {
return nil, err
}
c.recents.Add(snap.Hash, snap)

// If we've generated a new checkpoint snapshot, save to disk
if snap.Number%checkpointInterval == 0 && len(headers) > 0 {
if err = snap.store(c.db); err != nil {
return nil, err
}
log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash)
}
return snap, err
}

// VerifyUncles implements consensus.Engine, always returning an error for any
Expand Down Expand Up @@ -356,24 +431,29 @@ func (c *Consortium) verifySeal(chain consensus.ChainHeaderReader, header *types
return errUnknownBlock
}

// Verifying the genesis block is not supported
// Retrieve the snapshot needed to verify this header and cache it
snap, err := c.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil {
return err
}

// Resolve the authorization key and check against signers
signer, err := ecrecover(header, c.signatures)
if err != nil {
return err
}

if signer != header.Coinbase {
return errWrongCoinbase
}

validators, err := c.getValidatorsFromLastCheckpoint(chain, number-1, parents)
if err != nil {
return err
}
if !signerInList(signer, validators) {
if _, ok := snap.SignerSet[signer]; !ok {
return errUnauthorizedSigner
}

// Ensure that the difficulty corresponds to the turn-ness of the signer
validators := snap.SignerList
inturn := c.signerInTurn(signer, header.Number.Uint64(), validators)
if inturn && header.Difficulty.Cmp(diffInTurn) != 0 {
return errWrongDifficulty
Expand All @@ -392,11 +472,11 @@ func (c *Consortium) Prepare(chain consensus.ChainHeaderReader, header *types.He
header.Nonce = types.BlockNonce{}

number := header.Number.Uint64()
// Set the correct difficulty
validators, err := c.getValidatorsFromLastCheckpoint(chain, number-1, nil)
if err != nil {
return err
}
// Set the correct difficulty
header.Difficulty = c.doCalcDifficulty(c.signer, number, validators)

// Ensure the extra data has all its components
Expand Down
162 changes: 162 additions & 0 deletions consensus/consortium/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package consortium

import (
"encoding/json"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
lru "github.com/hashicorp/golang-lru"
)

// Snapshot is the state of the authorization voting at a given point in time.
type Snapshot struct {
config *params.ConsortiumConfig // 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
SignerSet map[common.Address]struct{} `json:"signerSet"` // Set of authorized signers at this moment
SignerList []common.Address `json:"signerList"` // List of authorized signers at this moment
}

// newSnapshot creates 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.ConsortiumConfig, sigcache *lru.ARCCache, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
snap := &Snapshot{
config: config,
sigcache: sigcache,
Number: number,
Hash: hash,
SignerSet: make(map[common.Address]struct{}),
SignerList: make([]common.Address, 0, len(signers)),
}

for _, signer := range signers {
snap.SignerSet[signer] = struct{}{}
snap.SignerList = append(snap.SignerList, signer)
}

return snap
}

// loadSnapshot loads an existing snapshot from the database.
func loadSnapshot(config *params.ConsortiumConfig, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
blob, err := db.Get(append([]byte("consortium-"), hash[:]...))
if err != nil {
return nil, err
}
snap := new(Snapshot)
if err := json.Unmarshal(blob, snap); err != nil {
return nil, err
}
snap.config = config
snap.sigcache = sigcache

return snap, nil
}

// store inserts the snapshot into the database.
func (s *Snapshot) store(db ethdb.Database) error {
blob, err := json.Marshal(s)
if err != nil {
return err
}
return db.Put(append([]byte("consortium-"), s.Hash[:]...), blob)
}

// copy creates a deep copy of the snapshot, though not the individual votes.
func (s *Snapshot) copy() *Snapshot {
cpy := &Snapshot{
config: s.config,
sigcache: s.sigcache,
Number: s.Number,
Hash: s.Hash,
SignerSet: make(map[common.Address]struct{}),
SignerList: make([]common.Address, 0, len(s.SignerList)),
}

for _, signer := range s.SignerList {
cpy.SignerSet[signer] = struct{}{}
cpy.SignerList = append(cpy.SignerList, signer)
}

return cpy
}

// apply creates a new authorization snapshot by applying the given headers to
// the original one.
func (s *Snapshot) apply(chain consensus.ChainHeaderReader, c *Consortium, headers []*types.Header, parents []*types.Header) (*Snapshot, error) {
// Allow passing in no headers for cleaner code
if len(headers) == 0 {
return s, nil
}

// Iterate through the headers and create a new snapshot
snap := s.copy()

var (
start = time.Now()
logged = time.Now()
)
for i, header := range headers {
// Resolve the authorization key and check against signers
signer, err := ecrecover(header, s.sigcache)
if err != nil {
return nil, err
}
if _, ok := snap.SignerSet[signer]; !ok {
return nil, errUnauthorizedSigner
}

// If we're taking too much time (ecrecover), notify the user once a while
if time.Since(logged) > 8*time.Second {
log.Info("Reconstructing snapshot", "processed", i, "total", len(headers), "elapsed", common.PrettyDuration(time.Since(start)))
logged = time.Now()
}
}

snap.Number += uint64(len(headers))
snap.Hash = headers[len(headers)-1].Hash()

// Update the list of signers
number := headers[len(headers)-1].Number.Uint64()
validators, err := c.getValidatorsFromLastCheckpoint(chain, number-1, parents)
if err != nil {
return nil, err
}
snap.SignerSet = make(map[common.Address]struct{})
snap.SignerList = make([]common.Address, 0, len(validators))
for _, signer := range validators {
snap.SignerSet[signer] = struct{}{}
snap.SignerList = append(snap.SignerList, signer)
}

// If we're taking too much time, notify the user once a while
if time.Since(start) > 8*time.Second {
log.Info("Reconstructed snapshot", "processed", len(headers), "elapsed", common.PrettyDuration(time.Since(start)))
}

return snap, nil
}

0 comments on commit 604c0d0

Please sign in to comment.