Add nitro wallet to BitswapEngine

This commit is contained in:
Mark Spanbroek 2021-04-08 15:05:04 +02:00 committed by markspanbroek
parent 22f4c277dd
commit 4ce3f6d3da
6 changed files with 28 additions and 10 deletions

View File

@ -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,
)

View File

@ -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

View File

@ -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(

View File

@ -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:

View File

@ -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..<num:
let
switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
network = BitswapNetwork.new(switch = switch)
bitswap = Bitswap.new(MemoryStore.new(blocks), network)
wallet = Wallet.example
network = BitswapNetwork.new(switch)
bitswap = Bitswap.new(MemoryStore.new(blocks), wallet, network)
switch.mount(network)

View File

@ -15,8 +15,11 @@ proc example*(_: type UInt48): UInt48 =
# workaround for https://github.com/nim-lang/Nim/issues/17670
uint64.rand mod (UInt48.high + 1)
proc example*(_: type Wallet): Wallet =
Wallet.init(EthPrivateKey.random())
proc example*(_: type SignedState): SignedState =
var wallet = Wallet.init(EthPrivateKey.random())
var wallet = Wallet.example
let hub, asset, receiver = EthAddress.example
let chainId, amount = UInt256.example
let nonce = UInt48.example