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),
41 func (p *P2PPeer) Addr() net.Addr {
45 func (p *P2PPeer) ID() string {
49 func (p *P2PPeer) IsLAN() bool {
53 func (p *P2PPeer) Moniker() string {
57 func (p *P2PPeer) RemoteAddrHost() string {
61 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
65 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
70 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
74 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
75 msgBytes := wire.BinaryBytes(msg)
79 msgType, msg, _ := decodeMessage(msgBytes)
80 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
85 func (p *P2PPeer) setAsync(b bool) {
89 func (p *P2PPeer) postMan() {
90 for msgBytes := range p.msgCh {
91 msgType, msg, _ := decodeMessage(msgBytes)
92 time.Sleep(10 * time.Millisecond)
93 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
99 func NewPeerSet() *PeerSet {
103 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
107 func (ps *PeerSet) StopPeerGracefully(string) {}
109 type NetWork struct {
110 nodes map[*Manager]P2PPeer
113 func NewNetWork() *NetWork {
114 return &NetWork{map[*Manager]P2PPeer{}}
117 func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
118 peer := NewP2PPeer(addr, id, flag)
119 nw.nodes[node] = *peer
122 func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
123 B2A, ok := nw.nodes[nodeA]
125 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
127 A2B, ok := nw.nodes[nodeB]
129 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
132 A2B.SetConnection(&B2A, nodeB)
133 B2A.SetConnection(&A2B, nodeA)
137 nodeA.SendStatus(B2A.srcPeer)
138 nodeB.SendStatus(A2B.srcPeer)
141 return &B2A, &A2B, nil
144 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
145 blocks := []*types.Block{}
146 indexBlock := &types.Block{}
147 if startBlock == nil {
148 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
149 blocks = append(blocks, indexBlock)
151 indexBlock = startBlock
154 for indexBlock.Height < height {
155 block := &types.Block{
156 BlockHeader: types.BlockHeader{
157 Height: indexBlock.Height + 1,
158 PreviousBlockHash: indexBlock.Hash(),
159 Version: uint64(rand.Uint32()),
162 blocks = append(blocks, block)
168 func mockErrorBlocks(startBlock *types.Block, height uint64, errBlockHeight uint64) []*types.Block {
169 blocks := []*types.Block{}
170 indexBlock := &types.Block{}
171 if startBlock == nil {
172 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
173 blocks = append(blocks, indexBlock)
175 indexBlock = startBlock
178 for indexBlock.Height < height {
179 block := &types.Block{
180 BlockHeader: types.BlockHeader{
181 Height: indexBlock.Height + 1,
182 PreviousBlockHash: indexBlock.Hash(),
183 Version: uint64(rand.Uint32()),
186 if block.Height == errBlockHeight {
187 block.TransactionsMerkleRoot = bc.NewHash([32]byte{0x1})
189 blocks = append(blocks, block)
195 func mockSync(blocks []*types.Block, mempool *mock.Mempool, fastSyncDB dbm.DB) *Manager {
196 chain := mock.NewChain(mempool)
197 peers := peers.NewPeerSet(NewPeerSet())
198 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
199 for _, block := range blocks {
200 chain.SetBlockByHeight(block.Height, block)
205 blockKeeper: newBlockKeeper(chain, peers, fastSyncDB),
208 txSyncCh: make(chan *txSyncMsg),
209 eventDispatcher: event.NewDispatcher(),
213 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
216 trueProg := mockControlProgram(60)
217 assetID := bc.AssetID{V0: 9999}
218 for i := uint64(0); i < uint64(txCount); i++ {
219 tx := types.NewTx(types.TxData{
221 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg)},
222 Outputs: []*types.TxOutput{types.NewIntraChainOutput(assetID, 1, trueProg)},
224 txs = append(txs, tx)
225 bcTxs = append(bcTxs, tx.Tx)
230 func mockControlProgram(length int) []byte {
232 for i := 0; i < length; i++ {
233 cp = append(cp, byte(rand.Intn(1<<8)))