github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/eth/downloader/downloader_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:37</date>
    10  //</624450087988236288>
    11  
    12  
    13  package downloader
    14  
    15  import (
    16  	"errors"
    17  	"fmt"
    18  	"math/big"
    19  	"strings"
    20  	"sync"
    21  	"sync/atomic"
    22  	"testing"
    23  	"time"
    24  
    25  	ethereum "github.com/ethereum/go-ethereum"
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	"github.com/ethereum/go-ethereum/ethdb"
    29  	"github.com/ethereum/go-ethereum/event"
    30  	"github.com/ethereum/go-ethereum/trie"
    31  )
    32  
    33  //减少一些参数以使测试仪更快。
    34  func init() {
    35  	MaxForkAncestry = uint64(10000)
    36  	blockCacheItems = 1024
    37  	fsHeaderContCheck = 500 * time.Millisecond
    38  }
    39  
    40  //下载测试仪是模拟本地区块链的测试模拟器。
    41  type downloadTester struct {
    42  	downloader *Downloader
    43  
    44  genesis *types.Block   //测试人员和同行使用的Genesis块
    45  stateDb ethdb.Database //测试人员用于从对等机同步的数据库
    46  peerDb  ethdb.Database //包含所有数据的对等数据库
    47  	peers   map[string]*downloadTesterPeer
    48  
    49  ownHashes   []common.Hash                  //属于测试人员的哈希链
    50  ownHeaders  map[common.Hash]*types.Header  //属于检测仪的收割台
    51  ownBlocks   map[common.Hash]*types.Block   //属于测试仪的块
    52  ownReceipts map[common.Hash]types.Receipts //属于测试人员的收据
    53  ownChainTd  map[common.Hash]*big.Int       //本地链中块的总困难
    54  
    55  	lock sync.RWMutex
    56  }
    57  
    58  //NewTester创建了一个新的下载程序测试mocker。
    59  func newTester() *downloadTester {
    60  	tester := &downloadTester{
    61  		genesis:     testGenesis,
    62  		peerDb:      testDB,
    63  		peers:       make(map[string]*downloadTesterPeer),
    64  		ownHashes:   []common.Hash{testGenesis.Hash()},
    65  		ownHeaders:  map[common.Hash]*types.Header{testGenesis.Hash(): testGenesis.Header()},
    66  		ownBlocks:   map[common.Hash]*types.Block{testGenesis.Hash(): testGenesis},
    67  		ownReceipts: map[common.Hash]types.Receipts{testGenesis.Hash(): nil},
    68  		ownChainTd:  map[common.Hash]*big.Int{testGenesis.Hash(): testGenesis.Difficulty()},
    69  	}
    70  	tester.stateDb = ethdb.NewMemDatabase()
    71  	tester.stateDb.Put(testGenesis.Root().Bytes(), []byte{0x00})
    72  	tester.downloader = New(FullSync, tester.stateDb, new(event.TypeMux), tester, nil, tester.dropPeer)
    73  	return tester
    74  }
    75  
    76  //终止中止嵌入式下载程序上的任何操作并释放所有
    77  //持有资源。
    78  func (dl *downloadTester) terminate() {
    79  	dl.downloader.Terminate()
    80  }
    81  
    82  //同步开始与远程对等机同步,直到同步完成。
    83  func (dl *downloadTester) sync(id string, td *big.Int, mode SyncMode) error {
    84  	dl.lock.RLock()
    85  	hash := dl.peers[id].chain.headBlock().Hash()
    86  //如果没有请求特定的TD,则从对等区块链加载
    87  	if td == nil {
    88  		td = dl.peers[id].chain.td(hash)
    89  	}
    90  	dl.lock.RUnlock()
    91  
    92  //与选定的对等机同步,并确保随后正确清理
    93  	err := dl.downloader.synchronise(id, hash, td, mode)
    94  	select {
    95  	case <-dl.downloader.cancelCh:
    96  //好的,下载程序在同步循环后完全取消
    97  	default:
    98  //下载程序仍在接受数据包,可能会阻止对等机
    99  panic("downloader active post sync cycle") //测试人员会发现恐慌
   100  	}
   101  	return err
   102  }
   103  
   104  //hasheader检查测试仪规范链中是否存在头。
   105  func (dl *downloadTester) HasHeader(hash common.Hash, number uint64) bool {
   106  	return dl.GetHeaderByHash(hash) != nil
   107  }
   108  
   109  //hasblock检查测试仪规范链中是否存在块。
   110  func (dl *downloadTester) HasBlock(hash common.Hash, number uint64) bool {
   111  	return dl.GetBlockByHash(hash) != nil
   112  }
   113  
   114  //HasFastBlock检查测试仪规范链中是否存在块。
   115  func (dl *downloadTester) HasFastBlock(hash common.Hash, number uint64) bool {
   116  	dl.lock.RLock()
   117  	defer dl.lock.RUnlock()
   118  
   119  	_, ok := dl.ownReceipts[hash]
   120  	return ok
   121  }
   122  
   123  //getheader从测试人员规范链中检索一个头。
   124  func (dl *downloadTester) GetHeaderByHash(hash common.Hash) *types.Header {
   125  	dl.lock.RLock()
   126  	defer dl.lock.RUnlock()
   127  
   128  	return dl.ownHeaders[hash]
   129  }
   130  
   131  //GetBlock从测试程序规范链中检索一个块。
   132  func (dl *downloadTester) GetBlockByHash(hash common.Hash) *types.Block {
   133  	dl.lock.RLock()
   134  	defer dl.lock.RUnlock()
   135  
   136  	return dl.ownBlocks[hash]
   137  }
   138  
   139  //当前头从规范链中检索当前头。
   140  func (dl *downloadTester) CurrentHeader() *types.Header {
   141  	dl.lock.RLock()
   142  	defer dl.lock.RUnlock()
   143  
   144  	for i := len(dl.ownHashes) - 1; i >= 0; i-- {
   145  		if header := dl.ownHeaders[dl.ownHashes[i]]; header != nil {
   146  			return header
   147  		}
   148  	}
   149  	return dl.genesis.Header()
   150  }
   151  
   152  //currentBlock从规范链中检索当前头块。
   153  func (dl *downloadTester) CurrentBlock() *types.Block {
   154  	dl.lock.RLock()
   155  	defer dl.lock.RUnlock()
   156  
   157  	for i := len(dl.ownHashes) - 1; i >= 0; i-- {
   158  		if block := dl.ownBlocks[dl.ownHashes[i]]; block != nil {
   159  			if _, err := dl.stateDb.Get(block.Root().Bytes()); err == nil {
   160  				return block
   161  			}
   162  		}
   163  	}
   164  	return dl.genesis
   165  }
   166  
   167  //CurrentFastBlock从规范链中检索当前磁头快速同步块。
   168  func (dl *downloadTester) CurrentFastBlock() *types.Block {
   169  	dl.lock.RLock()
   170  	defer dl.lock.RUnlock()
   171  
   172  	for i := len(dl.ownHashes) - 1; i >= 0; i-- {
   173  		if block := dl.ownBlocks[dl.ownHashes[i]]; block != nil {
   174  			return block
   175  		}
   176  	}
   177  	return dl.genesis
   178  }
   179  
   180  //fastsynccommithead手动将头块设置为给定哈希。
   181  func (dl *downloadTester) FastSyncCommitHead(hash common.Hash) error {
   182  //现在只检查状态trie是否正确。
   183  	if block := dl.GetBlockByHash(hash); block != nil {
   184  		_, err := trie.NewSecure(block.Root(), trie.NewDatabase(dl.stateDb), 0)
   185  		return err
   186  	}
   187  	return fmt.Errorf("non existent block: %x", hash[:4])
   188  }
   189  
   190  //gettd从规范链中检索块的总难度。
   191  func (dl *downloadTester) GetTd(hash common.Hash, number uint64) *big.Int {
   192  	dl.lock.RLock()
   193  	defer dl.lock.RUnlock()
   194  
   195  	return dl.ownChainTd[hash]
   196  }
   197  
   198  //InsertHeaderChain将新的一批头注入到模拟链中。
   199  func (dl *downloadTester) InsertHeaderChain(headers []*types.Header, checkFreq int) (i int, err error) {
   200  	dl.lock.Lock()
   201  	defer dl.lock.Unlock()
   202  
   203  //快速检查,因为区块链。插入头链不会插入任何内容以防出错。
   204  	if _, ok := dl.ownHeaders[headers[0].ParentHash]; !ok {
   205  		return 0, errors.New("unknown parent")
   206  	}
   207  	for i := 1; i < len(headers); i++ {
   208  		if headers[i].ParentHash != headers[i-1].Hash() {
   209  			return i, errors.New("unknown parent")
   210  		}
   211  	}
   212  //如果预检查通过,则执行完全插入
   213  	for i, header := range headers {
   214  		if _, ok := dl.ownHeaders[header.Hash()]; ok {
   215  			continue
   216  		}
   217  		if _, ok := dl.ownHeaders[header.ParentHash]; !ok {
   218  			return i, errors.New("unknown parent")
   219  		}
   220  		dl.ownHashes = append(dl.ownHashes, header.Hash())
   221  		dl.ownHeaders[header.Hash()] = header
   222  		dl.ownChainTd[header.Hash()] = new(big.Int).Add(dl.ownChainTd[header.ParentHash], header.Difficulty)
   223  	}
   224  	return len(headers), nil
   225  }
   226  
   227  //insertchain向模拟链中注入一批新的块。
   228  func (dl *downloadTester) InsertChain(blocks types.Blocks) (i int, err error) {
   229  	dl.lock.Lock()
   230  	defer dl.lock.Unlock()
   231  
   232  	for i, block := range blocks {
   233  		if parent, ok := dl.ownBlocks[block.ParentHash()]; !ok {
   234  			return i, errors.New("unknown parent")
   235  		} else if _, err := dl.stateDb.Get(parent.Root().Bytes()); err != nil {
   236  			return i, fmt.Errorf("unknown parent state %x: %v", parent.Root(), err)
   237  		}
   238  		if _, ok := dl.ownHeaders[block.Hash()]; !ok {
   239  			dl.ownHashes = append(dl.ownHashes, block.Hash())
   240  			dl.ownHeaders[block.Hash()] = block.Header()
   241  		}
   242  		dl.ownBlocks[block.Hash()] = block
   243  		dl.ownReceipts[block.Hash()] = make(types.Receipts, 0)
   244  		dl.stateDb.Put(block.Root().Bytes(), []byte{0x00})
   245  		dl.ownChainTd[block.Hash()] = new(big.Int).Add(dl.ownChainTd[block.ParentHash()], block.Difficulty())
   246  	}
   247  	return len(blocks), nil
   248  }
   249  
   250  //InsertReceiptChain将新的一批收据注入到模拟链中。
   251  func (dl *downloadTester) InsertReceiptChain(blocks types.Blocks, receipts []types.Receipts) (i int, err error) {
   252  	dl.lock.Lock()
   253  	defer dl.lock.Unlock()
   254  
   255  	for i := 0; i < len(blocks) && i < len(receipts); i++ {
   256  		if _, ok := dl.ownHeaders[blocks[i].Hash()]; !ok {
   257  			return i, errors.New("unknown owner")
   258  		}
   259  		if _, ok := dl.ownBlocks[blocks[i].ParentHash()]; !ok {
   260  			return i, errors.New("unknown parent")
   261  		}
   262  		dl.ownBlocks[blocks[i].Hash()] = blocks[i]
   263  		dl.ownReceipts[blocks[i].Hash()] = receipts[i]
   264  	}
   265  	return len(blocks), nil
   266  }
   267  
   268  //回滚从链中删除一些最近添加的元素。
   269  func (dl *downloadTester) Rollback(hashes []common.Hash) {
   270  	dl.lock.Lock()
   271  	defer dl.lock.Unlock()
   272  
   273  	for i := len(hashes) - 1; i >= 0; i-- {
   274  		if dl.ownHashes[len(dl.ownHashes)-1] == hashes[i] {
   275  			dl.ownHashes = dl.ownHashes[:len(dl.ownHashes)-1]
   276  		}
   277  		delete(dl.ownChainTd, hashes[i])
   278  		delete(dl.ownHeaders, hashes[i])
   279  		delete(dl.ownReceipts, hashes[i])
   280  		delete(dl.ownBlocks, hashes[i])
   281  	}
   282  }
   283  
   284  //newpeer向下载程序注册一个新的块下载源。
   285  func (dl *downloadTester) newPeer(id string, version int, chain *testChain) error {
   286  	dl.lock.Lock()
   287  	defer dl.lock.Unlock()
   288  
   289  	peer := &downloadTesterPeer{dl: dl, id: id, chain: chain}
   290  	dl.peers[id] = peer
   291  	return dl.downloader.RegisterPeer(id, version, peer)
   292  }
   293  
   294  //DropPeer模拟从连接池中删除硬对等。
   295  func (dl *downloadTester) dropPeer(id string) {
   296  	dl.lock.Lock()
   297  	defer dl.lock.Unlock()
   298  
   299  	delete(dl.peers, id)
   300  	dl.downloader.UnregisterPeer(id)
   301  }
   302  
   303  type downloadTesterPeer struct {
   304  	dl            *downloadTester
   305  	id            string
   306  	lock          sync.RWMutex
   307  	chain         *testChain
   308  missingStates map[common.Hash]bool //快速同步不应返回的状态项
   309  }
   310  
   311  //head构造一个函数来检索对等端的当前head哈希
   312  //以及全部的困难。
   313  func (dlp *downloadTesterPeer) Head() (common.Hash, *big.Int) {
   314  	b := dlp.chain.headBlock()
   315  	return b.Hash(), dlp.chain.td(b.Hash())
   316  }
   317  
   318  //RequestHeadersByHash基于哈希构造GetBlockHeaders函数
   319  //源站;与下载测试仪中的特定对等点关联。归还的人
   320  //函数可用于从特定的对等端检索成批的头。
   321  func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error {
   322  	if reverse {
   323  		panic("reverse header requests not supported")
   324  	}
   325  
   326  	result := dlp.chain.headersByHash(origin, amount, skip)
   327  	go dlp.dl.downloader.DeliverHeaders(dlp.id, result)
   328  	return nil
   329  }
   330  
   331  //RequestHeadersByNumber基于编号的
   332  //源站;与下载测试仪中的特定对等点关联。归还的人
   333  //函数可用于从特定的对等端检索成批的头。
   334  func (dlp *downloadTesterPeer) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error {
   335  	if reverse {
   336  		panic("reverse header requests not supported")
   337  	}
   338  
   339  	result := dlp.chain.headersByNumber(origin, amount, skip)
   340  	go dlp.dl.downloader.DeliverHeaders(dlp.id, result)
   341  	return nil
   342  }
   343  
   344  //RequestBodies构造与特定
   345  //在下载测试仪中查找。返回的函数可用于检索
   346  //来自特定请求对等端的一批块体。
   347  func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash) error {
   348  	txs, uncles := dlp.chain.bodies(hashes)
   349  	go dlp.dl.downloader.DeliverBodies(dlp.id, txs, uncles)
   350  	return nil
   351  }
   352  
   353  //RequestReceipts构造与特定
   354  //在下载测试仪中查找。返回的函数可用于检索
   355  //来自特定请求对等方的批量块接收。
   356  func (dlp *downloadTesterPeer) RequestReceipts(hashes []common.Hash) error {
   357  	receipts := dlp.chain.receipts(hashes)
   358  	go dlp.dl.downloader.DeliverReceipts(dlp.id, receipts)
   359  	return nil
   360  }
   361  
   362  //RequestNodeData构造与特定
   363  //在下载测试仪中查找。返回的函数可用于检索
   364  //来自特定请求对等端的节点状态数据批。
   365  func (dlp *downloadTesterPeer) RequestNodeData(hashes []common.Hash) error {
   366  	dlp.dl.lock.RLock()
   367  	defer dlp.dl.lock.RUnlock()
   368  
   369  	results := make([][]byte, 0, len(hashes))
   370  	for _, hash := range hashes {
   371  		if data, err := dlp.dl.peerDb.Get(hash.Bytes()); err == nil {
   372  			if !dlp.missingStates[hash] {
   373  				results = append(results, data)
   374  			}
   375  		}
   376  	}
   377  	go dlp.dl.downloader.DeliverNodeData(dlp.id, results)
   378  	return nil
   379  }
   380  
   381  //AssertownChain检查本地链是否包含正确数量的项
   382  //各种链条组件。
   383  func assertOwnChain(t *testing.T, tester *downloadTester, length int) {
   384  //将此方法标记为在调用站点(而不是在此处)报告错误的帮助程序
   385  	t.Helper()
   386  
   387  	assertOwnForkedChain(t, tester, 1, []int{length})
   388  }
   389  
   390  //AssertOwnMarkedChain检查本地分叉链是否包含正确的
   391  //各种链组件的项数。
   392  func assertOwnForkedChain(t *testing.T, tester *downloadTester, common int, lengths []int) {
   393  //将此方法标记为在调用站点(而不是在此处)报告错误的帮助程序
   394  	t.Helper()
   395  
   396  //初始化第一个分叉的计数器
   397  	headers, blocks, receipts := lengths[0], lengths[0], lengths[0]
   398  
   399  //更新每个后续分叉的计数器
   400  	for _, length := range lengths[1:] {
   401  		headers += length - common
   402  		blocks += length - common
   403  		receipts += length - common
   404  	}
   405  	if tester.downloader.mode == LightSync {
   406  		blocks, receipts = 1, 1
   407  	}
   408  	if hs := len(tester.ownHeaders); hs != headers {
   409  		t.Fatalf("synchronised headers mismatch: have %v, want %v", hs, headers)
   410  	}
   411  	if bs := len(tester.ownBlocks); bs != blocks {
   412  		t.Fatalf("synchronised blocks mismatch: have %v, want %v", bs, blocks)
   413  	}
   414  	if rs := len(tester.ownReceipts); rs != receipts {
   415  		t.Fatalf("synchronised receipts mismatch: have %v, want %v", rs, receipts)
   416  	}
   417  }
   418  
   419  //测试针对规范链的简单同步是否正常工作。
   420  //在此测试中,公共祖先查找应该是短路的,不需要
   421  //二进制搜索。
   422  func TestCanonicalSynchronisation62(t *testing.T)      { testCanonicalSynchronisation(t, 62, FullSync) }
   423  func TestCanonicalSynchronisation63Full(t *testing.T)  { testCanonicalSynchronisation(t, 63, FullSync) }
   424  func TestCanonicalSynchronisation63Fast(t *testing.T)  { testCanonicalSynchronisation(t, 63, FastSync) }
   425  func TestCanonicalSynchronisation64Full(t *testing.T)  { testCanonicalSynchronisation(t, 64, FullSync) }
   426  func TestCanonicalSynchronisation64Fast(t *testing.T)  { testCanonicalSynchronisation(t, 64, FastSync) }
   427  func TestCanonicalSynchronisation64Light(t *testing.T) { testCanonicalSynchronisation(t, 64, LightSync) }
   428  
   429  func testCanonicalSynchronisation(t *testing.T, protocol int, mode SyncMode) {
   430  	t.Parallel()
   431  
   432  	tester := newTester()
   433  	defer tester.terminate()
   434  
   435  //创建一个足够小的区块链来下载
   436  	chain := testChainBase.shorten(blockCacheItems - 15)
   437  	tester.newPeer("peer", protocol, chain)
   438  
   439  //与对等机同步并确保检索到所有相关数据
   440  	if err := tester.sync("peer", nil, mode); err != nil {
   441  		t.Fatalf("failed to synchronise blocks: %v", err)
   442  	}
   443  	assertOwnChain(t, tester, chain.len())
   444  }
   445  
   446  //测试如果下载了大量块,则会阻止
   447  //直到检索到缓存块。
   448  func TestThrottling62(t *testing.T)     { testThrottling(t, 62, FullSync) }
   449  func TestThrottling63Full(t *testing.T) { testThrottling(t, 63, FullSync) }
   450  func TestThrottling63Fast(t *testing.T) { testThrottling(t, 63, FastSync) }
   451  func TestThrottling64Full(t *testing.T) { testThrottling(t, 64, FullSync) }
   452  func TestThrottling64Fast(t *testing.T) { testThrottling(t, 64, FastSync) }
   453  
   454  func testThrottling(t *testing.T, protocol int, mode SyncMode) {
   455  	t.Parallel()
   456  	tester := newTester()
   457  	defer tester.terminate()
   458  
   459  //创建要下载的长区块链和测试仪
   460  	targetBlocks := testChainBase.len() - 1
   461  	tester.newPeer("peer", protocol, testChainBase)
   462  
   463  //包裹导入器以允许步进
   464  	blocked, proceed := uint32(0), make(chan struct{})
   465  	tester.downloader.chainInsertHook = func(results []*fetchResult) {
   466  		atomic.StoreUint32(&blocked, uint32(len(results)))
   467  		<-proceed
   468  	}
   469  //同时启动同步
   470  	errc := make(chan error)
   471  	go func() {
   472  		errc <- tester.sync("peer", nil, mode)
   473  	}()
   474  //迭代获取一些块,始终检查检索计数
   475  	for {
   476  //同步检查检索计数(!这个丑陋街区的原因)
   477  		tester.lock.RLock()
   478  		retrieved := len(tester.ownBlocks)
   479  		tester.lock.RUnlock()
   480  		if retrieved >= targetBlocks+1 {
   481  			break
   482  		}
   483  //等一下,让同步自行调节。
   484  		var cached, frozen int
   485  		for start := time.Now(); time.Since(start) < 3*time.Second; {
   486  			time.Sleep(25 * time.Millisecond)
   487  
   488  			tester.lock.Lock()
   489  			tester.downloader.queue.lock.Lock()
   490  			cached = len(tester.downloader.queue.blockDonePool)
   491  			if mode == FastSync {
   492  				if receipts := len(tester.downloader.queue.receiptDonePool); receipts < cached {
   493  					cached = receipts
   494  				}
   495  			}
   496  			frozen = int(atomic.LoadUint32(&blocked))
   497  			retrieved = len(tester.ownBlocks)
   498  			tester.downloader.queue.lock.Unlock()
   499  			tester.lock.Unlock()
   500  
   501  			if cached == blockCacheItems || cached == blockCacheItems-reorgProtHeaderDelay || retrieved+cached+frozen == targetBlocks+1 || retrieved+cached+frozen == targetBlocks+1-reorgProtHeaderDelay {
   502  				break
   503  			}
   504  		}
   505  //确保我们填满了缓存,然后耗尽它
   506  time.Sleep(25 * time.Millisecond) //给它一个搞砸的机会
   507  
   508  		tester.lock.RLock()
   509  		retrieved = len(tester.ownBlocks)
   510  		tester.lock.RUnlock()
   511  		if cached != blockCacheItems && cached != blockCacheItems-reorgProtHeaderDelay && retrieved+cached+frozen != targetBlocks+1 && retrieved+cached+frozen != targetBlocks+1-reorgProtHeaderDelay {
   512  			t.Fatalf("block count mismatch: have %v, want %v (owned %v, blocked %v, target %v)", cached, blockCacheItems, retrieved, frozen, targetBlocks+1)
   513  		}
   514  //允许导入被阻止的块
   515  		if atomic.LoadUint32(&blocked) > 0 {
   516  			atomic.StoreUint32(&blocked, uint32(0))
   517  			proceed <- struct{}{}
   518  		}
   519  	}
   520  //检查我们没有拖过多的街区
   521  	assertOwnChain(t, tester, targetBlocks+1)
   522  	if err := <-errc; err != nil {
   523  		t.Fatalf("block synchronization failed: %v", err)
   524  	}
   525  }
   526  
   527  //测试针对分叉链的简单同步是否正确工作。在
   528  //此测试公共祖先查找应*不*短路,并且
   529  //应执行二进制搜索。
   530  func TestForkedSync62(t *testing.T)      { testForkedSync(t, 62, FullSync) }
   531  func TestForkedSync63Full(t *testing.T)  { testForkedSync(t, 63, FullSync) }
   532  func TestForkedSync63Fast(t *testing.T)  { testForkedSync(t, 63, FastSync) }
   533  func TestForkedSync64Full(t *testing.T)  { testForkedSync(t, 64, FullSync) }
   534  func TestForkedSync64Fast(t *testing.T)  { testForkedSync(t, 64, FastSync) }
   535  func TestForkedSync64Light(t *testing.T) { testForkedSync(t, 64, LightSync) }
   536  
   537  func testForkedSync(t *testing.T, protocol int, mode SyncMode) {
   538  	t.Parallel()
   539  
   540  	tester := newTester()
   541  	defer tester.terminate()
   542  
   543  	chainA := testChainForkLightA.shorten(testChainBase.len() + 80)
   544  	chainB := testChainForkLightB.shorten(testChainBase.len() + 80)
   545  	tester.newPeer("fork A", protocol, chainA)
   546  	tester.newPeer("fork B", protocol, chainB)
   547  
   548  //与对等机同步并确保检索到所有块
   549  	if err := tester.sync("fork A", nil, mode); err != nil {
   550  		t.Fatalf("failed to synchronise blocks: %v", err)
   551  	}
   552  	assertOwnChain(t, tester, chainA.len())
   553  
   554  //与第二个对等机同步,并确保拨叉也被拉动
   555  	if err := tester.sync("fork B", nil, mode); err != nil {
   556  		t.Fatalf("failed to synchronise blocks: %v", err)
   557  	}
   558  	assertOwnForkedChain(t, tester, testChainBase.len(), []int{chainA.len(), chainB.len()})
   559  }
   560  
   561  //与较短但较重的叉子同步的测试
   562  //正确无误,不会掉落。
   563  func TestHeavyForkedSync62(t *testing.T)      { testHeavyForkedSync(t, 62, FullSync) }
   564  func TestHeavyForkedSync63Full(t *testing.T)  { testHeavyForkedSync(t, 63, FullSync) }
   565  func TestHeavyForkedSync63Fast(t *testing.T)  { testHeavyForkedSync(t, 63, FastSync) }
   566  func TestHeavyForkedSync64Full(t *testing.T)  { testHeavyForkedSync(t, 64, FullSync) }
   567  func TestHeavyForkedSync64Fast(t *testing.T)  { testHeavyForkedSync(t, 64, FastSync) }
   568  func TestHeavyForkedSync64Light(t *testing.T) { testHeavyForkedSync(t, 64, LightSync) }
   569  
   570  func testHeavyForkedSync(t *testing.T, protocol int, mode SyncMode) {
   571  	t.Parallel()
   572  
   573  	tester := newTester()
   574  	defer tester.terminate()
   575  
   576  	chainA := testChainForkLightA.shorten(testChainBase.len() + 80)
   577  	chainB := testChainForkHeavy.shorten(testChainBase.len() + 80)
   578  	tester.newPeer("light", protocol, chainA)
   579  	tester.newPeer("heavy", protocol, chainB)
   580  
   581  //与对等机同步并确保检索到所有块
   582  	if err := tester.sync("light", nil, mode); err != nil {
   583  		t.Fatalf("failed to synchronise blocks: %v", err)
   584  	}
   585  	assertOwnChain(t, tester, chainA.len())
   586  
   587  //与第二个对等机同步,并确保拨叉也被拉动
   588  	if err := tester.sync("heavy", nil, mode); err != nil {
   589  		t.Fatalf("failed to synchronise blocks: %v", err)
   590  	}
   591  	assertOwnForkedChain(t, tester, testChainBase.len(), []int{chainA.len(), chainB.len()})
   592  }
   593  
   594  //测试链叉是否包含在电流的某个间隔内
   595  //链头,确保恶意同行不会通过喂养浪费资源
   596  //死链长。
   597  func TestBoundedForkedSync62(t *testing.T)      { testBoundedForkedSync(t, 62, FullSync) }
   598  func TestBoundedForkedSync63Full(t *testing.T)  { testBoundedForkedSync(t, 63, FullSync) }
   599  func TestBoundedForkedSync63Fast(t *testing.T)  { testBoundedForkedSync(t, 63, FastSync) }
   600  func TestBoundedForkedSync64Full(t *testing.T)  { testBoundedForkedSync(t, 64, FullSync) }
   601  func TestBoundedForkedSync64Fast(t *testing.T)  { testBoundedForkedSync(t, 64, FastSync) }
   602  func TestBoundedForkedSync64Light(t *testing.T) { testBoundedForkedSync(t, 64, LightSync) }
   603  
   604  func testBoundedForkedSync(t *testing.T, protocol int, mode SyncMode) {
   605  	t.Parallel()
   606  
   607  	tester := newTester()
   608  	defer tester.terminate()
   609  
   610  	chainA := testChainForkLightA
   611  	chainB := testChainForkLightB
   612  	tester.newPeer("original", protocol, chainA)
   613  	tester.newPeer("rewriter", protocol, chainB)
   614  
   615  //与对等机同步并确保检索到所有块
   616  	if err := tester.sync("original", nil, mode); err != nil {
   617  		t.Fatalf("failed to synchronise blocks: %v", err)
   618  	}
   619  	assertOwnChain(t, tester, chainA.len())
   620  
   621  //与第二个对等机同步,并确保叉被拒绝太旧
   622  	if err := tester.sync("rewriter", nil, mode); err != errInvalidAncestor {
   623  		t.Fatalf("sync failure mismatch: have %v, want %v", err, errInvalidAncestor)
   624  	}
   625  }
   626  
   627  //测试链叉是否包含在电流的某个间隔内
   628  //链头也适用于短而重的叉子。这些有点特别,因为它们
   629  //采用不同的祖先查找路径。
   630  func TestBoundedHeavyForkedSync62(t *testing.T)      { testBoundedHeavyForkedSync(t, 62, FullSync) }
   631  func TestBoundedHeavyForkedSync63Full(t *testing.T)  { testBoundedHeavyForkedSync(t, 63, FullSync) }
   632  func TestBoundedHeavyForkedSync63Fast(t *testing.T)  { testBoundedHeavyForkedSync(t, 63, FastSync) }
   633  func TestBoundedHeavyForkedSync64Full(t *testing.T)  { testBoundedHeavyForkedSync(t, 64, FullSync) }
   634  func TestBoundedHeavyForkedSync64Fast(t *testing.T)  { testBoundedHeavyForkedSync(t, 64, FastSync) }
   635  func TestBoundedHeavyForkedSync64Light(t *testing.T) { testBoundedHeavyForkedSync(t, 64, LightSync) }
   636  
   637  func testBoundedHeavyForkedSync(t *testing.T, protocol int, mode SyncMode) {
   638  	t.Parallel()
   639  
   640  	tester := newTester()
   641  	defer tester.terminate()
   642  
   643  //创建足够长的分叉链
   644  	chainA := testChainForkLightA
   645  	chainB := testChainForkHeavy
   646  	tester.newPeer("original", protocol, chainA)
   647  	tester.newPeer("heavy-rewriter", protocol, chainB)
   648  
   649  //与对等机同步并确保检索到所有块
   650  	if err := tester.sync("original", nil, mode); err != nil {
   651  		t.Fatalf("failed to synchronise blocks: %v", err)
   652  	}
   653  	assertOwnChain(t, tester, chainA.len())
   654  
   655  //与第二个对等机同步,并确保叉被拒绝太旧
   656  	if err := tester.sync("heavy-rewriter", nil, mode); err != errInvalidAncestor {
   657  		t.Fatalf("sync failure mismatch: have %v, want %v", err, errInvalidAncestor)
   658  	}
   659  }
   660  
   661  //测试非活动下载程序是否不接受传入的块头,以及
   662  //身体。
   663  func TestInactiveDownloader62(t *testing.T) {
   664  	t.Parallel()
   665  
   666  	tester := newTester()
   667  	defer tester.terminate()
   668  
   669  //检查是否不接受块头和块体
   670  	if err := tester.downloader.DeliverHeaders("bad peer", []*types.Header{}); err != errNoSyncActive {
   671  		t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive)
   672  	}
   673  	if err := tester.downloader.DeliverBodies("bad peer", [][]*types.Transaction{}, [][]*types.Header{}); err != errNoSyncActive {
   674  		t.Errorf("error mismatch: have %v, want  %v", err, errNoSyncActive)
   675  	}
   676  }
   677  
   678  //测试非活动下载程序是否不接受传入的块头,
   679  //机构和收据。
   680  func TestInactiveDownloader63(t *testing.T) {
   681  	t.Parallel()
   682  
   683  	tester := newTester()
   684  	defer tester.terminate()
   685  
   686  //检查是否不接受块头和块体
   687  	if err := tester.downloader.DeliverHeaders("bad peer", []*types.Header{}); err != errNoSyncActive {
   688  		t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive)
   689  	}
   690  	if err := tester.downloader.DeliverBodies("bad peer", [][]*types.Transaction{}, [][]*types.Header{}); err != errNoSyncActive {
   691  		t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive)
   692  	}
   693  	if err := tester.downloader.DeliverReceipts("bad peer", [][]*types.Receipt{}); err != errNoSyncActive {
   694  		t.Errorf("error mismatch: have %v, want %v", err, errNoSyncActive)
   695  	}
   696  }
   697  
   698  //测试取消的下载是否会清除所有以前累积的状态。
   699  func TestCancel62(t *testing.T)      { testCancel(t, 62, FullSync) }
   700  func TestCancel63Full(t *testing.T)  { testCancel(t, 63, FullSync) }
   701  func TestCancel63Fast(t *testing.T)  { testCancel(t, 63, FastSync) }
   702  func TestCancel64Full(t *testing.T)  { testCancel(t, 64, FullSync) }
   703  func TestCancel64Fast(t *testing.T)  { testCancel(t, 64, FastSync) }
   704  func TestCancel64Light(t *testing.T) { testCancel(t, 64, LightSync) }
   705  
   706  func testCancel(t *testing.T, protocol int, mode SyncMode) {
   707  	t.Parallel()
   708  
   709  	tester := newTester()
   710  	defer tester.terminate()
   711  
   712  	chain := testChainBase.shorten(MaxHeaderFetch)
   713  	tester.newPeer("peer", protocol, chain)
   714  
   715  //确保取消与原始下载程序一起工作
   716  	tester.downloader.Cancel()
   717  	if !tester.downloader.queue.Idle() {
   718  		t.Errorf("download queue not idle")
   719  	}
   720  //与同级同步,但随后取消
   721  	if err := tester.sync("peer", nil, mode); err != nil {
   722  		t.Fatalf("failed to synchronise blocks: %v", err)
   723  	}
   724  	tester.downloader.Cancel()
   725  	if !tester.downloader.queue.Idle() {
   726  		t.Errorf("download queue not idle")
   727  	}
   728  }
   729  
   730  //多个对等机同步按预期工作的测试(多线程健全性测试)。
   731  func TestMultiSynchronisation62(t *testing.T)      { testMultiSynchronisation(t, 62, FullSync) }
   732  func TestMultiSynchronisation63Full(t *testing.T)  { testMultiSynchronisation(t, 63, FullSync) }
   733  func TestMultiSynchronisation63Fast(t *testing.T)  { testMultiSynchronisation(t, 63, FastSync) }
   734  func TestMultiSynchronisation64Full(t *testing.T)  { testMultiSynchronisation(t, 64, FullSync) }
   735  func TestMultiSynchronisation64Fast(t *testing.T)  { testMultiSynchronisation(t, 64, FastSync) }
   736  func TestMultiSynchronisation64Light(t *testing.T) { testMultiSynchronisation(t, 64, LightSync) }
   737  
   738  func testMultiSynchronisation(t *testing.T, protocol int, mode SyncMode) {
   739  	t.Parallel()
   740  
   741  	tester := newTester()
   742  	defer tester.terminate()
   743  
   744  //用链的不同部分创建不同的对等点
   745  	targetPeers := 8
   746  	chain := testChainBase.shorten(targetPeers * 100)
   747  
   748  	for i := 0; i < targetPeers; i++ {
   749  		id := fmt.Sprintf("peer #%d", i)
   750  		tester.newPeer(id, protocol, chain.shorten(chain.len()/(i+1)))
   751  	}
   752  	if err := tester.sync("peer #0", nil, mode); err != nil {
   753  		t.Fatalf("failed to synchronise blocks: %v", err)
   754  	}
   755  	assertOwnChain(t, tester, chain.len())
   756  }
   757  
   758  //在多版本协议环境中同步运行良好的测试
   759  //不会对网络中的其他节点造成破坏。
   760  func TestMultiProtoSynchronisation62(t *testing.T)      { testMultiProtoSync(t, 62, FullSync) }
   761  func TestMultiProtoSynchronisation63Full(t *testing.T)  { testMultiProtoSync(t, 63, FullSync) }
   762  func TestMultiProtoSynchronisation63Fast(t *testing.T)  { testMultiProtoSync(t, 63, FastSync) }
   763  func TestMultiProtoSynchronisation64Full(t *testing.T)  { testMultiProtoSync(t, 64, FullSync) }
   764  func TestMultiProtoSynchronisation64Fast(t *testing.T)  { testMultiProtoSync(t, 64, FastSync) }
   765  func TestMultiProtoSynchronisation64Light(t *testing.T) { testMultiProtoSync(t, 64, LightSync) }
   766  
   767  func testMultiProtoSync(t *testing.T, protocol int, mode SyncMode) {
   768  	t.Parallel()
   769  
   770  	tester := newTester()
   771  	defer tester.terminate()
   772  
   773  //创建一个足够小的区块链来下载
   774  	chain := testChainBase.shorten(blockCacheItems - 15)
   775  
   776  //创建每种类型的对等点
   777  	tester.newPeer("peer 62", 62, chain)
   778  	tester.newPeer("peer 63", 63, chain)
   779  	tester.newPeer("peer 64", 64, chain)
   780  
   781  //与请求的对等机同步,并确保检索到所有块
   782  	if err := tester.sync(fmt.Sprintf("peer %d", protocol), nil, mode); err != nil {
   783  		t.Fatalf("failed to synchronise blocks: %v", err)
   784  	}
   785  	assertOwnChain(t, tester, chain.len())
   786  
   787  //检查是否没有同伴掉线
   788  	for _, version := range []int{62, 63, 64} {
   789  		peer := fmt.Sprintf("peer %d", version)
   790  		if _, ok := tester.peers[peer]; !ok {
   791  			t.Errorf("%s dropped", peer)
   792  		}
   793  	}
   794  }
   795  
   796  //测试如果一个块为空(例如,仅限头),则不应执行任何Body请求
   797  //制作,而不是集管本身组装成一个整体。
   798  func TestEmptyShortCircuit62(t *testing.T)      { testEmptyShortCircuit(t, 62, FullSync) }
   799  func TestEmptyShortCircuit63Full(t *testing.T)  { testEmptyShortCircuit(t, 63, FullSync) }
   800  func TestEmptyShortCircuit63Fast(t *testing.T)  { testEmptyShortCircuit(t, 63, FastSync) }
   801  func TestEmptyShortCircuit64Full(t *testing.T)  { testEmptyShortCircuit(t, 64, FullSync) }
   802  func TestEmptyShortCircuit64Fast(t *testing.T)  { testEmptyShortCircuit(t, 64, FastSync) }
   803  func TestEmptyShortCircuit64Light(t *testing.T) { testEmptyShortCircuit(t, 64, LightSync) }
   804  
   805  func testEmptyShortCircuit(t *testing.T, protocol int, mode SyncMode) {
   806  	t.Parallel()
   807  
   808  	tester := newTester()
   809  	defer tester.terminate()
   810  
   811  //创建要下载的区块链
   812  	chain := testChainBase
   813  	tester.newPeer("peer", protocol, chain)
   814  
   815  //向下载器发送信号,以发出Body请求
   816  	bodiesHave, receiptsHave := int32(0), int32(0)
   817  	tester.downloader.bodyFetchHook = func(headers []*types.Header) {
   818  		atomic.AddInt32(&bodiesHave, int32(len(headers)))
   819  	}
   820  	tester.downloader.receiptFetchHook = func(headers []*types.Header) {
   821  		atomic.AddInt32(&receiptsHave, int32(len(headers)))
   822  	}
   823  //与对等机同步并确保检索到所有块
   824  	if err := tester.sync("peer", nil, mode); err != nil {
   825  		t.Fatalf("failed to synchronise blocks: %v", err)
   826  	}
   827  	assertOwnChain(t, tester, chain.len())
   828  
   829  //验证应请求的块体数
   830  	bodiesNeeded, receiptsNeeded := 0, 0
   831  	for _, block := range chain.blockm {
   832  		if mode != LightSync && block != tester.genesis && (len(block.Transactions()) > 0 || len(block.Uncles()) > 0) {
   833  			bodiesNeeded++
   834  		}
   835  	}
   836  	for _, receipt := range chain.receiptm {
   837  		if mode == FastSync && len(receipt) > 0 {
   838  			receiptsNeeded++
   839  		}
   840  	}
   841  	if int(bodiesHave) != bodiesNeeded {
   842  		t.Errorf("body retrieval count mismatch: have %v, want %v", bodiesHave, bodiesNeeded)
   843  	}
   844  	if int(receiptsHave) != receiptsNeeded {
   845  		t.Errorf("receipt retrieval count mismatch: have %v, want %v", receiptsHave, receiptsNeeded)
   846  	}
   847  }
   848  
   849  //测试头是否连续排队,以防止恶意节点
   850  //通过喂入有间隙的收割台链条来安装下载器。
   851  func TestMissingHeaderAttack62(t *testing.T)      { testMissingHeaderAttack(t, 62, FullSync) }
   852  func TestMissingHeaderAttack63Full(t *testing.T)  { testMissingHeaderAttack(t, 63, FullSync) }
   853  func TestMissingHeaderAttack63Fast(t *testing.T)  { testMissingHeaderAttack(t, 63, FastSync) }
   854  func TestMissingHeaderAttack64Full(t *testing.T)  { testMissingHeaderAttack(t, 64, FullSync) }
   855  func TestMissingHeaderAttack64Fast(t *testing.T)  { testMissingHeaderAttack(t, 64, FastSync) }
   856  func TestMissingHeaderAttack64Light(t *testing.T) { testMissingHeaderAttack(t, 64, LightSync) }
   857  
   858  func testMissingHeaderAttack(t *testing.T, protocol int, mode SyncMode) {
   859  	t.Parallel()
   860  
   861  	tester := newTester()
   862  	defer tester.terminate()
   863  
   864  	chain := testChainBase.shorten(blockCacheItems - 15)
   865  	brokenChain := chain.shorten(chain.len())
   866  	delete(brokenChain.headerm, brokenChain.chain[brokenChain.len()/2])
   867  	tester.newPeer("attack", protocol, brokenChain)
   868  
   869  	if err := tester.sync("attack", nil, mode); err == nil {
   870  		t.Fatalf("succeeded attacker synchronisation")
   871  	}
   872  //与有效的对等机同步并确保同步成功
   873  	tester.newPeer("valid", protocol, chain)
   874  	if err := tester.sync("valid", nil, mode); err != nil {
   875  		t.Fatalf("failed to synchronise blocks: %v", err)
   876  	}
   877  	assertOwnChain(t, tester, chain.len())
   878  }
   879  
   880  //测试如果请求的头被移动(即第一个头丢失),队列
   881  //检测无效编号。
   882  func TestShiftedHeaderAttack62(t *testing.T)      { testShiftedHeaderAttack(t, 62, FullSync) }
   883  func TestShiftedHeaderAttack63Full(t *testing.T)  { testShiftedHeaderAttack(t, 63, FullSync) }
   884  func TestShiftedHeaderAttack63Fast(t *testing.T)  { testShiftedHeaderAttack(t, 63, FastSync) }
   885  func TestShiftedHeaderAttack64Full(t *testing.T)  { testShiftedHeaderAttack(t, 64, FullSync) }
   886  func TestShiftedHeaderAttack64Fast(t *testing.T)  { testShiftedHeaderAttack(t, 64, FastSync) }
   887  func TestShiftedHeaderAttack64Light(t *testing.T) { testShiftedHeaderAttack(t, 64, LightSync) }
   888  
   889  func testShiftedHeaderAttack(t *testing.T, protocol int, mode SyncMode) {
   890  	t.Parallel()
   891  
   892  	tester := newTester()
   893  	defer tester.terminate()
   894  
   895  	chain := testChainBase.shorten(blockCacheItems - 15)
   896  
   897  //尝试与攻击者进行完全同步以输入移位的头文件
   898  	brokenChain := chain.shorten(chain.len())
   899  	delete(brokenChain.headerm, brokenChain.chain[1])
   900  	delete(brokenChain.blockm, brokenChain.chain[1])
   901  	delete(brokenChain.receiptm, brokenChain.chain[1])
   902  	tester.newPeer("attack", protocol, brokenChain)
   903  	if err := tester.sync("attack", nil, mode); err == nil {
   904  		t.Fatalf("succeeded attacker synchronisation")
   905  	}
   906  
   907  //与有效的对等机同步并确保同步成功
   908  	tester.newPeer("valid", protocol, chain)
   909  	if err := tester.sync("valid", nil, mode); err != nil {
   910  		t.Fatalf("failed to synchronise blocks: %v", err)
   911  	}
   912  	assertOwnChain(t, tester, chain.len())
   913  }
   914  
   915  //测试在检测到无效的头时,将回滚最近的头
   916  //对于各种故障情况。之后,尝试进行完全同步
   917  //确保没有状态损坏。
   918  func TestInvalidHeaderRollback63Fast(t *testing.T)  { testInvalidHeaderRollback(t, 63, FastSync) }
   919  func TestInvalidHeaderRollback64Fast(t *testing.T)  { testInvalidHeaderRollback(t, 64, FastSync) }
   920  func TestInvalidHeaderRollback64Light(t *testing.T) { testInvalidHeaderRollback(t, 64, LightSync) }
   921  
   922  func testInvalidHeaderRollback(t *testing.T, protocol int, mode SyncMode) {
   923  	t.Parallel()
   924  
   925  	tester := newTester()
   926  	defer tester.terminate()
   927  
   928  //创建一个足够小的区块链来下载
   929  	targetBlocks := 3*fsHeaderSafetyNet + 256 + fsMinFullBlocks
   930  	chain := testChainBase.shorten(targetBlocks)
   931  
   932  //尝试与在快速同步阶段提供垃圾的攻击者同步。
   933  //这将导致最后一个fsheadersafetynet头回滚。
   934  	missing := fsHeaderSafetyNet + MaxHeaderFetch + 1
   935  	fastAttackChain := chain.shorten(chain.len())
   936  	delete(fastAttackChain.headerm, fastAttackChain.chain[missing])
   937  	tester.newPeer("fast-attack", protocol, fastAttackChain)
   938  
   939  	if err := tester.sync("fast-attack", nil, mode); err == nil {
   940  		t.Fatalf("succeeded fast attacker synchronisation")
   941  	}
   942  	if head := tester.CurrentHeader().Number.Int64(); int(head) > MaxHeaderFetch {
   943  		t.Errorf("rollback head mismatch: have %v, want at most %v", head, MaxHeaderFetch)
   944  	}
   945  
   946  //尝试与在块导入阶段提供垃圾的攻击者同步。
   947  //这将导致最后一个fsheadersafetynet头的数量
   948  //回滚,同时将轴点还原为非块状态。
   949  	missing = 3*fsHeaderSafetyNet + MaxHeaderFetch + 1
   950  	blockAttackChain := chain.shorten(chain.len())
   951  delete(fastAttackChain.headerm, fastAttackChain.chain[missing]) //Make sure the fast-attacker doesn't fill in
   952  	delete(blockAttackChain.headerm, blockAttackChain.chain[missing])
   953  	tester.newPeer("block-attack", protocol, blockAttackChain)
   954  
   955  	if err := tester.sync("block-attack", nil, mode); err == nil {
   956  		t.Fatalf("succeeded block attacker synchronisation")
   957  	}
   958  	if head := tester.CurrentHeader().Number.Int64(); int(head) > 2*fsHeaderSafetyNet+MaxHeaderFetch {
   959  		t.Errorf("rollback head mismatch: have %v, want at most %v", head, 2*fsHeaderSafetyNet+MaxHeaderFetch)
   960  	}
   961  	if mode == FastSync {
   962  		if head := tester.CurrentBlock().NumberU64(); head != 0 {
   963  			t.Errorf("fast sync pivot block #%d not rolled back", head)
   964  		}
   965  	}
   966  
   967  //尝试与在
   968  //快速同步轴点。这可能是让节点
   969  //但已经导入了透视图块。
   970  	withholdAttackChain := chain.shorten(chain.len())
   971  	tester.newPeer("withhold-attack", protocol, withholdAttackChain)
   972  	tester.downloader.syncInitHook = func(uint64, uint64) {
   973  		for i := missing; i < withholdAttackChain.len(); i++ {
   974  			delete(withholdAttackChain.headerm, withholdAttackChain.chain[i])
   975  		}
   976  		tester.downloader.syncInitHook = nil
   977  	}
   978  	if err := tester.sync("withhold-attack", nil, mode); err == nil {
   979  		t.Fatalf("succeeded withholding attacker synchronisation")
   980  	}
   981  	if head := tester.CurrentHeader().Number.Int64(); int(head) > 2*fsHeaderSafetyNet+MaxHeaderFetch {
   982  		t.Errorf("rollback head mismatch: have %v, want at most %v", head, 2*fsHeaderSafetyNet+MaxHeaderFetch)
   983  	}
   984  	if mode == FastSync {
   985  		if head := tester.CurrentBlock().NumberU64(); head != 0 {
   986  			t.Errorf("fast sync pivot block #%d not rolled back", head)
   987  		}
   988  	}
   989  
   990  //与有效的对等机同步,并确保同步成功。自上次回滚以来
   991  //还应为此进程禁用快速同步,请验证我们是否执行了新的完全同步
   992  //同步。Note, we can't assert anything about the receipts since we won't purge the
   993  //它们的数据库,因此我们不能使用断言。
   994  	tester.newPeer("valid", protocol, chain)
   995  	if err := tester.sync("valid", nil, mode); err != nil {
   996  		t.Fatalf("failed to synchronise blocks: %v", err)
   997  	}
   998  	if hs := len(tester.ownHeaders); hs != chain.len() {
   999  		t.Fatalf("synchronised headers mismatch: have %v, want %v", hs, chain.len())
  1000  	}
  1001  	if mode != LightSync {
  1002  		if bs := len(tester.ownBlocks); bs != chain.len() {
  1003  			t.Fatalf("synchronised blocks mismatch: have %v, want %v", bs, chain.len())
  1004  		}
  1005  	}
  1006  }
  1007  
  1008  //测试一个发布高TD的同龄人是否能够阻止下载者
  1009  //之后不发送任何有用的哈希。
  1010  func TestHighTDStarvationAttack62(t *testing.T)      { testHighTDStarvationAttack(t, 62, FullSync) }
  1011  func TestHighTDStarvationAttack63Full(t *testing.T)  { testHighTDStarvationAttack(t, 63, FullSync) }
  1012  func TestHighTDStarvationAttack63Fast(t *testing.T)  { testHighTDStarvationAttack(t, 63, FastSync) }
  1013  func TestHighTDStarvationAttack64Full(t *testing.T)  { testHighTDStarvationAttack(t, 64, FullSync) }
  1014  func TestHighTDStarvationAttack64Fast(t *testing.T)  { testHighTDStarvationAttack(t, 64, FastSync) }
  1015  func TestHighTDStarvationAttack64Light(t *testing.T) { testHighTDStarvationAttack(t, 64, LightSync) }
  1016  
  1017  func testHighTDStarvationAttack(t *testing.T, protocol int, mode SyncMode) {
  1018  	t.Parallel()
  1019  
  1020  	tester := newTester()
  1021  	defer tester.terminate()
  1022  
  1023  	chain := testChainBase.shorten(1)
  1024  	tester.newPeer("attack", protocol, chain)
  1025  	if err := tester.sync("attack", big.NewInt(1000000), mode); err != errStallingPeer {
  1026  		t.Fatalf("synchronisation error mismatch: have %v, want %v", err, errStallingPeer)
  1027  	}
  1028  }
  1029  
  1030  //行为不端的测试是断开连接的,而行为不正确的测试则是断开连接的。
  1031  func TestBlockHeaderAttackerDropping62(t *testing.T) { testBlockHeaderAttackerDropping(t, 62) }
  1032  func TestBlockHeaderAttackerDropping63(t *testing.T) { testBlockHeaderAttackerDropping(t, 63) }
  1033  func TestBlockHeaderAttackerDropping64(t *testing.T) { testBlockHeaderAttackerDropping(t, 64) }
  1034  
  1035  func testBlockHeaderAttackerDropping(t *testing.T, protocol int) {
  1036  	t.Parallel()
  1037  
  1038  //定义单个哈希获取错误的断开连接要求
  1039  	tests := []struct {
  1040  		result error
  1041  		drop   bool
  1042  	}{
  1043  {nil, false},                        //同步成功,一切正常
  1044  {errBusy, false},                    //同步已在进行中,没问题
  1045  {errUnknownPeer, false},             //对等机未知,已被删除,不要重复删除
  1046  {errBadPeer, true},                  //因为某种原因,同伴被认为是坏的,放下它。
  1047  {errStallingPeer, true},             //检测到对等机正在停止,请删除它
  1048  {errNoPeers, false},                 //无需下载同龄人,软件竞赛,无问题
  1049  {errTimeout, true},                  //在适当的时间内没有收到哈希,请删除对等机
  1050  {errEmptyHeaderSet, true},           //No headers were returned as a response, drop as it's a dead end
  1051  {errPeersUnavailable, true},         //没有人有广告块,把广告丢了
  1052  {errInvalidAncestor, true},          //同意祖先是不可接受的,请删除链重写器
  1053  {errInvalidChain, true},             //哈希链被检测为无效,肯定会丢失
  1054  {errInvalidBlock, false},            //检测到一个错误的对等机,但不是同步源
  1055  {errInvalidBody, false},             //检测到一个错误的对等机,但不是同步源
  1056  {errInvalidReceipt, false},          //检测到一个错误的对等机,但不是同步源
  1057  {errCancelBlockFetch, false},        //同步被取消,原点可能是无辜的,不要掉下来
  1058  {errCancelHeaderFetch, false},       //同步被取消,原点可能是无辜的,不要掉下来
  1059  {errCancelBodyFetch, false},         //同步被取消,原点可能是无辜的,不要掉下来
  1060  {errCancelReceiptFetch, false},      //同步被取消,原点可能是无辜的,不要掉下来
  1061  {errCancelHeaderProcessing, false},  //同步被取消,原点可能是无辜的,不要掉下来
  1062  {errCancelContentProcessing, false}, //同步被取消,原点可能是无辜的,不要掉下来
  1063  	}
  1064  //运行测试并检查断开状态
  1065  	tester := newTester()
  1066  	defer tester.terminate()
  1067  	chain := testChainBase.shorten(1)
  1068  
  1069  	for i, tt := range tests {
  1070  //注册新的对等点并确保其存在
  1071  		id := fmt.Sprintf("test %d", i)
  1072  		if err := tester.newPeer(id, protocol, chain); err != nil {
  1073  			t.Fatalf("test %d: failed to register new peer: %v", i, err)
  1074  		}
  1075  		if _, ok := tester.peers[id]; !ok {
  1076  			t.Fatalf("test %d: registered peer not found", i)
  1077  		}
  1078  //模拟同步并检查所需结果
  1079  		tester.downloader.synchroniseMock = func(string, common.Hash) error { return tt.result }
  1080  
  1081  		tester.downloader.Synchronise(id, tester.genesis.Hash(), big.NewInt(1000), FullSync)
  1082  		if _, ok := tester.peers[id]; !ok != tt.drop {
  1083  			t.Errorf("test %d: peer drop mismatch for %v: have %v, want %v", i, tt.result, !ok, tt.drop)
  1084  		}
  1085  	}
  1086  }
  1087  
  1088  //Tests that synchronisation progress (origin block number, current block number
  1089  //以及最高的块号)被正确地跟踪和更新。
  1090  func TestSyncProgress62(t *testing.T)      { testSyncProgress(t, 62, FullSync) }
  1091  func TestSyncProgress63Full(t *testing.T)  { testSyncProgress(t, 63, FullSync) }
  1092  func TestSyncProgress63Fast(t *testing.T)  { testSyncProgress(t, 63, FastSync) }
  1093  func TestSyncProgress64Full(t *testing.T)  { testSyncProgress(t, 64, FullSync) }
  1094  func TestSyncProgress64Fast(t *testing.T)  { testSyncProgress(t, 64, FastSync) }
  1095  func TestSyncProgress64Light(t *testing.T) { testSyncProgress(t, 64, LightSync) }
  1096  
  1097  func testSyncProgress(t *testing.T, protocol int, mode SyncMode) {
  1098  	t.Parallel()
  1099  
  1100  	tester := newTester()
  1101  	defer tester.terminate()
  1102  	chain := testChainBase.shorten(blockCacheItems - 15)
  1103  
  1104  //设置同步初始化挂钩以捕获进度更改
  1105  	starting := make(chan struct{})
  1106  	progress := make(chan struct{})
  1107  
  1108  	tester.downloader.syncInitHook = func(origin, latest uint64) {
  1109  		starting <- struct{}{}
  1110  		<-progress
  1111  	}
  1112  	checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{})
  1113  
  1114  //同步一半块并检查初始进度
  1115  	tester.newPeer("peer-half", protocol, chain.shorten(chain.len()/2))
  1116  	pending := new(sync.WaitGroup)
  1117  	pending.Add(1)
  1118  
  1119  	go func() {
  1120  		defer pending.Done()
  1121  		if err := tester.sync("peer-half", nil, mode); err != nil {
  1122  			panic(fmt.Sprintf("failed to synchronise blocks: %v", err))
  1123  		}
  1124  	}()
  1125  	<-starting
  1126  	checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{
  1127  		HighestBlock: uint64(chain.len()/2 - 1),
  1128  	})
  1129  	progress <- struct{}{}
  1130  	pending.Wait()
  1131  
  1132  //同步所有块并检查继续进度
  1133  	tester.newPeer("peer-full", protocol, chain)
  1134  	pending.Add(1)
  1135  	go func() {
  1136  		defer pending.Done()
  1137  		if err := tester.sync("peer-full", nil, mode); err != nil {
  1138  			panic(fmt.Sprintf("failed to synchronise blocks: %v", err))
  1139  		}
  1140  	}()
  1141  	<-starting
  1142  	checkProgress(t, tester.downloader, "completing", ethereum.SyncProgress{
  1143  		StartingBlock: uint64(chain.len()/2 - 1),
  1144  		CurrentBlock:  uint64(chain.len()/2 - 1),
  1145  		HighestBlock:  uint64(chain.len() - 1),
  1146  	})
  1147  
  1148  //同步成功后检查最终进度
  1149  	progress <- struct{}{}
  1150  	pending.Wait()
  1151  	checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{
  1152  		StartingBlock: uint64(chain.len()/2 - 1),
  1153  		CurrentBlock:  uint64(chain.len() - 1),
  1154  		HighestBlock:  uint64(chain.len() - 1),
  1155  	})
  1156  }
  1157  
  1158  func checkProgress(t *testing.T, d *Downloader, stage string, want ethereum.SyncProgress) {
  1159  //将此方法标记为在调用站点(而不是在此处)报告错误的帮助程序
  1160  	t.Helper()
  1161  
  1162  	p := d.Progress()
  1163  	p.KnownStates, p.PulledStates = 0, 0
  1164  	want.KnownStates, want.PulledStates = 0, 0
  1165  	if p != want {
  1166  		t.Fatalf("%s progress mismatch:\nhave %+v\nwant %+v", stage, p, want)
  1167  	}
  1168  }
  1169  
  1170  //测试同步进程(起始块编号和最高块
  1171  //如果是叉子(或手动头),则正确跟踪和更新数字。
  1172  //回复)
  1173  func TestForkedSyncProgress62(t *testing.T)      { testForkedSyncProgress(t, 62, FullSync) }
  1174  func TestForkedSyncProgress63Full(t *testing.T)  { testForkedSyncProgress(t, 63, FullSync) }
  1175  func TestForkedSyncProgress63Fast(t *testing.T)  { testForkedSyncProgress(t, 63, FastSync) }
  1176  func TestForkedSyncProgress64Full(t *testing.T)  { testForkedSyncProgress(t, 64, FullSync) }
  1177  func TestForkedSyncProgress64Fast(t *testing.T)  { testForkedSyncProgress(t, 64, FastSync) }
  1178  func TestForkedSyncProgress64Light(t *testing.T) { testForkedSyncProgress(t, 64, LightSync) }
  1179  
  1180  func testForkedSyncProgress(t *testing.T, protocol int, mode SyncMode) {
  1181  	t.Parallel()
  1182  
  1183  	tester := newTester()
  1184  	defer tester.terminate()
  1185  	chainA := testChainForkLightA.shorten(testChainBase.len() + MaxHashFetch)
  1186  	chainB := testChainForkLightB.shorten(testChainBase.len() + MaxHashFetch)
  1187  
  1188  //设置同步初始化挂钩以捕获进度更改
  1189  	starting := make(chan struct{})
  1190  	progress := make(chan struct{})
  1191  
  1192  	tester.downloader.syncInitHook = func(origin, latest uint64) {
  1193  		starting <- struct{}{}
  1194  		<-progress
  1195  	}
  1196  	checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{})
  1197  
  1198  //与其中一个拨叉同步并检查进度
  1199  	tester.newPeer("fork A", protocol, chainA)
  1200  	pending := new(sync.WaitGroup)
  1201  	pending.Add(1)
  1202  	go func() {
  1203  		defer pending.Done()
  1204  		if err := tester.sync("fork A", nil, mode); err != nil {
  1205  			panic(fmt.Sprintf("failed to synchronise blocks: %v", err))
  1206  		}
  1207  	}()
  1208  	<-starting
  1209  
  1210  	checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{
  1211  		HighestBlock: uint64(chainA.len() - 1),
  1212  	})
  1213  	progress <- struct{}{}
  1214  	pending.Wait()
  1215  
  1216  //在分叉上方模拟成功的同步
  1217  	tester.downloader.syncStatsChainOrigin = tester.downloader.syncStatsChainHeight
  1218  
  1219  //与第二个拨叉同步并检查进度重置
  1220  	tester.newPeer("fork B", protocol, chainB)
  1221  	pending.Add(1)
  1222  	go func() {
  1223  		defer pending.Done()
  1224  		if err := tester.sync("fork B", nil, mode); err != nil {
  1225  			panic(fmt.Sprintf("failed to synchronise blocks: %v", err))
  1226  		}
  1227  	}()
  1228  	<-starting
  1229  	checkProgress(t, tester.downloader, "forking", ethereum.SyncProgress{
  1230  		StartingBlock: uint64(testChainBase.len()) - 1,
  1231  		CurrentBlock:  uint64(chainA.len() - 1),
  1232  		HighestBlock:  uint64(chainB.len() - 1),
  1233  	})
  1234  
  1235  //同步成功后检查最终进度
  1236  	progress <- struct{}{}
  1237  	pending.Wait()
  1238  	checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{
  1239  		StartingBlock: uint64(testChainBase.len()) - 1,
  1240  		CurrentBlock:  uint64(chainB.len() - 1),
  1241  		HighestBlock:  uint64(chainB.len() - 1),
  1242  	})
  1243  }
  1244  
  1245  //测试如果同步由于某些故障而中止,则进度
  1246  //原点不会在下一个同步周期中更新,因为它应该被视为
  1247  //继续上一次同步,而不是新实例。
  1248  func TestFailedSyncProgress62(t *testing.T)      { testFailedSyncProgress(t, 62, FullSync) }
  1249  func TestFailedSyncProgress63Full(t *testing.T)  { testFailedSyncProgress(t, 63, FullSync) }
  1250  func TestFailedSyncProgress63Fast(t *testing.T)  { testFailedSyncProgress(t, 63, FastSync) }
  1251  func TestFailedSyncProgress64Full(t *testing.T)  { testFailedSyncProgress(t, 64, FullSync) }
  1252  func TestFailedSyncProgress64Fast(t *testing.T)  { testFailedSyncProgress(t, 64, FastSync) }
  1253  func TestFailedSyncProgress64Light(t *testing.T) { testFailedSyncProgress(t, 64, LightSync) }
  1254  
  1255  func testFailedSyncProgress(t *testing.T, protocol int, mode SyncMode) {
  1256  	t.Parallel()
  1257  
  1258  	tester := newTester()
  1259  	defer tester.terminate()
  1260  	chain := testChainBase.shorten(blockCacheItems - 15)
  1261  
  1262  //设置同步初始化挂钩以捕获进度更改
  1263  	starting := make(chan struct{})
  1264  	progress := make(chan struct{})
  1265  
  1266  	tester.downloader.syncInitHook = func(origin, latest uint64) {
  1267  		starting <- struct{}{}
  1268  		<-progress
  1269  	}
  1270  	checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{})
  1271  
  1272  //尝试与有故障的对等机完全同步
  1273  	brokenChain := chain.shorten(chain.len())
  1274  	missing := brokenChain.len() / 2
  1275  	delete(brokenChain.headerm, brokenChain.chain[missing])
  1276  	delete(brokenChain.blockm, brokenChain.chain[missing])
  1277  	delete(brokenChain.receiptm, brokenChain.chain[missing])
  1278  	tester.newPeer("faulty", protocol, brokenChain)
  1279  
  1280  	pending := new(sync.WaitGroup)
  1281  	pending.Add(1)
  1282  	go func() {
  1283  		defer pending.Done()
  1284  		if err := tester.sync("faulty", nil, mode); err == nil {
  1285  			panic("succeeded faulty synchronisation")
  1286  		}
  1287  	}()
  1288  	<-starting
  1289  	checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{
  1290  		HighestBlock: uint64(brokenChain.len() - 1),
  1291  	})
  1292  	progress <- struct{}{}
  1293  	pending.Wait()
  1294  	afterFailedSync := tester.downloader.Progress()
  1295  
  1296  //与一个好的同行同步,并检查进度来源是否提醒相同
  1297  //失败后
  1298  	tester.newPeer("valid", protocol, chain)
  1299  	pending.Add(1)
  1300  	go func() {
  1301  		defer pending.Done()
  1302  		if err := tester.sync("valid", nil, mode); err != nil {
  1303  			panic(fmt.Sprintf("failed to synchronise blocks: %v", err))
  1304  		}
  1305  	}()
  1306  	<-starting
  1307  	checkProgress(t, tester.downloader, "completing", afterFailedSync)
  1308  
  1309  //同步成功后检查最终进度
  1310  	progress <- struct{}{}
  1311  	pending.Wait()
  1312  	checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{
  1313  		CurrentBlock: uint64(chain.len() - 1),
  1314  		HighestBlock: uint64(chain.len() - 1),
  1315  	})
  1316  }
  1317  
  1318  //测试在检测到攻击后,如果攻击者伪造链高度,
  1319  //在下次同步调用时,进度高度已成功降低。
  1320  func TestFakedSyncProgress62(t *testing.T)      { testFakedSyncProgress(t, 62, FullSync) }
  1321  func TestFakedSyncProgress63Full(t *testing.T)  { testFakedSyncProgress(t, 63, FullSync) }
  1322  func TestFakedSyncProgress63Fast(t *testing.T)  { testFakedSyncProgress(t, 63, FastSync) }
  1323  func TestFakedSyncProgress64Full(t *testing.T)  { testFakedSyncProgress(t, 64, FullSync) }
  1324  func TestFakedSyncProgress64Fast(t *testing.T)  { testFakedSyncProgress(t, 64, FastSync) }
  1325  func TestFakedSyncProgress64Light(t *testing.T) { testFakedSyncProgress(t, 64, LightSync) }
  1326  
  1327  func testFakedSyncProgress(t *testing.T, protocol int, mode SyncMode) {
  1328  	t.Parallel()
  1329  
  1330  	tester := newTester()
  1331  	defer tester.terminate()
  1332  	chain := testChainBase.shorten(blockCacheItems - 15)
  1333  
  1334  //设置同步初始化挂钩以捕获进度更改
  1335  	starting := make(chan struct{})
  1336  	progress := make(chan struct{})
  1337  	tester.downloader.syncInitHook = func(origin, latest uint64) {
  1338  		starting <- struct{}{}
  1339  		<-progress
  1340  	}
  1341  	checkProgress(t, tester.downloader, "pristine", ethereum.SyncProgress{})
  1342  
  1343  //创建一个攻击者并与之同步,该攻击者承诺拥有比可用的更高的链。
  1344  	brokenChain := chain.shorten(chain.len())
  1345  	numMissing := 5
  1346  	for i := brokenChain.len() - 2; i > brokenChain.len()-numMissing; i-- {
  1347  		delete(brokenChain.headerm, brokenChain.chain[i])
  1348  	}
  1349  	tester.newPeer("attack", protocol, brokenChain)
  1350  
  1351  	pending := new(sync.WaitGroup)
  1352  	pending.Add(1)
  1353  	go func() {
  1354  		defer pending.Done()
  1355  		if err := tester.sync("attack", nil, mode); err == nil {
  1356  			panic("succeeded attacker synchronisation")
  1357  		}
  1358  	}()
  1359  	<-starting
  1360  	checkProgress(t, tester.downloader, "initial", ethereum.SyncProgress{
  1361  		HighestBlock: uint64(brokenChain.len() - 1),
  1362  	})
  1363  	progress <- struct{}{}
  1364  	pending.Wait()
  1365  	afterFailedSync := tester.downloader.Progress()
  1366  
  1367  //与一个好的同伴同步,并检查进度高度是否已降低到
  1368  //真正的价值。
  1369  	validChain := chain.shorten(chain.len() - numMissing)
  1370  	tester.newPeer("valid", protocol, validChain)
  1371  	pending.Add(1)
  1372  
  1373  	go func() {
  1374  		defer pending.Done()
  1375  		if err := tester.sync("valid", nil, mode); err != nil {
  1376  			panic(fmt.Sprintf("failed to synchronise blocks: %v", err))
  1377  		}
  1378  	}()
  1379  	<-starting
  1380  	checkProgress(t, tester.downloader, "completing", ethereum.SyncProgress{
  1381  		CurrentBlock: afterFailedSync.CurrentBlock,
  1382  		HighestBlock: uint64(validChain.len() - 1),
  1383  	})
  1384  
  1385  //同步成功后检查最终进度。
  1386  	progress <- struct{}{}
  1387  	pending.Wait()
  1388  	checkProgress(t, tester.downloader, "final", ethereum.SyncProgress{
  1389  		CurrentBlock: uint64(validChain.len() - 1),
  1390  		HighestBlock: uint64(validChain.len() - 1),
  1391  	})
  1392  }
  1393  
  1394  //此测试复制了一个问题,即意外交付
  1395  //如果他们到达正确的时间,无限期地封锁。
  1396  func TestDeliverHeadersHang(t *testing.T) {
  1397  	t.Parallel()
  1398  
  1399  	testCases := []struct {
  1400  		protocol int
  1401  		syncMode SyncMode
  1402  	}{
  1403  		{62, FullSync},
  1404  		{63, FullSync},
  1405  		{63, FastSync},
  1406  		{64, FullSync},
  1407  		{64, FastSync},
  1408  		{64, LightSync},
  1409  	}
  1410  	for _, tc := range testCases {
  1411  		t.Run(fmt.Sprintf("protocol %d mode %v", tc.protocol, tc.syncMode), func(t *testing.T) {
  1412  			t.Parallel()
  1413  			testDeliverHeadersHang(t, tc.protocol, tc.syncMode)
  1414  		})
  1415  	}
  1416  }
  1417  
  1418  func testDeliverHeadersHang(t *testing.T, protocol int, mode SyncMode) {
  1419  	master := newTester()
  1420  	defer master.terminate()
  1421  	chain := testChainBase.shorten(15)
  1422  
  1423  	for i := 0; i < 200; i++ {
  1424  		tester := newTester()
  1425  		tester.peerDb = master.peerDb
  1426  		tester.newPeer("peer", protocol, chain)
  1427  
  1428  //每当下载程序请求报头时,就用
  1429  //很多未请求的头部交付。
  1430  		tester.downloader.peers.peers["peer"].peer = &floodingTestPeer{
  1431  			peer:   tester.downloader.peers.peers["peer"].peer,
  1432  			tester: tester,
  1433  		}
  1434  		if err := tester.sync("peer", nil, mode); err != nil {
  1435  			t.Errorf("test %d: sync failed: %v", i, err)
  1436  		}
  1437  		tester.terminate()
  1438  	}
  1439  }
  1440  
  1441  type floodingTestPeer struct {
  1442  	peer   Peer
  1443  	tester *downloadTester
  1444  }
  1445  
  1446  func (ftp *floodingTestPeer) Head() (common.Hash, *big.Int) { return ftp.peer.Head() }
  1447  func (ftp *floodingTestPeer) RequestHeadersByHash(hash common.Hash, count int, skip int, reverse bool) error {
  1448  	return ftp.peer.RequestHeadersByHash(hash, count, skip, reverse)
  1449  }
  1450  func (ftp *floodingTestPeer) RequestBodies(hashes []common.Hash) error {
  1451  	return ftp.peer.RequestBodies(hashes)
  1452  }
  1453  func (ftp *floodingTestPeer) RequestReceipts(hashes []common.Hash) error {
  1454  	return ftp.peer.RequestReceipts(hashes)
  1455  }
  1456  func (ftp *floodingTestPeer) RequestNodeData(hashes []common.Hash) error {
  1457  	return ftp.peer.RequestNodeData(hashes)
  1458  }
  1459  
  1460  func (ftp *floodingTestPeer) RequestHeadersByNumber(from uint64, count, skip int, reverse bool) error {
  1461  	deliveriesDone := make(chan struct{}, 500)
  1462  	for i := 0; i < cap(deliveriesDone)-1; i++ {
  1463  		peer := fmt.Sprintf("fake-peer%d", i)
  1464  		go func() {
  1465  			ftp.tester.downloader.DeliverHeaders(peer, []*types.Header{{}, {}, {}, {}})
  1466  			deliveriesDone <- struct{}{}
  1467  		}()
  1468  	}
  1469  
  1470  //额外交货不应受阻。
  1471  	timeout := time.After(60 * time.Second)
  1472  	launched := false
  1473  	for i := 0; i < cap(deliveriesDone); i++ {
  1474  		select {
  1475  		case <-deliveriesDone:
  1476  			if !launched {
  1477  //开始传递请求的邮件头
  1478  //在其中一个洪水响应到达之后。
  1479  				go func() {
  1480  					ftp.peer.RequestHeadersByNumber(from, count, skip, reverse)
  1481  					deliveriesDone <- struct{}{}
  1482  				}()
  1483  				launched = true
  1484  			}
  1485  		case <-timeout:
  1486  			panic("blocked")
  1487  		}
  1488  	}
  1489  	return nil
  1490  }
  1491  
  1492  func TestRemoteHeaderRequestSpan(t *testing.T) {
  1493  	testCases := []struct {
  1494  		remoteHeight uint64
  1495  		localHeight  uint64
  1496  		expected     []int
  1497  	}{
  1498  //遥控器更高。我们应该要遥控器的头,然后往回走
  1499  		{1500, 1000,
  1500  			[]int{1323, 1339, 1355, 1371, 1387, 1403, 1419, 1435, 1451, 1467, 1483, 1499},
  1501  		},
  1502  		{15000, 13006,
  1503  			[]int{14823, 14839, 14855, 14871, 14887, 14903, 14919, 14935, 14951, 14967, 14983, 14999},
  1504  		},
  1505  //遥控器离我们很近。我们不必取那么多
  1506  		{1200, 1150,
  1507  			[]int{1149, 1154, 1159, 1164, 1169, 1174, 1179, 1184, 1189, 1194, 1199},
  1508  		},
  1509  //远程等于我们(所以在TD较高的分叉上)
  1510  //我们应该找到最亲近的两个祖先
  1511  		{1500, 1500,
  1512  			[]int{1497, 1499},
  1513  		},
  1514  //我们比遥控器还高!奇数
  1515  		{1000, 1500,
  1516  			[]int{997, 999},
  1517  		},
  1518  //检查一些奇怪的边缘,因为它的行为有些理性。
  1519  		{0, 1500,
  1520  			[]int{0, 2},
  1521  		},
  1522  		{6000000, 0,
  1523  			[]int{5999823, 5999839, 5999855, 5999871, 5999887, 5999903, 5999919, 5999935, 5999951, 5999967, 5999983, 5999999},
  1524  		},
  1525  		{0, 0,
  1526  			[]int{0, 2},
  1527  		},
  1528  	}
  1529  	reqs := func(from, count, span int) []int {
  1530  		var r []int
  1531  		num := from
  1532  		for len(r) < count {
  1533  			r = append(r, num)
  1534  			num += span + 1
  1535  		}
  1536  		return r
  1537  	}
  1538  	for i, tt := range testCases {
  1539  		from, count, span, max := calculateRequestSpan(tt.remoteHeight, tt.localHeight)
  1540  		data := reqs(int(from), count, span)
  1541  
  1542  		if max != uint64(data[len(data)-1]) {
  1543  			t.Errorf("test %d: wrong last value %d != %d", i, data[len(data)-1], max)
  1544  		}
  1545  		failed := false
  1546  		if len(data) != len(tt.expected) {
  1547  			failed = true
  1548  			t.Errorf("test %d: length wrong, expected %d got %d", i, len(tt.expected), len(data))
  1549  		} else {
  1550  			for j, n := range data {
  1551  				if n != tt.expected[j] {
  1552  					failed = true
  1553  					break
  1554  				}
  1555  			}
  1556  		}
  1557  		if failed {
  1558  			res := strings.Replace(fmt.Sprint(data), " ", ",", -1)
  1559  			exp := strings.Replace(fmt.Sprint(tt.expected), " ", ",", -1)
  1560  			fmt.Printf("got: %v\n", res)
  1561  			fmt.Printf("exp: %v\n", exp)
  1562  			t.Errorf("test %d: wrong values", i)
  1563  		}
  1564  	}
  1565  }
  1566