core/types: fix rlp decoding

This commit is contained in:
Marius van der Wijden 2025-04-08 14:51:26 +02:00 committed by MariusVanDerWijden
parent 6349f6afb5
commit 3ef538502f
3 changed files with 30 additions and 10 deletions

View file

@ -173,6 +173,9 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
} }
func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) error { func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) error {
if sidecar.Version != 0 {
return fmt.Errorf("invalid sidecar version pre-osaka: %v", sidecar.Version)
}
if len(sidecar.Blobs) != len(hashes) { if len(sidecar.Blobs) != len(hashes) {
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes)) return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
} }
@ -193,6 +196,9 @@ func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) err
} }
func validateBlobSidecarOsaka(hashes []common.Hash, sidecar *types.BlobTxSidecar) error { func validateBlobSidecarOsaka(hashes []common.Hash, sidecar *types.BlobTxSidecar) error {
if sidecar.Version != 1 {
return fmt.Errorf("invalid sidecar version post-osaka: %v", sidecar.Version)
}
if len(sidecar.Blobs) != len(hashes) { if len(sidecar.Blobs) != len(hashes) {
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes)) return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
} }

View file

@ -55,6 +55,7 @@ type BlobTx struct {
// BlobTxSidecar contains the blobs of a blob transaction. // BlobTxSidecar contains the blobs of a blob transaction.
type BlobTxSidecar struct { type BlobTxSidecar struct {
Version byte // Version
Blobs []kzg4844.Blob // Blobs needed by the blob pool Blobs []kzg4844.Blob // Blobs needed by the blob pool
Commitments []kzg4844.Commitment // Commitments needed by the blob pool Commitments []kzg4844.Commitment // Commitments needed by the blob pool
Proofs []kzg4844.Proof // Proofs needed by the blob pool Proofs []kzg4844.Proof // Proofs needed by the blob pool
@ -125,8 +126,8 @@ type blobTxWithBlobs struct {
} }
type versionedBlobTxWithBlobs struct { type versionedBlobTxWithBlobs struct {
Version byte
BlobTx *BlobTx BlobTx *BlobTx
Version byte
Blobs []kzg4844.Blob Blobs []kzg4844.Blob
Commitments []kzg4844.Commitment Commitments []kzg4844.Commitment
Proofs []kzg4844.Proof Proofs []kzg4844.Proof
@ -240,6 +241,17 @@ func (tx *BlobTx) encode(b *bytes.Buffer) error {
if tx.Sidecar == nil { if tx.Sidecar == nil {
return rlp.Encode(b, tx) return rlp.Encode(b, tx)
} }
// Encode a cell proof transaction
if tx.Sidecar.Version != 0 {
inner := &versionedBlobTxWithBlobs{
BlobTx: tx,
Version: tx.Sidecar.Version,
Blobs: tx.Sidecar.Blobs,
Commitments: tx.Sidecar.Commitments,
Proofs: tx.Sidecar.Proofs,
}
return rlp.Encode(b, inner)
}
inner := &blobTxWithBlobs{ inner := &blobTxWithBlobs{
BlobTx: tx, BlobTx: tx,
Blobs: tx.Sidecar.Blobs, Blobs: tx.Sidecar.Blobs,
@ -269,20 +281,22 @@ func (tx *BlobTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx) return rlp.DecodeBytes(input, tx)
} }
// It's a tx with blobs. // It's a tx with blobs. Try to decode it as version 0.
var inner blobTxWithBlobs var inner versionedBlobTxWithBlobs
if err := rlp.DecodeBytes(input, &inner); err != nil { if err := rlp.DecodeBytes(input, &inner); err != nil {
var innerWithVersion versionedBlobTxWithBlobs var innerV0 blobTxWithBlobs
if err := rlp.DecodeBytes(input, &innerWithVersion); err != nil { if err := rlp.DecodeBytes(input, &innerV0); err != nil {
return err return err
} }
inner.BlobTx = innerWithVersion.BlobTx inner.BlobTx = innerV0.BlobTx
inner.Blobs = innerWithVersion.Blobs inner.Version = 0
inner.Commitments = innerWithVersion.Commitments inner.Blobs = innerV0.Blobs
inner.Proofs = innerWithVersion.Proofs inner.Commitments = innerV0.Commitments
inner.Proofs = innerV0.Proofs
} }
*tx = *inner.BlobTx *tx = *inner.BlobTx
tx.Sidecar = &BlobTxSidecar{ tx.Sidecar = &BlobTxSidecar{
Version: inner.Version,
Blobs: inner.Blobs, Blobs: inner.Blobs,
Commitments: inner.Commitments, Commitments: inner.Commitments,
Proofs: inner.Proofs, Proofs: inner.Proofs,

View file

@ -571,7 +571,7 @@ func (api *ConsensusAPI) GetBlobsV1(hashes []common.Hash) ([]*engine.BlobAndProo
index[hash] = i index[hash] = i
} }
for i, sidecar := range sidecars { for i, sidecar := range sidecars {
if res[i] != nil { if res[i] != nil && sidecar == nil {
// already filled // already filled
continue continue
} }