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/netsync/peers"
15 "github.com/vapor/protocol/bc"
16 "github.com/vapor/protocol/bc/types"
17 "github.com/vapor/test/mock"
23 flag consensus.ServiceFlag
31 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
34 ip: &net.IPAddr{IP: net.ParseIP(addr)},
36 msgCh: make(chan []byte),
41 func (p *P2PPeer) Addr() net.Addr {
45 func (p *P2PPeer) ID() string {
49 func (p *P2PPeer) IsLAN() bool {
53 func (p *P2PPeer) RemoteAddrHost() string {
57 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
61 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
66 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
70 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
71 msgBytes := wire.BinaryBytes(msg)
75 msgType, msg, _ := decodeMessage(msgBytes)
76 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
81 func (p *P2PPeer) setAsync(b bool) {
85 func (p *P2PPeer) postMan() {
86 for msgBytes := range p.msgCh {
87 msgType, msg, _ := decodeMessage(msgBytes)
88 time.Sleep(10 * time.Millisecond)
89 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
95 func NewPeerSet() *PeerSet {
99 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
103 func (ps *PeerSet) StopPeerGracefully(string) {}
105 type NetWork struct {
106 nodes map[*Manager]P2PPeer
109 func NewNetWork() *NetWork {
110 return &NetWork{map[*Manager]P2PPeer{}}
113 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
114 peer := NewP2PPeer(addr, id, flag)
115 nw.nodes[node] = *peer
118 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
119 B2A, ok := nw.nodes[nodeA]
121 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
123 A2B, ok := nw.nodes[nodeB]
125 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
128 A2B.SetConnection(&B2A, nodeB)
129 B2A.SetConnection(&A2B, nodeA)
133 nodeA.SendStatus(B2A.srcPeer)
134 nodeB.SendStatus(A2B.srcPeer)
137 return &B2A, &A2B, nil
140 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
141 blocks := []*types.Block{}
142 indexBlock := &types.Block{}
143 if startBlock == nil {
144 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
145 blocks = append(blocks, indexBlock)
147 indexBlock = startBlock
150 for indexBlock.Height < height {
151 block := &types.Block{
152 BlockHeader: types.BlockHeader{
153 Height: indexBlock.Height + 1,
154 PreviousBlockHash: indexBlock.Hash(),
155 Version: uint64(rand.Uint32()),
158 blocks = append(blocks, block)
164 func mockErrorBlocks(startBlock *types.Block, height uint64, errBlockHeight uint64) []*types.Block {
165 blocks := []*types.Block{}
166 indexBlock := &types.Block{}
167 if startBlock == nil {
168 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
169 blocks = append(blocks, indexBlock)
171 indexBlock = startBlock
174 for indexBlock.Height < height {
175 block := &types.Block{
176 BlockHeader: types.BlockHeader{
177 Height: indexBlock.Height + 1,
178 PreviousBlockHash: indexBlock.Hash(),
179 Version: uint64(rand.Uint32()),
182 if block.Height == errBlockHeight {
183 block.TransactionsMerkleRoot = bc.NewHash([32]byte{0x1})
185 blocks = append(blocks, block)
191 func mockSync(blocks []*types.Block, mempool *mock.Mempool, fastSyncDB dbm.DB) *Manager {
192 chain := mock.NewChain(mempool)
193 peers := peers.NewPeerSet(NewPeerSet())
194 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
195 for _, block := range blocks {
196 chain.SetBlockByHeight(block.Height, block)
201 blockKeeper: newBlockKeeper(chain, peers, fastSyncDB),
204 txSyncCh: make(chan *txSyncMsg),
208 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
211 trueProg := mockControlProgram(60)
212 assetID := bc.AssetID{V0: 9999}
213 for i := uint64(0); i < uint64(txCount); i++ {
214 tx := types.NewTx(types.TxData{
216 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
217 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
219 txs = append(txs, tx)
220 bcTxs = append(bcTxs, tx.Tx)
225 func mockControlProgram(length int) []byte {
227 for i := 0; i < length; i++ {
228 cp = append(cp, byte(rand.Intn(1<<8)))