8 wire "github.com/tendermint/go-wire"
9 "github.com/tendermint/tmlibs/flowrate"
11 "github.com/vapor/consensus"
12 "github.com/vapor/protocol/bc"
13 "github.com/vapor/protocol/bc/types"
14 "github.com/vapor/test/mock"
20 flag consensus.ServiceFlag
23 remoteNode *SyncManager
28 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
31 ip: &net.IPAddr{IP: net.ParseIP(addr)},
33 msgCh: make(chan []byte),
38 func (p *P2PPeer) Addr() net.Addr {
42 func (p *P2PPeer) ID() string {
46 func (p *P2PPeer) IsLAN() bool {
50 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
54 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *SyncManager) {
59 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
63 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
64 msgBytes := wire.BinaryBytes(msg)
68 msgType, msg, _ := DecodeMessage(msgBytes)
69 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
74 func (p *P2PPeer) setAsync(b bool) {
78 func (p *P2PPeer) postMan() {
79 for msgBytes := range p.msgCh {
80 msgType, msg, _ := DecodeMessage(msgBytes)
81 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
87 func NewPeerSet() *PeerSet {
91 func (ps *PeerSet) AddBannedPeer(string) error { return nil }
92 func (ps *PeerSet) StopPeerGracefully(string) {}
95 nodes map[*SyncManager]P2PPeer
98 func NewNetWork() *NetWork {
99 return &NetWork{map[*SyncManager]P2PPeer{}}
102 func (nw *NetWork) Register(node *SyncManager, addr, id string, flag consensus.ServiceFlag) {
103 peer := NewP2PPeer(addr, id, flag)
104 nw.nodes[node] = *peer
107 func (nw *NetWork) HandsShake(nodeA, nodeB *SyncManager) (*P2PPeer, *P2PPeer, error) {
108 B2A, ok := nw.nodes[nodeA]
110 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
112 A2B, ok := nw.nodes[nodeB]
114 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
117 A2B.SetConnection(&B2A, nodeB)
118 B2A.SetConnection(&A2B, nodeA)
120 nodeA.handleStatusRequestMsg(&A2B)
121 nodeB.handleStatusRequestMsg(&B2A)
125 return &B2A, &A2B, nil
128 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
129 blocks := []*types.Block{}
130 indexBlock := &types.Block{}
131 if startBlock == nil {
132 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
133 blocks = append(blocks, indexBlock)
135 indexBlock = startBlock
138 for indexBlock.Height < height {
139 block := &types.Block{
140 BlockHeader: types.BlockHeader{
141 Height: indexBlock.Height + 1,
142 PreviousBlockHash: indexBlock.Hash(),
143 Version: uint64(rand.Uint32()),
146 blocks = append(blocks, block)
152 func mockSync(blocks []*types.Block) *SyncManager {
153 chain := mock.NewChain()
154 peers := newPeerSet(NewPeerSet())
155 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
156 for _, block := range blocks {
157 chain.SetBlockByHeight(block.Height, block)
160 genesis, _ := chain.GetHeaderByHeight(0)
162 genesisHash: genesis.Hash(),
164 blockKeeper: newBlockKeeper(chain, peers),
169 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
172 trueProg := mockControlProgram(60)
173 assetID := bc.AssetID{V0: 9999}
174 for i := uint64(0); i < uint64(txCount); i++ {
175 tx := types.NewTx(types.TxData{
177 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
178 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
180 txs = append(txs, tx)
181 bcTxs = append(bcTxs, tx.Tx)
186 func mockControlProgram(length int) []byte {
188 for i := 0; i < length; i++ {
189 cp = append(cp, byte(rand.Intn(1<<8)))