mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-05-21 17:19:31 +00:00
Replace Channel by ChannelId
This commit is contained in:
parent
9efcf21722
commit
f498228bb9
@ -1,3 +1,4 @@
|
|||||||
|
import std/hashes
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import pkg/questionable/results
|
import pkg/questionable/results
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
@ -17,6 +18,7 @@ func parse*(_: type Destination, s: string): ?Destination =
|
|||||||
Destination(array[32, byte].fromHex(s)).catch.option
|
Destination(array[32, byte].fromHex(s)).catch.option
|
||||||
|
|
||||||
func `==`*(a, b: Destination): bool {.borrow.}
|
func `==`*(a, b: Destination): bool {.borrow.}
|
||||||
|
func hash*(destination: Destination): Hash {.borrow.}
|
||||||
|
|
||||||
func toDestination*(address: EthAddress): Destination =
|
func toDestination*(address: EthAddress): Destination =
|
||||||
var bytes: array[32, byte]
|
var bytes: array[32, byte]
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import std/tables
|
||||||
import ./basics
|
import ./basics
|
||||||
import ./keys
|
import ./keys
|
||||||
import ./protocol
|
import ./protocol
|
||||||
@ -13,9 +14,8 @@ export channelupdate
|
|||||||
type
|
type
|
||||||
Wallet* = object
|
Wallet* = object
|
||||||
key: PrivateKey
|
key: PrivateKey
|
||||||
channels*: seq[Channel]
|
channels: Table[ChannelId, ChannelUpdate]
|
||||||
Channel* = object
|
ChannelId* = Destination
|
||||||
latest*: ChannelUpdate
|
|
||||||
|
|
||||||
func init*(_: type Wallet, key: PrivateKey): Wallet =
|
func init*(_: type Wallet, key: PrivateKey): Wallet =
|
||||||
result.key = key
|
result.key = key
|
||||||
@ -23,31 +23,34 @@ func init*(_: type Wallet, key: PrivateKey): Wallet =
|
|||||||
func address*(wallet: Wallet): EthAddress =
|
func address*(wallet: Wallet): EthAddress =
|
||||||
wallet.key.toPublicKey.toAddress
|
wallet.key.toPublicKey.toAddress
|
||||||
|
|
||||||
|
func `[]`*(wallet: Wallet, channel: ChannelId): ?ChannelUpdate =
|
||||||
|
wallet.channels[channel].catch.option
|
||||||
|
|
||||||
func sign(wallet: Wallet, update: ChannelUpdate): ChannelUpdate =
|
func sign(wallet: Wallet, update: ChannelUpdate): ChannelUpdate =
|
||||||
var signed = update
|
var signed = update
|
||||||
signed.signatures &= @{wallet.address: wallet.key.sign(update.state)}
|
signed.signatures &= @{wallet.address: wallet.key.sign(update.state)}
|
||||||
signed
|
signed
|
||||||
|
|
||||||
func createChannel(wallet: var Wallet, update: ChannelUpdate): Channel =
|
func createChannel(wallet: var Wallet, update: ChannelUpdate): ChannelId =
|
||||||
let signed = wallet.sign(update)
|
let signed = wallet.sign(update)
|
||||||
let channel = Channel(latest: signed)
|
let id = getChannelId(signed.state.channel)
|
||||||
wallet.channels.add(channel)
|
wallet.channels[id] = signed
|
||||||
channel
|
id
|
||||||
|
|
||||||
func openLedgerChannel*(wallet: var Wallet,
|
func openLedgerChannel*(wallet: var Wallet,
|
||||||
hub: EthAddress,
|
hub: EthAddress,
|
||||||
chainId: UInt256,
|
chainId: UInt256,
|
||||||
nonce: UInt48,
|
nonce: UInt48,
|
||||||
asset: EthAddress,
|
asset: EthAddress,
|
||||||
amount: UInt256): Channel =
|
amount: UInt256): ChannelId =
|
||||||
let update = startLedger(wallet.address, hub, chainId, nonce, asset, amount)
|
let update = startLedger(wallet.address, hub, chainId, nonce, asset, amount)
|
||||||
wallet.createChannel(update)
|
wallet.createChannel(update)
|
||||||
|
|
||||||
func acceptChannel*(wallet: var Wallet, update: ChannelUpdate): ?!Channel =
|
func acceptChannel*(wallet: var Wallet, update: ChannelUpdate): ?!ChannelId =
|
||||||
if not update.participants.contains(wallet.address):
|
if not update.participants.contains(wallet.address):
|
||||||
return Channel.failure "wallet owner is not a participant"
|
return ChannelId.failure "wallet owner is not a participant"
|
||||||
|
|
||||||
if not verifySignatures(update):
|
if not verifySignatures(update):
|
||||||
return Channel.failure "incorrect signatures"
|
return ChannelId.failure "incorrect signatures"
|
||||||
|
|
||||||
wallet.createChannel(update).success
|
wallet.createChannel(update).success
|
||||||
|
|||||||
@ -17,34 +17,31 @@ suite "wallet: opening ledger channel":
|
|||||||
let nonce = UInt48.example
|
let nonce = UInt48.example
|
||||||
|
|
||||||
var wallet: Wallet
|
var wallet: Wallet
|
||||||
var channel: Channel
|
var channel: ChannelId
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
wallet = Wallet.init(key)
|
wallet = Wallet.init(key)
|
||||||
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount)
|
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount)
|
||||||
|
|
||||||
test "sets correct channel definition":
|
test "sets correct channel definition":
|
||||||
let definition = channel.latest.state.channel
|
let definition = wallet[channel].get.state.channel
|
||||||
check definition.chainId == chainId
|
check definition.chainId == chainId
|
||||||
check definition.nonce == nonce
|
check definition.nonce == nonce
|
||||||
check definition.participants == @[wallet.address, hub]
|
check definition.participants == @[wallet.address, hub]
|
||||||
|
|
||||||
test "provides correct outcome":
|
test "provides correct outcome":
|
||||||
let outcome = channel.latest.state.outcome
|
let outcome = wallet[channel].get.state.outcome
|
||||||
let destination = wallet.address.toDestination
|
let destination = wallet.address.toDestination
|
||||||
check outcome == Outcome.init(asset, {destination: amount})
|
check outcome == Outcome.init(asset, {destination: amount})
|
||||||
|
|
||||||
test "signs the upcoming state":
|
test "signs the state":
|
||||||
let state = channel.latest.state
|
let state = wallet[channel].get.state
|
||||||
let signatures = channel.latest.signatures
|
let signatures = wallet[channel].get.signatures
|
||||||
check signatures == @{wallet.address: key.sign(state)}
|
check signatures == @{wallet.address: key.sign(state)}
|
||||||
|
|
||||||
test "sets app definition and app data to zero":
|
test "sets app definition and app data to zero":
|
||||||
check channel.latest.state.appDefinition == EthAddress.zero
|
check wallet[channel].get.state.appDefinition == EthAddress.zero
|
||||||
check channel.latest.state.appData.len == 0
|
check wallet[channel].get.state.appData.len == 0
|
||||||
|
|
||||||
test "updates the list of channels":
|
|
||||||
check wallet.channels == @[channel]
|
|
||||||
|
|
||||||
suite "wallet: accepting incoming channel":
|
suite "wallet: accepting incoming channel":
|
||||||
|
|
||||||
@ -57,18 +54,14 @@ suite "wallet: accepting incoming channel":
|
|||||||
update = ChannelUpdate(state: State.example)
|
update = ChannelUpdate(state: State.example)
|
||||||
update.state.channel.participants &= @[wallet.address]
|
update.state.channel.participants &= @[wallet.address]
|
||||||
|
|
||||||
test "returns the new channel instance":
|
test "returns the new channel id":
|
||||||
let channel = wallet.acceptChannel(update).get
|
let channel = wallet.acceptChannel(update).get
|
||||||
check channel.latest.state == update.state
|
check wallet[channel].get.state == update.state
|
||||||
|
|
||||||
test "updates the list of channels":
|
|
||||||
let channel = wallet.acceptChannel(update).get
|
|
||||||
check wallet.channels == @[channel]
|
|
||||||
|
|
||||||
test "signs the channel state":
|
test "signs the channel state":
|
||||||
let channel = wallet.acceptChannel(update).get
|
let channel = wallet.acceptChannel(update).get
|
||||||
let expectedSignatures = @{wallet.address: key.sign(update.state)}
|
let expectedSignatures = @{wallet.address: key.sign(update.state)}
|
||||||
check channel.latest.signatures == expectedSignatures
|
check wallet[channel].get.signatures == expectedSignatures
|
||||||
|
|
||||||
test "fails when wallet address is not a participant":
|
test "fails when wallet address is not a participant":
|
||||||
let wrongParticipants = seq[EthAddress].example
|
let wrongParticipants = seq[EthAddress].example
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user