Add DB support for BlobSidecar (#4675)

This commit is contained in:
henridf 2023-02-27 15:02:37 +01:00 committed by GitHub
parent adeaa9e6c4
commit 144130e80e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 1 deletions

View File

@ -113,7 +113,7 @@ type
keyValues: KvStoreRef # Random stuff using DbKeyKind - suitable for small values mainly!
blocks: array[ConsensusFork, KvStoreRef] # BlockRoot -> TrustedSignedBeaconBlock
blobs: KvStoreRef # (BlockRoot -> BlobsSidecar)
blobs: KvStoreRef # (BlockRoot -> BlobSidecar)
stateRoots: KvStoreRef # (Slot, BlockRoot) -> StateRoot
@ -245,6 +245,11 @@ func subkey(root: Eth2Digest, slot: Slot): array[40, byte] =
ret
func blobkey(root: Eth2Digest, index: BlobIndex) : array[40, byte] =
var ret: array[40, byte]
ret[0..<8] = toBytes(index)
ret[8..<40] = root.data
template expectDb(x: auto): untyped =
# There's no meaningful error handling implemented for a corrupt database or
# full disk - this requires manual intervention, so we'll panic for now
@ -791,6 +796,11 @@ proc putBlock*(
db.blocks[type(value).toFork].putSZSSZ(value.root.data, value)
db.putBeaconBlockSummary(value.root, value.message.toBeaconBlockSummary())
proc putBlobSidecar*(
db: BeaconChainDB,
value: BlobSidecar) =
db.blobs.putSZSSZ(blobkey(value.block_root, value.index), value)
proc putBlobsSidecar*(
db: BeaconChainDB,
value: BlobsSidecar) =
@ -991,6 +1001,14 @@ proc getBlobsSidecar*(db: BeaconChainDB, key: Eth2Digest): Opt[BlobsSidecar] =
if db.blobs.getSZSSZ(key.data, result.get) != GetResult.found:
result.err()
proc getBlobSidecar*(db: BeaconChainDB, root: Eth2Digest, index: BlobIndex):
Opt[BlobSidecar] =
var blobs: BlobSidecar
result.ok(blobs)
if db.blobs.getSZSSZ(blobkey(root, index), result.get) != GetResult.found:
result.err()
proc getPhase0BlockSSZ(
db: BeaconChainDBV0, key: Eth2Digest, data: var seq[byte]): bool =
let dataPtr = addr data # Short-lived
@ -1067,6 +1085,14 @@ proc getBlobsSidecarSZ*(db: BeaconChainDB, key: Eth2Digest, data: var seq[byte])
assign(dataPtr[], data)
db.blobs.get(key.data, decode).expectDb()
proc getBlobSidecarSZ*(db: BeaconChainDB, root: Eth2Digest, index: BlobIndex,
data: var seq[byte]):
bool =
let dataPtr = addr data # Short-lived
func decode(data: openArray[byte]) =
assign(dataPtr[], data)
db.blobs.get(blobkey(root, index), decode).expectDb()
proc getBlockSZ*(
db: BeaconChainDB, key: Eth2Digest, data: var seq[byte],
T: type phase0.TrustedSignedBeaconBlock): bool =