8 "github.com/tendermint/go-wire"
9 "github.com/tendermint/tmlibs/flowrate"
10 dbm "github.com/vapor/database/leveldb"
12 "github.com/vapor/consensus"
13 "github.com/vapor/netsync/peers"
14 "github.com/vapor/protocol/bc"
15 "github.com/vapor/protocol/bc/types"
16 "github.com/vapor/test/mock"
22 flag consensus.ServiceFlag
30 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
33 ip: &net.IPAddr{IP: net.ParseIP(addr)},
35 msgCh: make(chan []byte),
40 func (p *P2PPeer) Addr() net.Addr {
44 func (p *P2PPeer) ID() string {
48 func (p *P2PPeer) IsLAN() bool {
52 func (p *P2PPeer) RemoteAddrHost() string {
56 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
60 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
65 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
69 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
70 msgBytes := wire.BinaryBytes(msg)
74 msgType, msg, _ := decodeMessage(msgBytes)
75 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
80 func (p *P2PPeer) setAsync(b bool) {
84 func (p *P2PPeer) postMan() {
85 for msgBytes := range p.msgCh {
86 msgType, msg, _ := decodeMessage(msgBytes)
87 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
93 func NewPeerSet() *PeerSet {
97 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
101 func (ps *PeerSet) StopPeerGracefully(string) {}
103 type NetWork struct {
104 nodes map[*Manager]P2PPeer
107 func NewNetWork() *NetWork {
108 return &NetWork{map[*Manager]P2PPeer{}}
111 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
112 peer := NewP2PPeer(addr, id, flag)
113 nw.nodes[node] = *peer
116 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
117 B2A, ok := nw.nodes[nodeA]
119 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
121 A2B, ok := nw.nodes[nodeB]
123 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
126 A2B.SetConnection(&B2A, nodeB)
127 B2A.SetConnection(&A2B, nodeA)
131 nodeA.SendStatus(B2A.srcPeer)
132 nodeB.SendStatus(A2B.srcPeer)
135 return &B2A, &A2B, nil
138 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
139 blocks := []*types.Block{}
140 indexBlock := &types.Block{}
141 if startBlock == nil {
142 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
143 blocks = append(blocks, indexBlock)
145 indexBlock = startBlock
148 for indexBlock.Height < height {
149 block := &types.Block{
150 BlockHeader: types.BlockHeader{
151 Height: indexBlock.Height + 1,
152 PreviousBlockHash: indexBlock.Hash(),
153 Version: uint64(rand.Uint32()),
156 blocks = append(blocks, block)
162 func mockSync(blocks []*types.Block, mempool *mock.Mempool, fastSyncDB dbm.DB) *Manager {
163 chain := mock.NewChain(mempool)
164 peers := peers.NewPeerSet(NewPeerSet())
165 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
166 for _, block := range blocks {
167 chain.SetBlockByHeight(block.Height, block)
172 blockKeeper: newBlockKeeper(chain, peers, fastSyncDB),
175 txSyncCh: make(chan *txSyncMsg),
179 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
182 trueProg := mockControlProgram(60)
183 assetID := bc.AssetID{V0: 9999}
184 for i := uint64(0); i < uint64(txCount); i++ {
185 tx := types.NewTx(types.TxData{
187 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
188 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
190 txs = append(txs, tx)
191 bcTxs = append(bcTxs, tx.Tx)
196 func mockControlProgram(length int) []byte {
198 for i := 0; i < length; i++ {
199 cp = append(cp, byte(rand.Intn(1<<8)))