2023-06-02 10:44:59 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2023-12-07 17:37:11 +00:00
|
|
|
"errors"
|
2023-11-22 15:23:50 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-11-27 23:44:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
|
2023-06-02 10:44:59 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces"
|
2023-11-22 15:23:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
2023-06-02 10:44:59 +00:00
|
|
|
enginev1 "github.com/prysmaticlabs/prysm/v4/proto/engine/v1"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/runtime/version"
|
|
|
|
)
|
|
|
|
|
2023-11-22 15:23:50 +00:00
|
|
|
var bundleCache = &blobsBundleCache{}
|
|
|
|
|
|
|
|
// BlobsBundleCache holds the KZG commitments and other relevant sidecar data for a local beacon block.
|
|
|
|
type blobsBundleCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
slot primitives.Slot
|
|
|
|
bundle *enginev1.BlobsBundle
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds a blobs bundle to the cache.
|
|
|
|
// same slot overwrites the previous bundle.
|
|
|
|
func (c *blobsBundleCache) add(slot primitives.Slot, bundle *enginev1.BlobsBundle) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if slot >= c.slot {
|
|
|
|
c.bundle = bundle
|
|
|
|
c.slot = slot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get gets a blobs bundle from the cache.
|
|
|
|
func (c *blobsBundleCache) get(slot primitives.Slot) *enginev1.BlobsBundle {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.slot == slot {
|
|
|
|
return c.bundle
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// prune acquires the lock before pruning.
|
|
|
|
func (c *blobsBundleCache) prune(minSlot primitives.Slot) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if minSlot > c.slot {
|
|
|
|
c.slot = 0
|
|
|
|
c.bundle = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 23:44:52 +00:00
|
|
|
// buildBlobSidecars given a block, builds the blob sidecars for the block.
|
2023-12-07 17:37:11 +00:00
|
|
|
func buildBlobSidecars(blk interfaces.SignedBeaconBlock, blobs [][]byte, kzgproofs [][]byte) ([]*ethpb.BlobSidecar, error) {
|
2023-08-26 00:23:19 +00:00
|
|
|
if blk.Version() < version.Deneb {
|
2023-11-27 23:44:52 +00:00
|
|
|
return nil, nil // No blobs before deneb.
|
2023-08-26 00:23:19 +00:00
|
|
|
}
|
2023-12-07 17:37:11 +00:00
|
|
|
denebBlk, err := blk.PbDenebBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cLen := len(denebBlk.Block.Body.BlobKzgCommitments)
|
|
|
|
if cLen != len(blobs) || cLen != len(kzgproofs) {
|
|
|
|
return nil, errors.New("blob KZG commitments don't match number of blobs or KZG proofs")
|
2023-08-26 00:23:19 +00:00
|
|
|
}
|
2023-12-07 17:37:11 +00:00
|
|
|
blobSidecars := make([]*ethpb.BlobSidecar, cLen)
|
2023-11-27 23:44:52 +00:00
|
|
|
header, err := blk.Header()
|
2023-06-02 10:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-27 23:44:52 +00:00
|
|
|
body := blk.Block().Body()
|
|
|
|
for i := range blobSidecars {
|
|
|
|
proof, err := blocks.MerkleProofKZGCommitment(body, i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blobSidecars[i] = ðpb.BlobSidecar{
|
|
|
|
Index: uint64(i),
|
2023-12-07 17:37:11 +00:00
|
|
|
Blob: blobs[i],
|
|
|
|
KzgCommitment: denebBlk.Block.Body.BlobKzgCommitments[i],
|
|
|
|
KzgProof: kzgproofs[i],
|
2023-11-27 23:44:52 +00:00
|
|
|
SignedBlockHeader: header,
|
|
|
|
CommitmentInclusionProof: proof,
|
2023-06-02 10:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-27 23:44:52 +00:00
|
|
|
return blobSidecars, nil
|
2023-06-02 10:44:59 +00:00
|
|
|
}
|