diff --git a/nimbus/db/storage_types.nim b/nimbus/db/storage_types.nim index 3783e6bdc..978efbc67 100644 --- a/nimbus/db/storage_types.nim +++ b/nimbus/db/storage_types.nim @@ -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") diff --git a/tests/test_storage_backends.nim b/tests/test_storage_backends.nim index e8de17b66..3869295eb 100644 --- a/tests/test_storage_backends.nim +++ b/tests/test_storage_backends.nim @@ -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]