9 wire "github.com/tendermint/go-wire"
10 "github.com/tendermint/tmlibs/flowrate"
12 "github.com/bytom/consensus"
13 "github.com/bytom/protocol/bc"
14 "github.com/bytom/protocol/bc/types"
15 "github.com/bytom/test/mock"
21 flag consensus.ServiceFlag
24 remoteNode *SyncManager
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) ServiceFlag() consensus.ServiceFlag {
55 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *SyncManager) {
60 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
64 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
65 msgBytes := wire.BinaryBytes(msg)
69 msgType, msg, _ := DecodeMessage(msgBytes)
70 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
75 func (p *P2PPeer) setAsync(b bool) {
79 func (p *P2PPeer) postMan() {
80 for msgBytes := range p.msgCh {
81 msgType, msg, _ := DecodeMessage(msgBytes)
82 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
88 func NewPeerSet() *PeerSet {
92 func (ps *PeerSet) IsBanned(peerID string, level byte, reason string) bool {
96 func (ps *PeerSet) StopPeerGracefully(string) {}
99 nodes map[*SyncManager]P2PPeer
102 func NewNetWork() *NetWork {
103 return &NetWork{map[*SyncManager]P2PPeer{}}
106 func (nw *NetWork) Register(node *SyncManager, addr, id string, flag consensus.ServiceFlag) {
107 peer := NewP2PPeer(addr, id, flag)
108 nw.nodes[node] = *peer
111 func (nw *NetWork) HandsShake(nodeA, nodeB *SyncManager) (*P2PPeer, *P2PPeer, error) {
112 B2A, ok := nw.nodes[nodeA]
114 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
116 A2B, ok := nw.nodes[nodeB]
118 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
121 A2B.SetConnection(&B2A, nodeB)
122 B2A.SetConnection(&A2B, nodeA)
124 nodeA.handleStatusRequestMsg(&A2B)
125 nodeB.handleStatusRequestMsg(&B2A)
129 return &B2A, &A2B, nil
132 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
133 blocks := []*types.Block{}
134 indexBlock := &types.Block{}
135 if startBlock == nil {
136 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Nonce: uint64(rand.Uint32())}}
137 blocks = append(blocks, indexBlock)
139 indexBlock = startBlock
142 for indexBlock.Height < height {
143 block := &types.Block{
144 BlockHeader: types.BlockHeader{
145 Height: indexBlock.Height + 1,
146 PreviousBlockHash: indexBlock.Hash(),
147 Nonce: uint64(rand.Uint32()),
150 blocks = append(blocks, block)
156 func mockSync(blocks []*types.Block) *SyncManager {
157 chain := mock.NewChain()
158 peers := newPeerSet(NewPeerSet())
159 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
160 for _, block := range blocks {
161 chain.SetBlockByHeight(block.Height, block)
164 genesis, _ := chain.GetHeaderByHeight(0)
166 genesisHash: genesis.Hash(),
168 blockKeeper: newBlockKeeper(chain, peers),
173 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
176 for i := 0; i < txCount; i++ {
177 trueProg := mockControlProgram(60)
178 assetID := bc.ComputeAssetID(trueProg, 1, &bc.EmptyStringHash)
179 now := []byte(time.Now().String())
180 issuanceInp := types.NewIssuanceInput(now, 1, trueProg, nil, nil)
181 tx := types.NewTx(types.TxData{
183 Inputs: []*types.TxInput{issuanceInp},
184 Outputs: []*types.TxOutput{types.NewTxOutput(assetID, 1, trueProg)},
186 txs = append(txs, tx)
187 bcTxs = append(bcTxs, tx.Tx)
192 func mockControlProgram(length int) []byte {
194 for i := 0; i < length; i++ {
195 cp = append(cp, byte(rand.Intn(1<<8)))