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*(
T: type Bitswap,
localStore: BlockStore,
wallet: Wallet,
wallet: WalletRef,
network: BitswapNetwork,
concurrentTasks = DefaultConcurrentTasks,
maxRetries = DefaultMaxRetries,

View File

@ -46,7 +46,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
wallet*: WalletRef # nitro wallet for micropayments
pricing*: ?Pricing # optional bandwidth pricing
proc contains*(a: AsyncHeapQueue[Entry], b: Cid): bool =
@ -342,7 +342,7 @@ proc taskHandler*(b: BitswapEngine, task: BitswapPeerCtx) {.gcsafe, async.} =
proc new*(
T: type BitswapEngine,
localStore: BlockStore,
wallet: Wallet,
wallet: WalletRef,
request: BitswapRequest = BitswapRequest(),
scheduleTask: TaskScheduler = nil,
peersPerRequest = DefaultMaxPeersPerRequest): T =

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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