mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-04 06:33:12 +00:00
Update to latest version of nim-contract-abi
Encoding is rewritten to make use of the new tuple api.
This commit is contained in:
parent
39ef902177
commit
5977ea47e3
@ -5,9 +5,9 @@ description = "Nitro state channels"
|
||||
|
||||
requires "nim >= 1.2.6 & < 2.0.0"
|
||||
requires "nimcrypto >= 0.5.4 & < 0.6.0"
|
||||
requires "questionable >= 0.9.1 & < 0.10.0"
|
||||
requires "questionable >= 0.10.1 & < 0.11.0"
|
||||
requires "upraises >= 0.1.0 & < 0.2.0"
|
||||
requires "https://github.com/status-im/nim-contract-abi >= 0.1.0 & < 0.2.0"
|
||||
requires "https://github.com/status-im/nim-contract-abi >= 0.3.0 & < 0.4.0"
|
||||
requires "secp256k1"
|
||||
requires "stint"
|
||||
requires "stew"
|
||||
|
||||
@ -13,10 +13,6 @@ type
|
||||
chainId*: UInt256
|
||||
|
||||
func getChannelId*(channel: ChannelDefinition): Destination =
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(channel.chainId)
|
||||
encoder.write(channel.participants)
|
||||
encoder.write(channel.nonce)
|
||||
encoder.finishTuple()
|
||||
Destination(keccak256.digest(encoder.finish()).data)
|
||||
let encoding = AbiEncoder.encode:
|
||||
(channel.chainId, channel.participants, channel.nonce)
|
||||
Destination(keccak256.digest(encoding).data)
|
||||
|
||||
@ -52,46 +52,25 @@ func `==`*(a, b: AssetOutcome): bool =
|
||||
proc `==`*(a, b: Outcome): bool {.borrow.}
|
||||
|
||||
func encode*(encoder: var AbiEncoder, guarantee: Guarantee) =
|
||||
encoder.startTuple()
|
||||
encoder.startTuple()
|
||||
encoder.write(guarantee.targetChannelId)
|
||||
encoder.write(guarantee.destinations)
|
||||
encoder.finishTuple()
|
||||
encoder.finishTuple()
|
||||
|
||||
func encode*(encoder: var AbiEncoder, item: AllocationItem) =
|
||||
encoder.startTuple()
|
||||
encoder.write(item.destination)
|
||||
encoder.write(item.amount)
|
||||
encoder.finishTuple()
|
||||
encoder.write:
|
||||
( (guarantee.targetChannelId, guarantee.destinations), )
|
||||
|
||||
func encode*(encoder: var AbiEncoder, allocation: Allocation) =
|
||||
encoder.startTuple()
|
||||
encoder.write(seq[AllocationItem](allocation))
|
||||
encoder.finishTuple()
|
||||
encoder.write: (seq[AllocationItem](allocation),)
|
||||
|
||||
func encode*(encoder: var AbiEncoder, assetOutcome: AssetOutcome) =
|
||||
var content= AbiEncoder.init()
|
||||
content.startTuple()
|
||||
content.startTuple()
|
||||
content.write(assetOutcome.kind)
|
||||
var content: seq[byte]
|
||||
case assetOutcome.kind:
|
||||
of allocationType:
|
||||
content.write(AbiEncoder.encode(assetOutcome.allocation))
|
||||
content = AbiEncoder.encode:
|
||||
( (assetOutcome.kind, ABiEncoder.encode(assetOutcome.allocation)), )
|
||||
of guaranteeType:
|
||||
content.write(AbiEncoder.encode(assetOutcome.guarantee))
|
||||
content.finishTuple()
|
||||
content.finishTuple()
|
||||
|
||||
encoder.startTuple()
|
||||
encoder.write(assetOutcome.assetHolder)
|
||||
encoder.write(content.finish())
|
||||
encoder.finishTuple()
|
||||
content = AbiEncoder.encode:
|
||||
( (assetOutcome.kind, AbiEncoder.encode(assetOutcome.guarantee)), )
|
||||
encoder.write( (assetOutcome.assetHolder, content) )
|
||||
|
||||
func encode*(encoder: var AbiEncoder, outcome: Outcome) =
|
||||
encoder.startTuple()
|
||||
encoder.write(seq[AssetOutcome](outcome))
|
||||
encoder.finishTuple()
|
||||
encoder.write: (seq[AssetOutcome](outcome),)
|
||||
|
||||
func hashOutcome*(outcome: Outcome): array[32, byte] =
|
||||
keccak256.digest(AbiEncoder.encode(outcome)).data
|
||||
|
||||
@ -45,21 +45,17 @@ func variablePart*(state: State): VariablePart =
|
||||
)
|
||||
|
||||
func hashAppPart*(state: State): array[32, byte] =
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(state.challengeDuration)
|
||||
encoder.write(state.appDefinition)
|
||||
encoder.write(state.appData)
|
||||
encoder.finishTuple()
|
||||
keccak256.digest(encoder.finish).data
|
||||
let encoding = AbiEncoder.encode:
|
||||
(state.challengeDuration, state.appDefinition, state.appData)
|
||||
keccak256.digest(encoding).data
|
||||
|
||||
func hashState*(state: State): array[32, byte] =
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(state.turnNum)
|
||||
encoder.write(state.isFinal)
|
||||
encoder.write(getChannelId(state.channel))
|
||||
encoder.write(hashAppPart(state))
|
||||
encoder.write(hashOutcome(state.outcome))
|
||||
encoder.finishTuple()
|
||||
keccak256.digest(encoder.finish).data
|
||||
let encoding = AbiEncoder.encode:
|
||||
(
|
||||
state.turnNum,
|
||||
state.isFinal,
|
||||
getChannelId(state.channel),
|
||||
hashAppPart(state),
|
||||
hashOutcome(state.outcome)
|
||||
)
|
||||
keccak256.digest(encoding).data
|
||||
|
||||
@ -7,13 +7,8 @@ suite "channel definition":
|
||||
let channel = ChannelDefinition.example
|
||||
|
||||
test "calculates channel id":
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(channel.chainId)
|
||||
encoder.write(channel.participants)
|
||||
encoder.write(channel.nonce)
|
||||
encoder.finishTuple()
|
||||
let encoded = encoder.finish()
|
||||
let encoded = AbiEncoder.encode:
|
||||
(channel.chainId, channel.participants, channel.nonce)
|
||||
let hashed = keccak256.digest(encoded).data
|
||||
check getChannelId(channel) == Destination(hashed)
|
||||
|
||||
|
||||
@ -6,31 +6,20 @@ suite "outcome":
|
||||
|
||||
test "encodes guarantees":
|
||||
let guarantee = Guarantee.example
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.startTuple()
|
||||
encoder.write(guarantee.targetChannelId)
|
||||
encoder.write(guarantee.destinations)
|
||||
encoder.finishTuple()
|
||||
encoder.finishTuple()
|
||||
check AbiEncoder.encode(guarantee) == encoder.finish()
|
||||
let expected = AbiEncoder.encode:
|
||||
((guarantee.targetChannelId, guarantee.destinations),)
|
||||
check AbiEncoder.encode(guarantee) == expected
|
||||
|
||||
test "encodes allocation items":
|
||||
let item = AllocationItem.example
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(item.destination)
|
||||
encoder.write(item.amount)
|
||||
encoder.finishTuple()
|
||||
check AbiEncoder.encode(item) == encoder.finish()
|
||||
let expected = AbiEncoder.encode: (item.destination, item.amount)
|
||||
check AbiEncoder.encode(item) == expected
|
||||
|
||||
test "encodes allocation":
|
||||
let allocation = Allocation.example
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(seq[AllocationItem](allocation))
|
||||
encoder.finishTuple()
|
||||
check AbiEncoder.encode(allocation) == encoder.finish()
|
||||
let expected = AbiEncoder.encode:
|
||||
(seq[AllocationItem](allocation),)
|
||||
check AbiEncoder.encode(allocation) == expected
|
||||
|
||||
test "encodes allocation outcome":
|
||||
let assetOutcome = AssetOutcome(
|
||||
@ -38,19 +27,11 @@ suite "outcome":
|
||||
assetHolder: EthAddress.example,
|
||||
allocation: Allocation.example
|
||||
)
|
||||
var content= AbiEncoder.init()
|
||||
content.startTuple()
|
||||
content.startTuple()
|
||||
content.write(allocationType)
|
||||
content.write(AbiEncoder.encode(assetOutcome.allocation))
|
||||
content.finishTuple()
|
||||
content.finishTuple()
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(assetOutcome.assetHolder)
|
||||
encoder.write(content.finish())
|
||||
encoder.finishTuple()
|
||||
check AbiEncoder.encode(assetOutcome) == encoder.finish()
|
||||
let content = AbiEncoder.encode:
|
||||
((allocationType, AbiEncoder.encode(assetOutcome.allocation)),)
|
||||
let expected = AbiEncoder.encode:
|
||||
(assetOutcome.assetHolder, content)
|
||||
check AbiEncoder.encode(assetOutcome) == expected
|
||||
|
||||
test "encodes guarantee outcome":
|
||||
let assetOutcome = AssetOutcome(
|
||||
@ -58,27 +39,17 @@ suite "outcome":
|
||||
assetHolder: EthAddress.example,
|
||||
guarantee: Guarantee.example
|
||||
)
|
||||
var content= AbiEncoder.init()
|
||||
content.startTuple()
|
||||
content.startTuple()
|
||||
content.write(guaranteeType)
|
||||
content.write(AbiEncoder.encode(assetOutcome.guarantee))
|
||||
content.finishTuple()
|
||||
content.finishTuple()
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(assetOutcome.assetHolder)
|
||||
encoder.write(content.finish())
|
||||
encoder.finishTuple()
|
||||
check AbiEncoder.encode(assetOutcome) == encoder.finish()
|
||||
let content = AbiEncoder.encode:
|
||||
((guaranteeType, AbiEncoder.encode(assetOutcome.guarantee)),)
|
||||
let expected = AbiEncoder.encode:
|
||||
(assetOutcome.assetHolder, content)
|
||||
check AbiEncoder.encode(assetOutcome) == expected
|
||||
|
||||
test "encodes outcomes":
|
||||
let outcome = Outcome.example()
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(seq[AssetOutcome](outcome))
|
||||
encoder.finishTuple()
|
||||
check AbiEncoder.encode(outcome) == encoder.finish()
|
||||
let expected = AbiEncoder.encode:
|
||||
(seq[AssetOutcome](outcome),)
|
||||
check AbiEncoder.encode(outcome) == expected
|
||||
|
||||
test "hashes outcomes":
|
||||
let outcome = Outcome.example
|
||||
|
||||
@ -23,26 +23,20 @@ suite "state":
|
||||
)
|
||||
|
||||
test "hashes app part of state":
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(state.challengeDuration)
|
||||
encoder.write(state.appDefinition)
|
||||
encoder.write(state.appData)
|
||||
encoder.finishTuple()
|
||||
let encoded = encoder.finish()
|
||||
let encoded = AbiEncoder.encode:
|
||||
(state.challengeDuration, state.appDefinition, state.appData)
|
||||
let hashed = keccak256.digest(encoded).data
|
||||
check hashAppPart(state) == hashed
|
||||
|
||||
test "hashes state":
|
||||
var encoder= AbiEncoder.init()
|
||||
encoder.startTuple()
|
||||
encoder.write(state.turnNum)
|
||||
encoder.write(state.isFinal)
|
||||
encoder.write(getChannelId(state.channel))
|
||||
encoder.write(hashAppPart(state))
|
||||
encoder.write(hashOutcome(state.outcome))
|
||||
encoder.finishTuple()
|
||||
let encoded = encoder.finish()
|
||||
let encoded = AbiEncoder.encode:
|
||||
(
|
||||
state.turnNum,
|
||||
state.isFinal,
|
||||
getChannelId(state.channel),
|
||||
hashAppPart(state),
|
||||
hashOutcome(state.outcome)
|
||||
)
|
||||
let hashed = keccak256.digest(encoded).data
|
||||
check hashState(state) == hashed
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user