From 9ed7e7b37b220e27933f06d12f36ed9773a755ba Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Sat, 31 Mar 2018 12:07:55 +0300 Subject: [PATCH] openarray[byte] is now treated as blob instead of list --- rlp/writer.nim | 15 ++++++++++----- tests/test_api_usage.nim | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/rlp/writer.nim b/rlp/writer.nim index 4d48244..f206d1f 100644 --- a/rlp/writer.nim +++ b/rlp/writer.nim @@ -144,7 +144,7 @@ template appendImpl(self; data, startMarker) = proc append*(self; data: string) = appendImpl(self, data, BLOB_START_MARKER) -proc append*(self; data: Bytes) = +proc appendBlob(self; data: openarray[byte]) = appendImpl(self, data, BLOB_START_MARKER) proc appendBytesRange(self; data: BytesRange) = @@ -165,10 +165,15 @@ proc append*(self; i: Integer) = self.maybeClosePendingLists() -proc append*[T](self; list: openarray[T]) = - self.startList list.len - for i in 0 ..< list.len: - self.append list[i] +proc append*[T](self; listOrBlob: openarray[T]) = + # TODO: This append proc should be overloaded by `openarray[byte]` after + # nim bug #7416 is fixed. + when T is byte: + self.appendBlob(listOrBlob) + else: + self.startList listOrBlob.len + for i in 0 ..< listOrBlob.len: + self.append listOrBlob[i] proc append*[T](self; list: seq[T]) = self.startList list.len diff --git a/tests/test_api_usage.nim b/tests/test_api_usage.nim index 2ec7898..bfaa25b 100644 --- a/tests/test_api_usage.nim +++ b/tests/test_api_usage.nim @@ -107,3 +107,15 @@ test "malformed/truncated RLP": var rlp = rlpFromHex("b8056d6f6f7365") expect MalformedRlpError: discard rlp.inspect + +test "encode byte arrays": + var b1 = [byte(1), 2, 5, 7, 8] + var b2 = [byte(6), 8, 12, 123] + var b3 = [byte(122), 56, 65, 12] + + let rlp = rlpFromBytes(encode((b1, b2, b3))) + check: + rlp.listLen == 3 + rlp.listElem(0).toBytes().toSeq() == @b1 + rlp.listElem(1).toBytes().toSeq() == @b2 + rlp.listElem(2).toBytes().toSeq() == @b3