mirror of
https://github.com/status-im/nim-eth.git
synced 2025-01-26 06:21:17 +00:00
make lengths a tuple
This commit is contained in:
parent
20b8ee7acc
commit
57de6079ec
@ -4,13 +4,13 @@ import
|
|||||||
nimcrypto/keccak,
|
nimcrypto/keccak,
|
||||||
stew/[arraybuf, shims/macros],
|
stew/[arraybuf, shims/macros],
|
||||||
./priv/defs,
|
./priv/defs,
|
||||||
utils
|
utils,
|
||||||
|
length_writer
|
||||||
|
|
||||||
type
|
type
|
||||||
RlpHashWriter* = object
|
RlpHashWriter* = object
|
||||||
keccak: keccak.keccak256
|
keccak: keccak.keccak256
|
||||||
listLengths*: seq[int]
|
lengths*: seq[tuple[listLen, prefixLen: int]]
|
||||||
prefixLengths*: seq[int]
|
|
||||||
listCount: int
|
listCount: int
|
||||||
bigEndianBuf: array[8, byte]
|
bigEndianBuf: array[8, byte]
|
||||||
|
|
||||||
@ -61,8 +61,8 @@ proc startList*(self: var RlpHashWriter, listSize: int) =
|
|||||||
self.writeCount(0, LIST_START_MARKER)
|
self.writeCount(0, LIST_START_MARKER)
|
||||||
else:
|
else:
|
||||||
let
|
let
|
||||||
prefixLen = self.prefixLengths[self.listCount]
|
prefixLen = self.lengths[self.listCount].prefixLen
|
||||||
listLen = self.listLengths[self.listCount]
|
listLen = self.lengths[self.listCount].listLen
|
||||||
|
|
||||||
self.listCount += 1
|
self.listCount += 1
|
||||||
|
|
||||||
@ -74,13 +74,14 @@ proc startList*(self: var RlpHashWriter, listSize: int) =
|
|||||||
|
|
||||||
self.updateBigEndian(uint64(listLen), listLenBytes)
|
self.updateBigEndian(uint64(listLen), listLenBytes)
|
||||||
|
|
||||||
|
func initHashWriter*(tracker: var RlpLengthTracker): RlpHashWriter =
|
||||||
|
result.lengths = move(tracker.lengths)
|
||||||
|
|
||||||
template finish*(self: var RlpHashWriter): MDigest[self.keccak.bits] =
|
template finish*(self: var RlpHashWriter): MDigest[self.keccak.bits] =
|
||||||
self.listLengths.setLen(0)
|
self.lengths.setLen(0)
|
||||||
self.prefixLengths.setLen(0)
|
|
||||||
self.keccak.finish()
|
self.keccak.finish()
|
||||||
|
|
||||||
func clear*(w: var RlpHashWriter) =
|
func clear*(w: var RlpHashWriter) =
|
||||||
# Prepare writer for reuse
|
# Prepare writer for reuse
|
||||||
w.listLengths.setLen(0)
|
w.lengths.setLen(0)
|
||||||
w.prefixLengths.setLen(0)
|
|
||||||
|
|
||||||
|
@ -7,8 +7,7 @@ import
|
|||||||
|
|
||||||
type
|
type
|
||||||
RlpLengthTracker* = object
|
RlpLengthTracker* = object
|
||||||
prefixLengths*: seq[int]
|
lengths*: seq[tuple[listLen, prefixLen: int]]
|
||||||
listLengths*: seq[int]
|
|
||||||
pendingLists: seq[tuple[idx, remainingItems, startLen: int]]
|
pendingLists: seq[tuple[idx, remainingItems, startLen: int]]
|
||||||
listCount: int
|
listCount: int
|
||||||
totalLength*: int
|
totalLength*: int
|
||||||
@ -29,8 +28,7 @@ proc maybeClosePendingLists(self: var RlpLengthTracker) =
|
|||||||
else: int(uint64(listLen).bytesNeeded) + 1
|
else: int(uint64(listLen).bytesNeeded) + 1
|
||||||
|
|
||||||
# save the list lengths and prefix lengths
|
# save the list lengths and prefix lengths
|
||||||
self.listLengths[listIdx] = listLen
|
self.lengths[listIdx] = (listLen, prefixLen)
|
||||||
self.prefixLengths[listIdx] = prefixLen
|
|
||||||
|
|
||||||
# close the list by deleting
|
# close the list by deleting
|
||||||
self.pendingLists.setLen(lastIdx)
|
self.pendingLists.setLen(lastIdx)
|
||||||
@ -51,9 +49,8 @@ proc startList*(self: var RlpLengthTracker, listSize: int) =
|
|||||||
# open a list
|
# open a list
|
||||||
self.pendingLists.add((self.listCount, listSize, self.totalLength))
|
self.pendingLists.add((self.listCount, listSize, self.totalLength))
|
||||||
self.listCount += 1
|
self.listCount += 1
|
||||||
if self.listCount == self.listLengths.len:
|
if self.listCount == self.lengths.len:
|
||||||
self.listLengths.setLen(self.listLengths.len + LIST_LENGTH)
|
self.lengths.setLen(self.lengths.len + LIST_LENGTH)
|
||||||
self.prefixLengths.setLen(self.prefixLengths.len + LIST_LENGTH)
|
|
||||||
|
|
||||||
func lengthCount(count: int): int {.inline.} =
|
func lengthCount(count: int): int {.inline.} =
|
||||||
return if count < THRESHOLD_LIST_LEN: 1
|
return if count < THRESHOLD_LIST_LEN: 1
|
||||||
@ -76,10 +73,7 @@ func writeInt*(self: var RlpLengthTracker, i: SomeUnsignedInt) =
|
|||||||
func initLengthTracker*(): RlpLengthTracker =
|
func initLengthTracker*(): RlpLengthTracker =
|
||||||
# we preset the lengths since we want to skip using add method for
|
# we preset the lengths since we want to skip using add method for
|
||||||
# these lists
|
# these lists
|
||||||
result.prefixLengths = newSeqOfCap[int](LIST_LENGTH)
|
result.lengths = newSeq[(int, int)](LIST_LENGTH)
|
||||||
result.prefixLengths.setLen(LIST_LENGTH)
|
|
||||||
result.listLengths = newSeqOfCap[int](LIST_LENGTH)
|
|
||||||
result.listLengths.setLen(LIST_LENGTH)
|
|
||||||
|
|
||||||
template finish*(self: RlpLengthTracker): int =
|
template finish*(self: RlpLengthTracker): int =
|
||||||
doAssert self.pendingLists.len == 0,
|
doAssert self.pendingLists.len == 0,
|
||||||
@ -89,5 +83,4 @@ template finish*(self: RlpLengthTracker): int =
|
|||||||
func clear*(w: var RlpLengthTracker) =
|
func clear*(w: var RlpLengthTracker) =
|
||||||
# Prepare writer for reuse
|
# Prepare writer for reuse
|
||||||
w.pendingLists.setLen(0)
|
w.pendingLists.setLen(0)
|
||||||
w.prefixLengths.setLen(0)
|
w.lengths.setLen(0)
|
||||||
w.listLengths.setLen(0)
|
|
||||||
|
@ -3,14 +3,14 @@ import
|
|||||||
pkg/results,
|
pkg/results,
|
||||||
stew/[arraybuf, assign2, shims/macros],
|
stew/[arraybuf, assign2, shims/macros],
|
||||||
./priv/defs,
|
./priv/defs,
|
||||||
utils
|
utils,
|
||||||
|
length_writer
|
||||||
|
|
||||||
type
|
type
|
||||||
RlpTwoPassWriter* = object
|
RlpTwoPassWriter* = object
|
||||||
pendingLists*: seq[tuple[remainingItems, startPos, prefixLen: int]]
|
pendingLists*: seq[tuple[remainingItems, startPos, prefixLen: int]]
|
||||||
output*: seq[byte]
|
output*: seq[byte]
|
||||||
prefixLengths*: seq[int]
|
lengths*: seq[tuple[listLen, prefixLen: int]]
|
||||||
listLengths*: seq[int]
|
|
||||||
fillLevel: int
|
fillLevel: int
|
||||||
listCount: int
|
listCount: int
|
||||||
|
|
||||||
@ -61,8 +61,8 @@ proc startList*(self: var RlpTwoPassWriter, listSize: int) =
|
|||||||
self.writeCount(0, LIST_START_MARKER)
|
self.writeCount(0, LIST_START_MARKER)
|
||||||
else:
|
else:
|
||||||
let
|
let
|
||||||
prefixLen = self.prefixLengths[self.listCount]
|
prefixLen = self.lengths[self.listCount].prefixLen
|
||||||
listLen = self.listLengths[self.listCount]
|
listLen = self.lengths[self.listCount].listLen
|
||||||
|
|
||||||
self.listCount += 1
|
self.listCount += 1
|
||||||
|
|
||||||
@ -76,22 +76,19 @@ proc startList*(self: var RlpTwoPassWriter, listSize: int) =
|
|||||||
self.output.writeBigEndian(uint64(listLen), self.fillLevel - 1,
|
self.output.writeBigEndian(uint64(listLen), self.fillLevel - 1,
|
||||||
listLenBytes)
|
listLenBytes)
|
||||||
|
|
||||||
func initTwoPassWriter*(length: int): RlpTwoPassWriter =
|
func initTwoPassWriter*(tracker: var RlpLengthTracker): RlpTwoPassWriter =
|
||||||
result.fillLevel = 0
|
result.fillLevel = 0
|
||||||
result.listCount = 0
|
result.listCount = 0
|
||||||
result.output = newSeqOfCap[byte](length)
|
result.output = newSeq[byte](tracker.totalLength)
|
||||||
result.output.setLen(length)
|
result.lengths = move(tracker.lengths)
|
||||||
|
|
||||||
template finish*(self: RlpTwoPassWriter): seq[byte] =
|
template finish*(self: RlpTwoPassWriter): seq[byte] =
|
||||||
doAssert self.pendingLists.len == 0,
|
doAssert self.pendingLists.len == 0,
|
||||||
"Insufficient number of elements written to a started list"
|
"Insufficient number of elements written to a started list"
|
||||||
self.prefixLengths.setLen(0)
|
self.lengths.setLen(0)
|
||||||
self.listLengths.setLen(0)
|
|
||||||
self.output
|
self.output
|
||||||
|
|
||||||
func clear*(w: var RlpTwoPassWriter) =
|
func clear*(w: var RlpTwoPassWriter) =
|
||||||
# Prepare writer for reuse
|
# Prepare writer for reuse
|
||||||
w.pendingLists.setLen(0)
|
w.pendingLists.setLen(0)
|
||||||
w.output.setLen(0)
|
w.output.setLen(0)
|
||||||
w.prefixLengths.setLen(0)
|
|
||||||
w.listLengths.setLen(0)
|
|
||||||
|
@ -189,9 +189,7 @@ proc encode*[T](v: T): seq[byte] =
|
|||||||
|
|
||||||
tracker.append(v)
|
tracker.append(v)
|
||||||
|
|
||||||
var writer = initTwoPassWriter(tracker.totalLength)
|
var writer = initTwoPassWriter(tracker)
|
||||||
writer.prefixLengths = tracker.prefixLengths
|
|
||||||
writer.listLengths = tracker.listLengths
|
|
||||||
writer.append(v)
|
writer.append(v)
|
||||||
|
|
||||||
move(writer.finish)
|
move(writer.finish)
|
||||||
@ -200,12 +198,10 @@ proc encodeHash*[T](v: T): Hash32 =
|
|||||||
mixin append
|
mixin append
|
||||||
|
|
||||||
var tracker = initLengthTracker()
|
var tracker = initLengthTracker()
|
||||||
var writer: RlpHashWriter
|
|
||||||
|
|
||||||
tracker.append(v)
|
tracker.append(v)
|
||||||
|
|
||||||
writer.prefixLengths = tracker.prefixLengths
|
var writer = initHashWriter(tracker)
|
||||||
writer.listLengths = tracker.listLengths
|
|
||||||
writer.append(v)
|
writer.append(v)
|
||||||
|
|
||||||
writer.finish.to(Hash32)
|
writer.finish.to(Hash32)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user