2023-03-21 13:27:12 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2018-06-24 15:39:03 +00:00
|
|
|
import
|
2022-09-03 18:15:35 +00:00
|
|
|
eth/common
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
DBKeyKind* = enum
|
|
|
|
genericHash
|
|
|
|
blockNumberToHash
|
|
|
|
blockHashToScore
|
|
|
|
transactionHashToBlock
|
|
|
|
canonicalHeadHash
|
2018-12-04 08:13:35 +00:00
|
|
|
slotHashToSlot
|
2018-12-31 03:27:02 +00:00
|
|
|
contractHash
|
2021-05-28 17:39:55 +00:00
|
|
|
cliqueSnapshot
|
2022-02-22 08:55:04 +00:00
|
|
|
transitionStatus
|
2022-06-27 04:15:23 +00:00
|
|
|
safeHash
|
|
|
|
finalizedHash
|
2022-09-15 07:50:16 +00:00
|
|
|
skeletonProgress
|
|
|
|
skeletonBlockHashToNumber
|
|
|
|
skeletonBlock
|
|
|
|
skeletonTransaction
|
2022-11-08 18:56:04 +00:00
|
|
|
snapSyncAccount
|
|
|
|
snapSyncStorageSlot
|
2022-11-16 23:51:06 +00:00
|
|
|
snapSyncStateRoot
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
DbKey* = object
|
|
|
|
# The first byte stores the key type. The rest are key-specific values
|
2018-07-05 12:41:01 +00:00
|
|
|
data*: array[33, byte]
|
|
|
|
dataEndPos*: uint8 # the last populated position in the data
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
proc genericHashKey*(h: Hash256): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(genericHash)
|
|
|
|
result.data[1 .. 32] = h.data
|
2018-06-26 09:11:17 +00:00
|
|
|
result.dataEndPos = uint8 32
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
proc blockHashToScoreKey*(h: Hash256): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(blockHashToScore)
|
|
|
|
result.data[1 .. 32] = h.data
|
2018-06-26 09:11:17 +00:00
|
|
|
result.dataEndPos = uint8 32
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
proc transactionHashToBlockKey*(h: Hash256): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(transactionHashToBlock)
|
|
|
|
result.data[1 .. 32] = h.data
|
2018-06-26 09:11:17 +00:00
|
|
|
result.dataEndPos = uint8 32
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
proc blockNumberToHashKey*(u: BlockNumber): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(blockNumberToHash)
|
2019-03-13 21:36:54 +00:00
|
|
|
doAssert sizeof(u) <= 32
|
2018-06-24 15:39:03 +00:00
|
|
|
copyMem(addr result.data[1], unsafeAddr u, sizeof(u))
|
2018-06-26 09:11:17 +00:00
|
|
|
result.dataEndPos = uint8 sizeof(u)
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
proc canonicalHeadHashKey*(): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(canonicalHeadHash)
|
2018-06-26 09:11:17 +00:00
|
|
|
result.dataEndPos = 1
|
2018-06-24 15:39:03 +00:00
|
|
|
|
2018-12-04 08:13:35 +00:00
|
|
|
proc slotHashToSlotKey*(h: openArray[byte]): DbKey {.inline.} =
|
2019-03-13 21:36:54 +00:00
|
|
|
doAssert(h.len == 32)
|
2018-12-04 08:13:35 +00:00
|
|
|
result.data[0] = byte ord(slotHashToSlot)
|
|
|
|
result.data[1 .. 32] = h
|
|
|
|
result.dataEndPos = uint8 32
|
|
|
|
|
2018-12-31 03:27:02 +00:00
|
|
|
proc contractHashKey*(h: Hash256): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(contractHash)
|
|
|
|
result.data[1 .. 32] = h.data
|
|
|
|
result.dataEndPos = uint8 32
|
|
|
|
|
2021-05-28 17:39:55 +00:00
|
|
|
proc cliqueSnapshotKey*(h: Hash256): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(cliqueSnapshot)
|
|
|
|
result.data[1 .. 32] = h.data
|
|
|
|
result.dataEndPos = uint8 32
|
|
|
|
|
2022-02-22 08:55:04 +00:00
|
|
|
proc transitionStatusKey*(): DbKey =
|
|
|
|
# ETH-2 Transition Status
|
|
|
|
result.data[0] = byte ord(transitionStatus)
|
|
|
|
result.dataEndPos = uint8 1
|
|
|
|
|
2022-06-27 04:15:23 +00:00
|
|
|
proc safeHashKey*(): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(safeHash)
|
|
|
|
result.dataEndPos = uint8 1
|
|
|
|
|
|
|
|
proc finalizedHashKey*(): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(finalizedHash)
|
|
|
|
result.dataEndPos = uint8 1
|
|
|
|
|
2022-09-15 07:50:16 +00:00
|
|
|
proc skeletonProgressKey*(): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(skeletonProgress)
|
|
|
|
result.dataEndPos = 1
|
|
|
|
|
|
|
|
proc skeletonBlockHashToNumberKey*(h: Hash256): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(skeletonBlockHashToNumber)
|
|
|
|
result.data[1 .. 32] = h.data
|
|
|
|
result.dataEndPos = uint8 32
|
|
|
|
|
|
|
|
proc skeletonBlockKey*(u: BlockNumber): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(skeletonBlock)
|
|
|
|
doAssert sizeof(u) <= 32
|
|
|
|
copyMem(addr result.data[1], unsafeAddr u, sizeof(u))
|
|
|
|
result.dataEndPos = uint8 sizeof(u)
|
|
|
|
|
|
|
|
proc skeletonTransactionKey*(u: BlockNumber): DbKey {.inline.} =
|
|
|
|
result.data[0] = byte ord(skeletonTransaction)
|
|
|
|
doAssert sizeof(u) <= 32
|
|
|
|
copyMem(addr result.data[1], unsafeAddr u, sizeof(u))
|
|
|
|
result.dataEndPos = uint8 sizeof(u)
|
|
|
|
|
2022-11-08 18:56:04 +00:00
|
|
|
proc snapSyncAccountKey*(h: openArray[byte]): DbKey {.inline.} =
|
|
|
|
doAssert(h.len == 32)
|
|
|
|
result.data[0] = byte ord(snapSyncAccount)
|
|
|
|
result.data[1 .. 32] = h
|
|
|
|
result.dataEndPos = uint8 sizeof(h)
|
|
|
|
|
|
|
|
proc snapSyncStorageSlotKey*(h: openArray[byte]): DbKey {.inline.} =
|
|
|
|
doAssert(h.len == 32)
|
|
|
|
result.data[0] = byte ord(snapSyncStorageSlot)
|
|
|
|
result.data[1 .. 32] = h
|
|
|
|
result.dataEndPos = uint8 sizeof(h)
|
|
|
|
|
2022-11-16 23:51:06 +00:00
|
|
|
proc snapSyncStateRootKey*(h: openArray[byte]): DbKey {.inline.} =
|
|
|
|
doAssert(h.len == 32)
|
|
|
|
result.data[0] = byte ord(snapSyncStateRoot)
|
|
|
|
result.data[1 .. 32] = h
|
|
|
|
result.dataEndPos = uint8 sizeof(h)
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
template toOpenArray*(k: DbKey): openArray[byte] =
|
2018-06-26 09:11:17 +00:00
|
|
|
k.data.toOpenArray(0, int(k.dataEndPos))
|
2018-06-24 15:39:03 +00:00
|
|
|
|
|
|
|
proc `==`*(a, b: DbKey): bool {.inline.} =
|
2018-06-26 09:11:17 +00:00
|
|
|
a.toOpenArray == b.toOpenArray
|
2018-06-24 15:39:03 +00:00
|
|
|
|