github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/netsync/chainmgr/tool_test.go (about)

     1  package chainmgr
     2  
     3  import (
     4  	"errors"
     5  	"math/rand"
     6  	"net"
     7  	"time"
     8  
     9  	dbm "github.com/bytom/bytom/database/leveldb"
    10  	"github.com/tendermint/go-wire"
    11  	"github.com/tendermint/tmlibs/flowrate"
    12  
    13  	"github.com/bytom/bytom/consensus"
    14  	"github.com/bytom/bytom/event"
    15  	"github.com/bytom/bytom/netsync/peers"
    16  	"github.com/bytom/bytom/protocol/bc"
    17  	"github.com/bytom/bytom/protocol/bc/types"
    18  	"github.com/bytom/bytom/test/mock"
    19  )
    20  
    21  type P2PPeer struct {
    22  	id   string
    23  	ip   *net.IPAddr
    24  	flag consensus.ServiceFlag
    25  
    26  	srcPeer    *P2PPeer
    27  	remoteNode *Manager
    28  	msgCh      chan []byte
    29  	async      bool
    30  }
    31  
    32  func NewP2PPeer(addr, id string, flag consensus.ServiceFlag) *P2PPeer {
    33  	return &P2PPeer{
    34  		id:    id,
    35  		ip:    &net.IPAddr{IP: net.ParseIP(addr)},
    36  		flag:  flag,
    37  		msgCh: make(chan []byte),
    38  		async: false,
    39  	}
    40  }
    41  
    42  func (p *P2PPeer) Addr() net.Addr {
    43  	return p.ip
    44  }
    45  
    46  func (p *P2PPeer) ID() string {
    47  	return p.id
    48  }
    49  
    50  func (p *P2PPeer) IsLAN() bool {
    51  	return false
    52  }
    53  
    54  func (p *P2PPeer) Moniker() string {
    55  	return ""
    56  }
    57  
    58  func (p *P2PPeer) RemoteAddrHost() string {
    59  	return ""
    60  }
    61  
    62  func (p *P2PPeer) ServiceFlag() consensus.ServiceFlag {
    63  	return p.flag
    64  }
    65  
    66  func (p *P2PPeer) SetConnection(srcPeer *P2PPeer, node *Manager) {
    67  	p.srcPeer = srcPeer
    68  	p.remoteNode = node
    69  }
    70  
    71  func (p *P2PPeer) TrafficStatus() (*flowrate.Status, *flowrate.Status) {
    72  	return nil, nil
    73  }
    74  
    75  func (p *P2PPeer) TrySend(b byte, msg interface{}) bool {
    76  	msgBytes := wire.BinaryBytes(msg)
    77  	if p.async {
    78  		p.msgCh <- msgBytes
    79  	} else {
    80  		msgType, msg, _ := decodeMessage(msgBytes)
    81  		p.remoteNode.processMsg(p.srcPeer, msgType, msg)
    82  	}
    83  	return true
    84  }
    85  
    86  func (p *P2PPeer) setAsync(b bool) {
    87  	p.async = b
    88  }
    89  
    90  func (p *P2PPeer) postMan() {
    91  	for msgBytes := range p.msgCh {
    92  		msgType, msg, _ := decodeMessage(msgBytes)
    93  		time.Sleep(10 * time.Millisecond)
    94  		p.remoteNode.processMsg(p.srcPeer, msgType, msg)
    95  	}
    96  }
    97  
    98  type PeerSet struct{}
    99  
   100  func NewPeerSet() *PeerSet {
   101  	return &PeerSet{}
   102  }
   103  
   104  func (ps *PeerSet) IsBanned(ip string, level byte, reason string) bool {
   105  	return false
   106  }
   107  
   108  func (ps *PeerSet) StopPeerGracefully(string) {}
   109  
   110  type NetWork struct {
   111  	nodes map[*Manager]P2PPeer
   112  }
   113  
   114  func NewNetWork() *NetWork {
   115  	return &NetWork{map[*Manager]P2PPeer{}}
   116  }
   117  
   118  func (nw *NetWork) Register(node *Manager, addr, id string, flag consensus.ServiceFlag) {
   119  	peer := NewP2PPeer(addr, id, flag)
   120  	nw.nodes[node] = *peer
   121  }
   122  
   123  func (nw *NetWork) HandsShake(nodeA, nodeB *Manager) (*P2PPeer, *P2PPeer, error) {
   124  	B2A, ok := nw.nodes[nodeA]
   125  	if !ok {
   126  		return nil, nil, errors.New("can't find nodeA's p2p peer on network")
   127  	}
   128  	A2B, ok := nw.nodes[nodeB]
   129  	if !ok {
   130  		return nil, nil, errors.New("can't find nodeB's p2p peer on network")
   131  	}
   132  
   133  	A2B.SetConnection(&B2A, nodeB)
   134  	B2A.SetConnection(&A2B, nodeA)
   135  
   136  	nodeA.AddPeer(&A2B)
   137  	nodeB.AddPeer(&B2A)
   138  	nodeA.SendStatus(B2A.srcPeer)
   139  	nodeB.SendStatus(A2B.srcPeer)
   140  	A2B.setAsync(true)
   141  	B2A.setAsync(true)
   142  	return &B2A, &A2B, nil
   143  }
   144  
   145  func mockBlocks(startBlock *types.Block, height uint64) []*types.Block {
   146  	blocks := []*types.Block{}
   147  	indexBlock := &types.Block{}
   148  	if startBlock == nil {
   149  		indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
   150  		blocks = append(blocks, indexBlock)
   151  	} else {
   152  		indexBlock = startBlock
   153  	}
   154  
   155  	for indexBlock.Height < height {
   156  		block := &types.Block{
   157  			BlockHeader: types.BlockHeader{
   158  				Height:            indexBlock.Height + 1,
   159  				PreviousBlockHash: indexBlock.Hash(),
   160  				Version:           uint64(rand.Uint32()),
   161  			},
   162  		}
   163  		blocks = append(blocks, block)
   164  		indexBlock = block
   165  	}
   166  	return blocks
   167  }
   168  
   169  func mockErrorBlocks(startBlock *types.Block, height uint64, errBlockHeight uint64) []*types.Block {
   170  	blocks := []*types.Block{}
   171  	indexBlock := &types.Block{}
   172  	if startBlock == nil {
   173  		indexBlock = &types.Block{BlockHeader: types.BlockHeader{Version: uint64(rand.Uint32())}}
   174  		blocks = append(blocks, indexBlock)
   175  	} else {
   176  		indexBlock = startBlock
   177  	}
   178  
   179  	for indexBlock.Height < height {
   180  		block := &types.Block{
   181  			BlockHeader: types.BlockHeader{
   182  				Height:            indexBlock.Height + 1,
   183  				PreviousBlockHash: indexBlock.Hash(),
   184  				Version:           uint64(rand.Uint32()),
   185  			},
   186  		}
   187  		if block.Height == errBlockHeight {
   188  			block.TransactionsMerkleRoot = bc.NewHash([32]byte{0x1})
   189  		}
   190  		blocks = append(blocks, block)
   191  		indexBlock = block
   192  	}
   193  	return blocks
   194  }
   195  
   196  func mockSync(blocks []*types.Block, mempool *mock.Mempool, fastSyncDB dbm.DB) *Manager {
   197  	chain := mock.NewChain()
   198  	peers := peers.NewPeerSet(NewPeerSet())
   199  	chain.SetBestBlockHeader(&blocks[len(blocks)-1].BlockHeader)
   200  	for _, block := range blocks {
   201  		chain.SetBlockByHeight(block.Height, block)
   202  	}
   203  
   204  	return &Manager{
   205  		chain:           chain,
   206  		blockKeeper:     newBlockKeeper(chain, peers, fastSyncDB),
   207  		peers:           peers,
   208  		mempool:         mempool,
   209  		txSyncCh:        make(chan *txSyncMsg),
   210  		eventDispatcher: event.NewDispatcher(),
   211  	}
   212  }
   213  
   214  func mockTxs(txCount int) ([]*types.Tx, []*bc.Tx) {
   215  	var txs []*types.Tx
   216  	var bcTxs []*bc.Tx
   217  	trueProg := mockControlProgram(60)
   218  	assetID := bc.AssetID{V0: 9999}
   219  	for i := uint64(0); i < uint64(txCount); i++ {
   220  		tx := types.NewTx(types.TxData{
   221  			Version: 1,
   222  			Inputs:  []*types.TxInput{types.NewSpendInput(nil, bc.Hash{V0: i + 1}, assetID, i, i, trueProg, nil)},
   223  			Outputs: []*types.TxOutput{types.NewOriginalTxOutput(assetID, 1, trueProg, nil)},
   224  		})
   225  		txs = append(txs, tx)
   226  		bcTxs = append(bcTxs, tx.Tx)
   227  	}
   228  	return txs, bcTxs
   229  }
   230  
   231  func mockControlProgram(length int) []byte {
   232  	var cp []byte
   233  	for i := 0; i < length; i++ {
   234  		cp = append(cp, byte(rand.Intn(1<<8)))
   235  	}
   236  	return cp
   237  }