8 "github.com/tendermint/go-wire"
9 "github.com/tendermint/tmlibs/flowrate"
11 "github.com/vapor/consensus"
12 "github.com/vapor/netsync/peers"
13 "github.com/vapor/protocol/bc"
14 "github.com/vapor/protocol/bc/types"
15 "github.com/vapor/test/mock"
21 flag consensus.ServiceFlag
29 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
32 ip: &net.IPAddr{IP: net.ParseIP(addr)},
34 msgCh: make(chan []byte),
39 func (p *P2PPeer) Addr() net.Addr {
43 func (p *P2PPeer) ID() string {
47 func (p *P2PPeer) IsLAN() bool {
51 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
55 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
60 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
64 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
65 msgBytes := wire.BinaryBytes(msg)
69 msgType, msg, _ := decodeMessage(msgBytes)
70 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
75 func (p *P2PPeer) setAsync(b bool) {
79 func (p *P2PPeer) postMan() {
80 for msgBytes := range p.msgCh {
81 msgType, msg, _ := decodeMessage(msgBytes)
82 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
88 func NewPeerSet() *PeerSet {
92 func (ps *PeerSet) IsBanned(peerID string, level byte, reason string) bool {
96 func (ps *PeerSet) StopPeerGracefully(string) {}
99 nodes map[*Manager]P2PPeer
102 func NewNetWork() *NetWork {
103 return &NetWork{map[*Manager]P2PPeer{}}
106 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
107 peer := NewP2PPeer(addr, id, flag)
108 nw.nodes[node] = *peer
111 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
112 B2A, ok := nw.nodes[nodeA]
114 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
116 A2B, ok := nw.nodes[nodeB]
118 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
121 A2B.SetConnection(&B2A, nodeB)
122 B2A.SetConnection(&A2B, nodeA)
126 nodeA.SendStatus(B2A.srcPeer)
127 nodeB.SendStatus(A2B.srcPeer)
130 return &B2A, &A2B, nil
133 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
134 blocks := []*types.Block{}
135 indexBlock := &types.Block{}
136 if startBlock == nil {
137 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
138 blocks = append(blocks, indexBlock)
140 indexBlock = startBlock
143 for indexBlock.Height < height {
144 block := &types.Block{
145 BlockHeader: types.BlockHeader{
146 Height: indexBlock.Height + 1,
147 PreviousBlockHash: indexBlock.Hash(),
148 Version: uint64(rand.Uint32()),
151 blocks = append(blocks, block)
157 func mockSync(blocks []*types.Block, mempool *mock.Mempool) *Manager {
158 chain := mock.NewChain(mempool)
159 peers := peers.NewPeerSet(NewPeerSet())
160 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
161 for _, block := range blocks {
162 chain.SetBlockByHeight(block.Height, block)
167 blockKeeper: newBlockKeeper(chain, peers),
170 txSyncCh: make(chan *txSyncMsg),
174 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
177 trueProg := mockControlProgram(60)
178 assetID := bc.AssetID{V0: 9999}
179 for i := uint64(0); i < uint64(txCount); i++ {
180 tx := types.NewTx(types.TxData{
182 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
183 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
185 txs = append(txs, tx)
186 bcTxs = append(bcTxs, tx.Tx)
191 func mockControlProgram(length int) []byte {
193 for i := 0; i < length; i++ {
194 cp = append(cp, byte(rand.Intn(1<<8)))