2023-04-13 19:11:40 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2023-04-13 19:11:40 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-01-13 09:52:13 +00:00
|
|
|
import
|
|
|
|
std/tables,
|
|
|
|
../spec/helpers
|
|
|
|
|
2023-08-29 16:46:25 +00:00
|
|
|
from std/sequtils import mapIt
|
|
|
|
from std/strutils import join
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
const
|
2024-03-24 03:29:44 +00:00
|
|
|
MaxBlobs = 3 * SLOTS_PER_EPOCH * MAX_BLOBS_PER_BLOCK
|
|
|
|
## Same limit as `MaxOrphans` in `block_quarantine`;
|
|
|
|
## blobs may arrive before an orphan is tagged `blobless`
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
BlobQuarantine* = object
|
2023-12-01 18:58:46 +00:00
|
|
|
blobs*:
|
|
|
|
OrderedTable[(Eth2Digest, BlobIndex, KzgCommitment), ref BlobSidecar]
|
2024-01-13 09:52:13 +00:00
|
|
|
onBlobSidecarCallback*: OnBlobSidecarCallback
|
|
|
|
|
2023-04-28 12:57:35 +00:00
|
|
|
BlobFetchRecord* = object
|
|
|
|
block_root*: Eth2Digest
|
|
|
|
indices*: seq[BlobIndex]
|
2023-04-13 19:11:40 +00:00
|
|
|
|
2024-01-13 09:52:13 +00:00
|
|
|
OnBlobSidecarCallback = proc(data: BlobSidecar) {.gcsafe, raises: [].}
|
|
|
|
|
2023-04-28 12:57:35 +00:00
|
|
|
func shortLog*(x: seq[BlobIndex]): string =
|
|
|
|
"<" & x.mapIt($it).join(", ") & ">"
|
|
|
|
|
|
|
|
func shortLog*(x: seq[BlobFetchRecord]): string =
|
|
|
|
"[" & x.mapIt(shortLog(it.block_root) & shortLog(it.indices)).join(", ") & "]"
|
2023-04-13 19:11:40 +00:00
|
|
|
|
|
|
|
func put*(quarantine: var BlobQuarantine, blobSidecar: ref BlobSidecar) =
|
2024-03-24 05:03:51 +00:00
|
|
|
if quarantine.blobs.lenu64 >= MaxBlobs:
|
2023-08-29 16:46:25 +00:00
|
|
|
# FIFO if full. For example, sync manager and request manager can race to
|
|
|
|
# put blobs in at the same time, so one gets blob insert -> block resolve
|
|
|
|
# -> blob insert sequence, which leaves garbage blobs.
|
2023-12-01 18:58:46 +00:00
|
|
|
#
|
|
|
|
# This also therefore automatically garbage-collects otherwise valid garbage
|
|
|
|
# blobs which are correctly signed, point to either correct block roots or a
|
|
|
|
# block root which isn't ever seen, and then are for any reason simply never
|
|
|
|
# used.
|
|
|
|
var oldest_blob_key: (Eth2Digest, BlobIndex, KzgCommitment)
|
2023-08-29 16:46:25 +00:00
|
|
|
for k in quarantine.blobs.keys:
|
|
|
|
oldest_blob_key = k
|
|
|
|
break
|
|
|
|
quarantine.blobs.del oldest_blob_key
|
2023-11-06 06:48:43 +00:00
|
|
|
let block_root = hash_tree_root(blobSidecar.signed_block_header.message)
|
2023-11-05 08:13:57 +00:00
|
|
|
discard quarantine.blobs.hasKeyOrPut(
|
2023-12-01 18:58:46 +00:00
|
|
|
(block_root, blobSidecar.index, blobSidecar.kzg_commitment), blobSidecar)
|
2023-04-13 19:11:40 +00:00
|
|
|
|
2023-11-05 08:13:57 +00:00
|
|
|
func hasBlob*(
|
|
|
|
quarantine: BlobQuarantine,
|
|
|
|
slot: Slot,
|
|
|
|
proposer_index: uint64,
|
|
|
|
index: BlobIndex): bool =
|
|
|
|
for blob_sidecar in quarantine.blobs.values:
|
2023-11-06 06:48:43 +00:00
|
|
|
template block_header: untyped = blob_sidecar.signed_block_header.message
|
|
|
|
if block_header.slot == slot and
|
|
|
|
block_header.proposer_index == proposer_index and
|
2023-11-05 08:13:57 +00:00
|
|
|
blob_sidecar.index == index:
|
|
|
|
return true
|
|
|
|
false
|
2023-04-13 19:11:40 +00:00
|
|
|
|
2023-12-01 18:58:46 +00:00
|
|
|
func popBlobs*(
|
|
|
|
quarantine: var BlobQuarantine, digest: Eth2Digest,
|
2024-02-25 19:42:44 +00:00
|
|
|
blck: deneb.SignedBeaconBlock): seq[ref BlobSidecar] =
|
2023-04-13 19:11:40 +00:00
|
|
|
var r: seq[ref BlobSidecar] = @[]
|
2023-12-01 18:58:46 +00:00
|
|
|
for idx, kzg_commitment in blck.message.body.blob_kzg_commitments:
|
2023-04-13 19:11:40 +00:00
|
|
|
var b: ref BlobSidecar
|
2023-12-01 18:58:46 +00:00
|
|
|
if quarantine.blobs.pop((digest, BlobIndex idx, kzg_commitment), b):
|
2023-04-13 19:11:40 +00:00
|
|
|
r.add(b)
|
|
|
|
r
|
|
|
|
|
2024-02-25 19:42:44 +00:00
|
|
|
func hasBlobs*(quarantine: BlobQuarantine, blck: deneb.SignedBeaconBlock):
|
|
|
|
bool =
|
2023-12-01 18:58:46 +00:00
|
|
|
for idx, kzg_commitment in blck.message.body.blob_kzg_commitments:
|
|
|
|
if (blck.root, BlobIndex idx, kzg_commitment) notin quarantine.blobs:
|
2023-04-13 19:11:40 +00:00
|
|
|
return false
|
|
|
|
true
|
2023-04-28 12:57:35 +00:00
|
|
|
|
|
|
|
func blobFetchRecord*(quarantine: BlobQuarantine, blck: deneb.SignedBeaconBlock):
|
|
|
|
BlobFetchRecord =
|
|
|
|
var indices: seq[BlobIndex]
|
|
|
|
for i in 0..<len(blck.message.body.blob_kzg_commitments):
|
|
|
|
let idx = BlobIndex(i)
|
2023-12-01 18:58:46 +00:00
|
|
|
if not quarantine.blobs.hasKey(
|
|
|
|
(blck.root, idx, blck.message.body.blob_kzg_commitments[i])):
|
2023-04-28 12:57:35 +00:00
|
|
|
indices.add(idx)
|
|
|
|
BlobFetchRecord(block_root: blck.root, indices: indices)
|
2024-01-13 09:52:13 +00:00
|
|
|
|
|
|
|
func init*(
|
|
|
|
T: type BlobQuarantine, onBlobSidecarCallback: OnBlobSidecarCallback): T =
|
|
|
|
T(onBlobSidecarCallback: onBlobSidecarCallback)
|