From b082a14a8b4ea86046a0dbfc3691d4427e48f439 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 21 Feb 2018 12:27:34 +0200 Subject: [PATCH 1/6] Add support for SomeOrdinal --- rlp.nim | 6 +++--- rlp.nimble | 2 +- rlp/object_serialization.nim | 1 - rlp/writer.nim | 21 ++++++++++----------- tests/test_object_serialization.nim | 4 ++-- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/rlp.nim b/rlp.nim index 33425ee..6185766 100644 --- a/rlp.nim +++ b/rlp.nim @@ -184,7 +184,7 @@ proc toInt*(self: Rlp, IntType: typedesc): IntType = raise newException(BadCastError, "") for i in payloadStart ..< (payloadStart + payloadSize): - result = (result shl 8) or int(self.bytes[self.position + i]) + result = cast[IntType](result shl 8) or cast[IntType](self.bytes[self.position + i]) proc toString*(self: Rlp): string = if not isBlob(): @@ -266,8 +266,8 @@ proc read*(rlp: var Rlp, T: type string): string = result = rlp.toString rlp.skipElem -proc read*(rlp: var Rlp, T: type int): int = - result = rlp.toInt(int) +proc read*(rlp: var Rlp, T: type Integer): Integer = + result = rlp.toInt(T) rlp.skipElem proc read*[E](rlp: var Rlp, T: typedesc[seq[E]]): T = diff --git a/rlp.nimble b/rlp.nimble index 873eb76..a988cb7 100644 --- a/rlp.nimble +++ b/rlp.nimble @@ -7,7 +7,7 @@ description = "RLP serialization library for Nim" license = "Apache2" skipDirs = @["tests"] -requires "nim >= 0.17.0", "ranges >= 0.0.1" +requires "nim >= 0.17.0", "https://github.com/status-im/nim-ranges >= 0.0.1" proc configForTests() = --hints: off diff --git a/rlp/object_serialization.nim b/rlp/object_serialization.nim index 37c43bf..2effbc8 100644 --- a/rlp/object_serialization.nim +++ b/rlp/object_serialization.nim @@ -15,4 +15,3 @@ macro rlpFields*(T: typedesc, fields: varargs[untyped]): untyped = result = quote do: template enumerateRlpFields*(`ins`: `T`, `op`: untyped) {.inject.} = `body` - diff --git a/rlp/writer.nim b/rlp/writer.nim index 471b642..74266b5 100644 --- a/rlp/writer.nim +++ b/rlp/writer.nim @@ -13,18 +13,17 @@ type PrematureFinalizationError* = object of Exception - IntLike* = concept x, y - x + y - x * y - x - y - x div y - x mod y - x shr y - x shl y + IntLike* = concept x, y, type T + x + y is T + x * y is T + x - y is T + x div y is T + x mod y is T + x shr y is T + x shl y is T x and int # for masking - # Integer* = SomeOrdinal or IntLike - Integer = int + Integer* = SomeOrdinal or IntLike proc bytesNeeded(num: Integer): int = var n = num @@ -154,7 +153,7 @@ proc append*(self; data: MemRange) = proc append*(self; i: Integer) = if i == 0: self.output.add BLOB_START_MARKER - elif i < Integer(BLOB_START_MARKER): + elif i < BLOB_START_MARKER.Integer: self.output.add byte(i) else: let bytesNeeded = i.bytesNeeded diff --git a/tests/test_object_serialization.nim b/tests/test_object_serialization.nim index 356158b..5a6898a 100644 --- a/tests/test_object_serialization.nim +++ b/tests/test_object_serialization.nim @@ -9,7 +9,7 @@ type receiver: string Foo = object - x: int + x: uint32 y: string z: seq[int] @@ -27,7 +27,7 @@ proc default(T: typedesc): T = discard test "encoding and decoding an object": var originalBar = Bar(b: "abracadabra", - f: Foo(x: 5, y: "hocus pocus", z: @[100, 200, 300])) + f: Foo(x: 5'u32, y: "hocus pocus", z: @[100, 200, 300])) var bytes = encode(originalBar) From 955a2afc87ff38f9aeaf282db66a3ecda73fb7b9 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 21 Feb 2018 12:34:07 +0200 Subject: [PATCH 2/6] Add support for uint --- rlp.nim | 2 +- rlp/writer.nim | 2 +- tests/test_object_serialization.nim | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rlp.nim b/rlp.nim index 6185766..4fa6ee9 100644 --- a/rlp.nim +++ b/rlp.nim @@ -168,7 +168,7 @@ proc isInt*(self: Rlp): bool = return bytes[offset] != 0 return false -template maxBytes*(o: typedesc[Ordinal]): int = sizeof(o) +template maxBytes*(o: typedesc[Ordinal | uint64 | uint]): int = sizeof(o) proc toInt*(self: Rlp, IntType: typedesc): IntType = # XXX: self insertions are not working in generic procs diff --git a/rlp/writer.nim b/rlp/writer.nim index 74266b5..e508928 100644 --- a/rlp/writer.nim +++ b/rlp/writer.nim @@ -23,7 +23,7 @@ type x shl y is T x and int # for masking - Integer* = SomeOrdinal or IntLike + Integer* = SomeOrdinal or IntLike or uint or uint64 proc bytesNeeded(num: Integer): int = var n = num diff --git a/tests/test_object_serialization.nim b/tests/test_object_serialization.nim index 5a6898a..f42ff9e 100644 --- a/tests/test_object_serialization.nim +++ b/tests/test_object_serialization.nim @@ -9,7 +9,7 @@ type receiver: string Foo = object - x: uint32 + x: uint64 y: string z: seq[int] @@ -27,7 +27,7 @@ proc default(T: typedesc): T = discard test "encoding and decoding an object": var originalBar = Bar(b: "abracadabra", - f: Foo(x: 5'u32, y: "hocus pocus", z: @[100, 200, 300])) + f: Foo(x: 5'u64, y: "hocus pocus", z: @[100, 200, 300])) var bytes = encode(originalBar) From 432eccaf3fd67ce22e795d19042aa3751fb5747d Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 21 Feb 2018 13:04:00 +0200 Subject: [PATCH 3/6] Fix travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b8fd3a5..874d233 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,5 @@ before_install: export PATH=$PWD/bin:$PATH cd .. script: - - nimble test + - nimble test -y From 1322b813935318b4370316675189529b250d9b5c Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Wed, 21 Feb 2018 13:40:43 +0200 Subject: [PATCH 4/6] Travis fix next try --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 874d233..ba786ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,5 +25,6 @@ before_install: export PATH=$PWD/bin:$PATH cd .. script: - - nimble test -y + - nimble install -y + - nimble test From 99b534f356a2e4f8e8c773be2e93f2939162df6d Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Mon, 26 Feb 2018 14:27:07 +0200 Subject: [PATCH 5/6] Use toInt --- rlp.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rlp.nim b/rlp.nim index 4fa6ee9..5eb166a 100644 --- a/rlp.nim +++ b/rlp.nim @@ -184,7 +184,7 @@ proc toInt*(self: Rlp, IntType: typedesc): IntType = raise newException(BadCastError, "") for i in payloadStart ..< (payloadStart + payloadSize): - result = cast[IntType](result shl 8) or cast[IntType](self.bytes[self.position + i]) + result = (result shl 8).toInt or (self.bytes[self.position + i]).toInt proc toString*(self: Rlp): string = if not isBlob(): @@ -306,6 +306,7 @@ proc decode*(bytes: openarray[byte]): RlpNode = bytesCopy = @bytes rlp = rlpFromBytes initBytesRange(bytesCopy) return rlp.toNodes + template decode*(bytes: BytesRange, T: typedesc): untyped = var rlp = rlpFromBytes bytes From 8cd02732ad52f27e5c08640e250e6f9317a108a5 Mon Sep 17 00:00:00 2001 From: Alexander Ivanov Date: Tue, 27 Feb 2018 16:46:11 +0200 Subject: [PATCH 6/6] Revert --- rlp.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rlp.nim b/rlp.nim index 5eb166a..3968897 100644 --- a/rlp.nim +++ b/rlp.nim @@ -184,7 +184,7 @@ proc toInt*(self: Rlp, IntType: typedesc): IntType = raise newException(BadCastError, "") for i in payloadStart ..< (payloadStart + payloadSize): - result = (result shl 8).toInt or (self.bytes[self.position + i]).toInt + result = cast[IntType](result shl 8) or cast[IntType](self.bytes[self.position + i]) proc toString*(self: Rlp): string = if not isBlob():