github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/whisper/whisperv5/peer_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package whisperv5 18 19 import ( 20 "bytes" 21 "crypto/ecdsa" 22 "net" 23 "sync" 24 "testing" 25 "time" 26 27 "github.com/ebceco/ebc/common" 28 "github.com/ebceco/ebc/crypto" 29 "github.com/ebceco/ebc/p2p" 30 "github.com/ebceco/ebc/p2p/enode" 31 "github.com/ebceco/ebc/p2p/nat" 32 ) 33 34 var keys = []string{ 35 "d49dcf37238dc8a7aac57dc61b9fee68f0a97f062968978b9fafa7d1033d03a9", 36 "73fd6143c48e80ed3c56ea159fe7494a0b6b393a392227b422f4c3e8f1b54f98", 37 "119dd32adb1daa7a4c7bf77f847fb28730785aa92947edf42fdd997b54de40dc", 38 "deeda8709dea935bb772248a3144dea449ffcc13e8e5a1fd4ef20ce4e9c87837", 39 "5bd208a079633befa349441bdfdc4d85ba9bd56081525008380a63ac38a407cf", 40 "1d27fb4912002d58a2a42a50c97edb05c1b3dffc665dbaa42df1fe8d3d95c9b5", 41 "15def52800c9d6b8ca6f3066b7767a76afc7b611786c1276165fbc61636afb68", 42 "51be6ab4b2dc89f251ff2ace10f3c1cc65d6855f3e083f91f6ff8efdfd28b48c", 43 "ef1ef7441bf3c6419b162f05da6037474664f198b58db7315a6f4de52414b4a0", 44 "09bdf6985aabc696dc1fbeb5381aebd7a6421727343872eb2fadfc6d82486fd9", 45 "15d811bf2e01f99a224cdc91d0cf76cea08e8c67905c16fee9725c9be71185c4", 46 "2f83e45cf1baaea779789f755b7da72d8857aeebff19362dd9af31d3c9d14620", 47 "73f04e34ac6532b19c2aae8f8e52f38df1ac8f5cd10369f92325b9b0494b0590", 48 "1e2e07b69e5025537fb73770f483dc8d64f84ae3403775ef61cd36e3faf162c1", 49 "8963d9bbb3911aac6d30388c786756b1c423c4fbbc95d1f96ddbddf39809e43a", 50 "0422da85abc48249270b45d8de38a4cc3c02032ede1fcf0864a51092d58a2f1f", 51 "8ae5c15b0e8c7cade201fdc149831aa9b11ff626a7ffd27188886cc108ad0fa8", 52 "acd8f5a71d4aecfcb9ad00d32aa4bcf2a602939b6a9dd071bab443154184f805", 53 "a285a922125a7481600782ad69debfbcdb0316c1e97c267aff29ef50001ec045", 54 "28fd4eee78c6cd4bf78f39f8ab30c32c67c24a6223baa40e6f9c9a0e1de7cef5", 55 "c5cca0c9e6f043b288c6f1aef448ab59132dab3e453671af5d0752961f013fc7", 56 "46df99b051838cb6f8d1b73f232af516886bd8c4d0ee07af9a0a033c391380fd", 57 "c6a06a53cbaadbb432884f36155c8f3244e244881b5ee3e92e974cfa166d793f", 58 "783b90c75c63dc72e2f8d11b6f1b4de54d63825330ec76ee8db34f06b38ea211", 59 "9450038f10ca2c097a8013e5121b36b422b95b04892232f930a29292d9935611", 60 "e215e6246ed1cfdcf7310d4d8cdbe370f0d6a8371e4eb1089e2ae05c0e1bc10f", 61 "487110939ed9d64ebbc1f300adeab358bc58875faf4ca64990fbd7fe03b78f2b", 62 "824a70ea76ac81366da1d4f4ac39de851c8ac49dca456bb3f0a186ceefa269a5", 63 "ba8f34fa40945560d1006a328fe70c42e35cc3d1017e72d26864cd0d1b150f15", 64 "30a5dfcfd144997f428901ea88a43c8d176b19c79dde54cc58eea001aa3d246c", 65 "de59f7183aca39aa245ce66a05245fecfc7e2c75884184b52b27734a4a58efa2", 66 "92629e2ff5f0cb4f5f08fffe0f64492024d36f045b901efb271674b801095c5a", 67 "7184c1701569e3a4c4d2ddce691edd983b81e42e09196d332e1ae2f1e062cff4", 68 } 69 70 const NumNodes = 16 // must not exceed the number of keys (32) 71 72 type TestData struct { 73 counter [NumNodes]int 74 mutex sync.RWMutex 75 } 76 77 type TestNode struct { 78 shh *Whisper 79 id *ecdsa.PrivateKey 80 server *p2p.Server 81 filerId string 82 } 83 84 var result TestData 85 var nodes [NumNodes]*TestNode 86 var sharedKey = []byte("some arbitrary data here") 87 var sharedTopic TopicType = TopicType{0xF, 0x1, 0x2, 0} 88 var expectedMessage = []byte("per rectum ad astra") 89 90 // This test does the following: 91 // 1. creates a chain of whisper nodes, 92 // 2. installs the filters with shared (predefined) parameters, 93 // 3. each node sends a number of random (undecryptable) messages, 94 // 4. first node sends one expected (decryptable) message, 95 // 5. checks if each node have received and decrypted exactly one message. 96 func TestSimulation(t *testing.T) { 97 initialize(t) 98 99 for i := 0; i < NumNodes; i++ { 100 sendMsg(t, false, i) 101 } 102 103 sendMsg(t, true, 0) 104 checkPropagation(t) 105 stopServers() 106 } 107 108 func initialize(t *testing.T) { 109 var err error 110 111 for i := 0; i < NumNodes; i++ { 112 var node TestNode 113 node.shh = New(&DefaultConfig) 114 node.shh.SetMinimumPoW(0.00000001) 115 node.shh.Start(nil) 116 topics := make([]TopicType, 0) 117 topics = append(topics, sharedTopic) 118 f := Filter{KeySym: sharedKey} 119 f.Topics = [][]byte{topics[0][:]} 120 node.filerId, err = node.shh.Subscribe(&f) 121 if err != nil { 122 t.Fatalf("failed to install the filter: %s.", err) 123 } 124 node.id, err = crypto.HexToECDSA(keys[i]) 125 if err != nil { 126 t.Fatalf("failed convert the key: %s.", keys[i]) 127 } 128 name := common.MakeName("whisper-go", "2.0") 129 node.server = &p2p.Server{ 130 Config: p2p.Config{ 131 PrivateKey: node.id, 132 MaxPeers: NumNodes/2 + 1, 133 Name: name, 134 Protocols: node.shh.Protocols(), 135 ListenAddr: "127.0.0.1:0", 136 NAT: nat.Any(), 137 }, 138 } 139 140 err = node.server.Start() 141 if err != nil { 142 t.Fatalf("failed to start server %d. err: %v", i, err) 143 } 144 145 for j := 0; j < i; j++ { 146 peerNodeId := nodes[j].id 147 address, _ := net.ResolveTCPAddr("tcp", nodes[j].server.ListenAddr) 148 peerPort := uint16(address.Port) 149 peer := enode.NewV4(&peerNodeId.PublicKey, address.IP, int(peerPort), int(peerPort)) 150 node.server.AddPeer(peer) 151 } 152 153 nodes[i] = &node 154 } 155 } 156 157 func stopServers() { 158 for i := 0; i < NumNodes; i++ { 159 n := nodes[i] 160 if n != nil { 161 n.shh.Unsubscribe(n.filerId) 162 n.shh.Stop() 163 n.server.Stop() 164 } 165 } 166 } 167 168 func checkPropagation(t *testing.T) { 169 if t.Failed() { 170 return 171 } 172 173 const cycle = 100 174 const iterations = 100 175 176 for j := 0; j < iterations; j++ { 177 time.Sleep(cycle * time.Millisecond) 178 179 for i := 0; i < NumNodes; i++ { 180 f := nodes[i].shh.GetFilter(nodes[i].filerId) 181 if f == nil { 182 t.Fatalf("failed to get filterId %s from node %d.", nodes[i].filerId, i) 183 } 184 185 mail := f.Retrieve() 186 if !validateMail(t, i, mail) { 187 return 188 } 189 190 if isTestComplete() { 191 return 192 } 193 } 194 } 195 196 t.Fatalf("Test was not complete: timeout %d seconds.", iterations*cycle/1000) 197 } 198 199 func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool { 200 var cnt int 201 for _, m := range mail { 202 if bytes.Equal(m.Payload, expectedMessage) { 203 cnt++ 204 } 205 } 206 207 if cnt == 0 { 208 // no messages received yet: nothing is wrong 209 return true 210 } 211 if cnt > 1 { 212 t.Fatalf("node %d received %d.", index, cnt) 213 return false 214 } 215 216 if cnt > 0 { 217 result.mutex.Lock() 218 defer result.mutex.Unlock() 219 result.counter[index] += cnt 220 if result.counter[index] > 1 { 221 t.Fatalf("node %d accumulated %d.", index, result.counter[index]) 222 } 223 } 224 return true 225 } 226 227 func isTestComplete() bool { 228 result.mutex.RLock() 229 defer result.mutex.RUnlock() 230 231 for i := 0; i < NumNodes; i++ { 232 if result.counter[i] < 1 { 233 return false 234 } 235 } 236 237 for i := 0; i < NumNodes; i++ { 238 envelopes := nodes[i].shh.Envelopes() 239 if len(envelopes) < 2 { 240 return false 241 } 242 } 243 244 return true 245 } 246 247 func sendMsg(t *testing.T, expected bool, id int) { 248 if t.Failed() { 249 return 250 } 251 252 opt := MessageParams{KeySym: sharedKey, Topic: sharedTopic, Payload: expectedMessage, PoW: 0.00000001, WorkTime: 1} 253 if !expected { 254 opt.KeySym[0]++ 255 opt.Topic[0]++ 256 opt.Payload = opt.Payload[1:] 257 } 258 259 msg, err := NewSentMessage(&opt) 260 if err != nil { 261 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 262 } 263 envelope, err := msg.Wrap(&opt) 264 if err != nil { 265 t.Fatalf("failed to seal message: %s", err) 266 } 267 268 err = nodes[id].shh.Send(envelope) 269 if err != nil { 270 t.Fatalf("failed to send message: %s", err) 271 } 272 } 273 274 func TestPeerBasic(t *testing.T) { 275 InitSingleTest() 276 277 params, err := generateMessageParams() 278 if err != nil { 279 t.Fatalf("failed generateMessageParams with seed %d.", seed) 280 } 281 282 params.PoW = 0.001 283 msg, err := NewSentMessage(params) 284 if err != nil { 285 t.Fatalf("failed to create new message with seed %d: %s.", seed, err) 286 } 287 env, err := msg.Wrap(params) 288 if err != nil { 289 t.Fatalf("failed Wrap with seed %d.", seed) 290 } 291 292 p := newPeer(nil, nil, nil) 293 p.mark(env) 294 if !p.marked(env) { 295 t.Fatalf("failed mark with seed %d.", seed) 296 } 297 }