github.com/annchain/OG@v0.0.9/og/hub_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package og 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/arefactor/og/types" 19 "github.com/annchain/OG/common/crypto" 20 "github.com/annchain/OG/og/downloader" 21 "github.com/annchain/OG/og/types/archive" 22 23 "github.com/annchain/gcache" 24 "github.com/sirupsen/logrus" 25 "testing" 26 "time" 27 ) 28 29 // Tests that protocol versions and modes of operations are matched up properly. 30 func TestProtocolCompatibility(t *testing.T) { 31 32 // Define the compatibility chart 33 tests := []struct { 34 version uint32 35 mode downloader.SyncMode 36 compatible bool 37 }{ 38 {0, downloader.FullSync, true}, {1, downloader.FullSync, true}, {2, downloader.FullSync, true}, 39 {0, downloader.FastSync, false}, {1, downloader.FastSync, false}, {2, downloader.FastSync, true}, 40 } 41 // Make sure anything we screw up is restored 42 backup := ProtocolVersions 43 defer func() { ProtocolVersions = backup }() 44 45 // Try all available compatibility configs and check for errors 46 for i, tt := range tests { 47 ProtocolVersions = []uint32{tt.version} 48 h, _, err := newTestHub(tt.mode) 49 if h != nil { 50 defer h.Stop() 51 } 52 if (err == nil && !tt.compatible) || (err != nil && tt.compatible) { 53 t.Errorf("test %d: compatibility mismatch: have error %v, want compatibility %v tt %v", i, err, tt.compatible, tt) 54 } 55 } 56 } 57 58 func TestSh256(t *testing.T) { 59 var msg []types 60 for i := 0; i < 10000; i++ { 61 var m types 62 m.MessageType = archive.MessageTypeBodiesResponse 63 h := types.RandomHash() 64 m.Data = append(m.Data, h.Bytes[:]...) 65 msg = append(msg, m) 66 } 67 start := time.Now() 68 for _, m := range msg { 69 m.CalculateHash() 70 } 71 fmt.Println("used time ", time.Now().Sub(start)) 72 } 73 74 func TestP2PMessage_Encrypt(t *testing.T) { 75 for i := 0; i < 2; i++ { 76 logrus.SetLevel(logrus.TraceLevel) 77 msg := p2p_message.MessageConsensusDkgDeal{ 78 Data: []byte("this is a test of og Message"), 79 Id: 12, 80 } 81 m := types{Message: &msg, MessageType: message_archive.MessageTypeConsensusDkgDeal} 82 s := crypto.NewSigner(crypto.CryptoType(i)) 83 fmt.Println(s.GetCryptoType()) 84 pk, sk := s.RandomKeyPair() 85 m.Marshal() 86 logrus.Debug(len(m.Data)) 87 err := m.Encrypt(&pk) 88 if err != nil { 89 t.Fatal(err) 90 } 91 logrus.Debug(len(m.Data)) 92 mm := types{Data: m.Data, MessageType: message_archive.MessageTypeSecret} 93 ok := mm.checkRequiredSize() 94 logrus.Debug(ok) 95 ok = mm.MaybeIsforMe(&pk) 96 if !ok { 97 t.Fatal(ok) 98 } 99 err = mm.Decrypt(&sk) 100 if err != nil { 101 t.Fatal(err) 102 } 103 logrus.Debug(len(mm.Data), mm.MessageType) 104 err = mm.Unmarshal() 105 if err != nil { 106 t.Fatal(err) 107 } 108 logrus.Debug(len(mm.Data)) 109 dkgMsg := mm.Message.(*p2p_message.MessageConsensusDkgDeal) 110 logrus.Debug(dkgMsg.Id, " ", string(dkgMsg.Data)) 111 logrus.Debug(mm.Message) 112 } 113 } 114 115 116 func TestCache(t *testing.T) { 117 config := DefaultHubConfig() 118 hub := &Hub{ 119 messageCache: gcache.New(config.MessageCacheMaxSize).LRU(). 120 Expiration(time.Second * time.Duration(config.MessageCacheExpirationSeconds)).Build(), 121 } 122 123 tx := archive.SampleTx() 124 msg := &p2p_message.MessageNewTx{ 125 RawTx: tx.RawTx(), 126 } 127 data, _ := msg.MarshalMsg(nil) 128 p2pM := &types{MessageType: message_archive.MessageTypeNewTx, Data: data, SourceID: "123", Message: msg} 129 p2pM.CalculateHash() 130 hub.cacheMessage(p2pM) 131 ids := hub.getMsgFromCache(message_archive.MessageTypeNewTx, *p2pM.Hash) 132 fmt.Println(ids) 133 p2pM = nil 134 ids = hub.getMsgFromCache(message_archive.MessageTypeNewTx, tx.GetHash()) 135 fmt.Println(ids) 136 }