9 wire "github.com/tendermint/go-wire"
11 "github.com/bytom/consensus"
12 "github.com/bytom/protocol/bc"
13 "github.com/bytom/protocol/bc/types"
14 "github.com/bytom/test/mock"
20 flag consensus.ServiceFlag
23 remoteNode *SyncManager
28 func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
31 ip: &net.IPAddr{IP: net.ParseIP(addr)},
33 msgCh: make(chan []byte),
38 func (p *P2PPeer) Addr() net.Addr {
42 func (p *P2PPeer) ID() string {
46 func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
50 func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *SyncManager) {
55 func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
56 msgBytes := wire.BinaryBytes(msg)
60 msgType, msg, _ := DecodeMessage(msgBytes)
61 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
66 func (p *P2PPeer) setAsync(b bool) {
70 func (p *P2PPeer) postMan() {
71 for msgBytes := range p.msgCh {
72 msgType, msg, _ := DecodeMessage(msgBytes)
73 p.remoteNode.processMsg(p.srcPeer, msgType, msg)
79 func NewPeerSet() *PeerSet {
83 func (ps *PeerSet) AddBannedPeer(string) error { return nil }
84 func (ps *PeerSet) StopPeerGracefully(string) {}
87 nodes map[*SyncManager]P2PPeer
90 func NewNetWork() *NetWork {
91 return &NetWork{map[*SyncManager]P2PPeer{}}
94 func (nw *NetWork) Register(node *SyncManager, addr, id string, flag consensus.ServiceFlag) {
95 peer := NewP2PPeer(addr, id, flag)
96 nw.nodes[node] = *peer
99 func (nw *NetWork) HandsShake(nodeA, nodeB *SyncManager) (*P2PPeer, *P2PPeer, error) {
100 B2A, ok := nw.nodes[nodeA]
102 return nil, nil, errors.New("can't find nodeA's p2p peer on network")
104 A2B, ok := nw.nodes[nodeB]
106 return nil, nil, errors.New("can't find nodeB's p2p peer on network")
109 A2B.SetConnection(&B2A, nodeB)
110 B2A.SetConnection(&A2B, nodeA)
112 nodeA.handleStatusRequestMsg(&A2B)
113 nodeB.handleStatusRequestMsg(&B2A)
117 return &B2A, &A2B, nil
120 func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
121 blocks := []*types.Block{}
122 indexBlock := &types.Block{}
123 if startBlock == nil {
124 indexBlock = &types.Block{BlockHeader: types.BlockHeader{Nonce: uint64(rand.Uint32())}}
125 blocks = append(blocks, indexBlock)
127 indexBlock = startBlock
130 for indexBlock.Height < height {
131 block := &types.Block{
132 BlockHeader: types.BlockHeader{
133 Height: indexBlock.Height + 1,
134 PreviousBlockHash: indexBlock.Hash(),
135 Nonce: uint64(rand.Uint32()),
138 blocks = append(blocks, block)
144 func mockSync(blocks []*types.Block) *SyncManager {
145 chain := mock.NewChain()
146 peers := newPeerSet(NewPeerSet())
147 chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
148 for _, block := range blocks {
149 chain.SetBlockByHeight(block.Height, block)
152 genesis, _ := chain.GetHeaderByHeight(0)
154 genesisHash: genesis.Hash(),
156 blockKeeper: newBlockKeeper(chain, peers),
161 func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
164 for i := 0; i < txCount; i++ {
165 trueProg := mockControlProgram(60)
166 assetID := bc.ComputeAssetID(trueProg, 1, &bc.EmptyStringHash)
167 now := []byte(time.Now().String())
168 issuanceInp := types.NewIssuanceInput(now, 1, trueProg, nil, nil)
169 tx := types.NewTx(types.TxData{
171 Inputs: []*types.TxInput{issuanceInp},
172 Outputs: []*types.TxOutput{types.NewTxOutput(assetID, 1, trueProg)},
174 txs = append(txs, tx)
175 bcTxs = append(bcTxs, tx.Tx)
180 func mockControlProgram(length int) []byte {
182 for i := 0; i < length; i++ {
183 cp = append(cp, byte(rand.Intn(1<<8)))