mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-23 02:29:26 +00:00
review changes
* `usedBytes` was not named appropriately. it indicates the last populated position in the data (the last valid index) for slicing purposes (i.e. the position is passed to `toOpenArray`) * Fixed the `==` operator for DBKeys * Use one-byte key for the 'canonical head hash'
This commit is contained in:
parent
583c72fa54
commit
3060452d96
@ -12,45 +12,56 @@ type
|
||||
DbKey* = object
|
||||
# The first byte stores the key type. The rest are key-specific values
|
||||
data: array[33, byte]
|
||||
usedBytes: uint8
|
||||
dataEndPos: uint8 # the last populated position in the data
|
||||
|
||||
StorageError* = object of Exception
|
||||
|
||||
proc genericHashKey*(h: Hash256): DbKey {.inline.} =
|
||||
result.data[0] = byte ord(genericHash)
|
||||
result.data[1 .. 32] = h.data
|
||||
result.usedBytes = uint8 32
|
||||
result.dataEndPos = uint8 32
|
||||
|
||||
proc blockHashToScoreKey*(h: Hash256): DbKey {.inline.} =
|
||||
result.data[0] = byte ord(blockHashToScore)
|
||||
result.data[1 .. 32] = h.data
|
||||
result.usedBytes = uint8 32
|
||||
result.dataEndPos = uint8 32
|
||||
|
||||
proc transactionHashToBlockKey*(h: Hash256): DbKey {.inline.} =
|
||||
result.data[0] = byte ord(transactionHashToBlock)
|
||||
result.data[1 .. 32] = h.data
|
||||
result.usedBytes = uint8 32
|
||||
result.dataEndPos = uint8 32
|
||||
|
||||
proc blockNumberToHashKey*(u: BlockNumber): DbKey {.inline.} =
|
||||
result.data[0] = byte ord(blockNumberToHash)
|
||||
assert sizeof(u) <= 32
|
||||
copyMem(addr result.data[1], unsafeAddr u, sizeof(u))
|
||||
result.usedBytes = uint8 sizeof(u)
|
||||
result.dataEndPos = uint8 sizeof(u)
|
||||
|
||||
proc canonicalHeadHashKey*(): DbKey {.inline.} =
|
||||
result.data[0] = byte ord(canonicalHeadHash)
|
||||
result.usedBytes = 32
|
||||
result.dataEndPos = 1
|
||||
|
||||
const hashHolderKinds = {genericHash, blockHashToScore, transactionHashToBlock}
|
||||
|
||||
template toOpenArray*(k: DbKey): openarray[byte] =
|
||||
k.data.toOpenArray(0, int k.usedBytes)
|
||||
k.data.toOpenArray(0, int(k.dataEndPos))
|
||||
|
||||
proc hash*(k: DbKey): Hash =
|
||||
result = hash(k.toOpenArray)
|
||||
|
||||
# TODO: this should be added to Nim
|
||||
proc `==`*[T](lhs, rhs: openarray[T]): bool =
|
||||
if lhs.len != rhs.len:
|
||||
return false
|
||||
|
||||
for i in 0 ..< lhs.len:
|
||||
if lhs[i] != rhs[i]:
|
||||
return false
|
||||
|
||||
return true
|
||||
|
||||
proc `==`*(a, b: DbKey): bool {.inline.} =
|
||||
equalMem(unsafeAddr a, unsafeAddr b, sizeof(a))
|
||||
a.toOpenArray == b.toOpenArray
|
||||
|
||||
template raiseStorageInitError* =
|
||||
raise newException(StorageError, "failure to initialize storage")
|
||||
|
@ -1,6 +1,6 @@
|
||||
import
|
||||
unittest, macros,
|
||||
nimcrypto/[keccak, hash], ranges, eth_common/eth_types,
|
||||
stint, nimcrypto/[keccak, hash], ranges, eth_common/eth_types,
|
||||
../nimbus/db/[storage_types],
|
||||
../nimbus/db/backends/[sqlite_backend, rocksdb_backend]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user