From ab2a39a70407d5da514f332f26906cc7ce0bcc43 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 22 Feb 2021 15:32:48 +0100 Subject: [PATCH] Nitro state, including ABI encoding and hashing --- .editorconfig | 5 ++ .gitignore | 3 + .tool-versions | 1 + nitro.nim | 3 + nitro.nimble | 9 +++ nitro/abi.nim | 101 +++++++++++++++++++++++ nitro/channel.nim | 18 +++++ nitro/outcome.nim | 60 ++++++++++++++ nitro/state.nim | 61 ++++++++++++++ nitro/types.nim | 8 ++ tests/nitro/examples.nim | 71 ++++++++++++++++ tests/nitro/nim.cfg | 1 + tests/nitro/testAbi.nim | 156 ++++++++++++++++++++++++++++++++++++ tests/nitro/testChannel.nim | 19 +++++ tests/nitro/testOutcome.nim | 66 +++++++++++++++ tests/nitro/testState.nim | 48 +++++++++++ tests/testAll.nim | 6 ++ 17 files changed, 636 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .tool-versions create mode 100644 nitro.nim create mode 100644 nitro.nimble create mode 100644 nitro/abi.nim create mode 100644 nitro/channel.nim create mode 100644 nitro/outcome.nim create mode 100644 nitro/state.nim create mode 100644 nitro/types.nim create mode 100644 tests/nitro/examples.nim create mode 100644 tests/nitro/nim.cfg create mode 100644 tests/nitro/testAbi.nim create mode 100644 tests/nitro/testChannel.nim create mode 100644 tests/nitro/testOutcome.nim create mode 100644 tests/nitro/testState.nim create mode 100644 tests/testAll.nim diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1963f8d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +[*] +indent_style = space +insert_final_newline = true +indent_size = 2 +trim_trailing_whitespace = true \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..becd7f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +* +!*/ +!*.* diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..9ed7760 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +nim 1.4.2 diff --git a/nitro.nim b/nitro.nim new file mode 100644 index 0000000..bcb56ab --- /dev/null +++ b/nitro.nim @@ -0,0 +1,3 @@ +import ./nitro/state + +export state diff --git a/nitro.nimble b/nitro.nimble new file mode 100644 index 0000000..c0f49e9 --- /dev/null +++ b/nitro.nimble @@ -0,0 +1,9 @@ +version = "0.1.0" +author = "Nim Nitro developers" +license = "MIT" +description = "Nitro state channels" + +requires "nim >= 1.2.6 & < 2.0.0" +requires "nimcrypto >= 0.5.4 & < 0.6.0" +requires "stint" +requires "stew" diff --git a/nitro/abi.nim b/nitro/abi.nim new file mode 100644 index 0000000..f64b0e0 --- /dev/null +++ b/nitro/abi.nim @@ -0,0 +1,101 @@ +import pkg/stew/endians2 +import pkg/stint + +type + Abi* = object + AbiWriter* = object + bytes: seq[byte] + tuples: seq[Tuple] + Tuple = object + postponed: seq[Split] + Split = object + head: Slice[int] + tail: seq[byte] + +proc isStatic*(_: type Abi, t: type SomeUnsignedInt): bool = true +proc isStatic*(_: type Abi, t: type StUint): bool = true +proc isStatic*(_: type Abi, t: type bool): bool = true +proc isStatic*(_: type Abi, t: type enum): bool = true +proc isStatic*[T](_: type Abi, t: type seq[T]): bool = false +proc isStatic*[I, T](_: type Abi, t: type array[I, T]): bool = Abi.isStatic(T) + +proc encode*[T](_: type Abi, value: T): seq[byte] + +proc pad(writer: var AbiWriter, len: int) = + let padlen = (32 - len mod 32) mod 32 + for _ in 0..