wip: strange error

This commit is contained in:
Jacek Sieka 2018-09-24 19:13:59 -06:00
parent 0426e181af
commit 796222a9d2
No known key found for this signature in database
GPG Key ID: 6299FEB3EB6FA465
5 changed files with 21 additions and 19 deletions

View File

@ -391,12 +391,14 @@ proc decode*(bytes: openarray[byte]): RlpNode =
template decode*(bytes: BytesRange, T: typedesc): untyped = template decode*(bytes: BytesRange, T: typedesc): untyped =
mixin read mixin read
var rlp = rlpFromBytes bytes var rlp = rlpFromBytes(bytes)
rlp.read(T) rlp.read(T)
template decode*(bytes: openarray[byte], T: typedesc): T = template decode*(bytes: openarray[byte], T: typedesc): T =
var bytesCopy = @bytes var bytesCopy = @bytes
decode(initBytesRange(bytesCopy), T) decode(bytesCopy.toRange(), T)
template decode*(bytes: seq[byte], T: typedesc): T =
decode(bytes.toRange(), T)
proc append*(writer: var RlpWriter; rlp: Rlp) = proc append*(writer: var RlpWriter; rlp: Rlp) =
appendRawBytes(writer, rlp.rawData) appendRawBytes(writer, rlp.rawData)

View File

@ -244,24 +244,24 @@ proc initRlpList*(listSize: int): RlpWriter =
result = initRlpWriter() result = initRlpWriter()
startList(result, listSize) startList(result, listSize)
proc finish*(self): BytesRange = proc finish*(self): seq[byte] =
if pendingLists.len > 0: if pendingLists.len > 0:
raise newException(PrematureFinalizationError, raise newException(PrematureFinalizationError,
"Insufficient number of elements written to a started list") "Insufficient number of elements written to a started list")
result = output.toRange() result = output
proc encode*[T](v: T): BytesRange = proc encode*[T](v: T): seq[byte] =
mixin append mixin append
var writer = initRlpWriter() var writer = initRlpWriter()
writer.append(v) writer.append(v)
return writer.finish return writer.finish
proc encodeInt*(i: Integer): BytesRange = proc encodeInt*(i: Integer): seq[byte] =
var writer = initRlpWriter() var writer = initRlpWriter()
writer.appendInt(i) writer.appendInt(i)
return writer.finish return writer.finish
macro encodeList*(args: varargs[untyped]): BytesRange = macro encodeList*(args: varargs[untyped]): seq[byte] =
var var
listLen = args.len listLen = args.len
writer = genSym(nskVar, "rlpWriter") writer = genSym(nskVar, "rlpWriter")

View File

@ -54,7 +54,7 @@ test "encode/decode object":
var writer = initRlpWriter() var writer = initRlpWriter()
writer.append(input) writer.append(input)
let bytes = writer.finish() let bytes = writer.finish()
var rlp = rlpFromBytes(bytes) var rlp = rlpFromBytes(bytes.toRange)
var output = rlp.read(MyObj) var output = rlp.read(MyObj)
check: check:
@ -68,7 +68,7 @@ test "encode and decode lists":
var var
bytes = writer.finish bytes = writer.finish
rlp = rlpFromBytes bytes rlp = rlpFromBytes bytes.toRange
check: check:
bytes.hexRepr == "d183666f6fc8836261728362617ac31e2832" bytes.hexRepr == "d183666f6fc8836261728362617ac31e2832"
@ -91,7 +91,7 @@ test "encode and decode lists":
"Lorem ipsum dolor sit amet", "Lorem ipsum dolor sit amet",
"Donec ligula tortor, egestas eu est vitae") "Donec ligula tortor, egestas eu est vitae")
rlp = rlpFromBytes bytes rlp = rlpFromBytes bytes.toRange
check: check:
rlp.listLen == 3 rlp.listLen == 3
rlp.listElem(0).toInt(int) == 6000 rlp.listElem(0).toInt(int) == 6000
@ -99,7 +99,7 @@ test "encode and decode lists":
rlp.listElem(2).toString == "Donec ligula tortor, egestas eu est vitae" rlp.listElem(2).toString == "Donec ligula tortor, egestas eu est vitae"
# test creating RLPs from other RLPs # test creating RLPs from other RLPs
var list = rlpFromBytes encodeList(rlp.listELem(1), rlp.listELem(0)) var list = rlpFromBytes encodeList(rlp.listELem(1), rlp.listELem(0)).toRange
# test that iteration with enterList/skipElem works as expected # test that iteration with enterList/skipElem works as expected
list.enterList list.enterList
@ -119,7 +119,7 @@ test "toBytes":
test "nested lists": test "nested lists":
let listBytes = encode([[1, 2, 3], [5, 6, 7]]) let listBytes = encode([[1, 2, 3], [5, 6, 7]])
let listRlp = rlpFromBytes listBytes let listRlp = rlpFromBytes listBytes.toRange
let sublistRlp0 = listRlp.listElem(0) let sublistRlp0 = listRlp.listElem(0)
let sublistRlp1 = listRlp.listElem(1) let sublistRlp1 = listRlp.listElem(1)
check sublistRlp0.listElem(0).toInt(int) == 1 check sublistRlp0.listElem(0).toInt(int) == 1
@ -131,12 +131,12 @@ test "nested lists":
test "encoding length": test "encoding length":
let listBytes = encode([1,2,3,4,5]) let listBytes = encode([1,2,3,4,5])
let listRlp = rlpFromBytes listBytes let listRlp = rlpFromBytes listBytes.toRange
check listRlp.listLen == 5 check listRlp.listLen == 5
let emptyListBytes = encode "" let emptyListBytes = encode ""
check emptyListBytes.len == 1 check emptyListBytes.len == 1
let emptyListRlp = rlpFromBytes emptyListBytes let emptyListRlp = rlpFromBytes emptyListBytes.toRange
check emptyListRlp.blobLen == 0 check emptyListRlp.blobLen == 0
test "basic decoding": test "basic decoding":
@ -157,7 +157,7 @@ test "encode byte arrays":
var b2 = [byte(6), 8, 12, 123] var b2 = [byte(6), 8, 12, 123]
var b3 = @[byte(122), 56, 65, 12] var b3 = @[byte(122), 56, 65, 12]
let rlp = rlpFromBytes(encode((b1, b2, b3))) let rlp = rlpFromBytes(encode((b1, b2, b3)).toRange)
check: check:
rlp.listLen == 3 rlp.listLen == 3
rlp.listElem(0).toBytes().toSeq() == @b1 rlp.listElem(0).toBytes().toSeq() == @b1
@ -169,6 +169,6 @@ test "encode byte arrays":
test "empty byte arrays": test "empty byte arrays":
var var
rlp = rlpFromBytes rlp.encode("") rlp = rlpFromBytes rlp.encode("").toRange
b = rlp.toBytes b = rlp.toBytes
check $b == "R[]" check $b == "R[]"

View File

@ -46,7 +46,7 @@ test "encoding and decoding an object":
f: Foo(x: 5'u64, y: "hocus pocus", z: @[100, 200, 300])) f: Foo(x: 5'u64, y: "hocus pocus", z: @[100, 200, 300]))
var bytes = encode(originalBar) var bytes = encode(originalBar)
var r = rlpFromBytes(bytes) var r = rlpFromBytes(bytes.toRange)
var restoredBar = r.read(Bar) var restoredBar = r.read(Bar)
check: check:
@ -66,7 +66,7 @@ test "encoding and decoding an object":
test "custom field serialization": test "custom field serialization":
var origVal = CustomSerialized(customFoo: Foo(x: 10'u64, y: "y", z: @[]), ignored: 5) var origVal = CustomSerialized(customFoo: Foo(x: 10'u64, y: "y", z: @[]), ignored: 5)
var bytes = encode(origVal) var bytes = encode(origVal)
var r = rlpFromBytes(bytes) var r = rlpFromBytes(bytes.toRange)
var restored = r.read(CustomSerialized) var restored = r.read(CustomSerialized)
check: check:

View File

@ -14,7 +14,7 @@ proc append(output: var RlpWriter, js: JsonNode) =
of JArray: of JArray:
output.append js.elems output.append js.elems
proc hexRepr*(bytes: BytesRange): string = proc hexRepr*(bytes: BytesRange|Bytes): string =
result = newStringOfCap(bytes.len * 2) result = newStringOfCap(bytes.len * 2)
for byte in bytes: for byte in bytes:
result.add(toHex(int(byte), 2).toLowerAscii) result.add(toHex(int(byte), 2).toLowerAscii)