9 wire "github.com/tendermint/go-wire"
10 "github.com/tendermint/tmlibs/flowrate"
12 "github.com/bytom/bytom/consensus"
13 "github.com/bytom/bytom/protocol/bc"
14 "github.com/bytom/bytom/protocol/bc/types"
15 "github.com/bytom/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) RemoteAddrHost() string {
55 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
59 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *SyncManager) {
64 func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
68 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
69 msgBytes := wire.BinaryBytes(msg)
73 msgType, msg, _ := DecodeMessage(msgBytes)
74 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
79 func (p *P2PPeer) setAsync(b bool) {
83 func (p *P2PPeer) postMan() {
84 for msgBytes := range p.msgCh {
85 msgType, msg, _ := DecodeMessage(msgBytes)
86 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
92 func NewPeerSet() *PeerSet {
96 func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
100 func (ps *PeerSet) StopPeerGracefully(string) {}
102 type NetWork struct {
103 nodes map[*SyncManager]P2PPeer
106 func NewNetWork() *NetWork {
107 return &NetWork{map[*SyncManager]P2PPeer{}}
110 func (nw *NetWork) Register(node *SyncManager, addr, id string, flag consensus.ServiceFlag) {
111 peer := NewP2PPeer(addr, id, flag)
112 nw.nodes[node] = *peer
115 func (nw *NetWork) HandsShake(nodeA, nodeB *SyncManager) (*P2PPeer, *P2PPeer, error) {
116 B2A, ok := nw.nodes[nodeA]
118 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
120 A2B, ok := nw.nodes[nodeB]
122 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
125 A2B.SetConnection(&B2A, nodeB)
126 B2A.SetConnection(&A2B, nodeA)
128 nodeA.handleStatusRequestMsg(&A2B)
129 nodeB.handleStatusRequestMsg(&B2A)
133 return &B2A, &A2B, nil
136 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
137 blocks := []*types.Block{}
138 indexBlock := &types.Block{}
139 if startBlock == nil {
140 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Nonce: uint64(rand.Uint32())}}
141 blocks = append(blocks, indexBlock)
143 indexBlock = startBlock
146 for indexBlock.Height < height {
147 block := &types.Block{
148 BlockHeader: types.BlockHeader{
149 Height: indexBlock.Height + 1,
150 PreviousBlockHash: indexBlock.Hash(),
151 Nonce: uint64(rand.Uint32()),
154 blocks = append(blocks, block)
160 func mockSync(blocks []*types.Block) *SyncManager {
161 chain := mock.NewChain()
162 peers := newPeerSet(NewPeerSet())
163 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
164 for _, block := range blocks {
165 chain.SetBlockByHeight(block.Height, block)
168 genesis, _ := chain.GetHeaderByHeight(0)
170 genesisHash: genesis.Hash(),
172 blockKeeper: newBlockKeeper(chain, peers),
177 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
180 for i := 0; i < txCount; i++ {
181 trueProg := mockControlProgram(60)
182 assetID := bc.ComputeAssetID(trueProg, 1, &bc.EmptyStringHash)
183 now := []byte(time.Now().String())
184 issuanceInp := types.NewIssuanceInput(now, 1, trueProg, nil, nil)
185 tx := types.NewTx(types.TxData{
187 Inputs: []*types.TxInput{issuanceInp},
188 Outputs: []*types.TxOutput{types.NewTxOutput(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)))