2023-10-16 05:30:33 +00:00
package verify
import (
"github.com/pkg/errors"
2024-02-15 05:46:47 +00:00
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
2023-10-16 05:30:33 +00:00
)
var (
errBlobVerification = errors . New ( "unable to verify blobs" )
2023-11-21 18:44:38 +00:00
ErrIncorrectBlobIndex = errors . New ( "incorrect blob index" )
ErrBlobBlockMisaligned = errors . Wrap ( errBlobVerification , "root of block header in blob sidecar does not match block root" )
2023-10-16 05:30:33 +00:00
ErrMismatchedBlobCommitments = errors . Wrap ( errBlobVerification , "commitments at given slot, root and index do not match" )
)
// BlobAlignsWithBlock verifies if the blob aligns with the block.
2023-11-21 18:44:38 +00:00
func BlobAlignsWithBlock ( blob blocks . ROBlob , block blocks . ROBlock ) error {
2023-10-16 05:30:33 +00:00
if block . Version ( ) < version . Deneb {
return nil
}
2023-11-21 18:44:38 +00:00
if blob . Index >= fieldparams . MaxBlobsPerBlock {
return errors . Wrapf ( ErrIncorrectBlobIndex , "index %d exceeds MAX_BLOBS_PER_BLOCK %d" , blob . Index , fieldparams . MaxBlobsPerBlock )
2023-10-16 05:30:33 +00:00
}
2023-11-21 18:44:38 +00:00
if blob . BlockRoot ( ) != block . Root ( ) {
return ErrBlobBlockMisaligned
2023-10-16 05:30:33 +00:00
}
2023-11-21 18:44:38 +00:00
// Verify commitment byte values match
// TODO: verify commitment inclusion proof - actually replace this with a better rpc blob verification stack altogether.
commits , err := block . Block ( ) . Body ( ) . BlobKzgCommitments ( )
if err != nil {
return err
2023-10-16 05:30:33 +00:00
}
blockCommitment := bytesutil . ToBytes48 ( commits [ blob . Index ] )
blobCommitment := bytesutil . ToBytes48 ( blob . KzgCommitment )
if blobCommitment != blockCommitment {
2023-11-21 18:44:38 +00:00
return errors . Wrapf ( ErrMismatchedBlobCommitments , "commitment %#x != block commitment %#x, at index %d for block root %#x at slot %d " , blobCommitment , blockCommitment , blob . Index , block . Root ( ) , blob . Slot ( ) )
2023-10-16 05:30:33 +00:00
}
return nil
}