9 "github.com/tendermint/go-wire"
10 "github.com/tendermint/tmlibs/flowrate"
11 dbm "github.com/bytom/vapor/database/leveldb"
13 "github.com/bytom/vapor/consensus"
14 "github.com/bytom/vapor/event"
15 "github.com/bytom/vapor/netsync/peers"
16 "github.com/bytom/vapor/protocol/bc"
17 "github.com/bytom/vapor/protocol/bc/types"
18 "github.com/bytom/vapor/test/mock"
24 flag consensus.ServiceFlag
32 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
35 ip: &net.IPAddr{IP: net.ParseIP(addr)},
37 msgCh: make(chan []byte),
42 func (p *P2PPeer) Addr() net.Addr {
46 func (p *P2PPeer) ID() string {
50 func (p *P2PPeer) IsLAN() bool {
54 func (p *P2PPeer) Moniker() string {
58 func (p *P2PPeer) RemoteAddrHost() string {
62 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
66 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
71 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
75 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
76 msgBytes := wire.BinaryBytes(msg)
80 msgType, msg, _ := decodeMessage(msgBytes)
81 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
86 func (p *P2PPeer) setAsync(b bool) {
90 func (p *P2PPeer) postMan() {
91 for msgBytes := range p.msgCh {
92 msgType, msg, _ := decodeMessage(msgBytes)
93 time.Sleep(10 * time.Millisecond)
94 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
100 func NewPeerSet() *PeerSet {
104 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
108 func (ps *PeerSet) StopPeerGracefully(string) {}
110 type NetWork struct {
111 nodes map[*Manager]P2PPeer
114 func NewNetWork() *NetWork {
115 return &NetWork{map[*Manager]P2PPeer{}}
118 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
119 peer := NewP2PPeer(addr, id, flag)
120 nw.nodes[node] = *peer
123 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
124 B2A, ok := nw.nodes[nodeA]
126 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
128 A2B, ok := nw.nodes[nodeB]
130 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
133 A2B.SetConnection(&B2A, nodeB)
134 B2A.SetConnection(&A2B, nodeA)
138 nodeA.SendStatus(B2A.srcPeer)
139 nodeB.SendStatus(A2B.srcPeer)
142 return &B2A, &A2B, nil
145 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
146 blocks := []*types.Block{}
147 indexBlock := &types.Block{}
148 if startBlock == nil {
149 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
150 blocks = append(blocks, indexBlock)
152 indexBlock = startBlock
155 for indexBlock.Height < height {
156 block := &types.Block{
157 BlockHeader: types.BlockHeader{
158 Height: indexBlock.Height + 1,
159 PreviousBlockHash: indexBlock.Hash(),
160 Version: uint64(rand.Uint32()),
163 blocks = append(blocks, block)
169 func mockErrorBlocks(startBlock *types.Block, height uint64, errBlockHeight uint64) []*types.Block {
170 blocks := []*types.Block{}
171 indexBlock := &types.Block{}
172 if startBlock == nil {
173 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
174 blocks = append(blocks, indexBlock)
176 indexBlock = startBlock
179 for indexBlock.Height < height {
180 block := &types.Block{
181 BlockHeader: types.BlockHeader{
182 Height: indexBlock.Height + 1,
183 PreviousBlockHash: indexBlock.Hash(),
184 Version: uint64(rand.Uint32()),
187 if block.Height == errBlockHeight {
188 block.TransactionsMerkleRoot = bc.NewHash([32]byte{0x1})
190 blocks = append(blocks, block)
196 func mockSync(blocks []*types.Block, mempool *mock.Mempool, fastSyncDB dbm.DB) *Manager {
197 chain := mock.NewChain(mempool)
198 peers := peers.NewPeerSet(NewPeerSet())
199 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
200 for _, block := range blocks {
201 chain.SetBlockByHeight(block.Height, block)
206 blockKeeper: newBlockKeeper(chain, peers, fastSyncDB),
209 txSyncCh: make(chan *txSyncMsg),
210 eventDispatcher: event.NewDispatcher(),
214 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
217 trueProg := mockControlProgram(60)
218 assetID := bc.AssetID{V0: 9999}
219 for i := uint64(0); i < uint64(txCount); i++ {
220 tx := types.NewTx(types.TxData{
222 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
223 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
225 txs = append(txs, tx)
226 bcTxs = append(bcTxs, tx.Tx)
231 func mockControlProgram(length int) []byte {
233 for i := 0; i < length; i++ {
234 cp = append(cp, byte(rand.Intn(1<<8)))