2023-05-11 14:25:29 +00:00
|
|
|
# nimbus-eth1
|
2024-02-20 03:07:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-05-11 14:25:29 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-01-24 05:09:05 +00:00
|
|
|
std/[bitops, sequtils, sets, tables],
|
2023-11-08 12:18:32 +00:00
|
|
|
eth/[common, trie/nibbles],
|
2023-09-15 15:23:53 +00:00
|
|
|
results,
|
|
|
|
stew/endians2,
|
2023-11-08 12:18:32 +00:00
|
|
|
./aristo_desc
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-07-05 20:27:48 +00:00
|
|
|
# Private helper
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc load64(data: openArray[byte]; start: var int): Result[uint64,AristoError] =
|
2023-07-05 20:27:48 +00:00
|
|
|
if data.len < start + 9:
|
|
|
|
return err(DeblobPayloadTooShortInt64)
|
2024-05-23 15:37:51 +00:00
|
|
|
let val = uint64.fromBytesBE(data.toOpenArray(start, start + 7))
|
2023-07-05 20:27:48 +00:00
|
|
|
start += 8
|
|
|
|
ok val
|
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc load256(data: openArray[byte]; start: var int): Result[UInt256,AristoError] =
|
2023-07-05 20:27:48 +00:00
|
|
|
if data.len < start + 33:
|
|
|
|
return err(DeblobPayloadTooShortInt256)
|
2024-05-23 15:37:51 +00:00
|
|
|
let val = UInt256.fromBytesBE(data.toOpenArray(start, start + 31))
|
2023-07-05 20:27:48 +00:00
|
|
|
start += 32
|
|
|
|
ok val
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-09-15 15:23:53 +00:00
|
|
|
# Public functions
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2023-07-05 20:27:48 +00:00
|
|
|
proc blobify*(pyl: PayloadRef): Blob =
|
|
|
|
if pyl.isNil:
|
|
|
|
return
|
|
|
|
case pyl.pType
|
|
|
|
of RawData:
|
2023-08-21 18:18:06 +00:00
|
|
|
result = pyl.rawBlob & @[0x6b.byte]
|
2023-07-05 20:27:48 +00:00
|
|
|
of RlpData:
|
2023-08-21 18:18:06 +00:00
|
|
|
result = pyl.rlpBlob & @[0x6a.byte]
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
of AccountData:
|
|
|
|
var mask: byte
|
|
|
|
if 0 < pyl.account.nonce:
|
|
|
|
mask = mask or 0x01
|
|
|
|
result &= pyl.account.nonce.uint64.toBytesBE.toSeq
|
|
|
|
|
|
|
|
if high(uint64).u256 < pyl.account.balance:
|
|
|
|
mask = mask or 0x08
|
2023-11-24 22:16:21 +00:00
|
|
|
result &= pyl.account.balance.toBytesBE.toSeq
|
2023-07-05 20:27:48 +00:00
|
|
|
elif 0 < pyl.account.balance:
|
|
|
|
mask = mask or 0x04
|
|
|
|
result &= pyl.account.balance.truncate(uint64).uint64.toBytesBE.toSeq
|
|
|
|
|
|
|
|
if VertexID(0) < pyl.account.storageID:
|
|
|
|
mask = mask or 0x10
|
|
|
|
result &= pyl.account.storageID.uint64.toBytesBE.toSeq
|
|
|
|
|
|
|
|
if pyl.account.codeHash != VOID_CODE_HASH:
|
|
|
|
mask = mask or 0x80
|
|
|
|
result &= pyl.account.codeHash.data.toSeq
|
|
|
|
|
|
|
|
result &= @[mask]
|
|
|
|
|
2023-09-12 18:45:12 +00:00
|
|
|
proc blobify*(vtx: VertexRef; data: var Blob): Result[void,AristoError] =
|
2023-06-30 22:22:33 +00:00
|
|
|
## This function serialises the vertex argument to a database record.
|
|
|
|
## Contrary to RLP based serialisation, these records aim to align on
|
|
|
|
## fixed byte boundaries.
|
2023-05-11 14:25:29 +00:00
|
|
|
## ::
|
|
|
|
## Branch:
|
2023-06-12 18:16:03 +00:00
|
|
|
## uint64, ... -- list of up to 16 child vertices lookup keys
|
2023-05-11 14:25:29 +00:00
|
|
|
## uint16 -- index bitmap
|
2023-08-21 18:18:06 +00:00
|
|
|
## 0x08 -- marker(8)
|
2023-05-11 14:25:29 +00:00
|
|
|
##
|
|
|
|
## Extension:
|
2023-06-12 18:16:03 +00:00
|
|
|
## uint64 -- child vertex lookup key
|
2023-05-11 14:25:29 +00:00
|
|
|
## Blob -- hex encoded partial path (at least one byte)
|
2023-08-21 18:18:06 +00:00
|
|
|
## 0x80 + xx -- marker(2) + pathSegmentLen(6)
|
2023-05-11 14:25:29 +00:00
|
|
|
##
|
|
|
|
## Leaf:
|
|
|
|
## Blob -- opaque leaf data payload (might be zero length)
|
|
|
|
## Blob -- hex encoded partial path (at least one byte)
|
2023-08-21 18:18:06 +00:00
|
|
|
## 0xc0 + yy -- marker(2) + partialPathLen(6)
|
2023-05-11 14:25:29 +00:00
|
|
|
##
|
|
|
|
## For a branch record, the bytes of the `access` array indicate the position
|
2023-06-12 18:16:03 +00:00
|
|
|
## of the Patricia Trie vertex reference. So the `vertexID` with index `n` has
|
2023-05-11 14:25:29 +00:00
|
|
|
## ::
|
|
|
|
## 8 * n * ((access shr (n * 4)) and 15)
|
|
|
|
##
|
2023-09-05 13:57:20 +00:00
|
|
|
if not vtx.isValid:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyNilVertex)
|
2023-06-30 22:22:33 +00:00
|
|
|
case vtx.vType:
|
2023-05-11 14:25:29 +00:00
|
|
|
of Branch:
|
|
|
|
var
|
|
|
|
access = 0u16
|
|
|
|
refs: Blob
|
|
|
|
for n in 0..15:
|
2023-06-30 22:22:33 +00:00
|
|
|
if vtx.bVid[n].isValid:
|
2023-05-11 14:25:29 +00:00
|
|
|
access = access or (1u16 shl n)
|
2023-06-30 22:22:33 +00:00
|
|
|
refs &= vtx.bVid[n].uint64.toBytesBE.toSeq
|
|
|
|
if refs.len < 16:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyBranchMissingRefs)
|
2023-08-21 18:18:06 +00:00
|
|
|
data = refs & access.toBytesBE.toSeq & @[0x08u8]
|
2023-05-11 14:25:29 +00:00
|
|
|
of Extension:
|
|
|
|
let
|
2023-06-30 22:22:33 +00:00
|
|
|
pSegm = vtx.ePfx.hexPrefixEncode(isleaf = false)
|
2023-05-11 14:25:29 +00:00
|
|
|
psLen = pSegm.len.byte
|
2024-02-20 03:07:38 +00:00
|
|
|
if psLen == 0 or 33 < psLen:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyExtPathOverflow)
|
2023-06-30 22:22:33 +00:00
|
|
|
if not vtx.eVid.isValid:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyExtMissingRefs)
|
2023-06-30 22:22:33 +00:00
|
|
|
data = vtx.eVid.uint64.toBytesBE.toSeq & pSegm & @[0x80u8 or psLen]
|
2023-05-11 14:25:29 +00:00
|
|
|
of Leaf:
|
|
|
|
let
|
2023-06-30 22:22:33 +00:00
|
|
|
pSegm = vtx.lPfx.hexPrefixEncode(isleaf = true)
|
2023-05-11 14:25:29 +00:00
|
|
|
psLen = pSegm.len.byte
|
|
|
|
if psLen == 0 or 33 < psLen:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyLeafPathOverflow)
|
2023-07-05 20:27:48 +00:00
|
|
|
data = vtx.lData.blobify & pSegm & @[0xC0u8 or psLen]
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-07-05 20:27:48 +00:00
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-06-30 22:22:33 +00:00
|
|
|
proc blobify*(vtx: VertexRef): Result[Blob, AristoError] =
|
2023-05-11 14:25:29 +00:00
|
|
|
## Variant of `blobify()`
|
2023-09-12 18:45:12 +00:00
|
|
|
var data: Blob
|
|
|
|
? vtx.blobify data
|
2024-05-24 09:27:17 +00:00
|
|
|
ok(move(data))
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
proc blobify*(vGen: openArray[VertexID]; data: var Blob) =
|
2023-08-21 18:18:06 +00:00
|
|
|
## This function serialises a list of vertex IDs.
|
2023-05-11 14:25:29 +00:00
|
|
|
## ::
|
2023-08-21 18:18:06 +00:00
|
|
|
## uint64, ... -- list of IDs
|
|
|
|
## 0x7c -- marker(8)
|
2023-05-11 14:25:29 +00:00
|
|
|
##
|
|
|
|
data.setLen(0)
|
2023-06-20 13:26:25 +00:00
|
|
|
for w in vGen:
|
|
|
|
data &= w.uint64.toBytesBE.toSeq
|
2023-08-21 18:18:06 +00:00
|
|
|
data.add 0x7Cu8
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-06-20 13:26:25 +00:00
|
|
|
proc blobify*(vGen: openArray[VertexID]): Blob =
|
|
|
|
## Variant of `blobify()`
|
|
|
|
vGen.blobify result
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-08-21 18:18:06 +00:00
|
|
|
|
2023-09-12 18:45:12 +00:00
|
|
|
proc blobify*(filter: FilterRef; data: var Blob): Result[void,AristoError] =
|
2023-08-18 19:46:55 +00:00
|
|
|
## This function serialises an Aristo DB filter object
|
|
|
|
## ::
|
2023-09-05 13:57:20 +00:00
|
|
|
## uint64 -- filter ID
|
2023-08-21 18:18:06 +00:00
|
|
|
## Uint256 -- source key
|
|
|
|
## Uint256 -- target key
|
|
|
|
## uint32 -- number of vertex IDs (vertex ID generator state)
|
|
|
|
## uint32 -- number of (id,key,vertex) triplets
|
2023-08-18 19:46:55 +00:00
|
|
|
##
|
2023-08-21 18:18:06 +00:00
|
|
|
## uint64, ... -- list of vertex IDs (vertex ID generator state)
|
2023-08-18 19:46:55 +00:00
|
|
|
##
|
2023-08-21 18:18:06 +00:00
|
|
|
## uint32 -- flag(3) + vtxLen(29), first triplet
|
|
|
|
## uint64 -- vertex ID
|
|
|
|
## Uint256 -- optional key
|
|
|
|
## Blob -- optional vertex
|
|
|
|
##
|
|
|
|
## ... -- more triplets
|
|
|
|
## 0x7d -- marker(8)
|
2023-08-18 19:46:55 +00:00
|
|
|
##
|
2023-11-08 12:18:32 +00:00
|
|
|
func blobify(lid: HashKey): Blob =
|
|
|
|
let n = lid.len
|
2024-05-30 17:48:38 +00:00
|
|
|
if n < 32: @[n.byte] & @(lid.data) & 0u8.repeat(31 - n) else: @(lid.data)
|
2023-11-08 12:18:32 +00:00
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
if not filter.isValid:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyNilFilter)
|
2023-08-18 19:46:55 +00:00
|
|
|
data.setLen(0)
|
2023-09-05 13:57:20 +00:00
|
|
|
data &= filter.fid.uint64.toBytesBE.toSeq
|
2023-11-08 12:18:32 +00:00
|
|
|
data &= @(filter.src.data)
|
|
|
|
data &= @(filter.trg.data)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
data &= filter.vGen.len.uint32.toBytesBE.toSeq
|
|
|
|
data &= newSeq[byte](4) # place holder
|
|
|
|
|
|
|
|
# Store vertex ID generator state
|
|
|
|
for w in filter.vGen:
|
|
|
|
data &= w.uint64.toBytesBE.toSeq
|
|
|
|
|
|
|
|
var
|
|
|
|
n = 0
|
|
|
|
leftOver = filter.kMap.keys.toSeq.toHashSet
|
|
|
|
|
|
|
|
# Loop over vertex table
|
|
|
|
for (vid,vtx) in filter.sTab.pairs:
|
|
|
|
n.inc
|
|
|
|
leftOver.excl vid
|
|
|
|
|
|
|
|
var
|
2023-11-08 12:18:32 +00:00
|
|
|
keyMode = 0u # default: ignore that key
|
|
|
|
vtxLen = 0u # default: ignore that vertex
|
2023-08-18 19:46:55 +00:00
|
|
|
keyBlob: Blob
|
|
|
|
vtxBlob: Blob
|
|
|
|
|
|
|
|
let key = filter.kMap.getOrVoid vid
|
|
|
|
if key.isValid:
|
2023-11-08 12:18:32 +00:00
|
|
|
keyBlob = key.blobify
|
|
|
|
keyMode = if key.len < 32: 0xc000_0000u else: 0x8000_0000u
|
2023-08-18 19:46:55 +00:00
|
|
|
elif filter.kMap.hasKey vid:
|
2023-11-08 12:18:32 +00:00
|
|
|
keyMode = 0x4000_0000u # void hash key => considered deleted
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
if vtx.isValid:
|
2023-09-12 18:45:12 +00:00
|
|
|
? vtx.blobify vtxBlob
|
2023-11-08 12:18:32 +00:00
|
|
|
vtxLen = vtxBlob.len.uint
|
|
|
|
if 0x3fff_ffff <= vtxLen:
|
|
|
|
return err(BlobifyFilterRecordOverflow)
|
2023-08-18 19:46:55 +00:00
|
|
|
else:
|
2023-11-08 12:18:32 +00:00
|
|
|
vtxLen = 0x3fff_ffff # nil vertex => considered deleted
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
data &=
|
2023-11-08 12:18:32 +00:00
|
|
|
(keyMode or vtxLen).uint32.toBytesBE.toSeq &
|
2023-08-18 19:46:55 +00:00
|
|
|
vid.uint64.toBytesBE.toSeq &
|
|
|
|
keyBlob &
|
|
|
|
vtxBlob
|
|
|
|
|
|
|
|
# Loop over remaining data from key table
|
|
|
|
for vid in leftOver:
|
|
|
|
n.inc
|
|
|
|
var
|
2023-11-08 12:18:32 +00:00
|
|
|
keyMode = 0u # present and usable
|
2023-08-18 19:46:55 +00:00
|
|
|
keyBlob: Blob
|
|
|
|
|
|
|
|
let key = filter.kMap.getOrVoid vid
|
|
|
|
if key.isValid:
|
2023-11-08 12:18:32 +00:00
|
|
|
keyBlob = key.blobify
|
|
|
|
keyMode = if key.len < 32: 0xc000_0000u else: 0x8000_0000u
|
2023-08-18 19:46:55 +00:00
|
|
|
else:
|
2023-11-08 12:18:32 +00:00
|
|
|
keyMode = 0x4000_0000u # void hash key => considered deleted
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
data &=
|
2023-11-08 12:18:32 +00:00
|
|
|
keyMode.uint32.toBytesBE.toSeq &
|
2023-08-18 19:46:55 +00:00
|
|
|
vid.uint64.toBytesBE.toSeq &
|
|
|
|
keyBlob
|
|
|
|
|
2023-09-05 13:57:20 +00:00
|
|
|
data[76 ..< 80] = n.uint32.toBytesBE.toSeq
|
2023-08-21 18:18:06 +00:00
|
|
|
data.add 0x7Du8
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
proc blobify*(filter: FilterRef): Result[Blob, AristoError] =
|
|
|
|
## ...
|
|
|
|
var data: Blob
|
2023-09-12 18:45:12 +00:00
|
|
|
? filter.blobify data
|
2024-05-24 09:27:17 +00:00
|
|
|
ok move(data)
|
2023-08-21 18:18:06 +00:00
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
proc blobify*(vFqs: openArray[(QueueID,QueueID)]; data: var Blob) =
|
|
|
|
## This function serialises a list of filter queue IDs.
|
2023-08-21 18:18:06 +00:00
|
|
|
## ::
|
|
|
|
## uint64, ... -- list of IDs
|
|
|
|
## 0x7e -- marker(8)
|
|
|
|
##
|
|
|
|
data.setLen(0)
|
2023-08-25 22:53:59 +00:00
|
|
|
for w in vFqs:
|
|
|
|
data &= w[0].uint64.toBytesBE.toSeq
|
|
|
|
data &= w[1].uint64.toBytesBE.toSeq
|
2023-08-21 18:18:06 +00:00
|
|
|
data.add 0x7Eu8
|
|
|
|
|
2023-08-25 22:53:59 +00:00
|
|
|
proc blobify*(vFqs: openArray[(QueueID,QueueID)]): Blob =
|
2023-08-21 18:18:06 +00:00
|
|
|
## Variant of `blobify()`
|
2023-08-25 22:53:59 +00:00
|
|
|
vFqs.blobify result
|
2023-08-21 18:18:06 +00:00
|
|
|
|
2023-07-05 20:27:48 +00:00
|
|
|
# -------------
|
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc deblobify(data: openArray[byte]; pyl: var PayloadRef): Result[void,AristoError] =
|
2023-07-05 20:27:48 +00:00
|
|
|
if data.len == 0:
|
|
|
|
pyl = PayloadRef(pType: RawData)
|
2023-09-12 18:45:12 +00:00
|
|
|
return ok()
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
let mask = data[^1]
|
2023-08-21 18:18:06 +00:00
|
|
|
if mask == 0x6b: # unstructured payload
|
2023-07-05 20:27:48 +00:00
|
|
|
pyl = PayloadRef(pType: RawData, rawBlob: data[0 .. ^2])
|
2023-09-12 18:45:12 +00:00
|
|
|
return ok()
|
2023-08-21 18:18:06 +00:00
|
|
|
if mask == 0x6a: # RLP encoded payload
|
2023-07-05 20:27:48 +00:00
|
|
|
pyl = PayloadRef(pType: RlpData, rlpBlob: data[0 .. ^2])
|
2023-09-12 18:45:12 +00:00
|
|
|
return ok()
|
|
|
|
|
2023-07-05 20:27:48 +00:00
|
|
|
var
|
|
|
|
pAcc = PayloadRef(pType: AccountData)
|
|
|
|
start = 0
|
|
|
|
|
|
|
|
case mask and 0x03:
|
|
|
|
of 0x00:
|
|
|
|
discard
|
|
|
|
of 0x01:
|
2023-09-12 18:45:12 +00:00
|
|
|
pAcc.account.nonce = (? data.load64 start).AccountNonce
|
2023-07-05 20:27:48 +00:00
|
|
|
else:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobNonceLenUnsupported)
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
case mask and 0x0c:
|
|
|
|
of 0x00:
|
|
|
|
discard
|
|
|
|
of 0x04:
|
2023-09-12 18:45:12 +00:00
|
|
|
pAcc.account.balance = (? data.load64 start).u256
|
2023-07-05 20:27:48 +00:00
|
|
|
of 0x08:
|
2023-09-12 18:45:12 +00:00
|
|
|
pAcc.account.balance = (? data.load256 start)
|
2023-07-05 20:27:48 +00:00
|
|
|
else:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobBalanceLenUnsupported)
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
case mask and 0x30:
|
|
|
|
of 0x00:
|
|
|
|
discard
|
|
|
|
of 0x10:
|
2023-09-12 18:45:12 +00:00
|
|
|
pAcc.account.storageID = (? data.load64 start).VertexID
|
2023-07-05 20:27:48 +00:00
|
|
|
else:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobStorageLenUnsupported)
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
case mask and 0xc0:
|
|
|
|
of 0x00:
|
2023-07-12 23:03:14 +00:00
|
|
|
pAcc.account.codeHash = VOID_CODE_HASH
|
2023-07-05 20:27:48 +00:00
|
|
|
of 0x80:
|
|
|
|
if data.len < start + 33:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobPayloadTooShortInt256)
|
2023-07-05 20:27:48 +00:00
|
|
|
(addr pAcc.account.codeHash.data[0]).copyMem(unsafeAddr data[start], 32)
|
|
|
|
else:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobCodeLenUnsupported)
|
2023-07-05 20:27:48 +00:00
|
|
|
|
2024-02-20 03:07:38 +00:00
|
|
|
pyl = pAcc
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc deblobify*(record: openArray[byte]; vtx: var VertexRef): Result[void,AristoError] =
|
2023-05-11 14:25:29 +00:00
|
|
|
## De-serialise a data record encoded with `blobify()`. The second
|
|
|
|
## argument `vtx` can be `nil`.
|
|
|
|
if record.len < 3: # minimum `Leaf` record
|
2023-11-08 12:18:32 +00:00
|
|
|
return err(DeblobVtxTooShort)
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
case record[^1] shr 6:
|
2023-06-12 18:16:03 +00:00
|
|
|
of 0: # `Branch` vertex
|
2023-08-21 18:18:06 +00:00
|
|
|
if record[^1] != 0x08u8:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobUnknown)
|
2023-05-11 14:25:29 +00:00
|
|
|
if record.len < 19: # at least two edges
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobBranchTooShort)
|
2023-05-11 14:25:29 +00:00
|
|
|
if (record.len mod 8) != 3:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobBranchSizeGarbled)
|
2023-05-11 14:25:29 +00:00
|
|
|
let
|
|
|
|
maxOffset = record.len - 11
|
|
|
|
aInx = record.len - 3
|
|
|
|
aIny = record.len - 2
|
|
|
|
var
|
|
|
|
offs = 0
|
2024-05-23 15:37:51 +00:00
|
|
|
access = uint16.fromBytesBE record.toOpenArray(aInx, aIny) # bitmap
|
2023-05-11 14:25:29 +00:00
|
|
|
vtxList: array[16,VertexID]
|
|
|
|
while access != 0:
|
|
|
|
if maxOffset < offs:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobBranchInxOutOfRange)
|
2023-05-11 14:25:29 +00:00
|
|
|
let n = access.firstSetBit - 1
|
|
|
|
access.clearBit n
|
2024-05-23 15:37:51 +00:00
|
|
|
vtxList[n] = (uint64.fromBytesBE record.toOpenArray(offs, offs + 7)).VertexID
|
2023-05-11 14:25:29 +00:00
|
|
|
offs += 8
|
|
|
|
# End `while`
|
|
|
|
vtx = VertexRef(
|
|
|
|
vType: Branch,
|
2023-05-14 17:43:01 +00:00
|
|
|
bVid: vtxList)
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
of 2: # `Extension` vertex
|
2023-05-11 14:25:29 +00:00
|
|
|
let
|
|
|
|
sLen = record[^1].int and 0x3f # length of path segment
|
2024-02-20 03:07:38 +00:00
|
|
|
rLen = record.len - 1 # `vertexID` + path segm
|
2023-05-11 14:25:29 +00:00
|
|
|
if record.len < 10:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobExtTooShort)
|
2024-02-20 03:07:38 +00:00
|
|
|
if 8 + sLen != rLen: # => slen is at least 1
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobExtSizeGarbled)
|
2024-05-23 15:37:51 +00:00
|
|
|
let (isLeaf, pathSegment) = hexPrefixDecode record.toOpenArray(8, rLen - 1)
|
2023-05-11 14:25:29 +00:00
|
|
|
if isLeaf:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobExtGotLeafPrefix)
|
2023-05-11 14:25:29 +00:00
|
|
|
vtx = VertexRef(
|
|
|
|
vType: Extension,
|
2024-05-23 15:37:51 +00:00
|
|
|
eVid: (uint64.fromBytesBE record.toOpenArray(0, 7)).VertexID,
|
2023-05-11 14:25:29 +00:00
|
|
|
ePfx: pathSegment)
|
|
|
|
|
2023-06-12 18:16:03 +00:00
|
|
|
of 3: # `Leaf` vertex
|
2023-05-11 14:25:29 +00:00
|
|
|
let
|
|
|
|
sLen = record[^1].int and 0x3f # length of path segment
|
2024-02-20 03:07:38 +00:00
|
|
|
rLen = record.len - 1 # payload + path segment
|
2023-05-11 14:25:29 +00:00
|
|
|
pLen = rLen - sLen # payload length
|
2024-02-20 03:07:38 +00:00
|
|
|
if rLen < sLen:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobLeafSizeGarbled)
|
2024-05-23 15:37:51 +00:00
|
|
|
let (isLeaf, pathSegment) = hexPrefixDecode record.toOpenArray(pLen, rLen-1)
|
2023-05-11 14:25:29 +00:00
|
|
|
if not isLeaf:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobLeafGotExtPrefix)
|
2023-07-05 20:27:48 +00:00
|
|
|
var pyl: PayloadRef
|
2024-05-23 15:37:51 +00:00
|
|
|
? record.toOpenArray(0, pLen - 1).deblobify(pyl)
|
2023-05-11 14:25:29 +00:00
|
|
|
vtx = VertexRef(
|
2023-07-05 20:27:48 +00:00
|
|
|
vType: Leaf,
|
|
|
|
lPfx: pathSegment,
|
|
|
|
lData: pyl)
|
2023-09-12 18:45:12 +00:00
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
else:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobUnknown)
|
|
|
|
ok()
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc deblobify*(data: openArray[byte]; T: type VertexRef): Result[T,AristoError] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Variant of `deblobify()` for vertex deserialisation.
|
|
|
|
var vtx = T(nil) # will be auto-initialised
|
2023-09-12 18:45:12 +00:00
|
|
|
? data.deblobify vtx
|
2023-06-20 13:26:25 +00:00
|
|
|
ok vtx
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-08-21 18:18:06 +00:00
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc deblobify*(data: openArray[byte]; vGen: var seq[VertexID]): Result[void,AristoError] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## De-serialise the data record encoded with `blobify()` into the vertex ID
|
|
|
|
## generator argument `vGen`.
|
2023-05-11 14:25:29 +00:00
|
|
|
if data.len == 0:
|
2023-06-22 11:13:24 +00:00
|
|
|
vGen = @[]
|
2023-05-11 14:25:29 +00:00
|
|
|
else:
|
|
|
|
if (data.len mod 8) != 1:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobSizeGarbled)
|
2023-08-21 18:18:06 +00:00
|
|
|
if data[^1] != 0x7c:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobWrongType)
|
2023-05-11 14:25:29 +00:00
|
|
|
for n in 0 ..< (data.len div 8):
|
|
|
|
let w = n * 8
|
2024-05-23 15:37:51 +00:00
|
|
|
vGen.add (uint64.fromBytesBE data.toOpenArray(w, w+7)).VertexID
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
proc deblobify*(data: openArray[byte]; T: type seq[VertexID]): Result[T,AristoError] =
|
2023-06-20 13:26:25 +00:00
|
|
|
## Variant of `deblobify()` for deserialising the vertex ID generator state
|
|
|
|
var vGen: seq[VertexID]
|
2023-09-12 18:45:12 +00:00
|
|
|
? data.deblobify vGen
|
2024-05-24 09:27:17 +00:00
|
|
|
ok move(vGen)
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2023-09-12 18:45:12 +00:00
|
|
|
proc deblobify*(data: Blob; filter: var FilterRef): Result[void,AristoError] =
|
2023-08-18 19:46:55 +00:00
|
|
|
## De-serialise an Aristo DB filter object
|
2023-09-05 13:57:20 +00:00
|
|
|
if data.len < 80: # minumum length 80 for an empty filter
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobFilterTooShort)
|
2023-08-21 18:18:06 +00:00
|
|
|
if data[^1] != 0x7d:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobWrongType)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
func deblob(data: openArray[byte]; shortKey: bool): Result[HashKey,void] =
|
|
|
|
if shortKey:
|
2024-05-23 15:37:51 +00:00
|
|
|
HashKey.fromBytes data.toOpenArray(1, min(int data[0],31))
|
2023-11-08 12:18:32 +00:00
|
|
|
else:
|
|
|
|
HashKey.fromBytes data
|
|
|
|
|
2023-08-18 19:46:55 +00:00
|
|
|
let f = FilterRef()
|
2024-05-23 15:37:51 +00:00
|
|
|
f.fid = (uint64.fromBytesBE data.toOpenArray(0, 7)).FilterID
|
2023-11-08 12:18:32 +00:00
|
|
|
(addr f.src.data[0]).copyMem(unsafeAddr data[8], 32)
|
|
|
|
(addr f.trg.data[0]).copyMem(unsafeAddr data[40], 32)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
let
|
2024-05-23 15:37:51 +00:00
|
|
|
nVids = uint32.fromBytesBE data.toOpenArray(72, 75)
|
|
|
|
nTriplets = uint32.fromBytesBE data.toOpenArray(76, 79)
|
2023-09-05 13:57:20 +00:00
|
|
|
nTrplStart = (80 + nVids * 8).int
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
if data.len < nTrplStart:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobFilterGenTooShort)
|
2023-08-18 19:46:55 +00:00
|
|
|
for n in 0 ..< nVids:
|
2023-09-05 13:57:20 +00:00
|
|
|
let w = 80 + n * 8
|
2024-05-23 15:37:51 +00:00
|
|
|
f.vGen.add (uint64.fromBytesBE data.toOpenArray(int w, int w+7)).VertexID
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
var offs = nTrplStart
|
|
|
|
for n in 0 ..< nTriplets:
|
|
|
|
if data.len < offs + 12:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobFilterTrpTooShort)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
let
|
2023-11-08 12:18:32 +00:00
|
|
|
keyFlag = data[offs] shr 6
|
2024-05-23 15:37:51 +00:00
|
|
|
vtxFlag = ((uint32.fromBytesBE data.toOpenArray(offs, offs+3)) and 0x3fff_ffff).int
|
2023-11-08 12:18:32 +00:00
|
|
|
vLen = if vtxFlag == 0x3fff_ffff: 0 else: vtxFlag
|
|
|
|
if keyFlag == 0 and vtxFlag == 0:
|
|
|
|
return err(DeblobFilterTrpVtxSizeGarbled) # no blind records
|
2023-08-18 19:46:55 +00:00
|
|
|
offs = offs + 4
|
|
|
|
|
2024-05-23 15:37:51 +00:00
|
|
|
let vid = (uint64.fromBytesBE data.toOpenArray(offs, offs+7)).VertexID
|
2023-08-18 19:46:55 +00:00
|
|
|
offs = offs + 8
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
if data.len < offs + (1 < keyFlag).ord * 32 + vLen:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobFilterTrpTooShort)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
if 1 < keyFlag:
|
2024-05-23 15:37:51 +00:00
|
|
|
f.kMap[vid] = data.toOpenArray(offs, offs+31).deblob(keyFlag == 3).valueOr:
|
2023-11-08 12:18:32 +00:00
|
|
|
return err(DeblobHashKeyExpected)
|
2023-08-18 19:46:55 +00:00
|
|
|
offs = offs + 32
|
2023-11-08 12:18:32 +00:00
|
|
|
elif keyFlag == 1:
|
2023-08-18 19:46:55 +00:00
|
|
|
f.kMap[vid] = VOID_HASH_KEY
|
|
|
|
|
2023-11-08 12:18:32 +00:00
|
|
|
if vtxFlag == 0x3fff_ffff:
|
|
|
|
f.sTab[vid] = VertexRef(nil)
|
|
|
|
elif 0 < vLen:
|
2023-08-18 19:46:55 +00:00
|
|
|
var vtx: VertexRef
|
2024-05-23 15:37:51 +00:00
|
|
|
? data.toOpenArray(offs, offs + vLen - 1).deblobify vtx
|
2023-08-18 19:46:55 +00:00
|
|
|
f.sTab[vid] = vtx
|
|
|
|
offs = offs + vLen
|
|
|
|
|
2023-08-21 18:18:06 +00:00
|
|
|
if data.len != offs + 1:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobFilterSizeGarbled)
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
filter = f
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-08-18 19:46:55 +00:00
|
|
|
|
|
|
|
proc deblobify*(data: Blob; T: type FilterRef): Result[T,AristoError] =
|
|
|
|
## Variant of `deblobify()` for deserialising an Aristo DB filter object
|
|
|
|
var filter: T
|
2023-09-12 18:45:12 +00:00
|
|
|
? data.deblobify filter
|
2023-08-18 19:46:55 +00:00
|
|
|
ok filter
|
|
|
|
|
2023-09-12 18:45:12 +00:00
|
|
|
proc deblobify*(
|
|
|
|
data: Blob;
|
|
|
|
vFqs: var seq[(QueueID,QueueID)];
|
|
|
|
): Result[void,AristoError] =
|
2023-08-25 22:53:59 +00:00
|
|
|
## De-serialise the data record encoded with `blobify()` into a filter queue
|
|
|
|
## ID argument liet `vFqs`.
|
2023-08-21 18:18:06 +00:00
|
|
|
if data.len == 0:
|
2023-08-25 22:53:59 +00:00
|
|
|
vFqs = @[]
|
2023-08-21 18:18:06 +00:00
|
|
|
else:
|
2023-08-25 22:53:59 +00:00
|
|
|
if (data.len mod 16) != 1:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobSizeGarbled)
|
2023-08-21 18:18:06 +00:00
|
|
|
if data[^1] != 0x7e:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(DeblobWrongType)
|
2023-08-25 22:53:59 +00:00
|
|
|
for n in 0 ..< (data.len div 16):
|
|
|
|
let
|
|
|
|
w = n * 16
|
2024-05-23 15:37:51 +00:00
|
|
|
a = (uint64.fromBytesBE data.toOpenArray(w, w + 7)).QueueID
|
|
|
|
b = (uint64.fromBytesBE data.toOpenArray(w + 8, w + 15)).QueueID
|
2023-08-25 22:53:59 +00:00
|
|
|
vFqs.add (a,b)
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-08-25 22:53:59 +00:00
|
|
|
|
|
|
|
proc deblobify*(
|
|
|
|
data: Blob;
|
|
|
|
T: type seq[(QueueID,QueueID)];
|
|
|
|
): Result[T,AristoError] =
|
2023-08-21 18:18:06 +00:00
|
|
|
## Variant of `deblobify()` for deserialising the vertex ID generator state
|
2023-08-25 22:53:59 +00:00
|
|
|
var vFqs: seq[(QueueID,QueueID)]
|
2023-09-12 18:45:12 +00:00
|
|
|
? data.deblobify vFqs
|
2023-08-25 22:53:59 +00:00
|
|
|
ok vFqs
|
2023-08-21 18:18:06 +00:00
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|