From 4ce3f6d3da95c86aa85a6f113f967e13adf645b8 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 8 Apr 2021 15:05:04 +0200 Subject: [PATCH] Add nitro wallet to BitswapEngine --- dagger/bitswap.nim | 2 ++ dagger/bitswap/engine.nim | 3 +++ tests/dagger/bitswap/testbitswap.nim | 7 +++++-- tests/dagger/bitswap/testengine.nim | 14 +++++++++----- tests/dagger/bitswap/utils.nim | 7 +++++-- tests/dagger/examples.nim | 5 ++++- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/dagger/bitswap.nim b/dagger/bitswap.nim index e22909b1..31d8316f 100644 --- a/dagger/bitswap.nim +++ b/dagger/bitswap.nim @@ -104,6 +104,7 @@ method putBlocks*(b: Bitswap, blocks: seq[bt.Block]) = proc new*( T: type Bitswap, localStore: BlockStore, + wallet: Wallet, network: BitswapNetwork, concurrentTasks = DefaultConcurrentTasks, maxRetries = DefaultMaxRetries, @@ -111,6 +112,7 @@ proc new*( let engine = BitswapEngine.new( localStore = localStore, + wallet = wallet, peersPerRequest = peersPerRequest, request = network.request, ) diff --git a/dagger/bitswap/engine.nim b/dagger/bitswap/engine.nim index 22044aad..36f17b25 100644 --- a/dagger/bitswap/engine.nim +++ b/dagger/bitswap/engine.nim @@ -52,6 +52,7 @@ type peersPerRequest: int # max number of peers to request from scheduleTask*: TaskScheduler # schedule a new task with the task runner request*: BitswapRequest # bitswap network requests + wallet*: Wallet # nitro wallet for micropayments pricing*: ?Pricing # optional bandwidth pricing proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool = @@ -341,6 +342,7 @@ proc taskHandler*(b: BitswapEngine, task: BitswapPeerCtx) {.gcsafe, async.} = proc new*( T: type BitswapEngine, localStore: BlockStore, + wallet: Wallet, request: BitswapRequest = BitswapRequest(), scheduleTask: TaskScheduler = nil, peersPerRequest = DefaultMaxPeersPerRequest): T = @@ -355,6 +357,7 @@ proc new*( peersPerRequest: peersPerRequest, scheduleTask: taskScheduler, request: request, + wallet: wallet ) return b diff --git a/tests/dagger/bitswap/testbitswap.nim b/tests/dagger/bitswap/testbitswap.nim index f44e48cb..b04d6bb8 100644 --- a/tests/dagger/bitswap/testbitswap.nim +++ b/tests/dagger/bitswap/testbitswap.nim @@ -28,6 +28,7 @@ suite "Bitswap engine - 2 nodes": var switch1, switch2: Switch + wallet1, wallet2: Wallet pricing1, pricing2: Pricing network1, network2: BitswapNetwork bitswap1, bitswap2: Bitswap @@ -41,6 +42,8 @@ suite "Bitswap engine - 2 nodes": switch1 = newStandardSwitch() switch2 = newStandardSwitch() + wallet1 = Wallet.example + wallet2 = Wallet.example pricing1 = Pricing.example pricing2 = Pricing.example awaiters.add(await switch1.start()) @@ -50,11 +53,11 @@ suite "Bitswap engine - 2 nodes": peerId2 = switch2.peerInfo.peerId network1 = BitswapNetwork.new(switch = switch1) - bitswap1 = Bitswap.new(MemoryStore.new(blocks1), network1) + bitswap1 = Bitswap.new(MemoryStore.new(blocks1), wallet1, network1) switch1.mount(network1) network2 = BitswapNetwork.new(switch = switch2) - bitswap2 = Bitswap.new(MemoryStore.new(blocks2), network2) + bitswap2 = Bitswap.new(MemoryStore.new(blocks2), wallet2, network2) switch2.mount(network2) await allFuturesThrowing( diff --git a/tests/dagger/bitswap/testengine.nim b/tests/dagger/bitswap/testengine.nim index 0e865f2a..2a9b143c 100644 --- a/tests/dagger/bitswap/testengine.nim +++ b/tests/dagger/bitswap/testengine.nim @@ -23,6 +23,7 @@ suite "Bitswap engine basic": peerId = PeerID.init(seckey.getKey().tryGet()).tryGet() chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256) blocks = chunker.mapIt( bt.Block.new(it) ) + wallet = Wallet.example var done: Future[void] @@ -47,7 +48,7 @@ suite "Bitswap engine basic": sendWantList: sendWantList, ) - let engine = BitswapEngine.new(MemoryStore.new(blocks), request) + let engine = BitswapEngine.new(MemoryStore.new(blocks), wallet, request) engine.wantList = blocks.mapIt( it.cid ) engine.setupPeer(peerId) @@ -61,7 +62,7 @@ suite "Bitswap engine basic": done.complete() let request = BitswapRequest(sendPricing: sendPricing) - let engine = BitswapEngine.new(MemoryStore.new, request) + let engine = BitswapEngine.new(MemoryStore.new, wallet, request) engine.pricing = pricing.some engine.setupPeer(peerId) @@ -75,6 +76,7 @@ suite "Bitswap engine handlers": peerId = PeerID.init(seckey.getKey().tryGet()).tryGet() chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256) blocks = chunker.mapIt( bt.Block.new(it) ) + wallet = Wallet.example var engine: BitswapEngine @@ -83,7 +85,7 @@ suite "Bitswap engine handlers": setup: done = newFuture[void]() - engine = BitswapEngine.new(MemoryStore.new()) + engine = BitswapEngine.new(MemoryStore.new(), wallet) peerCtx = BitswapPeerCtx( id: peerId ) @@ -164,6 +166,7 @@ suite "Bitswap engine blocks": rng = Rng.instance() chunker = newRandomChunker(Rng.instance(), size = 2048, chunkSize = 256) blocks = chunker.mapIt( bt.Block.new(it) ) + wallet = Wallet.example var engine: BitswapEngine @@ -173,7 +176,7 @@ suite "Bitswap engine blocks": setup: done = newFuture[void]() - engine = BitswapEngine.new(MemoryStore.new()) + engine = BitswapEngine.new(MemoryStore.new(), wallet) peersCtx = @[] for i in 0..3: @@ -256,6 +259,7 @@ suite "Task Handler": rng = Rng.instance() chunker = newRandomChunker(Rng.instance(), size = 2048, chunkSize = 256) blocks = chunker.mapIt( bt.Block.new(it) ) + wallet = Wallet.example var engine: BitswapEngine @@ -265,7 +269,7 @@ suite "Task Handler": setup: done = newFuture[void]() - engine = BitswapEngine.new(MemoryStore.new()) + engine = BitswapEngine.new(MemoryStore.new(), wallet) peersCtx = @[] for i in 0..3: diff --git a/tests/dagger/bitswap/utils.nim b/tests/dagger/bitswap/utils.nim index 9bf3d4b2..5dd5002c 100644 --- a/tests/dagger/bitswap/utils.nim +++ b/tests/dagger/bitswap/utils.nim @@ -7,6 +7,8 @@ import pkg/dagger/bitswap import pkg/dagger/stores/memorystore import pkg/dagger/blocktype as bt +import ../examples + proc generateNodes*( num: Natural, blocks: openArray[bt.Block] = [], @@ -17,8 +19,9 @@ proc generateNodes*( for i in 0..