github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/netsync/tool_test.go (about) 1 package netsync 2 3 import ( 4 "errors" 5 "math/rand" 6 "net" 7 "time" 8 9 wire "github.com/tendermint/go-wire" 10 "github.com/tendermint/tmlibs/flowrate" 11 12 "github.com/bytom/bytom/consensus" 13 "github.com/bytom/bytom/protocol/bc" 14 "github.com/bytom/bytom/protocol/bc/types" 15 "github.com/bytom/bytom/test/mock" 16 ) 17 18 type P2PPeer struct { 19 id string 20 ip *net.IPAddr 21 flag consensus.ServiceFlag 22 23 srcPeer *P2PPeer 24 remoteNode *SyncManager 25 msgCh chan []byte 26 async bool 27 } 28 29 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer { 30 return &P2PPeer{ 31 id: id, 32 ip: &net.IPAddr{IP: net.ParseIP(addr)}, 33 flag: flag, 34 msgCh: make(chan []byte), 35 async: false, 36 } 37 } 38 39 func (p *P2PPeer) Addr() net.Addr { 40 return p.ip 41 } 42 43 func (p *P2PPeer) ID() string { 44 return p.id 45 } 46 47 func (p *P2PPeer) IsLAN() bool { 48 return false 49 } 50 51 func (p *P2PPeer) RemoteAddrHost() string { 52 return "" 53 } 54 55 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag { 56 return p.flag 57 } 58 59 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *SyncManager) { 60 p.srcPeer = srcPeer 61 p.remoteNode = node 62 } 63 64 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) { 65 return nil, nil 66 } 67 68 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool { 69 msgBytes := wire.BinaryBytes(msg) 70 if p.async { 71 p.msgCh <- msgBytes 72 } else { 73 msgType, msg, _ := DecodeMessage(msgBytes) 74 p.remoteNode.processMsg(p.srcPeer, msgType, msg) 75 } 76 return true 77 } 78 79 func (p *P2PPeer) setAsync(b bool) { 80 p.async = b 81 } 82 83 func (p *P2PPeer) postMan() { 84 for msgBytes := range p.msgCh { 85 msgType, msg, _ := DecodeMessage(msgBytes) 86 p.remoteNode.processMsg(p.srcPeer, msgType, msg) 87 } 88 } 89 90 type PeerSet struct{} 91 92 func NewPeerSet() *PeerSet { 93 return &PeerSet{} 94 } 95 96 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool { 97 return false 98 } 99 100 func (ps *PeerSet) StopPeerGracefully(string) {} 101 102 type NetWork struct { 103 nodes map[*SyncManager]P2PPeer 104 } 105 106 func NewNetWork() *NetWork { 107 return &NetWork{map[*SyncManager]P2PPeer{}} 108 } 109 110 func (nw *NetWork) Register(node *SyncManager, addr, id string, flag consensus.ServiceFlag) { 111 peer := NewP2PPeer(addr, id, flag) 112 nw.nodes[node] = *peer 113 } 114 115 func (nw *NetWork) HandsShake(nodeA, nodeB *SyncManager) (*P2PPeer, *P2PPeer, error) { 116 B2A, ok := nw.nodes[nodeA] 117 if !ok { 118 return nil, nil, errors.New("can't find nodeA's p2p peer on network") 119 } 120 A2B, ok := nw.nodes[nodeB] 121 if !ok { 122 return nil, nil, errors.New("can't find nodeB's p2p peer on network") 123 } 124 125 A2B.SetConnection(&B2A, nodeB) 126 B2A.SetConnection(&A2B, nodeA) 127 128 nodeA.handleStatusRequestMsg(&A2B) 129 nodeB.handleStatusRequestMsg(&B2A) 130 131 A2B.setAsync(true) 132 B2A.setAsync(true) 133 return &B2A, &A2B, nil 134 } 135 136 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block { 137 blocks := []*types.Block{} 138 indexBlock := &types.Block{} 139 if startBlock == nil { 140 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Nonce: uint64(rand.Uint32())}} 141 blocks = append(blocks, indexBlock) 142 } else { 143 indexBlock = startBlock 144 } 145 146 for indexBlock.Height < height { 147 block := &types.Block{ 148 BlockHeader: types.BlockHeader{ 149 Height: indexBlock.Height + 1, 150 PreviousBlockHash: indexBlock.Hash(), 151 Nonce: uint64(rand.Uint32()), 152 }, 153 } 154 blocks = append(blocks, block) 155 indexBlock = block 156 } 157 return blocks 158 } 159 160 func mockSync(blocks []*types.Block) *SyncManager { 161 chain := mock.NewChain() 162 peers := newPeerSet(NewPeerSet()) 163 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader) 164 for _, block := range blocks { 165 chain.SetBlockByHeight(block.Height, block) 166 } 167 168 genesis, _ := chain.GetHeaderByHeight(0) 169 return &SyncManager{ 170 genesisHash: genesis.Hash(), 171 chain: chain, 172 blockKeeper: newBlockKeeper(chain, peers), 173 peers: peers, 174 } 175 } 176 177 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) { 178 var txs []*types.Tx 179 var bcTxs []*bc.Tx 180 for i := 0; i < txCount; i++ { 181 trueProg := mockControlProgram(60) 182 assetID := bc.ComputeAssetID(trueProg, 1, &bc.EmptyStringHash) 183 now := []byte(time.Now().String()) 184 issuanceInp := types.NewIssuanceInput(now, 1, trueProg, nil, nil) 185 tx := types.NewTx(types.TxData{ 186 Version: 1, 187 Inputs: []*types.TxInput{issuanceInp}, 188 Outputs: []*types.TxOutput{types.NewTxOutput(assetID, 1, trueProg)}, 189 }) 190 txs = append(txs, tx) 191 bcTxs = append(bcTxs, tx.Tx) 192 } 193 return txs, bcTxs 194 } 195 196 func mockControlProgram(length int) []byte { 197 var cp []byte 198 for i := 0; i < length; i++ { 199 cp = append(cp, byte(rand.Intn(1<<8))) 200 } 201 return cp 202 }