Update to questionable 0.5.0

This commit is contained in:
Mark Spanbroek 2021-04-12 16:29:44 +02:00
parent f7d06a995b
commit 2ac173f87c
4 changed files with 11 additions and 11 deletions

View File

@ -1,11 +1,11 @@
version = "0.1.0" version = "0.2.0"
author = "Nim Nitro developers" author = "Nim Nitro developers"
license = "MIT" license = "MIT"
description = "Nitro state channels" description = "Nitro state channels"
requires "nim >= 1.2.6 & < 2.0.0" requires "nim >= 1.2.6 & < 2.0.0"
requires "nimcrypto >= 0.5.4 & < 0.6.0" requires "nimcrypto >= 0.5.4 & < 0.6.0"
requires "questionable >= 0.4.3 & < 0.5.0" requires "questionable >= 0.5.0 & < 0.6.0"
requires "upraises >= 0.1.0 & < 0.2.0" requires "upraises >= 0.1.0 & < 0.2.0"
requires "secp256k1" requires "secp256k1"
requires "stint" requires "stint"

View File

@ -32,7 +32,7 @@ func recover(signature: Signature, hash: array[32, byte]): ?EthPublicKey =
func recover*(signature: Signature, state: State): ?EthAddress = func recover*(signature: Signature, state: State): ?EthAddress =
let hash = hashMessage(hashState(state)) let hash = hashMessage(hashState(state))
recover(signature, hash)?.toAddress recover(signature, hash).?toAddress
func verify*(signature: Signature, state: State, signer: EthAddress): bool = func verify*(signature: Signature, state: State, signer: EthAddress): bool =
recover(signature, state) == signer.some recover(signature, state) == signer.some

View File

@ -70,13 +70,13 @@ func acceptChannel*(wallet: var Wallet, signed: SignedState): ?!ChannelId =
wallet.createChannel(signed) wallet.createChannel(signed)
func latestSignedState*(wallet: Wallet, channel: ChannelId): ?SignedState = func latestSignedState*(wallet: Wallet, channel: ChannelId): ?SignedState =
wallet.channels?[channel] wallet.channels.?[channel]
func state*(wallet: Wallet, channel: ChannelId): ?State = func state*(wallet: Wallet, channel: ChannelId): ?State =
wallet.latestSignedState(channel)?.state wallet.latestSignedState(channel).?state
func signatures*(wallet: Wallet, channel: ChannelId): ?seq[Signature] = func signatures*(wallet: Wallet, channel: ChannelId): ?seq[Signature] =
wallet.latestSignedState(channel)?.signatures wallet.latestSignedState(channel).?signatures
func signature*(wallet: Wallet, func signature*(wallet: Wallet,
channel: ChannelId, channel: ChannelId,
@ -92,7 +92,7 @@ func balance(state: State,
asset: EthAddress, asset: EthAddress,
destination: Destination): UInt256 = destination: Destination): UInt256 =
if balances =? state.outcome.balances(asset): if balances =? state.outcome.balances(asset):
if balance =? (balances?[destination]): if balance =? balances.?[destination]:
balance balance
else: else:
0.u256 0.u256
@ -140,7 +140,7 @@ func pay*(wallet: var Wallet,
?balances.move(wallet.destination, receiver, amount) ?balances.move(wallet.destination, receiver, amount)
state.outcome.update(asset, balances) state.outcome.update(asset, balances)
wallet.updateChannel(SignedState(state: state)) wallet.updateChannel(SignedState(state: state))
ok(wallet.channels?[channel].get) ok(wallet.channels.?[channel].get)
else: else:
SignedState.failure "asset not found" SignedState.failure "asset not found"
else: else:
@ -178,7 +178,7 @@ func acceptPayment*(wallet: var Wallet,
return void.failure "missing signature on payment" return void.failure "missing signature on payment"
if updatedBalances =? payment.state.outcome.balances(asset): if updatedBalances =? payment.state.outcome.balances(asset):
var expectedState: State = wallet.channels?[channel]?.state.get var expectedState: State = wallet.channels.?[channel].?state.get
expectedState.outcome.update(asset, updatedBalances) expectedState.outcome.update(asset, updatedBalances)
if payment.state != expectedState: if payment.state != expectedState:
return void.failure "payment has unexpected changes in state" return void.failure "payment has unexpected changes in state"

View File

@ -114,8 +114,8 @@ suite "wallet: making payments":
wallet = Wallet.init(key) wallet = Wallet.init(key)
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, 42.u256).get channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, 42.u256).get
let updated = wallet.pay(channel, asset, hub, 1.u256).option let updated = wallet.pay(channel, asset, hub, 1.u256).option
check updated?.state == wallet.state(channel) check updated.?state == wallet.state(channel)
check updated?.signatures == wallet.signatures(channel) check updated.?signatures == wallet.signatures(channel)
test "payment fails when channel not found": test "payment fails when channel not found":
wallet = Wallet.init(key) wallet = Wallet.init(key)