Skip to content

Commit b069645

Browse files
committed
core/txpool: refactor blob tx validation
1 parent 61cc5d0 commit b069645

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

core/txpool/validation.go

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -138,35 +138,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
138138
return fmt.Errorf("%w: gas tip cap %v, minimum needed %v", ErrTxGasPriceTooLow, tx.GasTipCap(), opts.MinTip)
139139
}
140140
if tx.Type() == types.BlobTxType {
141-
// Ensure the blob fee cap satisfies the minimum blob gas price
142-
if tx.BlobGasFeeCapIntCmp(blobTxMinBlobGasPrice) < 0 {
143-
return fmt.Errorf("%w: blob fee cap %v, minimum needed %v", ErrTxGasPriceTooLow, tx.BlobGasFeeCap(), blobTxMinBlobGasPrice)
144-
}
145-
sidecar := tx.BlobTxSidecar()
146-
if sidecar == nil {
147-
return errors.New("missing sidecar in blob transaction")
148-
}
149-
// Ensure the number of items in the blob transaction and various side
150-
// data match up before doing any expensive validations
151-
hashes := tx.BlobHashes()
152-
if len(hashes) == 0 {
153-
return errors.New("blobless blob transaction")
154-
}
155-
maxBlobs := eip4844.MaxBlobsPerBlock(opts.Config, head.Time)
156-
if len(hashes) > maxBlobs {
157-
return fmt.Errorf("too many blobs in transaction: have %d, permitted %d", len(hashes), maxBlobs)
158-
}
159-
if opts.Config.IsOsaka(head.Number, head.Time) {
160-
// Ensure commitments, cell proofs and hashes are valid
161-
if err := validateBlobSidecarOsaka(hashes, sidecar); err != nil {
162-
return err
163-
}
164-
} else {
165-
// Ensure commitments, proofs and hashes are valid
166-
if err := validateBlobSidecar(hashes, sidecar); err != nil {
167-
return err
168-
}
169-
}
141+
return validateBlobTx(tx, head, opts)
170142
}
171143
if tx.Type() == types.SetCodeTxType {
172144
if len(tx.SetCodeAuthorizations()) == 0 {
@@ -176,21 +148,46 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
176148
return nil
177149
}
178150

179-
func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) error {
180-
if sidecar.Version != 0 {
181-
return fmt.Errorf("invalid sidecar version pre-osaka: %v", sidecar.Version)
151+
// validateBlobTx implements the blob-transaction specific validations.
152+
func validateBlobTx(tx *types.Transaction, head *types.Header, opts *ValidationOptions) error {
153+
sidecar := tx.BlobTxSidecar()
154+
if sidecar == nil {
155+
return errors.New("missing sidecar in blob transaction")
156+
}
157+
// Ensure the blob fee cap satisfies the minimum blob gas price
158+
if tx.BlobGasFeeCapIntCmp(blobTxMinBlobGasPrice) < 0 {
159+
return fmt.Errorf("%w: blob fee cap %v, minimum needed %v", ErrTxGasPriceTooLow, tx.BlobGasFeeCap(), blobTxMinBlobGasPrice)
160+
}
161+
// Ensure the number of items in the blob transaction and various side
162+
// data match up before doing any expensive validations
163+
hashes := tx.BlobHashes()
164+
if len(hashes) == 0 {
165+
return errors.New("blobless blob transaction")
166+
}
167+
maxBlobs := eip4844.MaxBlobsPerBlock(opts.Config, head.Time)
168+
if len(hashes) > maxBlobs {
169+
return fmt.Errorf("too many blobs in transaction: have %d, permitted %d", len(hashes), maxBlobs)
182170
}
183171
if len(sidecar.Blobs) != len(hashes) {
184172
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
185173
}
174+
// Fork-specific sidecar checks, including proof verification.
175+
if opts.Config.IsOsaka(head.Number, head.Time) {
176+
return validateBlobSidecarOsaka(sidecar, hashes)
177+
}
178+
return validateBlobSidecarLegacy(sidecar, hashes)
179+
}
180+
181+
func validateBlobSidecarLegacy(sidecar *types.BlobTxSidecar, hashes []common.Hash) error {
182+
if sidecar.Version != 0 {
183+
return fmt.Errorf("invalid sidecar version pre-osaka: %v", sidecar.Version)
184+
}
186185
if len(sidecar.Proofs) != len(hashes) {
187-
return fmt.Errorf("invalid number of %d blob proofs compared to %d blob hashes", len(sidecar.Proofs), len(hashes))
186+
return fmt.Errorf("invalid number of %d blob proofs expected %d", len(sidecar.Proofs), len(hashes))
188187
}
189188
if err := sidecar.ValidateBlobCommitmentHashes(hashes); err != nil {
190189
return err
191190
}
192-
// Blob commitments match with the hashes in the transaction, verify the
193-
// blobs themselves via KZG
194191
for i := range sidecar.Blobs {
195192
if err := kzg4844.VerifyBlobProof(&sidecar.Blobs[i], sidecar.Commitments[i], sidecar.Proofs[i]); err != nil {
196193
return fmt.Errorf("invalid blob %d: %v", i, err)
@@ -199,29 +196,20 @@ func validateBlobSidecar(hashes []common.Hash, sidecar *types.BlobTxSidecar) err
199196
return nil
200197
}
201198

202-
func validateBlobSidecarOsaka(hashes []common.Hash, sidecar *types.BlobTxSidecar) error {
199+
func validateBlobSidecarOsaka(sidecar *types.BlobTxSidecar, hashes []common.Hash) error {
203200
if sidecar.Version != 1 {
204201
return fmt.Errorf("invalid sidecar version post-osaka: %v", sidecar.Version)
205202
}
206-
if len(sidecar.Blobs) != len(hashes) {
207-
return fmt.Errorf("invalid number of %d blobs compared to %d blob hashes", len(sidecar.Blobs), len(hashes))
208-
}
209-
if len(sidecar.Commitments) != len(hashes) {
210-
return fmt.Errorf("invalid number of %d commitments compared to %d blob hashes", len(sidecar.Commitments), len(hashes))
211-
}
212203
if len(sidecar.Proofs) != len(hashes)*kzg4844.CellProofsPerBlob {
213204
return fmt.Errorf("invalid number of %d blob proofs expected %d", len(sidecar.Proofs), len(hashes)*kzg4844.CellProofsPerBlob)
214205
}
215206
if err := sidecar.ValidateBlobCommitmentHashes(hashes); err != nil {
216207
return err
217208
}
218-
// Blob commitments match with the hashes in the transaction, verify the
219-
// blobs themselves via KZG
220209
var blobs []*kzg4844.Blob
221210
for _, blob := range sidecar.Blobs {
222211
blobs = append(blobs, &blob)
223212
}
224-
225213
if err := kzg4844.VerifyCellProofs(blobs, sidecar.Commitments, sidecar.Proofs); err != nil {
226214
return err
227215
}

0 commit comments

Comments
 (0)