snapshot_sync_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. package stream
  17. import (
  18. "context"
  19. "fmt"
  20. "os"
  21. "runtime"
  22. "sync"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/log"
  27. "github.com/ethereum/go-ethereum/node"
  28. "github.com/ethereum/go-ethereum/p2p"
  29. "github.com/ethereum/go-ethereum/p2p/enode"
  30. "github.com/ethereum/go-ethereum/p2p/simulations"
  31. "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
  32. "github.com/ethereum/go-ethereum/swarm/network"
  33. "github.com/ethereum/go-ethereum/swarm/network/simulation"
  34. "github.com/ethereum/go-ethereum/swarm/pot"
  35. "github.com/ethereum/go-ethereum/swarm/state"
  36. "github.com/ethereum/go-ethereum/swarm/storage"
  37. mockdb "github.com/ethereum/go-ethereum/swarm/storage/mock/db"
  38. "github.com/ethereum/go-ethereum/swarm/testutil"
  39. )
  40. const MaxTimeout = 600
  41. type synctestConfig struct {
  42. addrs [][]byte
  43. hashes []storage.Address
  44. idToChunksMap map[enode.ID][]int
  45. //chunksToNodesMap map[string][]int
  46. addrToIDMap map[string]enode.ID
  47. }
  48. const (
  49. // EventTypeNode is the type of event emitted when a node is either
  50. // created, started or stopped
  51. EventTypeChunkCreated simulations.EventType = "chunkCreated"
  52. EventTypeChunkOffered simulations.EventType = "chunkOffered"
  53. EventTypeChunkWanted simulations.EventType = "chunkWanted"
  54. EventTypeChunkDelivered simulations.EventType = "chunkDelivered"
  55. EventTypeChunkArrived simulations.EventType = "chunkArrived"
  56. EventTypeSimTerminated simulations.EventType = "simTerminated"
  57. )
  58. // Tests in this file should not request chunks from peers.
  59. // This function will panic indicating that there is a problem if request has been made.
  60. func dummyRequestFromPeers(_ context.Context, req *network.Request) (*enode.ID, chan struct{}, error) {
  61. panic(fmt.Sprintf("unexpected request: address %s, source %s", req.Addr.String(), req.Source.String()))
  62. }
  63. //This test is a syncing test for nodes.
  64. //One node is randomly selected to be the pivot node.
  65. //A configurable number of chunks and nodes can be
  66. //provided to the test, the number of chunks is uploaded
  67. //to the pivot node, and we check that nodes get the chunks
  68. //they are expected to store based on the syncing protocol.
  69. //Number of chunks and nodes can be provided via commandline too.
  70. func TestSyncingViaGlobalSync(t *testing.T) {
  71. if runtime.GOOS == "darwin" && os.Getenv("TRAVIS") == "true" {
  72. t.Skip("Flaky on mac on travis")
  73. }
  74. //if nodes/chunks have been provided via commandline,
  75. //run the tests with these values
  76. if *nodes != 0 && *chunks != 0 {
  77. log.Info(fmt.Sprintf("Running test with %d chunks and %d nodes...", *chunks, *nodes))
  78. testSyncingViaGlobalSync(t, *chunks, *nodes)
  79. } else {
  80. var nodeCnt []int
  81. var chnkCnt []int
  82. //if the `longrunning` flag has been provided
  83. //run more test combinations
  84. if *longrunning {
  85. chnkCnt = []int{1, 8, 32, 256, 1024}
  86. nodeCnt = []int{16, 32, 64, 128, 256}
  87. } else {
  88. //default test
  89. chnkCnt = []int{4, 32}
  90. nodeCnt = []int{32, 16}
  91. }
  92. for _, chnk := range chnkCnt {
  93. for _, n := range nodeCnt {
  94. log.Info(fmt.Sprintf("Long running test with %d chunks and %d nodes...", chnk, n))
  95. testSyncingViaGlobalSync(t, chnk, n)
  96. }
  97. }
  98. }
  99. }
  100. func TestSyncingViaDirectSubscribe(t *testing.T) {
  101. if runtime.GOOS == "darwin" && os.Getenv("TRAVIS") == "true" {
  102. t.Skip("Flaky on mac on travis")
  103. }
  104. //if nodes/chunks have been provided via commandline,
  105. //run the tests with these values
  106. if *nodes != 0 && *chunks != 0 {
  107. log.Info(fmt.Sprintf("Running test with %d chunks and %d nodes...", *chunks, *nodes))
  108. err := testSyncingViaDirectSubscribe(t, *chunks, *nodes)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. } else {
  113. var nodeCnt []int
  114. var chnkCnt []int
  115. //if the `longrunning` flag has been provided
  116. //run more test combinations
  117. if *longrunning {
  118. chnkCnt = []int{1, 8, 32, 256, 1024}
  119. nodeCnt = []int{32, 16}
  120. } else {
  121. //default test
  122. chnkCnt = []int{4, 32}
  123. nodeCnt = []int{32, 16}
  124. }
  125. for _, chnk := range chnkCnt {
  126. for _, n := range nodeCnt {
  127. log.Info(fmt.Sprintf("Long running test with %d chunks and %d nodes...", chnk, n))
  128. err := testSyncingViaDirectSubscribe(t, chnk, n)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. }
  133. }
  134. }
  135. }
  136. var simServiceMap = map[string]simulation.ServiceFunc{
  137. "streamer": streamerFunc,
  138. }
  139. func streamerFunc(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) {
  140. n := ctx.Config.Node()
  141. addr := network.NewAddr(n)
  142. store, datadir, err := createTestLocalStorageForID(n.ID(), addr)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. bucket.Store(bucketKeyStore, store)
  147. localStore := store.(*storage.LocalStore)
  148. netStore, err := storage.NewNetStore(localStore, nil)
  149. if err != nil {
  150. return nil, nil, err
  151. }
  152. kad := network.NewKademlia(addr.Over(), network.NewKadParams())
  153. delivery := NewDelivery(kad, netStore)
  154. netStore.NewNetFetcherFunc = network.NewFetcherFactory(dummyRequestFromPeers, true).New
  155. r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{
  156. Retrieval: RetrievalDisabled,
  157. Syncing: SyncingAutoSubscribe,
  158. SyncUpdateDelay: 3 * time.Second,
  159. })
  160. bucket.Store(bucketKeyRegistry, r)
  161. cleanup = func() {
  162. os.RemoveAll(datadir)
  163. netStore.Close()
  164. r.Close()
  165. }
  166. return r, cleanup, nil
  167. }
  168. func testSyncingViaGlobalSync(t *testing.T, chunkCount int, nodeCount int) {
  169. sim := simulation.New(simServiceMap)
  170. defer sim.Close()
  171. log.Info("Initializing test config")
  172. conf := &synctestConfig{}
  173. //map of discover ID to indexes of chunks expected at that ID
  174. conf.idToChunksMap = make(map[enode.ID][]int)
  175. //map of overlay address to discover ID
  176. conf.addrToIDMap = make(map[string]enode.ID)
  177. //array where the generated chunk hashes will be stored
  178. conf.hashes = make([]storage.Address, 0)
  179. err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. ctx, cancelSimRun := context.WithTimeout(context.Background(), 2*time.Minute)
  184. defer cancelSimRun()
  185. if _, err := sim.WaitTillHealthy(ctx, 2); err != nil {
  186. t.Fatal(err)
  187. }
  188. disconnections := sim.PeerEvents(
  189. context.Background(),
  190. sim.NodeIDs(),
  191. simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeDrop),
  192. )
  193. go func() {
  194. for d := range disconnections {
  195. log.Error("peer drop", "node", d.NodeID, "peer", d.Event.Peer)
  196. t.Fatal("unexpected disconnect")
  197. cancelSimRun()
  198. }
  199. }()
  200. result := runSim(conf, ctx, sim, chunkCount)
  201. if result.Error != nil {
  202. t.Fatal(result.Error)
  203. }
  204. log.Info("Simulation ended")
  205. }
  206. func runSim(conf *synctestConfig, ctx context.Context, sim *simulation.Simulation, chunkCount int) simulation.Result {
  207. return sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error {
  208. nodeIDs := sim.UpNodeIDs()
  209. for _, n := range nodeIDs {
  210. //get the kademlia overlay address from this ID
  211. a := n.Bytes()
  212. //append it to the array of all overlay addresses
  213. conf.addrs = append(conf.addrs, a)
  214. //the proximity calculation is on overlay addr,
  215. //the p2p/simulations check func triggers on enode.ID,
  216. //so we need to know which overlay addr maps to which nodeID
  217. conf.addrToIDMap[string(a)] = n
  218. }
  219. //get the node at that index
  220. //this is the node selected for upload
  221. node := sim.RandomUpNode()
  222. item, ok := sim.NodeItem(node.ID, bucketKeyStore)
  223. if !ok {
  224. return fmt.Errorf("No localstore")
  225. }
  226. lstore := item.(*storage.LocalStore)
  227. hashes, err := uploadFileToSingleNodeStore(node.ID, chunkCount, lstore)
  228. if err != nil {
  229. return err
  230. }
  231. for _, h := range hashes {
  232. evt := &simulations.Event{
  233. Type: EventTypeChunkCreated,
  234. Node: sim.Net.GetNode(node.ID),
  235. Data: h.String(),
  236. }
  237. sim.Net.Events().Send(evt)
  238. }
  239. conf.hashes = append(conf.hashes, hashes...)
  240. mapKeysToNodes(conf)
  241. // File retrieval check is repeated until all uploaded files are retrieved from all nodes
  242. // or until the timeout is reached.
  243. var gDir string
  244. var globalStore *mockdb.GlobalStore
  245. if *useMockStore {
  246. gDir, globalStore, err = createGlobalStore()
  247. if err != nil {
  248. return fmt.Errorf("Something went wrong; using mockStore enabled but globalStore is nil")
  249. }
  250. defer func() {
  251. os.RemoveAll(gDir)
  252. err := globalStore.Close()
  253. if err != nil {
  254. log.Error("Error closing global store! %v", "err", err)
  255. }
  256. }()
  257. }
  258. REPEAT:
  259. for {
  260. for _, id := range nodeIDs {
  261. //for each expected chunk, check if it is in the local store
  262. localChunks := conf.idToChunksMap[id]
  263. for _, ch := range localChunks {
  264. //get the real chunk by the index in the index array
  265. chunk := conf.hashes[ch]
  266. log.Trace(fmt.Sprintf("node has chunk: %s:", chunk))
  267. //check if the expected chunk is indeed in the localstore
  268. var err error
  269. if *useMockStore {
  270. //use the globalStore if the mockStore should be used; in that case,
  271. //the complete localStore stack is bypassed for getting the chunk
  272. _, err = globalStore.Get(common.BytesToAddress(id.Bytes()), chunk)
  273. } else {
  274. //use the actual localstore
  275. item, ok := sim.NodeItem(id, bucketKeyStore)
  276. if !ok {
  277. return fmt.Errorf("Error accessing localstore")
  278. }
  279. lstore := item.(*storage.LocalStore)
  280. _, err = lstore.Get(ctx, chunk)
  281. }
  282. if err != nil {
  283. log.Debug(fmt.Sprintf("Chunk %s NOT found for id %s", chunk, id))
  284. // Do not get crazy with logging the warn message
  285. time.Sleep(500 * time.Millisecond)
  286. continue REPEAT
  287. }
  288. evt := &simulations.Event{
  289. Type: EventTypeChunkArrived,
  290. Node: sim.Net.GetNode(id),
  291. Data: chunk.String(),
  292. }
  293. sim.Net.Events().Send(evt)
  294. log.Debug(fmt.Sprintf("Chunk %s IS FOUND for id %s", chunk, id))
  295. }
  296. }
  297. return nil
  298. }
  299. })
  300. }
  301. /*
  302. The test generates the given number of chunks
  303. For every chunk generated, the nearest node addresses
  304. are identified, we verify that the nodes closer to the
  305. chunk addresses actually do have the chunks in their local stores.
  306. The test loads a snapshot file to construct the swarm network,
  307. assuming that the snapshot file identifies a healthy
  308. kademlia network. The snapshot should have 'streamer' in its service list.
  309. */
  310. func testSyncingViaDirectSubscribe(t *testing.T, chunkCount int, nodeCount int) error {
  311. sim := simulation.New(map[string]simulation.ServiceFunc{
  312. "streamer": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) {
  313. n := ctx.Config.Node()
  314. addr := network.NewAddr(n)
  315. store, datadir, err := createTestLocalStorageForID(n.ID(), addr)
  316. if err != nil {
  317. return nil, nil, err
  318. }
  319. bucket.Store(bucketKeyStore, store)
  320. localStore := store.(*storage.LocalStore)
  321. netStore, err := storage.NewNetStore(localStore, nil)
  322. if err != nil {
  323. return nil, nil, err
  324. }
  325. kad := network.NewKademlia(addr.Over(), network.NewKadParams())
  326. delivery := NewDelivery(kad, netStore)
  327. netStore.NewNetFetcherFunc = network.NewFetcherFactory(dummyRequestFromPeers, true).New
  328. r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{
  329. Retrieval: RetrievalDisabled,
  330. Syncing: SyncingRegisterOnly,
  331. })
  332. bucket.Store(bucketKeyRegistry, r)
  333. fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams())
  334. bucket.Store(bucketKeyFileStore, fileStore)
  335. cleanup = func() {
  336. os.RemoveAll(datadir)
  337. netStore.Close()
  338. r.Close()
  339. }
  340. return r, cleanup, nil
  341. },
  342. })
  343. defer sim.Close()
  344. ctx, cancelSimRun := context.WithTimeout(context.Background(), 2*time.Minute)
  345. defer cancelSimRun()
  346. conf := &synctestConfig{}
  347. //map of discover ID to indexes of chunks expected at that ID
  348. conf.idToChunksMap = make(map[enode.ID][]int)
  349. //map of overlay address to discover ID
  350. conf.addrToIDMap = make(map[string]enode.ID)
  351. //array where the generated chunk hashes will be stored
  352. conf.hashes = make([]storage.Address, 0)
  353. err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
  354. if err != nil {
  355. return err
  356. }
  357. if _, err := sim.WaitTillHealthy(ctx, 2); err != nil {
  358. return err
  359. }
  360. disconnections := sim.PeerEvents(
  361. context.Background(),
  362. sim.NodeIDs(),
  363. simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeDrop),
  364. )
  365. go func() {
  366. for d := range disconnections {
  367. log.Error("peer drop", "node", d.NodeID, "peer", d.Event.Peer)
  368. t.Fatal("unexpected disconnect")
  369. cancelSimRun()
  370. }
  371. }()
  372. result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error {
  373. nodeIDs := sim.UpNodeIDs()
  374. for _, n := range nodeIDs {
  375. //get the kademlia overlay address from this ID
  376. a := n.Bytes()
  377. //append it to the array of all overlay addresses
  378. conf.addrs = append(conf.addrs, a)
  379. //the proximity calculation is on overlay addr,
  380. //the p2p/simulations check func triggers on enode.ID,
  381. //so we need to know which overlay addr maps to which nodeID
  382. conf.addrToIDMap[string(a)] = n
  383. }
  384. var subscriptionCount int
  385. filter := simulation.NewPeerEventsFilter().Type(p2p.PeerEventTypeMsgRecv).Protocol("stream").MsgCode(4)
  386. eventC := sim.PeerEvents(ctx, nodeIDs, filter)
  387. for j, node := range nodeIDs {
  388. log.Trace(fmt.Sprintf("Start syncing subscriptions: %d", j))
  389. //start syncing!
  390. item, ok := sim.NodeItem(node, bucketKeyRegistry)
  391. if !ok {
  392. return fmt.Errorf("No registry")
  393. }
  394. registry := item.(*Registry)
  395. var cnt int
  396. cnt, err = startSyncing(registry, conf)
  397. if err != nil {
  398. return err
  399. }
  400. //increment the number of subscriptions we need to wait for
  401. //by the count returned from startSyncing (SYNC subscriptions)
  402. subscriptionCount += cnt
  403. }
  404. for e := range eventC {
  405. if e.Error != nil {
  406. return e.Error
  407. }
  408. subscriptionCount--
  409. if subscriptionCount == 0 {
  410. break
  411. }
  412. }
  413. //select a random node for upload
  414. node := sim.RandomUpNode()
  415. item, ok := sim.NodeItem(node.ID, bucketKeyStore)
  416. if !ok {
  417. return fmt.Errorf("No localstore")
  418. }
  419. lstore := item.(*storage.LocalStore)
  420. hashes, err := uploadFileToSingleNodeStore(node.ID, chunkCount, lstore)
  421. if err != nil {
  422. return err
  423. }
  424. conf.hashes = append(conf.hashes, hashes...)
  425. mapKeysToNodes(conf)
  426. if _, err := sim.WaitTillHealthy(ctx, 2); err != nil {
  427. return err
  428. }
  429. var gDir string
  430. var globalStore *mockdb.GlobalStore
  431. if *useMockStore {
  432. gDir, globalStore, err = createGlobalStore()
  433. if err != nil {
  434. return fmt.Errorf("Something went wrong; using mockStore enabled but globalStore is nil")
  435. }
  436. defer os.RemoveAll(gDir)
  437. }
  438. // File retrieval check is repeated until all uploaded files are retrieved from all nodes
  439. // or until the timeout is reached.
  440. REPEAT:
  441. for {
  442. for _, id := range nodeIDs {
  443. //for each expected chunk, check if it is in the local store
  444. localChunks := conf.idToChunksMap[id]
  445. for _, ch := range localChunks {
  446. //get the real chunk by the index in the index array
  447. chunk := conf.hashes[ch]
  448. log.Trace(fmt.Sprintf("node has chunk: %s:", chunk))
  449. //check if the expected chunk is indeed in the localstore
  450. var err error
  451. if *useMockStore {
  452. //use the globalStore if the mockStore should be used; in that case,
  453. //the complete localStore stack is bypassed for getting the chunk
  454. _, err = globalStore.Get(common.BytesToAddress(id.Bytes()), chunk)
  455. } else {
  456. //use the actual localstore
  457. item, ok := sim.NodeItem(id, bucketKeyStore)
  458. if !ok {
  459. return fmt.Errorf("Error accessing localstore")
  460. }
  461. lstore := item.(*storage.LocalStore)
  462. _, err = lstore.Get(ctx, chunk)
  463. }
  464. if err != nil {
  465. log.Debug(fmt.Sprintf("Chunk %s NOT found for id %s", chunk, id))
  466. // Do not get crazy with logging the warn message
  467. time.Sleep(500 * time.Millisecond)
  468. continue REPEAT
  469. }
  470. log.Debug(fmt.Sprintf("Chunk %s IS FOUND for id %s", chunk, id))
  471. }
  472. }
  473. return nil
  474. }
  475. })
  476. if result.Error != nil {
  477. return result.Error
  478. }
  479. log.Info("Simulation ended")
  480. return nil
  481. }
  482. //the server func to start syncing
  483. //issues `RequestSubscriptionMsg` to peers, based on po, by iterating over
  484. //the kademlia's `EachBin` function.
  485. //returns the number of subscriptions requested
  486. func startSyncing(r *Registry, conf *synctestConfig) (int, error) {
  487. var err error
  488. kad := r.delivery.kad
  489. subCnt := 0
  490. //iterate over each bin and solicit needed subscription to bins
  491. kad.EachBin(r.addr[:], pof, 0, func(conn *network.Peer, po int) bool {
  492. //identify begin and start index of the bin(s) we want to subscribe to
  493. subCnt++
  494. err = r.RequestSubscription(conf.addrToIDMap[string(conn.Address())], NewStream("SYNC", FormatSyncBinKey(uint8(po)), true), NewRange(0, 0), High)
  495. if err != nil {
  496. log.Error(fmt.Sprintf("Error in RequestSubsciption! %v", err))
  497. return false
  498. }
  499. return true
  500. })
  501. return subCnt, nil
  502. }
  503. //map chunk keys to addresses which are responsible
  504. func mapKeysToNodes(conf *synctestConfig) {
  505. nodemap := make(map[string][]int)
  506. //build a pot for chunk hashes
  507. np := pot.NewPot(nil, 0)
  508. indexmap := make(map[string]int)
  509. for i, a := range conf.addrs {
  510. indexmap[string(a)] = i
  511. np, _, _ = pot.Add(np, a, pof)
  512. }
  513. var kadMinProxSize = 2
  514. ppmap := network.NewPeerPotMap(kadMinProxSize, conf.addrs)
  515. //for each address, run EachNeighbour on the chunk hashes pot to identify closest nodes
  516. log.Trace(fmt.Sprintf("Generated hash chunk(s): %v", conf.hashes))
  517. for i := 0; i < len(conf.hashes); i++ {
  518. var a []byte
  519. np.EachNeighbour([]byte(conf.hashes[i]), pof, func(val pot.Val, po int) bool {
  520. // take the first address
  521. a = val.([]byte)
  522. return false
  523. })
  524. nns := ppmap[common.Bytes2Hex(a)].NNSet
  525. nns = append(nns, a)
  526. for _, p := range nns {
  527. nodemap[string(p)] = append(nodemap[string(p)], i)
  528. }
  529. }
  530. for addr, chunks := range nodemap {
  531. //this selects which chunks are expected to be found with the given node
  532. conf.idToChunksMap[conf.addrToIDMap[addr]] = chunks
  533. }
  534. log.Debug(fmt.Sprintf("Map of expected chunks by ID: %v", conf.idToChunksMap))
  535. }
  536. //upload a file(chunks) to a single local node store
  537. func uploadFileToSingleNodeStore(id enode.ID, chunkCount int, lstore *storage.LocalStore) ([]storage.Address, error) {
  538. log.Debug(fmt.Sprintf("Uploading to node id: %s", id))
  539. fileStore := storage.NewFileStore(lstore, storage.NewFileStoreParams())
  540. size := chunkSize
  541. var rootAddrs []storage.Address
  542. for i := 0; i < chunkCount; i++ {
  543. rk, wait, err := fileStore.Store(context.TODO(), testutil.RandomReader(i, size), int64(size), false)
  544. if err != nil {
  545. return nil, err
  546. }
  547. err = wait(context.TODO())
  548. if err != nil {
  549. return nil, err
  550. }
  551. rootAddrs = append(rootAddrs, (rk))
  552. }
  553. return rootAddrs, nil
  554. }