9 "github.com/tendermint/go-wire"
10 "github.com/tendermint/tmlibs/flowrate"
11 dbm "github.com/vapor/database/leveldb"
13 "github.com/vapor/consensus"
14 "github.com/vapor/event"
15 "github.com/vapor/netsync/peers"
16 "github.com/vapor/protocol/bc"
17 "github.com/vapor/protocol/bc/types"
18 "github.com/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) RemoteAddrHost() string {
58 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
62 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
67 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
71 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
72 msgBytes := wire.BinaryBytes(msg)
76 msgType, msg, _ := decodeMessage(msgBytes)
77 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
82 func (p *P2PPeer) setAsync(b bool) {
86 func (p *P2PPeer) postMan() {
87 for msgBytes := range p.msgCh {
88 msgType, msg, _ := decodeMessage(msgBytes)
89 time.Sleep(10 * time.Millisecond)
90 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
96 func NewPeerSet() *PeerSet {
100 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
104 func (ps *PeerSet) StopPeerGracefully(string) {}
106 type NetWork struct {
107 nodes map[*Manager]P2PPeer
110 func NewNetWork() *NetWork {
111 return &NetWork{map[*Manager]P2PPeer{}}
114 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
115 peer := NewP2PPeer(addr, id, flag)
116 nw.nodes[node] = *peer
119 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
120 B2A, ok := nw.nodes[nodeA]
122 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
124 A2B, ok := nw.nodes[nodeB]
126 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
129 A2B.SetConnection(&B2A, nodeB)
130 B2A.SetConnection(&A2B, nodeA)
134 nodeA.SendStatus(B2A.srcPeer)
135 nodeB.SendStatus(A2B.srcPeer)
138 return &B2A, &A2B, nil
141 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
142 blocks := []*types.Block{}
143 indexBlock := &types.Block{}
144 if startBlock == nil {
145 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
146 blocks = append(blocks, indexBlock)
148 indexBlock = startBlock
151 for indexBlock.Height < height {
152 block := &types.Block{
153 BlockHeader: types.BlockHeader{
154 Height: indexBlock.Height + 1,
155 PreviousBlockHash: indexBlock.Hash(),
156 Version: uint64(rand.Uint32()),
159 blocks = append(blocks, block)
165 func mockErrorBlocks(startBlock *types.Block, height uint64, errBlockHeight uint64) []*types.Block {
166 blocks := []*types.Block{}
167 indexBlock := &types.Block{}
168 if startBlock == nil {
169 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
170 blocks = append(blocks, indexBlock)
172 indexBlock = startBlock
175 for indexBlock.Height < height {
176 block := &types.Block{
177 BlockHeader: types.BlockHeader{
178 Height: indexBlock.Height + 1,
179 PreviousBlockHash: indexBlock.Hash(),
180 Version: uint64(rand.Uint32()),
183 if block.Height == errBlockHeight {
184 block.TransactionsMerkleRoot = bc.NewHash([32]byte{0x1})
186 blocks = append(blocks, block)
192 func mockSync(blocks []*types.Block, mempool *mock.Mempool, fastSyncDB dbm.DB) *Manager {
193 chain := mock.NewChain(mempool)
194 peers := peers.NewPeerSet(NewPeerSet())
195 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
196 for _, block := range blocks {
197 chain.SetBlockByHeight(block.Height, block)
202 blockKeeper: newBlockKeeper(chain, peers, fastSyncDB),
205 txSyncCh: make(chan *txSyncMsg),
206 eventDispatcher: event.NewDispatcher(),
210 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
213 trueProg := mockControlProgram(60)
214 assetID := bc.AssetID{V0: 9999}
215 for i := uint64(0); i < uint64(txCount); i++ {
216 tx := types.NewTx(types.TxData{
218 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
219 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
221 txs = append(txs, tx)
222 bcTxs = append(bcTxs, tx.Tx)
227 func mockControlProgram(length int) []byte {
229 for i := 0; i < length; i++ {
230 cp = append(cp, byte(rand.Intn(1<<8)))