Fixed listElem

This commit is contained in:
Yuriy Glukhov 2018-03-20 15:44:17 +02:00 committed by zah
parent 61da7236f4
commit 069d73df31
2 changed files with 13 additions and 1 deletions

View File

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

View File

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