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-06-03 20:10:35 +00:00
|
|
|
std/bitops,
|
2024-06-22 20:33:37 +00:00
|
|
|
eth/common,
|
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
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-06-01 15:13:24 +00:00
|
|
|
proc blobifyTo*(pyl: PayloadRef, data: var Blob) =
|
2023-07-05 20:27:48 +00:00
|
|
|
if pyl.isNil:
|
|
|
|
return
|
|
|
|
case pyl.pType
|
|
|
|
of RawData:
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= pyl.rawBlob
|
|
|
|
data &= [0x6b.byte]
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
of AccountData:
|
|
|
|
var mask: byte
|
|
|
|
if 0 < pyl.account.nonce:
|
|
|
|
mask = mask or 0x01
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= pyl.account.nonce.uint64.toBytesBE
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
if high(uint64).u256 < pyl.account.balance:
|
|
|
|
mask = mask or 0x08
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= pyl.account.balance.toBytesBE
|
2023-07-05 20:27:48 +00:00
|
|
|
elif 0 < pyl.account.balance:
|
|
|
|
mask = mask or 0x04
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= pyl.account.balance.truncate(uint64).uint64.toBytesBE
|
2023-07-05 20:27:48 +00:00
|
|
|
|
2024-06-27 09:01:26 +00:00
|
|
|
if VertexID(0) < pyl.stoID:
|
2023-07-05 20:27:48 +00:00
|
|
|
mask = mask or 0x10
|
2024-06-27 09:01:26 +00:00
|
|
|
data &= pyl.stoID.uint64.toBytesBE
|
2023-07-05 20:27:48 +00:00
|
|
|
|
|
|
|
if pyl.account.codeHash != VOID_CODE_HASH:
|
|
|
|
mask = mask or 0x80
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= pyl.account.codeHash.data
|
2023-07-05 20:27:48 +00:00
|
|
|
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= [mask]
|
2023-07-05 20:27:48 +00:00
|
|
|
|
2024-06-01 15:13:24 +00:00
|
|
|
proc blobifyTo*(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
|
2024-06-01 15:13:24 +00:00
|
|
|
pos = data.len
|
2023-05-11 14:25:29 +00:00
|
|
|
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)
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= vtx.bVid[n].uint64.toBytesBE
|
|
|
|
if data.len - pos < 16:
|
2023-09-12 18:45:12 +00:00
|
|
|
return err(BlobifyBranchMissingRefs)
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= access.toBytesBE
|
|
|
|
data &= [0x08u8]
|
2023-05-11 14:25:29 +00:00
|
|
|
of Extension:
|
|
|
|
let
|
2024-06-22 20:33:37 +00:00
|
|
|
pSegm = vtx.ePfx.toHexPrefix(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)
|
2024-06-01 15:13:24 +00:00
|
|
|
data &= vtx.eVid.uint64.toBytesBE
|
|
|
|
data &= pSegm
|
|
|
|
data &= [0x80u8 or psLen]
|
2023-05-11 14:25:29 +00:00
|
|
|
of Leaf:
|
|
|
|
let
|
2024-06-22 20:33:37 +00:00
|
|
|
pSegm = vtx.lPfx.toHexPrefix(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)
|
2024-06-01 15:13:24 +00:00
|
|
|
vtx.lData.blobifyTo(data)
|
|
|
|
data &= pSegm
|
|
|
|
data &= [0xC0u8 or psLen]
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-07-05 20:27:48 +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
|
2024-06-01 15:13:24 +00:00
|
|
|
? vtx.blobifyTo data
|
2024-05-24 09:27:17 +00:00
|
|
|
ok(move(data))
|
2023-05-11 14:25:29 +00:00
|
|
|
|
|
|
|
|
2024-06-04 15:05:13 +00:00
|
|
|
proc blobifyTo*(tuv: VertexID; data: var Blob) =
|
|
|
|
## This function serialises a top used vertex ID.
|
|
|
|
data.setLen(9)
|
|
|
|
let w = tuv.uint64.toBytesBE
|
|
|
|
(addr data[0]).copyMem(unsafeAddr w[0], 8)
|
|
|
|
data[8] = 0x7Cu8
|
|
|
|
|
|
|
|
proc blobify*(tuv: VertexID): Blob =
|
|
|
|
## Variant of `blobifyTo()`
|
|
|
|
tuv.blobifyTo result
|
|
|
|
|
2024-06-05 18:17:50 +00:00
|
|
|
proc blobifyTo*(lSst: SavedState; data: var Blob): Result[void,AristoError] =
|
2024-05-31 17:32:22 +00:00
|
|
|
## Serialise a last saved state record
|
2024-06-28 18:43:04 +00:00
|
|
|
data.add lSst.key.data
|
2024-06-03 20:10:35 +00:00
|
|
|
data.add lSst.serial.toBytesBE
|
|
|
|
data.add @[0x7fu8]
|
2024-06-05 18:17:50 +00:00
|
|
|
ok()
|
2024-05-31 17:32:22 +00:00
|
|
|
|
2024-06-05 18:17:50 +00:00
|
|
|
proc blobify*(lSst: SavedState): Result[Blob,AristoError] =
|
2024-05-31 17:32:22 +00:00
|
|
|
## Variant of `blobify()`
|
2024-06-05 18:17:50 +00:00
|
|
|
var data: Blob
|
|
|
|
? lSst.blobifyTo data
|
|
|
|
ok(move(data))
|
2024-05-31 17:32:22 +00:00
|
|
|
|
2023-07-05 20:27:48 +00:00
|
|
|
# -------------
|
|
|
|
|
2024-06-04 10:38:11 +00:00
|
|
|
proc deblobifyTo(
|
2024-05-31 17:32:22 +00:00
|
|
|
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-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:
|
2024-06-27 09:01:26 +00:00
|
|
|
pAcc.stoID = (? 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-06-04 10:38:11 +00:00
|
|
|
proc deblobifyTo*(
|
2024-06-03 20:10:35 +00:00
|
|
|
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-06-22 20:33:37 +00:00
|
|
|
let (isLeaf, pathSegment) =
|
|
|
|
NibblesBuf.fromHexPrefix 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-06-22 20:33:37 +00:00
|
|
|
let (isLeaf, pathSegment) =
|
|
|
|
NibblesBuf.fromHexPrefix 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-06-04 10:38:11 +00:00
|
|
|
? record.toOpenArray(0, pLen - 1).deblobifyTo(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-06-03 20:10:35 +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
|
2024-06-04 10:38:11 +00:00
|
|
|
? data.deblobifyTo 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-06-04 10:38:11 +00:00
|
|
|
proc deblobifyTo*(
|
2024-05-31 17:32:22 +00:00
|
|
|
data: openArray[byte];
|
2024-06-04 15:05:13 +00:00
|
|
|
tuv: var VertexID;
|
2024-05-31 17:32:22 +00:00
|
|
|
): Result[void,AristoError] =
|
2024-06-04 15:05:13 +00:00
|
|
|
## De-serialise a top level vertex ID.
|
2023-05-11 14:25:29 +00:00
|
|
|
if data.len == 0:
|
2024-06-04 15:05:13 +00:00
|
|
|
tuv = VertexID(0)
|
|
|
|
elif data.len != 9:
|
|
|
|
return err(DeblobSizeGarbled)
|
|
|
|
elif data[^1] != 0x7c:
|
|
|
|
return err(DeblobWrongType)
|
2023-05-11 14:25:29 +00:00
|
|
|
else:
|
2024-06-04 15:05:13 +00:00
|
|
|
tuv = (uint64.fromBytesBE data.toOpenArray(0, 7)).VertexID
|
2023-09-12 18:45:12 +00:00
|
|
|
ok()
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2024-05-31 17:32:22 +00:00
|
|
|
proc deblobify*(
|
|
|
|
data: openArray[byte];
|
2024-06-04 15:05:13 +00:00
|
|
|
T: type VertexID;
|
2024-05-31 17:32:22 +00:00
|
|
|
): Result[T,AristoError] =
|
2024-06-04 15:05:13 +00:00
|
|
|
## Variant of `deblobify()` for deserialising a top level vertex ID.
|
|
|
|
var vTop: T
|
|
|
|
? data.deblobifyTo vTop
|
|
|
|
ok move(vTop)
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
|
2024-06-04 10:38:11 +00:00
|
|
|
proc deblobifyTo*(
|
2024-05-31 17:32:22 +00:00
|
|
|
data: openArray[byte];
|
|
|
|
lSst: var SavedState;
|
|
|
|
): Result[void,AristoError] =
|
|
|
|
## De-serialise the last saved state data record previously encoded with
|
|
|
|
## `blobify()`.
|
2024-06-28 18:43:04 +00:00
|
|
|
# Keep that legacy setting for a while
|
|
|
|
if data.len == 73:
|
|
|
|
if data[^1] != 0x7f:
|
|
|
|
return err(DeblobWrongType)
|
|
|
|
lSst.key = EMPTY_ROOT_HASH
|
|
|
|
lSst.serial = uint64.fromBytesBE data.toOpenArray(64, 71)
|
|
|
|
return ok()
|
|
|
|
# -----
|
|
|
|
if data.len != 41:
|
2024-05-31 17:32:22 +00:00
|
|
|
return err(DeblobWrongSize)
|
|
|
|
if data[^1] != 0x7f:
|
|
|
|
return err(DeblobWrongType)
|
2024-06-28 18:43:04 +00:00
|
|
|
(addr lSst.key.data[0]).copyMem(unsafeAddr data[0], 32)
|
|
|
|
lSst.serial = uint64.fromBytesBE data.toOpenArray(32, 39)
|
2024-05-31 17:32:22 +00:00
|
|
|
ok()
|
|
|
|
|
|
|
|
proc deblobify*(
|
|
|
|
data: openArray[byte];
|
|
|
|
T: type SavedState;
|
|
|
|
): Result[T,AristoError] =
|
|
|
|
## Variant of `deblobify()` for deserialising a last saved state data record
|
|
|
|
var lSst: T
|
2024-06-04 10:38:11 +00:00
|
|
|
? data.deblobifyTo lSst
|
2024-05-31 17:32:22 +00:00
|
|
|
ok move(lSst)
|
|
|
|
|
2023-05-11 14:25:29 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|