github.com/aergoio/aergo@v1.3.1/p2p/const_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"encoding/base64"
    10  	"encoding/hex"
    11  	"fmt"
    12  	"github.com/aergoio/aergo-lib/log"
    13  	"github.com/aergoio/aergo/internal/enc"
    14  	"github.com/aergoio/aergo/p2p/p2pcommon"
    15  	"github.com/aergoio/aergo/p2p/p2pmock"
    16  	"github.com/aergoio/aergo/types"
    17  	"github.com/golang/mock/gomock"
    18  	"github.com/libp2p/go-libp2p-core/crypto"
    19  )
    20  
    21  // this file collect sample global constants used in unit test. I'm tired of creating less meaningful variables in each tests.
    22  
    23  var dummyBlockHash, _ = hex.DecodeString("4f461d85e869ade8a0544f8313987c33a9c06534e50c4ad941498299579bd7ac")
    24  var dummyBlockHeight uint64 = 100215
    25  var dummyTxHash, _ = enc.ToBytes("4H4zAkAyRV253K5SNBJtBxqUgHEbZcXbWFFc6cmQHY45")
    26  
    27  var samplePeerID types.PeerID
    28  var sampleMeta p2pcommon.PeerMeta
    29  var sampleErr error
    30  
    31  var logger *log.Logger
    32  
    33  func init() {
    34  	logger = log.NewLogger("test")
    35  	samplePeerID, _ = types.IDB58Decode("16Uiu2HAkvvhjxVm2WE9yFBDdPQ9qx6pX9taF6TTwDNHs8VPi1EeR")
    36  	sampleErr = fmt.Errorf("err in unittest")
    37  	sampleMeta = p2pcommon.PeerMeta{ID: samplePeerID, IPAddress: "192.168.1.2", Port: 7845}
    38  
    39  }
    40  
    41  const (
    42  	sampleKey1PrivBase64 = "CAISIM1yE7XjJyKTw4fQYMROnlxmEBut5OPPGVde7PeVAf0x"
    43  	sampelKey1PubBase64  = "CAISIQOMA3AHgprpAb7goiDGLI6b/je3JKiYSBHyb46twYV7RA=="
    44  	sampleKey1IDbase58   = "16Uiu2HAmN5YU8V2LnTy9neuuJCLNsxLnd5xVSRZqkjvZUHS3mLoD"
    45  
    46  	sampleKey2PrivBase64 = "CAISIPK7nwYwZwl0LVvjJjZ58gN/4z0iAIOEOi5fKDIMBKCN"
    47  )
    48  
    49  var sampleMsgID p2pcommon.MsgID
    50  var sampleKey1Priv crypto.PrivKey
    51  var sampleKey1Pub crypto.PubKey
    52  var sampleKey1ID types.PeerID
    53  
    54  var sampleKey2Priv crypto.PrivKey
    55  var sampleKey2Pub crypto.PubKey
    56  var sampleKey2ID types.PeerID
    57  
    58  var dummyPeerID types.PeerID
    59  var dummyPeerID2 types.PeerID
    60  var dummyPeerID3 types.PeerID
    61  
    62  var dummyBestBlock *types.Block
    63  var dummyMeta p2pcommon.PeerMeta
    64  
    65  func init() {
    66  	bytes, _ := base64.StdEncoding.DecodeString(sampleKey1PrivBase64)
    67  	sampleKey1Priv, _ = crypto.UnmarshalPrivateKey(bytes)
    68  	bytes, _ = base64.StdEncoding.DecodeString(sampelKey1PubBase64)
    69  	sampleKey1Pub, _ = crypto.UnmarshalPublicKey(bytes)
    70  	if !sampleKey1Priv.GetPublic().Equals(sampleKey1Pub) {
    71  		panic("problem in pk generation ")
    72  	}
    73  	sampleKey1ID, _ = types.IDFromPublicKey(sampleKey1Pub)
    74  	if sampleKey1ID.Pretty() != sampleKey1IDbase58 {
    75  		panic("problem in id generation")
    76  	}
    77  
    78  	bytes, _ = base64.StdEncoding.DecodeString(sampleKey2PrivBase64)
    79  	sampleKey2Priv, _ = crypto.UnmarshalPrivateKey(bytes)
    80  	sampleKey2Pub = sampleKey2Priv.GetPublic()
    81  	sampleKey2ID, _ = types.IDFromPublicKey(sampleKey2Pub)
    82  
    83  	sampleMsgID = p2pcommon.NewMsgID()
    84  
    85  	dummyPeerID = sampleKey1ID
    86  	dummyPeerID2, _ = types.IDB58Decode("16Uiu2HAmFqptXPfcdaCdwipB2fhHATgKGVFVPehDAPZsDKSU7jRm")
    87  	dummyPeerID3, _ = types.IDB58Decode("16Uiu2HAmU8Wc925gZ5QokM4sGDKjysdPwRCQFoYobvoVnyutccCD")
    88  
    89  	dummyBestBlock = &types.Block{Header: &types.BlockHeader{}}
    90  	dummyMeta = p2pcommon.PeerMeta{ID: dummyPeerID}
    91  }
    92  
    93  const hashSize = 32
    94  
    95  var sampleTxsB58 = []string{
    96  	"4H4zAkAyRV253K5SNBJtBxqUgHEbZcXbWFFc6cmQHY45",
    97  	"6xfk39kuyDST7NwCu8tx3wqwFZ5dwKPDjxUS14tU7NZb8",
    98  	"E8dbBGe9Hnuhk35cJoekPjL3VoL4xAxtnRuP47UoxzHd",
    99  	"HB7Hg5GUbHuxwe8Lp5PcYUoAaQ7EZjRNG6RuvS6DnDRf",
   100  	"BxKmDg9VbWHxrWnStEeTzJ2Ze7RF7YK4rpyjcsWSsnxs",
   101  	"DwmGqFU4WgADpYN36FXKsYxMjeppvh9Najg4KxJ8gtX3",
   102  }
   103  
   104  var sampleTxs [][]byte
   105  var sampleTxHashes []types.TxID
   106  
   107  var sampleBlksB58 = []string{
   108  	"v6zbuQ4aVSdbTwQhaiZGp5pcL5uL55X3kt2wfxor5W6",
   109  	"2VEPg4MqJUoaS3EhZ6WWSAUuFSuD4oSJ645kSQsGV7H9",
   110  	"AtzTZ2CZS45F1276RpTdLfYu2DLgRcd9HL3aLqDT1qte",
   111  	"2n9QWNDoUvML756X7xdHWCFLZrM4CQEtnVH2RzG5FYAw",
   112  	"6cy7U7XKYtDTMnF3jNkcJvJN5Rn85771NSKjc5Tfo2DM",
   113  	"3bmB8D37XZr4DNPs64NiGRa2Vw3i8VEgEy6Xc2XBmRXC",
   114  }
   115  var sampleBlks [][]byte
   116  var sampleBlksHashes []types.BlockID
   117  
   118  func init() {
   119  	sampleTxs = make([][]byte, len(sampleTxsB58))
   120  	sampleTxHashes = make([]types.TxID, len(sampleTxsB58))
   121  	for i, hashb58 := range sampleTxsB58 {
   122  		hash, _ := enc.ToBytes(hashb58)
   123  		sampleTxs[i] = hash
   124  		copy(sampleTxHashes[i][:], hash)
   125  	}
   126  
   127  	sampleBlks = make([][]byte, len(sampleBlksB58))
   128  	sampleBlksHashes = make([]types.BlockID, len(sampleBlksB58))
   129  	for i, hashb58 := range sampleTxsB58 {
   130  		hash, _ := enc.ToBytes(hashb58)
   131  		sampleBlks[i] = hash
   132  		copy(sampleBlksHashes[i][:], hash)
   133  	}
   134  }
   135  
   136  var dummyMo *p2pmock.MockMsgOrder
   137  
   138  func createDummyMo(ctrl *gomock.Controller) *p2pmock.MockMsgOrder {
   139  	dummyMo = p2pmock.NewMockMsgOrder(ctrl)
   140  	dummyMo.EXPECT().IsNeedSign().Return(true).AnyTimes()
   141  	dummyMo.EXPECT().IsRequest().Return(true).AnyTimes()
   142  	dummyMo.EXPECT().GetProtocolID().Return(p2pcommon.NewTxNotice).AnyTimes()
   143  	dummyMo.EXPECT().GetMsgID().Return(p2pcommon.NewMsgID()).AnyTimes()
   144  	return dummyMo
   145  }