github.com/vccomnet/occchain@v0.0.0-20181129092339-c57d4bab23fb/swarm/network/stream/syncer_test.go (about) 1 // Copyright 2018 The go-blockchain Authors 2 // This file is part of the go-blockchain library. 3 // 4 // The go-blockchain 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-blockchain 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-blockchain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package stream 18 19 import ( 20 "context" 21 crand "crypto/rand" 22 "fmt" 23 "io" 24 "io/ioutil" 25 "math" 26 "os" 27 "sync" 28 "testing" 29 "time" 30 31 "github.com/blockchain/go-blockchain/common" 32 "github.com/blockchain/go-blockchain/node" 33 "github.com/blockchain/go-blockchain/p2p" 34 "github.com/blockchain/go-blockchain/p2p/enode" 35 "github.com/blockchain/go-blockchain/p2p/simulations/adapters" 36 "github.com/blockchain/go-blockchain/swarm/log" 37 "github.com/blockchain/go-blockchain/swarm/network" 38 "github.com/blockchain/go-blockchain/swarm/network/simulation" 39 "github.com/blockchain/go-blockchain/swarm/state" 40 "github.com/blockchain/go-blockchain/swarm/storage" 41 mockdb "github.com/blockchain/go-blockchain/swarm/storage/mock/db" 42 ) 43 44 const dataChunkCount = 200 45 46 func TestSyncerSimulation(t *testing.T) { 47 testSyncBetweenNodes(t, 2, 1, dataChunkCount, true, 1) 48 testSyncBetweenNodes(t, 4, 1, dataChunkCount, true, 1) 49 testSyncBetweenNodes(t, 8, 1, dataChunkCount, true, 1) 50 testSyncBetweenNodes(t, 16, 1, dataChunkCount, true, 1) 51 } 52 53 func createMockStore(globalStore *mockdb.GlobalStore, id enode.ID, addr *network.BzzAddr) (lstore storage.ChunkStore, datadir string, err error) { 54 address := common.BytesToAddress(id.Bytes()) 55 mockStore := globalStore.NewNodeStore(address) 56 params := storage.NewDefaultLocalStoreParams() 57 58 datadir, err = ioutil.TempDir("", "localMockStore-"+id.TerminalString()) 59 if err != nil { 60 return nil, "", err 61 } 62 params.Init(datadir) 63 params.BaseKey = addr.Over() 64 lstore, err = storage.NewLocalStore(params, mockStore) 65 if err != nil { 66 return nil, "", err 67 } 68 return lstore, datadir, nil 69 } 70 71 func testSyncBetweenNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck bool, po uint8) { 72 sim := simulation.New(map[string]simulation.ServiceFunc{ 73 "streamer": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) { 74 var store storage.ChunkStore 75 var globalStore *mockdb.GlobalStore 76 var gDir, datadir string 77 78 node := ctx.Config.Node() 79 addr := network.NewAddr(node) 80 //hack to put addresses in same space 81 addr.OAddr[0] = byte(0) 82 83 if *useMockStore { 84 gDir, globalStore, err = createGlobalStore() 85 if err != nil { 86 return nil, nil, fmt.Errorf("Something went wrong; using mockStore enabled but globalStore is nil") 87 } 88 store, datadir, err = createMockStore(globalStore, node.ID(), addr) 89 } else { 90 store, datadir, err = createTestLocalStorageForID(node.ID(), addr) 91 } 92 if err != nil { 93 return nil, nil, err 94 } 95 bucket.Store(bucketKeyStore, store) 96 cleanup = func() { 97 store.Close() 98 os.RemoveAll(datadir) 99 if *useMockStore { 100 err := globalStore.Close() 101 if err != nil { 102 log.Error("Error closing global store! %v", "err", err) 103 } 104 os.RemoveAll(gDir) 105 } 106 } 107 localStore := store.(*storage.LocalStore) 108 netStore, err := storage.NewNetStore(localStore, nil) 109 if err != nil { 110 return nil, nil, err 111 } 112 bucket.Store(bucketKeyDB, netStore) 113 kad := network.NewKademlia(addr.Over(), network.NewKadParams()) 114 delivery := NewDelivery(kad, netStore) 115 netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, true).New 116 117 bucket.Store(bucketKeyDelivery, delivery) 118 119 r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{ 120 Retrieval: RetrievalDisabled, 121 Syncing: SyncingAutoSubscribe, 122 SkipCheck: skipCheck, 123 }) 124 125 fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams()) 126 bucket.Store(bucketKeyFileStore, fileStore) 127 128 return r, cleanup, nil 129 130 }, 131 }) 132 defer sim.Close() 133 134 // create context for simulation run 135 timeout := 30 * time.Second 136 ctx, cancel := context.WithTimeout(context.Background(), timeout) 137 // defer cancel should come before defer simulation teardown 138 defer cancel() 139 140 _, err := sim.AddNodesAndConnectChain(nodes) 141 if err != nil { 142 t.Fatal(err) 143 } 144 result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error { 145 nodeIDs := sim.UpNodeIDs() 146 147 nodeIndex := make(map[enode.ID]int) 148 for i, id := range nodeIDs { 149 nodeIndex[id] = i 150 } 151 152 disconnections := sim.PeerEvents( 153 context.Background(), 154 sim.NodeIDs(), 155 simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeDrop), 156 ) 157 158 go func() { 159 for d := range disconnections { 160 if d.Error != nil { 161 log.Error("peer drop", "node", d.NodeID, "peer", d.Event.Peer) 162 t.Fatal(d.Error) 163 } 164 } 165 }() 166 167 // each node Subscribes to each other's swarmChunkServerStreamName 168 for j := 0; j < nodes-1; j++ { 169 id := nodeIDs[j] 170 client, err := sim.Net.GetNode(id).Client() 171 if err != nil { 172 t.Fatal(err) 173 } 174 sid := nodeIDs[j+1] 175 client.CallContext(ctx, nil, "stream_subscribeStream", sid, NewStream("SYNC", FormatSyncBinKey(1), false), NewRange(0, 0), Top) 176 if err != nil { 177 return err 178 } 179 if j > 0 || nodes == 2 { 180 item, ok := sim.NodeItem(nodeIDs[j], bucketKeyFileStore) 181 if !ok { 182 return fmt.Errorf("No filestore") 183 } 184 fileStore := item.(*storage.FileStore) 185 size := chunkCount * chunkSize 186 _, wait, err := fileStore.Store(ctx, io.LimitReader(crand.Reader, int64(size)), int64(size), false) 187 if err != nil { 188 t.Fatal(err.Error()) 189 } 190 wait(ctx) 191 } 192 } 193 // here we distribute chunks of a random file into stores 1...nodes 194 if _, err := sim.WaitTillHealthy(ctx, 2); err != nil { 195 return err 196 } 197 198 // collect hashes in po 1 bin for each node 199 hashes := make([][]storage.Address, nodes) 200 totalHashes := 0 201 hashCounts := make([]int, nodes) 202 for i := nodes - 1; i >= 0; i-- { 203 if i < nodes-1 { 204 hashCounts[i] = hashCounts[i+1] 205 } 206 item, ok := sim.NodeItem(nodeIDs[i], bucketKeyDB) 207 if !ok { 208 return fmt.Errorf("No DB") 209 } 210 netStore := item.(*storage.NetStore) 211 netStore.Iterator(0, math.MaxUint64, po, func(addr storage.Address, index uint64) bool { 212 hashes[i] = append(hashes[i], addr) 213 totalHashes++ 214 hashCounts[i]++ 215 return true 216 }) 217 } 218 var total, found int 219 for _, node := range nodeIDs { 220 i := nodeIndex[node] 221 222 for j := i; j < nodes; j++ { 223 total += len(hashes[j]) 224 for _, key := range hashes[j] { 225 item, ok := sim.NodeItem(nodeIDs[j], bucketKeyDB) 226 if !ok { 227 return fmt.Errorf("No DB") 228 } 229 db := item.(*storage.NetStore) 230 _, err := db.Get(ctx, key) 231 if err == nil { 232 found++ 233 } 234 } 235 } 236 log.Debug("sync check", "node", node, "index", i, "bin", po, "found", found, "total", total) 237 } 238 if total == found && total > 0 { 239 return nil 240 } 241 return fmt.Errorf("Total not equallying found: total is %d", total) 242 }) 243 244 if result.Error != nil { 245 t.Fatal(result.Error) 246 } 247 }