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