From 069d73df3152d489e9916829251664d09619ca1f Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Tue, 20 Mar 2018 15:44:17 +0200 Subject: [PATCH] Fixed listElem --- rlp.nim | 2 +- tests/test_api_usage.nim | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/rlp.nim b/rlp.nim index c7ca633..fbff936 100644 --- a/rlp.nim +++ b/rlp.nim @@ -247,7 +247,7 @@ iterator items*(self: var Rlp): var Rlp = position = elemEnd proc listElem*(self: Rlp, i: int): Rlp = - let payload = bytes.slice payloadOffset() + let payload = bytes.slice(position + payloadOffset()) result = rlpFromBytes payload var pos = 0 while pos < i and result.hasData: diff --git a/tests/test_api_usage.nim b/tests/test_api_usage.nim index 04418e2..73d3a75 100644 --- a/tests/test_api_usage.nim +++ b/tests/test_api_usage.nim @@ -70,6 +70,18 @@ test "encode and decode lists": rlp.listElem(1).toString == "Lorem ipsum dolor sit amet" rlp.listElem(2).toString == "Donec ligula tortor, egestas eu est vitae" +test "nested lists": + let listBytes = encode([[1, 2, 3], [5, 6, 7]]) + let listRlp = rlpFromBytes listBytes + let sublistRlp0 = listRlp.listElem(0) + let sublistRlp1 = listRlp.listElem(1) + check sublistRlp0.listElem(0).toInt(int) == 1 + check sublistRlp0.listElem(1).toInt(int) == 2 + check sublistRlp0.listElem(2).toInt(int) == 3 + check sublistRlp1.listElem(0).toInt(int) == 5 + check sublistRlp1.listElem(1).toInt(int) == 6 + check sublistRlp1.listElem(2).toInt(int) == 7 + test "encoding length": let listBytes = encode([1,2,3,4,5]) let listRlp = rlpFromBytes listBytes