github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/eth/sync_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 12:09:39</date>
    10  //</624342636668391424>
    11  
    12  
    13  package eth
    14  
    15  import (
    16  	"sync/atomic"
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/ethereum/go-ethereum/eth/downloader"
    21  	"github.com/ethereum/go-ethereum/p2p"
    22  	"github.com/ethereum/go-ethereum/p2p/discover"
    23  )
    24  
    25  //一旦一个真正的块成功,快速同步就被禁用的测试
    26  //导入到区块链中。
    27  func TestFastSyncDisabling(t *testing.T) {
    28  //创建一个原始协议管理器,检查是否启用了快速同步
    29  	pmEmpty, _ := newTestProtocolManagerMust(t, downloader.FastSync, 0, nil, nil)
    30  	if atomic.LoadUint32(&pmEmpty.fastSync) == 0 {
    31  		t.Fatalf("fast sync disabled on pristine blockchain")
    32  	}
    33  //创建完整的协议管理器,检查是否禁用快速同步
    34  	pmFull, _ := newTestProtocolManagerMust(t, downloader.FastSync, 1024, nil, nil)
    35  	if atomic.LoadUint32(&pmFull.fastSync) == 1 {
    36  		t.Fatalf("fast sync not disabled on non-empty blockchain")
    37  	}
    38  //同步两个对等机
    39  	io1, io2 := p2p.MsgPipe()
    40  
    41  	go pmFull.handle(pmFull.newPeer(63, p2p.NewPeer(discover.NodeID{}, "empty", nil), io2))
    42  	go pmEmpty.handle(pmEmpty.newPeer(63, p2p.NewPeer(discover.NodeID{}, "full", nil), io1))
    43  
    44  	time.Sleep(250 * time.Millisecond)
    45  	pmEmpty.synchronise(pmEmpty.peers.BestPeer())
    46  
    47  //检查是否禁用了快速同步
    48  	if atomic.LoadUint32(&pmEmpty.fastSync) == 1 {
    49  		t.Fatalf("fast sync not disabled after successful synchronisation")
    50  	}
    51  }
    52