diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index 9f8f98800..3262070e0 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -1497,6 +1497,35 @@ func readSszForkedSignedBeaconBlock*( withBlck(result): readSszBytes(data, forkyBlck) +func readSszForkedBlobSidecar*( + cfg: RuntimeConfig, data: openArray[byte] +): ForkedBlobSidecar {.raises: [SerializationError].} = + ## Helper to read `BlobSidecar` from bytes when it's not certain what + ## `BlobFork` it is + type ForkedBlobSidecarHeader = object + index: BlobIndex + blob: Blob + kzg_commitment: KzgCommitment + kzg_proof: KzgProof + signed_block_header*: SignedBeaconBlockHeader + + const numHeaderBytes = fixedPortionSize(ForkedBlobSidecarHeader) + if data.len() < numHeaderBytes: + raise (ref MalformedSszError)(msg: "Incomplete BlobSidecar header") + let + header = SSZ.decode( + data.toOpenArray(0, numHeaderBytes - 1), ForkedBlobSidecarHeader) + consensusFork = cfg.consensusForkAtEpoch( + header.signed_block_header.message.slot.epoch) + blobFork = blobForkAtConsensusFork(consensusFork).valueOr: + raise (ref MalformedSszError)(msg: "BlobSidecar slot is pre-Deneb") + + # TODO https://github.com/nim-lang/Nim/issues/19357 + result = ForkedBlobSidecar(kind: blobFork) + withForkyBlob(result): + forkyBlob = new blobFork.BlobSidecar() + readSszBytes(data, forkyBlob[]) + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/beacon-chain.md#compute_fork_data_root func compute_fork_data_root*(current_version: Version, genesis_validators_root: Eth2Digest): Eth2Digest = diff --git a/ncli/ncli_db.nim b/ncli/ncli_db.nim index 3129e31e1..c2678dd37 100644 --- a/ncli/ncli_db.nim +++ b/ncli/ncli_db.nim @@ -489,19 +489,22 @@ proc cmdPutBlob(conf: DbConf, cfg: RuntimeConfig) = let blob = try: - SSZ.decode(readAllBytes(file).tryGet(), BlobSidecar) + readSszForkedBlobSidecar( + cfg, readAllBytes(file).tryGet()) except ResultError[IoErrorCode] as e: echo "Couldn't load ", file, ": ", e.msg continue except SerializationError as e: echo "Malformed ", file, ": ", e.msg continue - res = blob.verify_blob_sidecar_inclusion_proof() + res = withForkyBlob(blob): + forkyBlob[].verify_blob_sidecar_inclusion_proof() if res.isErr: echo "Invalid ", file, ": ", res.error continue - db.putBlobSidecar(blob) + withForkyBlob(blob): + db.putBlobSidecar(forkyBlob[]) proc cmdRewindState(conf: DbConf, cfg: RuntimeConfig) = echo "Opening database..." @@ -1214,4 +1217,4 @@ when isMainModule: of DbCmd.validatorPerf: cmdValidatorPerf(conf, cfg) of DbCmd.validatorDb: - cmdValidatorDb(conf, cfg) \ No newline at end of file + cmdValidatorDb(conf, cfg)