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*( proc new*(
T: type Bitswap, T: type Bitswap,
localStore: BlockStore, localStore: BlockStore,
wallet: Wallet,
network: BitswapNetwork, network: BitswapNetwork,
concurrentTasks = DefaultConcurrentTasks, concurrentTasks = DefaultConcurrentTasks,
maxRetries = DefaultMaxRetries, maxRetries = DefaultMaxRetries,
@ -111,6 +112,7 @@ proc new*(
let engine = BitswapEngine.new( let engine = BitswapEngine.new(
localStore = localStore, localStore = localStore,
wallet = wallet,
peersPerRequest = peersPerRequest, peersPerRequest = peersPerRequest,
request = network.request, request = network.request,
) )

View File

@ -52,6 +52,7 @@ type
peersPerRequest: int # max number of peers to request from peersPerRequest: int # max number of peers to request from
scheduleTask*: TaskScheduler # schedule a new task with the task runner scheduleTask*: TaskScheduler # schedule a new task with the task runner
request*: BitswapRequest # bitswap network requests request*: BitswapRequest # bitswap network requests
wallet*: Wallet # nitro wallet for micropayments
pricing*: ?Pricing # optional bandwidth pricing pricing*: ?Pricing # optional bandwidth pricing
proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool = proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool =
@ -341,6 +342,7 @@ proc taskHandler*(b: BitswapEngine, task: BitswapPeerCtx) {.gcsafe, async.} =
proc new*( proc new*(
T: type BitswapEngine, T: type BitswapEngine,
localStore: BlockStore, localStore: BlockStore,
wallet: Wallet,
request: BitswapRequest = BitswapRequest(), request: BitswapRequest = BitswapRequest(),
scheduleTask: TaskScheduler = nil, scheduleTask: TaskScheduler = nil,
peersPerRequest = DefaultMaxPeersPerRequest): T = peersPerRequest = DefaultMaxPeersPerRequest): T =
@ -355,6 +357,7 @@ proc new*(
peersPerRequest: peersPerRequest, peersPerRequest: peersPerRequest,
scheduleTask: taskScheduler, scheduleTask: taskScheduler,
request: request, request: request,
wallet: wallet
) )
return b return b

View File

@ -28,6 +28,7 @@ suite "Bitswap engine - 2 nodes":
var var
switch1, switch2: Switch switch1, switch2: Switch
wallet1, wallet2: Wallet
pricing1, pricing2: Pricing pricing1, pricing2: Pricing
network1, network2: BitswapNetwork network1, network2: BitswapNetwork
bitswap1, bitswap2: Bitswap bitswap1, bitswap2: Bitswap
@ -41,6 +42,8 @@ suite "Bitswap engine - 2 nodes":
switch1 = newStandardSwitch() switch1 = newStandardSwitch()
switch2 = newStandardSwitch() switch2 = newStandardSwitch()
wallet1 = Wallet.example
wallet2 = Wallet.example
pricing1 = Pricing.example pricing1 = Pricing.example
pricing2 = Pricing.example pricing2 = Pricing.example
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())
@ -50,11 +53,11 @@ suite "Bitswap engine - 2 nodes":
peerId2 = switch2.peerInfo.peerId peerId2 = switch2.peerInfo.peerId
network1 = BitswapNetwork.new(switch = switch1) network1 = BitswapNetwork.new(switch = switch1)
bitswap1 = Bitswap.new(MemoryStore.new(blocks1), network1) bitswap1 = Bitswap.new(MemoryStore.new(blocks1), wallet1, network1)
switch1.mount(network1) switch1.mount(network1)
network2 = BitswapNetwork.new(switch = switch2) network2 = BitswapNetwork.new(switch = switch2)
bitswap2 = Bitswap.new(MemoryStore.new(blocks2), network2) bitswap2 = Bitswap.new(MemoryStore.new(blocks2), wallet2, network2)
switch2.mount(network2) switch2.mount(network2)
await allFuturesThrowing( await allFuturesThrowing(

View File

@ -23,6 +23,7 @@ suite "Bitswap engine basic":
peerId = PeerID.init(seckey.getKey().tryGet()).tryGet() peerId = PeerID.init(seckey.getKey().tryGet()).tryGet()
chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256) chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256)
blocks = chunker.mapIt( bt.Block.new(it) ) blocks = chunker.mapIt( bt.Block.new(it) )
wallet = Wallet.example
var var
done: Future[void] done: Future[void]
@ -47,7 +48,7 @@ suite "Bitswap engine basic":
sendWantList: sendWantList, 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.wantList = blocks.mapIt( it.cid )
engine.setupPeer(peerId) engine.setupPeer(peerId)
@ -61,7 +62,7 @@ suite "Bitswap engine basic":
done.complete() done.complete()
let request = BitswapRequest(sendPricing: sendPricing) 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.pricing = pricing.some
engine.setupPeer(peerId) engine.setupPeer(peerId)
@ -75,6 +76,7 @@ suite "Bitswap engine handlers":
peerId = PeerID.init(seckey.getKey().tryGet()).tryGet() peerId = PeerID.init(seckey.getKey().tryGet()).tryGet()
chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256) chunker = newRandomChunker(Rng.instance(), size = 1024, chunkSize = 256)
blocks = chunker.mapIt( bt.Block.new(it) ) blocks = chunker.mapIt( bt.Block.new(it) )
wallet = Wallet.example
var var
engine: BitswapEngine engine: BitswapEngine
@ -83,7 +85,7 @@ suite "Bitswap engine handlers":
setup: setup:
done = newFuture[void]() done = newFuture[void]()
engine = BitswapEngine.new(MemoryStore.new()) engine = BitswapEngine.new(MemoryStore.new(), wallet)
peerCtx = BitswapPeerCtx( peerCtx = BitswapPeerCtx(
id: peerId id: peerId
) )
@ -164,6 +166,7 @@ suite "Bitswap engine blocks":
rng = Rng.instance() rng = Rng.instance()
chunker = newRandomChunker(Rng.instance(), size = 2048, chunkSize = 256) chunker = newRandomChunker(Rng.instance(), size = 2048, chunkSize = 256)
blocks = chunker.mapIt( bt.Block.new(it) ) blocks = chunker.mapIt( bt.Block.new(it) )
wallet = Wallet.example
var var
engine: BitswapEngine engine: BitswapEngine
@ -173,7 +176,7 @@ suite "Bitswap engine blocks":
setup: setup:
done = newFuture[void]() done = newFuture[void]()
engine = BitswapEngine.new(MemoryStore.new()) engine = BitswapEngine.new(MemoryStore.new(), wallet)
peersCtx = @[] peersCtx = @[]
for i in 0..3: for i in 0..3:
@ -256,6 +259,7 @@ suite "Task Handler":
rng = Rng.instance() rng = Rng.instance()
chunker = newRandomChunker(Rng.instance(), size = 2048, chunkSize = 256) chunker = newRandomChunker(Rng.instance(), size = 2048, chunkSize = 256)
blocks = chunker.mapIt( bt.Block.new(it) ) blocks = chunker.mapIt( bt.Block.new(it) )
wallet = Wallet.example
var var
engine: BitswapEngine engine: BitswapEngine
@ -265,7 +269,7 @@ suite "Task Handler":
setup: setup:
done = newFuture[void]() done = newFuture[void]()
engine = BitswapEngine.new(MemoryStore.new()) engine = BitswapEngine.new(MemoryStore.new(), wallet)
peersCtx = @[] peersCtx = @[]
for i in 0..3: for i in 0..3:

View File

@ -7,6 +7,8 @@ import pkg/dagger/bitswap
import pkg/dagger/stores/memorystore import pkg/dagger/stores/memorystore
import pkg/dagger/blocktype as bt import pkg/dagger/blocktype as bt
import ../examples
proc generateNodes*( proc generateNodes*(
num: Natural, num: Natural,
blocks: openArray[bt.Block] = [], blocks: openArray[bt.Block] = [],
@ -17,8 +19,9 @@ proc generateNodes*(
for i in 0..<num: for i in 0..<num:
let let
switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr}) switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
network = BitswapNetwork.new(switch = switch) wallet = Wallet.example
bitswap = Bitswap.new(MemoryStore.new(blocks), network) network = BitswapNetwork.new(switch)
bitswap = Bitswap.new(MemoryStore.new(blocks), wallet, network)
switch.mount(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 # workaround for https://github.com/nim-lang/Nim/issues/17670
uint64.rand mod (UInt48.high + 1) uint64.rand mod (UInt48.high + 1)
proc example*(_: type Wallet): Wallet =
Wallet.init(EthPrivateKey.random())
proc example*(_: type SignedState): SignedState = proc example*(_: type SignedState): SignedState =
var wallet = Wallet.init(EthPrivateKey.random()) var wallet = Wallet.example
let hub, asset, receiver = EthAddress.example let hub, asset, receiver = EthAddress.example
let chainId, amount = UInt256.example let chainId, amount = UInt256.example
let nonce = UInt48.example let nonce = UInt48.example