github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/consensus/clique/snapshot.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package clique
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/ethdb"
    26  	"github.com/ethereum/go-ethereum/params"
    27  	lru "github.com/hashicorp/golang-lru"
    28  )
    29  
    30  // Vote represents a single vote that an authorized signer made to modify the
    31  // list of authorizations.
    32  // 授权签名者为修改授权列表而进行的单一投票
    33  type Vote struct {
    34  	// 提出投票的signer
    35  	Signer    common.Address `json:"signer"`    // Authorized signer that cast this vote
    36  	// 投票所在的区块编号
    37  	Block     uint64         `json:"block"`     // Block number the vote was cast in (expire old votes)
    38  	// 被投票更改认证状态的地址
    39  	Address   common.Address `json:"address"`   // Account being voted on to change its authorization
    40  	// 是否授权或取消对已投票帐户的授权
    41  	Authorize bool           `json:"authorize"` // Whether to authorize or deauthorize the voted account
    42  }
    43  
    44  // Tally is a simple vote tally to keep the current score of votes. Votes that
    45  // go against the proposal aren't counted since it's equivalent to not voting.
    46  // 一个简单的投票结果,以保持当前的投票得分。
    47  type Tally struct {
    48  	// 投票是关于授权新的signer还是踢掉signer
    49  	Authorize bool `json:"authorize"` // Whether the vote is about authorizing or kicking someone
    50  	// 到目前为止希望通过提案的投票数
    51  	Votes     int  `json:"votes"`     // Number of votes until now wanting to pass the proposal
    52  }
    53  
    54  // Snapshot is the state of the authorization voting at a given point in time.
    55  // 指定时间点的投票状态
    56  type Snapshot struct {
    57  	// clique共识配置
    58  	config   *params.CliqueConfig // Consensus engine parameters to fine tune behavior
    59  	// 最近区块签名的缓存,为了加速恢复
    60  	sigcache *lru.ARCCache        // Cache of recent block signatures to speed up ecrecover
    61  
    62  	// 快照建立的区块号
    63  	Number  uint64                      `json:"number"`  // Block number where the snapshot was created
    64  	// 区块hash
    65  	Hash    common.Hash                 `json:"hash"`    // Block hash where the snapshot was created
    66  	// 当下Signer的集合
    67  	Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
    68  	// 最近签名区块地址的集合
    69  	Recents map[uint64]common.Address   `json:"recents"` // Set of recent signers for spam protections
    70  	// 按顺序排列的投票列表
    71  	Votes   []*Vote                     `json:"votes"`   // List of votes cast in chronological order
    72  	// 当前投票结果,可以避免重新计算
    73  	Tally   map[common.Address]Tally    `json:"tally"`   // Current vote tally to avoid recalculating
    74  }
    75  
    76  // newSnapshot creates a new snapshot with the specified startup parameters. This
    77  // method does not initialize the set of recent signers, so only ever use if for
    78  // the genesis block.
    79  func newSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, number uint64, hash common.Hash, signers []common.Address) *Snapshot {
    80  	snap := &Snapshot{
    81  		config:   config,
    82  		sigcache: sigcache,
    83  		Number:   number,
    84  		Hash:     hash,
    85  		Signers:  make(map[common.Address]struct{}),
    86  		Recents:  make(map[uint64]common.Address),
    87  		Tally:    make(map[common.Address]Tally),
    88  	}
    89  	for _, signer := range signers {
    90  		snap.Signers[signer] = struct{}{}
    91  	}
    92  	return snap
    93  }
    94  
    95  // loadSnapshot loads an existing snapshot from the database.
    96  func loadSnapshot(config *params.CliqueConfig, sigcache *lru.ARCCache, db ethdb.Database, hash common.Hash) (*Snapshot, error) {
    97  	blob, err := db.Get(append([]byte("clique-"), hash[:]...))
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	snap := new(Snapshot)
   102  	if err := json.Unmarshal(blob, snap); err != nil {
   103  		return nil, err
   104  	}
   105  	snap.config = config
   106  	snap.sigcache = sigcache
   107  
   108  	return snap, nil
   109  }
   110  
   111  // store inserts the snapshot into the database.
   112  func (s *Snapshot) store(db ethdb.Database) error {
   113  	blob, err := json.Marshal(s)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	return db.Put(append([]byte("clique-"), s.Hash[:]...), blob)
   118  }
   119  
   120  // copy creates a deep copy of the snapshot, though not the individual votes.
   121  func (s *Snapshot) copy() *Snapshot {
   122  	cpy := &Snapshot{
   123  		config:   s.config,
   124  		sigcache: s.sigcache,
   125  		Number:   s.Number,
   126  		Hash:     s.Hash,
   127  		Signers:  make(map[common.Address]struct{}),
   128  		Recents:  make(map[uint64]common.Address),
   129  		Votes:    make([]*Vote, len(s.Votes)),
   130  		Tally:    make(map[common.Address]Tally),
   131  	}
   132  	for signer := range s.Signers {
   133  		cpy.Signers[signer] = struct{}{}
   134  	}
   135  	for block, signer := range s.Recents {
   136  		cpy.Recents[block] = signer
   137  	}
   138  	for address, tally := range s.Tally {
   139  		cpy.Tally[address] = tally
   140  	}
   141  	copy(cpy.Votes, s.Votes)
   142  
   143  	return cpy
   144  }
   145  
   146  // validVote returns whether it makes sense to cast the specified vote in the
   147  // given snapshot context (e.g. don't try to add an already authorized signer).
   148  func (s *Snapshot) validVote(address common.Address, authorize bool) bool {
   149  	_, signer := s.Signers[address]
   150  	return (signer && !authorize) || (!signer && authorize)
   151  }
   152  
   153  // cast adds a new vote into the tally.
   154  func (s *Snapshot) cast(address common.Address, authorize bool) bool {
   155  	// Ensure the vote is meaningful
   156  	if !s.validVote(address, authorize) {
   157  		return false
   158  	}
   159  	// Cast the vote into an existing or new tally
   160  	if old, ok := s.Tally[address]; ok {
   161  		old.Votes++
   162  		s.Tally[address] = old
   163  	} else {
   164  		s.Tally[address] = Tally{Authorize: authorize, Votes: 1}
   165  	}
   166  	return true
   167  }
   168  
   169  // uncast removes a previously cast vote from the tally.
   170  func (s *Snapshot) uncast(address common.Address, authorize bool) bool {
   171  	// If there's no tally, it's a dangling vote, just drop
   172  	tally, ok := s.Tally[address]
   173  	if !ok {
   174  		return false
   175  	}
   176  	// Ensure we only revert counted votes
   177  	if tally.Authorize != authorize {
   178  		return false
   179  	}
   180  	// Otherwise revert the vote
   181  	if tally.Votes > 1 {
   182  		tally.Votes--
   183  		s.Tally[address] = tally
   184  	} else {
   185  		delete(s.Tally, address)
   186  	}
   187  	return true
   188  }
   189  
   190  // apply creates a new authorization snapshot by applying the given headers to
   191  // the original one.
   192  // 根据区块头创建一个新的signer的快照
   193  func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
   194  	// Allow passing in no headers for cleaner code
   195  	if len(headers) == 0 {
   196  		return s, nil
   197  	}
   198  	// Sanity check that the headers can be applied
   199  	// 对入参区块头做完整性检查
   200  	for i := 0; i < len(headers)-1; i++ {
   201  		if headers[i+1].Number.Uint64() != headers[i].Number.Uint64()+1 {
   202  			return nil, errInvalidVotingChain
   203  		}
   204  	}
   205  
   206  	// 判断区块序号是否连续
   207  	if headers[0].Number.Uint64() != s.Number+1 {
   208  		return nil, errInvalidVotingChain
   209  	}
   210  	// Iterate through the headers and create a new snapshot
   211  	// 遍历区块头数组并建立新的侉子好
   212  	snap := s.copy()
   213  
   214  	for _, header := range headers {
   215  		// Remove any votes on checkpoint blocks
   216  		// 移除检查点快照上的任何投票
   217  		number := header.Number.Uint64()
   218  		if number%s.config.Epoch == 0 {
   219  			snap.Votes = nil
   220  			snap.Tally = make(map[common.Address]Tally)
   221  		}
   222  		// Delete the oldest signer from the recent list to allow it signing again
   223  		// 移除投票票数过半,移除signer
   224  		if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
   225  			delete(snap.Recents, number-limit)
   226  		}
   227  		// Resolve the authorization key and check against signers
   228  		// 解析授权密钥并检查签名者
   229  		signer, err := ecrecover(header, s.sigcache)
   230  		if err != nil {
   231  			return nil, err
   232  		}
   233  		if _, ok := snap.Signers[signer]; !ok {
   234  			return nil, errUnauthorized
   235  		}
   236  		for _, recent := range snap.Recents {
   237  			if recent == signer {
   238  				return nil, errUnauthorized
   239  			}
   240  		}
   241  		// 记录signer为该区块的签名者
   242  		snap.Recents[number] = signer
   243  
   244  		// Header authorized, discard any previous votes from the signer
   245  		// 丢弃之前的投票
   246  		for i, vote := range snap.Votes {
   247  			if vote.Signer == signer && vote.Address == header.Coinbase {
   248  				// Uncast the vote from the cached tally
   249  				// 从缓存的计数中取消投票
   250  				snap.uncast(vote.Address, vote.Authorize)
   251  
   252  				// Uncast the vote from the chronological list
   253  				// 从时间顺序列表中取消投票
   254  				snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
   255  				break // only one vote allowed
   256  			}
   257  		}
   258  		// Tally up the new vote from the signer
   259  		// 从签名者那里获得新的投票
   260  		var authorize bool
   261  		switch {
   262  		case bytes.Equal(header.Nonce[:], nonceAuthVote):
   263  			authorize = true
   264  		case bytes.Equal(header.Nonce[:], nonceDropVote):
   265  			authorize = false
   266  		default:
   267  			return nil, errInvalidVote
   268  		}
   269  		if snap.cast(header.Coinbase, authorize) {
   270  			snap.Votes = append(snap.Votes, &Vote{
   271  				Signer:    signer,
   272  				Block:     number,
   273  				Address:   header.Coinbase,
   274  				Authorize: authorize,
   275  			})
   276  		}
   277  		// If the vote passed, update the list of signers
   278  		// 投票通过,更新signers列表
   279  		if tally := snap.Tally[header.Coinbase]; tally.Votes > len(snap.Signers)/2 {
   280  
   281  			// 投票是选举新signer
   282  			if tally.Authorize {
   283  				snap.Signers[header.Coinbase] = struct{}{}
   284  			} else {
   285  				// 投票是选移除signer
   286  				delete(snap.Signers, header.Coinbase)
   287  
   288  				// Signer list shrunk, delete any leftover recent caches
   289  				// 签名者列表缩小,删除任何剩余的最近缓存
   290  				if limit := uint64(len(snap.Signers)/2 + 1); number >= limit {
   291  					delete(snap.Recents, number-limit)
   292  				}
   293  				// Discard any previous votes the deauthorized signer cast
   294  				// 放弃任何以前的授权签名者投票
   295  				for i := 0; i < len(snap.Votes); i++ {
   296  					if snap.Votes[i].Signer == header.Coinbase {
   297  						// Uncast the vote from the cached tally
   298  						snap.uncast(snap.Votes[i].Address, snap.Votes[i].Authorize)
   299  
   300  						// Uncast the vote from the chronological list
   301  						snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
   302  
   303  						i--
   304  					}
   305  				}
   306  			}
   307  			// Discard any previous votes around the just changed account
   308  			// 放弃已更改授权状态的账户之前的投票
   309  			for i := 0; i < len(snap.Votes); i++ {
   310  				if snap.Votes[i].Address == header.Coinbase {
   311  					snap.Votes = append(snap.Votes[:i], snap.Votes[i+1:]...)
   312  					i--
   313  				}
   314  			}
   315  			delete(snap.Tally, header.Coinbase)
   316  		}
   317  	}
   318  	snap.Number += uint64(len(headers))
   319  	snap.Hash = headers[len(headers)-1].Hash()
   320  
   321  	return snap, nil
   322  }
   323  
   324  // signers retrieves the list of authorized signers in ascending order.
   325  func (s *Snapshot) signers() []common.Address {
   326  	signers := make([]common.Address, 0, len(s.Signers))
   327  	for signer := range s.Signers {
   328  		signers = append(signers, signer)
   329  	}
   330  	for i := 0; i < len(signers); i++ {
   331  		for j := i + 1; j < len(signers); j++ {
   332  			if bytes.Compare(signers[i][:], signers[j][:]) > 0 {
   333  				signers[i], signers[j] = signers[j], signers[i]
   334  			}
   335  		}
   336  	}
   337  	return signers
   338  }
   339  
   340  // inturn returns if a signer at a given block height is in-turn or not.
   341  // 给定高度块的签名者是否轮流
   342  func (s *Snapshot) inturn(number uint64, signer common.Address) bool {
   343  	signers, offset := s.signers(), 0
   344  	for offset < len(signers) && signers[offset] != signer {
   345  		offset++
   346  	}
   347  	return (number % uint64(len(signers))) == uint64(offset)
   348  }