Merge pull request #7 from yglukhov/fix-append-openarray-byte

openarray[byte] is now treated as blob instead of list
This commit is contained in:
Yuriy Glukhov 2018-03-31 12:57:12 +03:00 committed by GitHub
commit e0fa989449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -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

View File

@ -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