Use reference semantics for wallets

This commit is contained in:
Mark Spanbroek 2021-04-19 16:37:38 +02:00 committed by markspanbroek
parent 2d4a735e5f
commit 5160b267b6
8 changed files with 19 additions and 16 deletions

View File

@ -104,7 +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, wallet: WalletRef,
network: BitswapNetwork, network: BitswapNetwork,
concurrentTasks = DefaultConcurrentTasks, concurrentTasks = DefaultConcurrentTasks,
maxRetries = DefaultMaxRetries, maxRetries = DefaultMaxRetries,

View File

@ -46,7 +46,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 wallet*: WalletRef # 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 =
@ -342,7 +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, wallet: WalletRef,
request: BitswapRequest = BitswapRequest(), request: BitswapRequest = BitswapRequest(),
scheduleTask: TaskScheduler = nil, scheduleTask: TaskScheduler = nil,
peersPerRequest = DefaultMaxPeersPerRequest): T = peersPerRequest = DefaultMaxPeersPerRequest): T =

View File

@ -12,12 +12,12 @@ push: {.upraises: [].}
const ChainId = 0.u256 # invalid chain id for now const ChainId = 0.u256 # invalid chain id for now
const AmountPerChannel = (10'u64^18).u256 # 1 asset, ERC20 default is 18 decimals const AmountPerChannel = (10'u64^18).u256 # 1 asset, ERC20 default is 18 decimals
func openLedgerChannel*(wallet: var Wallet, func openLedgerChannel*(wallet: WalletRef,
hub: EthAddress, hub: EthAddress,
asset: EthAddress): ?!ChannelId = asset: EthAddress): ?!ChannelId =
wallet.openLedgerChannel(hub, ChainId, asset, AmountPerChannel) wallet.openLedgerChannel(hub, ChainId, asset, AmountPerChannel)
func getOrOpenChannel(wallet: var Wallet, peer: BitswapPeerCtx): ?!ChannelId = func getOrOpenChannel(wallet: WalletRef, peer: BitswapPeerCtx): ?!ChannelId =
if channel =? peer.paymentChannel: if channel =? peer.paymentChannel:
success channel success channel
elif pricing =? peer.pricing: elif pricing =? peer.pricing:
@ -27,7 +27,7 @@ func getOrOpenChannel(wallet: var Wallet, peer: BitswapPeerCtx): ?!ChannelId =
else: else:
failure "no pricing set for peer" failure "no pricing set for peer"
func pay*(wallet: var Wallet, func pay*(wallet: WalletRef,
peer: BitswapPeerCtx, peer: BitswapPeerCtx,
amountOfBytes: int): ?!SignedState = amountOfBytes: int): ?!SignedState =
if pricing =? peer.pricing: if pricing =? peer.pricing:

View File

@ -6,11 +6,11 @@ suite "engine payments":
let amountOfBytes = 42 let amountOfBytes = 42
var wallet: Wallet var wallet: WalletRef
var peer: BitswapPeerCtx var peer: BitswapPeerCtx
setup: setup:
wallet = Wallet.example wallet = WalletRef.example
peer = BitswapPeerCtx.example peer = BitswapPeerCtx.example
peer.pricing = Pricing.example.some peer.pricing = Pricing.example.some

View File

@ -28,7 +28,7 @@ suite "Bitswap engine - 2 nodes":
var var
switch1, switch2: Switch switch1, switch2: Switch
wallet1, wallet2: Wallet wallet1, wallet2: WalletRef
pricing1, pricing2: Pricing pricing1, pricing2: Pricing
network1, network2: BitswapNetwork network1, network2: BitswapNetwork
bitswap1, bitswap2: Bitswap bitswap1, bitswap2: Bitswap
@ -42,8 +42,8 @@ suite "Bitswap engine - 2 nodes":
switch1 = newStandardSwitch() switch1 = newStandardSwitch()
switch2 = newStandardSwitch() switch2 = newStandardSwitch()
wallet1 = Wallet.example wallet1 = WalletRef.example
wallet2 = Wallet.example wallet2 = WalletRef.example
pricing1 = Pricing.example pricing1 = Pricing.example
pricing2 = Pricing.example pricing2 = Pricing.example
awaiters.add(await switch1.start()) awaiters.add(await switch1.start())

View File

@ -23,7 +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 wallet = WalletRef.example
var var
done: Future[void] done: Future[void]
@ -76,7 +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 wallet = WalletRef.example
var var
engine: BitswapEngine engine: BitswapEngine
@ -181,7 +181,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 wallet = WalletRef.example
var var
engine: BitswapEngine engine: BitswapEngine
@ -274,7 +274,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 wallet = WalletRef.example
var var
engine: BitswapEngine engine: BitswapEngine

View File

@ -19,7 +19,7 @@ proc generateNodes*(
for i in 0..<num: for i in 0..<num:
let let
switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr}) switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
wallet = Wallet.example wallet = WalletRef.example
network = BitswapNetwork.new(switch) network = BitswapNetwork.new(switch)
bitswap = Bitswap.new(MemoryStore.new(blocks), wallet, network) bitswap = Bitswap.new(MemoryStore.new(blocks), wallet, network)

View File

@ -23,6 +23,9 @@ proc example*(_: type UInt48): UInt48 =
proc example*(_: type Wallet): Wallet = proc example*(_: type Wallet): Wallet =
Wallet.init(EthPrivateKey.random()) Wallet.init(EthPrivateKey.random())
proc example*(_: type WalletRef): WalletRef =
WalletRef.new(EthPrivateKey.random())
proc example*(_: type SignedState): SignedState = proc example*(_: type SignedState): SignedState =
var wallet = Wallet.example var wallet = Wallet.example
let hub, asset, receiver = EthAddress.example let hub, asset, receiver = EthAddress.example