github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/network/stream/intervals_test.go (about) 1 // Copyright 2018 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 stream 18 19 import ( 20 "context" 21 crand "crypto/rand" 22 "encoding/binary" 23 "fmt" 24 "io" 25 "os" 26 "sync" 27 "testing" 28 "time" 29 30 "github.com/FusionFoundation/efsn/log" 31 "github.com/FusionFoundation/efsn/node" 32 "github.com/FusionFoundation/efsn/p2p" 33 "github.com/FusionFoundation/efsn/p2p/discover" 34 "github.com/FusionFoundation/efsn/p2p/simulations/adapters" 35 "github.com/FusionFoundation/efsn/swarm/network" 36 "github.com/FusionFoundation/efsn/swarm/network/simulation" 37 "github.com/FusionFoundation/efsn/swarm/state" 38 "github.com/FusionFoundation/efsn/swarm/storage" 39 ) 40 41 func TestIntervalsLive(t *testing.T) { 42 testIntervals(t, true, nil, false) 43 testIntervals(t, true, nil, true) 44 } 45 46 func TestIntervalsHistory(t *testing.T) { 47 testIntervals(t, false, NewRange(9, 26), false) 48 testIntervals(t, false, NewRange(9, 26), true) 49 } 50 51 func TestIntervalsLiveAndHistory(t *testing.T) { 52 testIntervals(t, true, NewRange(9, 26), false) 53 testIntervals(t, true, NewRange(9, 26), true) 54 } 55 56 func testIntervals(t *testing.T, live bool, history *Range, skipCheck bool) { 57 nodes := 2 58 chunkCount := dataChunkCount 59 externalStreamName := "externalStream" 60 externalStreamSessionAt := uint64(50) 61 externalStreamMaxKeys := uint64(100) 62 63 sim := simulation.New(map[string]simulation.ServiceFunc{ 64 "intervalsStreamer": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) { 65 66 id := ctx.Config.ID 67 addr := network.NewAddrFromNodeID(id) 68 store, datadir, err := createTestLocalStorageForID(id, addr) 69 if err != nil { 70 return nil, nil, err 71 } 72 bucket.Store(bucketKeyStore, store) 73 cleanup = func() { 74 store.Close() 75 os.RemoveAll(datadir) 76 } 77 localStore := store.(*storage.LocalStore) 78 netStore, err := storage.NewNetStore(localStore, nil) 79 if err != nil { 80 return nil, nil, err 81 } 82 kad := network.NewKademlia(addr.Over(), network.NewKadParams()) 83 delivery := NewDelivery(kad, netStore) 84 netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, true).New 85 86 r := NewRegistry(addr, delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{ 87 SkipCheck: skipCheck, 88 }) 89 bucket.Store(bucketKeyRegistry, r) 90 91 r.RegisterClientFunc(externalStreamName, func(p *Peer, t string, live bool) (Client, error) { 92 return newTestExternalClient(netStore), nil 93 }) 94 r.RegisterServerFunc(externalStreamName, func(p *Peer, t string, live bool) (Server, error) { 95 return newTestExternalServer(t, externalStreamSessionAt, externalStreamMaxKeys, nil), nil 96 }) 97 98 fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams()) 99 bucket.Store(bucketKeyFileStore, fileStore) 100 101 return r, cleanup, nil 102 103 }, 104 }) 105 defer sim.Close() 106 107 log.Info("Adding nodes to simulation") 108 _, err := sim.AddNodesAndConnectChain(nodes) 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) 114 defer cancel() 115 116 if _, err := sim.WaitTillHealthy(ctx, 2); err != nil { 117 t.Fatal(err) 118 } 119 120 result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error { 121 nodeIDs := sim.UpNodeIDs() 122 storer := nodeIDs[0] 123 checker := nodeIDs[1] 124 125 item, ok := sim.NodeItem(storer, bucketKeyFileStore) 126 if !ok { 127 return fmt.Errorf("No filestore") 128 } 129 fileStore := item.(*storage.FileStore) 130 131 size := chunkCount * chunkSize 132 _, wait, err := fileStore.Store(ctx, io.LimitReader(crand.Reader, int64(size)), int64(size), false) 133 if err != nil { 134 log.Error("Store error: %v", "err", err) 135 t.Fatal(err) 136 } 137 err = wait(ctx) 138 if err != nil { 139 log.Error("Wait error: %v", "err", err) 140 t.Fatal(err) 141 } 142 143 item, ok = sim.NodeItem(checker, bucketKeyRegistry) 144 if !ok { 145 return fmt.Errorf("No registry") 146 } 147 registry := item.(*Registry) 148 149 liveErrC := make(chan error) 150 historyErrC := make(chan error) 151 152 log.Debug("Watching for disconnections") 153 disconnections := sim.PeerEvents( 154 context.Background(), 155 sim.NodeIDs(), 156 simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeDrop), 157 ) 158 159 err = registry.Subscribe(storer, NewStream(externalStreamName, "", live), history, Top) 160 if err != nil { 161 return err 162 } 163 164 go func() { 165 for d := range disconnections { 166 if d.Error != nil { 167 log.Error("peer drop", "node", d.NodeID, "peer", d.Event.Peer) 168 t.Fatal(d.Error) 169 } 170 } 171 }() 172 173 go func() { 174 if !live { 175 close(liveErrC) 176 return 177 } 178 179 var err error 180 defer func() { 181 liveErrC <- err 182 }() 183 184 // live stream 185 var liveHashesChan chan []byte 186 liveHashesChan, err = getHashes(ctx, registry, storer, NewStream(externalStreamName, "", true)) 187 if err != nil { 188 log.Error("get hashes", "err", err) 189 return 190 } 191 i := externalStreamSessionAt 192 193 // we have subscribed, enable notifications 194 err = enableNotifications(registry, storer, NewStream(externalStreamName, "", true)) 195 if err != nil { 196 return 197 } 198 199 for { 200 select { 201 case hash := <-liveHashesChan: 202 h := binary.BigEndian.Uint64(hash) 203 if h != i { 204 err = fmt.Errorf("expected live hash %d, got %d", i, h) 205 return 206 } 207 i++ 208 if i > externalStreamMaxKeys { 209 return 210 } 211 case <-ctx.Done(): 212 return 213 } 214 } 215 }() 216 217 go func() { 218 if live && history == nil { 219 close(historyErrC) 220 return 221 } 222 223 var err error 224 defer func() { 225 historyErrC <- err 226 }() 227 228 // history stream 229 var historyHashesChan chan []byte 230 historyHashesChan, err = getHashes(ctx, registry, storer, NewStream(externalStreamName, "", false)) 231 if err != nil { 232 log.Error("get hashes", "err", err) 233 return 234 } 235 236 var i uint64 237 historyTo := externalStreamMaxKeys 238 if history != nil { 239 i = history.From 240 if history.To != 0 { 241 historyTo = history.To 242 } 243 } 244 245 // we have subscribed, enable notifications 246 err = enableNotifications(registry, storer, NewStream(externalStreamName, "", false)) 247 if err != nil { 248 return 249 } 250 251 for { 252 select { 253 case hash := <-historyHashesChan: 254 h := binary.BigEndian.Uint64(hash) 255 if h != i { 256 err = fmt.Errorf("expected history hash %d, got %d", i, h) 257 return 258 } 259 i++ 260 if i > historyTo { 261 return 262 } 263 case <-ctx.Done(): 264 return 265 } 266 } 267 }() 268 269 if err := <-liveErrC; err != nil { 270 return err 271 } 272 if err := <-historyErrC; err != nil { 273 return err 274 } 275 276 return nil 277 }) 278 279 if result.Error != nil { 280 t.Fatal(result.Error) 281 } 282 } 283 284 func getHashes(ctx context.Context, r *Registry, peerID discover.NodeID, s Stream) (chan []byte, error) { 285 peer := r.getPeer(peerID) 286 287 client, err := peer.getClient(ctx, s) 288 if err != nil { 289 return nil, err 290 } 291 292 c := client.Client.(*testExternalClient) 293 294 return c.hashes, nil 295 } 296 297 func enableNotifications(r *Registry, peerID discover.NodeID, s Stream) error { 298 peer := r.getPeer(peerID) 299 300 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 301 defer cancel() 302 303 client, err := peer.getClient(ctx, s) 304 if err != nil { 305 return err 306 } 307 308 close(client.Client.(*testExternalClient).enableNotificationsC) 309 310 return nil 311 } 312 313 type testExternalClient struct { 314 hashes chan []byte 315 store storage.SyncChunkStore 316 enableNotificationsC chan struct{} 317 } 318 319 func newTestExternalClient(store storage.SyncChunkStore) *testExternalClient { 320 return &testExternalClient{ 321 hashes: make(chan []byte), 322 store: store, 323 enableNotificationsC: make(chan struct{}), 324 } 325 } 326 327 func (c *testExternalClient) NeedData(ctx context.Context, hash []byte) func(context.Context) error { 328 wait := c.store.FetchFunc(ctx, storage.Address(hash)) 329 if wait == nil { 330 return nil 331 } 332 select { 333 case c.hashes <- hash: 334 case <-ctx.Done(): 335 log.Warn("testExternalClient NeedData context", "err", ctx.Err()) 336 return func(_ context.Context) error { 337 return ctx.Err() 338 } 339 } 340 return wait 341 } 342 343 func (c *testExternalClient) BatchDone(Stream, uint64, []byte, []byte) func() (*TakeoverProof, error) { 344 return nil 345 } 346 347 func (c *testExternalClient) Close() {} 348 349 const testExternalServerBatchSize = 10 350 351 type testExternalServer struct { 352 t string 353 keyFunc func(key []byte, index uint64) 354 sessionAt uint64 355 maxKeys uint64 356 } 357 358 func newTestExternalServer(t string, sessionAt, maxKeys uint64, keyFunc func(key []byte, index uint64)) *testExternalServer { 359 if keyFunc == nil { 360 keyFunc = binary.BigEndian.PutUint64 361 } 362 return &testExternalServer{ 363 t: t, 364 keyFunc: keyFunc, 365 sessionAt: sessionAt, 366 maxKeys: maxKeys, 367 } 368 } 369 370 func (s *testExternalServer) SetNextBatch(from uint64, to uint64) ([]byte, uint64, uint64, *HandoverProof, error) { 371 if from == 0 && to == 0 { 372 from = s.sessionAt 373 to = s.sessionAt + testExternalServerBatchSize 374 } 375 if to-from > testExternalServerBatchSize { 376 to = from + testExternalServerBatchSize - 1 377 } 378 if from >= s.maxKeys && to > s.maxKeys { 379 return nil, 0, 0, nil, io.EOF 380 } 381 if to > s.maxKeys { 382 to = s.maxKeys 383 } 384 b := make([]byte, HashSize*(to-from+1)) 385 for i := from; i <= to; i++ { 386 s.keyFunc(b[(i-from)*HashSize:(i-from+1)*HashSize], i) 387 } 388 return b, from, to, nil, nil 389 } 390 391 func (s *testExternalServer) GetData(context.Context, []byte) ([]byte, error) { 392 return make([]byte, 4096), nil 393 } 394 395 func (s *testExternalServer) Close() {}