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) RemoteAddrHost() string {
55 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
59 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
64 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
68 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
69 msgBytes := wire.BinaryBytes(msg)
73 msgType, msg, _ := decodeMessage(msgBytes)
74 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
79 func (p *P2PPeer) setAsync(b bool) {
83 func (p *P2PPeer) postMan() {
84 for msgBytes := range p.msgCh {
85 msgType, msg, _ := decodeMessage(msgBytes)
86 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
92 func NewPeerSet() *PeerSet {
96 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
100 func (ps *PeerSet) StopPeerGracefully(string) {}
102 type NetWork struct {
103 nodes map[*Manager]P2PPeer
106 func NewNetWork() *NetWork {
107 return &NetWork{map[*Manager]P2PPeer{}}
110 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
111 peer := NewP2PPeer(addr, id, flag)
112 nw.nodes[node] = *peer
115 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
116 B2A, ok := nw.nodes[nodeA]
118 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
120 A2B, ok := nw.nodes[nodeB]
122 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
125 A2B.SetConnection(&B2A, nodeB)
126 B2A.SetConnection(&A2B, nodeA)
130 nodeA.SendStatus(B2A.srcPeer)
131 nodeB.SendStatus(A2B.srcPeer)
134 return &B2A, &A2B, nil
137 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
138 blocks := []*types.Block{}
139 indexBlock := &types.Block{}
140 if startBlock == nil {
141 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
142 blocks = append(blocks, indexBlock)
144 indexBlock = startBlock
147 for indexBlock.Height < height {
148 block := &types.Block{
149 BlockHeader: types.BlockHeader{
150 Height: indexBlock.Height + 1,
151 PreviousBlockHash: indexBlock.Hash(),
152 Version: uint64(rand.Uint32()),
155 blocks = append(blocks, block)
161 func mockSync(blocks []*types.Block, mempool *mock.Mempool) *Manager {
162 chain := mock.NewChain(mempool)
163 peers := peers.NewPeerSet(NewPeerSet())
164 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
165 for _, block := range blocks {
166 chain.SetBlockByHeight(block.Height, block)
171 blockKeeper: newBlockKeeper(chain, peers),
174 txSyncCh: make(chan *txSyncMsg),
178 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
181 trueProg := mockControlProgram(60)
182 assetID := bc.AssetID{V0: 9999}
183 for i := uint64(0); i < uint64(txCount); i++ {
184 tx := types.NewTx(types.TxData{
186 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
187 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
189 txs = append(txs, tx)
190 bcTxs = append(bcTxs, tx.Tx)
195 func mockControlProgram(length int) []byte {
197 for i := 0; i < length; i++ {
198 cp = append(cp, byte(rand.Intn(1<<8)))