2018-01-25 19:22:13 +00:00
|
|
|
import
|
2018-02-16 15:28:19 +00:00
|
|
|
macros, types,
|
|
|
|
ranges/[memranges, ptr_arith],
|
|
|
|
object_serialization, priv/defs
|
|
|
|
|
|
|
|
export
|
|
|
|
memranges
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
RlpWriter* = object
|
|
|
|
pendingLists: seq[tuple[remainingItems, outBytes: int]]
|
|
|
|
output: Bytes
|
|
|
|
|
|
|
|
PrematureFinalizationError* = object of Exception
|
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
IntLike* = concept x, y
|
|
|
|
type T = type(x)
|
|
|
|
|
|
|
|
# arithmetic ops
|
2018-02-21 10:27:34 +00:00
|
|
|
x + y is T
|
|
|
|
x * y is T
|
|
|
|
x - y is T
|
|
|
|
x div y is T
|
|
|
|
x mod y is T
|
2018-01-31 20:55:12 +00:00
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
# some int compatibility required for big endian encoding:
|
|
|
|
x shr int is T
|
|
|
|
x shl int is T
|
|
|
|
x and 0xff is int
|
|
|
|
x < 128 is bool
|
|
|
|
|
|
|
|
Integer* = SomeInteger # or IntLike
|
2018-01-31 20:55:12 +00:00
|
|
|
|
2018-03-13 16:16:53 +00:00
|
|
|
const
|
|
|
|
wrapObjectsInList* = true
|
|
|
|
|
2018-01-31 20:55:12 +00:00
|
|
|
proc bytesNeeded(num: Integer): int =
|
2018-04-01 01:37:06 +00:00
|
|
|
type IntType = type(num)
|
2018-01-25 19:22:13 +00:00
|
|
|
var n = num
|
2018-04-01 01:37:06 +00:00
|
|
|
while n != IntType(0):
|
2018-01-25 19:22:13 +00:00
|
|
|
inc result
|
|
|
|
n = n shr 8
|
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
proc writeBigEndian(outStream: var Bytes, number: Integer,
|
2018-01-31 20:55:12 +00:00
|
|
|
lastByteIdx: int, numberOfBytes: int) =
|
2018-06-26 23:40:49 +00:00
|
|
|
mixin `and`, `shr`
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
var n = number
|
|
|
|
for i in countdown(lastByteIdx, lastByteIdx - int(numberOfBytes) + 1):
|
|
|
|
outStream[i] = byte(n and 0xff)
|
|
|
|
n = n shr 8
|
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
proc writeBigEndian(outStream: var Bytes, number: Integer,
|
2018-01-25 19:22:13 +00:00
|
|
|
numberOfBytes: int) {.inline.} =
|
|
|
|
outStream.setLen(outStream.len + numberOfBytes)
|
|
|
|
outStream.writeBigEndian(number, outStream.len - 1, numberOfBytes)
|
|
|
|
|
|
|
|
proc writeCount(bytes: var Bytes, count: int, baseMarker: byte) =
|
|
|
|
if count < THRESHOLD_LIST_LEN:
|
|
|
|
bytes.add(baseMarker + byte(count))
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
origLen = bytes.len
|
|
|
|
lenPrefixBytes = count.bytesNeeded
|
|
|
|
|
|
|
|
bytes.setLen(origLen + int(lenPrefixBytes) + 1)
|
|
|
|
bytes[origLen] = baseMarker + (THRESHOLD_LIST_LEN - 1) + byte(lenPrefixBytes)
|
|
|
|
bytes.writeBigEndian(count, bytes.len - 1, lenPrefixBytes)
|
|
|
|
|
|
|
|
proc add(outStream: var Bytes, newChunk: BytesRange) =
|
|
|
|
let prevLen = outStream.len
|
|
|
|
outStream.setLen(prevLen + newChunk.len)
|
|
|
|
# XXX: Use copyMem here
|
|
|
|
for i in 0 ..< newChunk.len:
|
|
|
|
outStream[prevLen + i] = newChunk[i]
|
|
|
|
|
|
|
|
{.this: self.}
|
|
|
|
{.experimental.}
|
|
|
|
|
|
|
|
using
|
|
|
|
self: var RlpWriter
|
|
|
|
|
|
|
|
proc initRlpWriter*: RlpWriter =
|
|
|
|
newSeq(result.pendingLists, 0)
|
|
|
|
newSeq(result.output, 0)
|
|
|
|
|
|
|
|
proc decRet(n: var int, delta: int): int =
|
|
|
|
n -= delta
|
|
|
|
return n
|
|
|
|
|
|
|
|
proc maybeClosePendingLists(self) =
|
|
|
|
while pendingLists.len > 0:
|
|
|
|
let lastListIdx = pendingLists.len - 1
|
|
|
|
assert pendingLists[lastListIdx].remainingItems >= 1
|
|
|
|
if decRet(pendingLists[lastListIdx].remainingItems, 1) == 0:
|
|
|
|
# A list have been just finished. It was started in `startList`.
|
|
|
|
let listStartPos = pendingLists[lastListIdx].outBytes
|
|
|
|
pendingLists.setLen lastListIdx
|
|
|
|
|
|
|
|
# How many bytes were written since the start?
|
|
|
|
let listLen = output.len - listStartPos
|
|
|
|
|
|
|
|
# Compute the number of bytes required to write down the list length
|
|
|
|
let totalPrefixBytes = if listLen < int(THRESHOLD_LIST_LEN): 1
|
|
|
|
else: int(listLen.bytesNeeded) + 1
|
|
|
|
|
|
|
|
# Shift the written data to make room for the prefix length
|
|
|
|
output.setLen(output.len + totalPrefixBytes)
|
|
|
|
let outputBaseAddr = output.baseAddr
|
|
|
|
|
|
|
|
moveMem(outputBaseAddr.shift(listStartPos + totalPrefixBytes),
|
|
|
|
outputBaseAddr.shift(listStartPos),
|
|
|
|
listLen)
|
|
|
|
|
|
|
|
# Write out the prefix length
|
|
|
|
if listLen < THRESHOLD_LIST_LEN:
|
|
|
|
output[listStartPos] = LIST_START_MARKER + byte(listLen)
|
|
|
|
else:
|
|
|
|
let listLenBytes = totalPrefixBytes - 1
|
|
|
|
output[listStartPos] = LEN_PREFIXED_LIST_MARKER + byte(listLenBytes)
|
|
|
|
output.writeBigEndian(listLen, listStartPos + listLenBytes, listLenBytes)
|
|
|
|
else:
|
|
|
|
# The currently open list is not finished yet. Nothing to do.
|
|
|
|
return
|
|
|
|
|
|
|
|
proc appendRawList(self; bytes: BytesRange) =
|
|
|
|
output.writeCount(bytes.len, LIST_START_MARKER)
|
|
|
|
output.add(bytes)
|
|
|
|
maybeClosePendingLists()
|
|
|
|
|
2018-06-23 16:49:04 +00:00
|
|
|
proc appendRawBytes*(self; bytes: BytesRange) =
|
|
|
|
output.add(bytes)
|
|
|
|
maybeClosePendingLists()
|
|
|
|
|
2018-05-16 07:37:26 +00:00
|
|
|
proc startList*(self; listSize: int) =
|
2018-01-25 19:22:13 +00:00
|
|
|
if listSize == 0:
|
2018-03-26 15:14:51 +00:00
|
|
|
appendRawList(BytesRange())
|
2018-01-25 19:22:13 +00:00
|
|
|
else:
|
|
|
|
pendingLists.add((listSize, output.len))
|
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
template appendBlob(self; data, startMarker) =
|
2018-02-16 15:28:19 +00:00
|
|
|
mixin baseAddr
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
if data.len == 1 and byte(data[0]) < BLOB_START_MARKER:
|
2018-02-16 15:28:19 +00:00
|
|
|
self.output.add byte(data[0])
|
2018-01-25 19:22:13 +00:00
|
|
|
else:
|
2018-02-16 15:28:19 +00:00
|
|
|
self.output.writeCount(data.len, startMarker)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
let startPos = output.len
|
2018-02-16 15:28:19 +00:00
|
|
|
self.output.setLen(startPos + data.len)
|
|
|
|
copyMem(shift(baseAddr(self.output), startPos),
|
|
|
|
baseAddr(data),
|
|
|
|
data.len)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
maybeClosePendingLists()
|
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
proc appendImpl(self; data: string) =
|
|
|
|
appendBlob(self, data, BLOB_START_MARKER)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
2018-03-31 09:07:55 +00:00
|
|
|
proc appendBlob(self; data: openarray[byte]) =
|
2018-05-18 11:32:06 +00:00
|
|
|
appendBlob(self, data, BLOB_START_MARKER)
|
2018-01-31 20:55:12 +00:00
|
|
|
|
2018-04-01 02:37:50 +00:00
|
|
|
proc appendBlob(self; data: openarray[char]) =
|
2018-05-18 11:32:06 +00:00
|
|
|
appendBlob(self, data, BLOB_START_MARKER)
|
2018-04-01 02:37:50 +00:00
|
|
|
|
2018-03-26 15:14:51 +00:00
|
|
|
proc appendBytesRange(self; data: BytesRange) =
|
2018-05-18 11:32:06 +00:00
|
|
|
appendBlob(self, data, BLOB_START_MARKER)
|
2018-02-16 15:28:19 +00:00
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
proc appendImpl(self; data: MemRange) =
|
|
|
|
appendBlob(self, data, BLOB_START_MARKER)
|
2018-02-16 15:28:19 +00:00
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
proc appendInt(self; i: Integer) =
|
|
|
|
# this is created as a separate proc as an extra precaution against
|
|
|
|
# any overloading resolution problems when matching the IntLike concept.
|
2018-04-01 01:37:06 +00:00
|
|
|
type IntType = type(i)
|
|
|
|
|
|
|
|
if i == IntType(0):
|
2018-01-31 20:55:12 +00:00
|
|
|
self.output.add BLOB_START_MARKER
|
2018-02-21 10:27:34 +00:00
|
|
|
elif i < BLOB_START_MARKER.Integer:
|
2018-01-31 20:55:12 +00:00
|
|
|
self.output.add byte(i)
|
2018-01-25 19:22:13 +00:00
|
|
|
else:
|
|
|
|
let bytesNeeded = i.bytesNeeded
|
2018-01-31 20:55:12 +00:00
|
|
|
self.output.writeCount(bytesNeeded, BLOB_START_MARKER)
|
2018-06-26 23:40:49 +00:00
|
|
|
self.output.writeBigEndian(i, bytesNeeded)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
2018-01-31 20:55:12 +00:00
|
|
|
self.maybeClosePendingLists()
|
2018-01-25 19:22:13 +00:00
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
template appendImpl(self; i: Integer) =
|
|
|
|
appendInt(self, i)
|
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
template appendImpl(self; e: enum) =
|
|
|
|
appendImpl(self, int(e))
|
2018-04-01 01:37:06 +00:00
|
|
|
|
2018-07-22 11:19:24 +00:00
|
|
|
template appendImpl(self; b: bool) =
|
|
|
|
appendImpl(self, int(b))
|
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
proc appendImpl[T](self; listOrBlob: openarray[T]) =
|
2018-04-01 02:20:46 +00:00
|
|
|
mixin append
|
|
|
|
|
2018-03-31 09:07:55 +00:00
|
|
|
# TODO: This append proc should be overloaded by `openarray[byte]` after
|
|
|
|
# nim bug #7416 is fixed.
|
2018-04-01 02:37:50 +00:00
|
|
|
when T is (byte or char):
|
2018-03-31 09:07:55 +00:00
|
|
|
self.appendBlob(listOrBlob)
|
|
|
|
else:
|
|
|
|
self.startList listOrBlob.len
|
|
|
|
for i in 0 ..< listOrBlob.len:
|
|
|
|
self.append listOrBlob[i]
|
2018-01-25 19:22:13 +00:00
|
|
|
|
2018-05-16 07:37:26 +00:00
|
|
|
proc appendTupleOrObject(self; data: object|tuple, wrapInList: bool) =
|
|
|
|
mixin enumerateRlpFields, append
|
|
|
|
|
|
|
|
const wrapInList = wrapObjectsInList
|
|
|
|
|
|
|
|
if wrapInList:
|
|
|
|
var fieldsCount = 0
|
|
|
|
template countFields(x) = inc fieldsCount
|
|
|
|
enumerateRlpFields(data, countFields)
|
|
|
|
self.startList(fieldsCount)
|
|
|
|
|
|
|
|
template op(x) = append(self, x)
|
|
|
|
enumerateRlpFields(data, op)
|
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
proc appendImpl(self; data: object, wrapInList = wrapObjectsInList) {.inline.} =
|
2018-03-26 15:14:51 +00:00
|
|
|
# TODO: This append proc should be overloaded by `BytesRange` after
|
|
|
|
# nim bug #7416 is fixed.
|
|
|
|
when data is BytesRange:
|
2018-05-16 07:37:26 +00:00
|
|
|
self.appendBytesRange(data)
|
2018-03-26 15:14:51 +00:00
|
|
|
else:
|
2018-05-16 07:37:26 +00:00
|
|
|
self.appendTupleOrObject(data, wrapInList)
|
2018-03-13 16:16:53 +00:00
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
proc appendImpl(self; data: tuple, wrapInList = wrapObjectsInList) {.inline.} =
|
2018-05-16 07:37:26 +00:00
|
|
|
self.appendTupleOrObject(data, wrapInList)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
2018-05-18 11:32:06 +00:00
|
|
|
# We define a single `append` template with a pretty low specifity
|
|
|
|
# score in order to facilitate easier overloading with user types:
|
|
|
|
template append*[T](self; data: T) = appendImpl(self, data)
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
proc initRlpList*(listSize: int): RlpWriter =
|
|
|
|
result = initRlpWriter()
|
|
|
|
startList(result, listSize)
|
|
|
|
|
|
|
|
proc finish*(self): BytesRange =
|
|
|
|
if pendingLists.len > 0:
|
|
|
|
raise newException(PrematureFinalizationError,
|
|
|
|
"Insufficient number of elements written to a started list")
|
2018-03-26 15:14:51 +00:00
|
|
|
result = output.toRange()
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
proc encode*[T](v: T): BytesRange =
|
2018-05-18 11:32:06 +00:00
|
|
|
mixin append
|
2018-01-25 19:22:13 +00:00
|
|
|
var writer = initRlpWriter()
|
|
|
|
writer.append(v)
|
|
|
|
return writer.finish
|
|
|
|
|
2018-06-26 23:40:49 +00:00
|
|
|
proc encodeInt*(i: Integer): BytesRange =
|
|
|
|
var writer = initRlpWriter()
|
|
|
|
writer.appendInt(i)
|
|
|
|
return writer.finish
|
|
|
|
|
2018-01-25 19:22:13 +00:00
|
|
|
macro encodeList*(args: varargs[untyped]): BytesRange =
|
|
|
|
var
|
|
|
|
listLen = args.len
|
|
|
|
writer = genSym(nskVar, "rlpWriter")
|
|
|
|
body = newStmtList()
|
2018-05-18 11:32:06 +00:00
|
|
|
append = bindSym("append", brForceOpen)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
for arg in args:
|
|
|
|
body.add quote do:
|
2018-05-18 11:32:06 +00:00
|
|
|
`append`(`writer`, `arg`)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
result = quote do:
|
|
|
|
var `writer` = initRlpList(`listLen`)
|
|
|
|
`body`
|
2018-02-16 15:28:19 +00:00
|
|
|
finish(`writer`)
|
2018-01-25 19:22:13 +00:00
|
|
|
|
|
|
|
when false:
|
|
|
|
# XXX: Currently fails with a malformed AST error on the args.len expression
|
|
|
|
template encodeList*(args: varargs[untyped]): BytesRange =
|
2018-05-18 11:32:06 +00:00
|
|
|
mixin append
|
2018-01-25 19:22:13 +00:00
|
|
|
var writer = initRlpList(args.len)
|
|
|
|
for arg in args:
|
|
|
|
writer.append(arg)
|
|
|
|
writer.finish
|
|
|
|
|