Skip to content

Commit 1601f39

Browse files
cskiralyfjl
andauthored
core/txpool/blobpool: remove conversion in GetBlobs (#32578)
This disables blob proof conversion in `GetBlobs` by default, making it conditional. --------- Co-authored-by: Felix Lange <[email protected]>
1 parent 103b8b2 commit 1601f39

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

core/txpool/blobpool/blobpool.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,11 @@ func (p *BlobPool) GetMetadata(hash common.Hash) *txpool.TxMetadata {
13311331
//
13321332
// This is a utility method for the engine API, enabling consensus clients to
13331333
// retrieve blobs from the pools directly instead of the network.
1334-
func (p *BlobPool) GetBlobs(vhashes []common.Hash, version byte) ([]*kzg4844.Blob, []kzg4844.Commitment, [][]kzg4844.Proof, error) {
1334+
//
1335+
// The version argument specifies the type of proofs to return, either the
1336+
// blob proofs (version 0) or the cell proofs (version 1). Proofs conversion is
1337+
// CPU intensive, so only done if explicitly requested with the convert flag.
1338+
func (p *BlobPool) GetBlobs(vhashes []common.Hash, version byte, convert bool) ([]*kzg4844.Blob, []kzg4844.Commitment, [][]kzg4844.Proof, error) {
13351339
var (
13361340
blobs = make([]*kzg4844.Blob, len(vhashes))
13371341
commitments = make([]kzg4844.Commitment, len(vhashes))
@@ -1343,13 +1347,14 @@ func (p *BlobPool) GetBlobs(vhashes []common.Hash, version byte) ([]*kzg4844.Blo
13431347
for i, h := range vhashes {
13441348
indices[h] = append(indices[h], i)
13451349
}
1350+
13461351
for _, vhash := range vhashes {
1347-
// Skip duplicate vhash that was already resolved in a previous iteration
13481352
if _, ok := filled[vhash]; ok {
1353+
// Skip vhash that was already resolved in a previous iteration
13491354
continue
13501355
}
1351-
// Retrieve the corresponding blob tx with the vhash, skip blob resolution
1352-
// if it's not found locally and place the null instead.
1356+
1357+
// Retrieve the corresponding blob tx with the vhash.
13531358
p.lock.RLock()
13541359
txID, exists := p.lookup.storeidOfBlob(vhash)
13551360
p.lock.RUnlock()
@@ -1379,6 +1384,14 @@ func (p *BlobPool) GetBlobs(vhashes []common.Hash, version byte) ([]*kzg4844.Blo
13791384
if !ok {
13801385
continue // non-interesting blob
13811386
}
1387+
// Mark hash as seen.
1388+
filled[hash] = struct{}{}
1389+
if sidecar.Version != version && !convert {
1390+
// Skip blobs with incompatible version. Note we still track the blob hash
1391+
// in `filled` here, ensuring that we do not resolve this tx another time.
1392+
continue
1393+
}
1394+
// Get or convert the proof.
13821395
var pf []kzg4844.Proof
13831396
switch version {
13841397
case types.BlobSidecarVersion0:
@@ -1411,7 +1424,6 @@ func (p *BlobPool) GetBlobs(vhashes []common.Hash, version byte) ([]*kzg4844.Blo
14111424
commitments[index] = sidecar.Commitments[i]
14121425
proofs[index] = pf
14131426
}
1414-
filled[hash] = struct{}{}
14151427
}
14161428
}
14171429
return blobs, commitments, proofs, nil

core/txpool/blobpool/blobpool_test.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,11 @@ func verifyBlobRetrievals(t *testing.T, pool *BlobPool) {
423423
hashes = append(hashes, tx.vhashes...)
424424
}
425425
}
426-
blobs1, _, proofs1, err := pool.GetBlobs(hashes, types.BlobSidecarVersion0)
426+
blobs1, _, proofs1, err := pool.GetBlobs(hashes, types.BlobSidecarVersion0, false)
427427
if err != nil {
428428
t.Fatal(err)
429429
}
430-
blobs2, _, proofs2, err := pool.GetBlobs(hashes, types.BlobSidecarVersion1)
430+
blobs2, _, proofs2, err := pool.GetBlobs(hashes, types.BlobSidecarVersion1, false)
431431
if err != nil {
432432
t.Fatal(err)
433433
}
@@ -441,22 +441,18 @@ func verifyBlobRetrievals(t *testing.T, pool *BlobPool) {
441441
return
442442
}
443443
for i, hash := range hashes {
444-
// If an item is missing, but shouldn't, error
445-
if blobs1[i] == nil || proofs1[i] == nil {
446-
t.Errorf("tracked blob retrieval failed: item %d, hash %x", i, hash)
447-
continue
448-
}
449-
if blobs2[i] == nil || proofs2[i] == nil {
444+
// If an item is missing from both, but shouldn't, error
445+
if (blobs1[i] == nil || proofs1[i] == nil) && (blobs2[i] == nil || proofs2[i] == nil) {
450446
t.Errorf("tracked blob retrieval failed: item %d, hash %x", i, hash)
451447
continue
452448
}
453449
// Item retrieved, make sure it matches the expectation
454450
index := testBlobIndices[hash]
455-
if *blobs1[i] != *testBlobs[index] || proofs1[i][0] != testBlobProofs[index] {
451+
if blobs1[i] != nil && (*blobs1[i] != *testBlobs[index] || proofs1[i][0] != testBlobProofs[index]) {
456452
t.Errorf("retrieved blob or proof mismatch: item %d, hash %x", i, hash)
457453
continue
458454
}
459-
if *blobs2[i] != *testBlobs[index] || !slices.Equal(proofs2[i], testBlobCellProofs[index]) {
455+
if blobs2[i] != nil && (*blobs2[i] != *testBlobs[index] || !slices.Equal(proofs2[i], testBlobCellProofs[index])) {
460456
t.Errorf("retrieved blob or proof mismatch: item %d, hash %x", i, hash)
461457
continue
462458
}
@@ -1926,8 +1922,9 @@ func TestGetBlobs(t *testing.T) {
19261922
cases := []struct {
19271923
start int
19281924
limit int
1929-
fillRandom bool
1930-
version byte
1925+
fillRandom bool // Whether to randomly fill some of the requested blobs with unknowns
1926+
version byte // Blob sidecar version to request
1927+
convert bool // Whether to convert version on retrieval
19311928
}{
19321929
{
19331930
start: 0, limit: 6,
@@ -1993,6 +1990,11 @@ func TestGetBlobs(t *testing.T) {
19931990
start: 0, limit: 18, fillRandom: true,
19941991
version: types.BlobSidecarVersion1,
19951992
},
1993+
{
1994+
start: 0, limit: 18, fillRandom: true,
1995+
version: types.BlobSidecarVersion1,
1996+
convert: true, // Convert some version 0 blobs to version 1 while retrieving
1997+
},
19961998
}
19971999
for i, c := range cases {
19982000
var (
@@ -2014,7 +2016,7 @@ func TestGetBlobs(t *testing.T) {
20142016
filled[len(vhashes)] = struct{}{}
20152017
vhashes = append(vhashes, testrand.Hash())
20162018
}
2017-
blobs, _, proofs, err := pool.GetBlobs(vhashes, c.version)
2019+
blobs, _, proofs, err := pool.GetBlobs(vhashes, c.version, c.convert)
20182020
if err != nil {
20192021
t.Errorf("Unexpected error for case %d, %v", i, err)
20202022
}
@@ -2029,6 +2031,7 @@ func TestGetBlobs(t *testing.T) {
20292031

20302032
var unknown int
20312033
for j := 0; j < len(blobs); j++ {
2034+
testBlobIndex := c.start + j - unknown
20322035
if _, exist := filled[j]; exist {
20332036
if blobs[j] != nil || proofs[j] != nil {
20342037
t.Errorf("Unexpected blob and proof, item %d", j)
@@ -2038,17 +2041,22 @@ func TestGetBlobs(t *testing.T) {
20382041
}
20392042
// If an item is missing, but shouldn't, error
20402043
if blobs[j] == nil || proofs[j] == nil {
2041-
t.Errorf("tracked blob retrieval failed: item %d, hash %x", j, vhashes[j])
2044+
// This is only an error if there was no version mismatch
2045+
if c.convert ||
2046+
(c.version == types.BlobSidecarVersion1 && 6 <= testBlobIndex && testBlobIndex < 12) ||
2047+
(c.version == types.BlobSidecarVersion0 && (testBlobIndex < 6 || 12 <= testBlobIndex)) {
2048+
t.Errorf("tracked blob retrieval failed: item %d, hash %x", j, vhashes[j])
2049+
}
20422050
continue
20432051
}
20442052
// Item retrieved, make sure the blob matches the expectation
2045-
if *blobs[j] != *testBlobs[c.start+j-unknown] {
2053+
if *blobs[j] != *testBlobs[testBlobIndex] {
20462054
t.Errorf("retrieved blob mismatch: item %d, hash %x", j, vhashes[j])
20472055
continue
20482056
}
20492057
// Item retrieved, make sure the proof matches the expectation
20502058
if c.version == types.BlobSidecarVersion0 {
2051-
if proofs[j][0] != testBlobProofs[c.start+j-unknown] {
2059+
if proofs[j][0] != testBlobProofs[testBlobIndex] {
20522060
t.Errorf("retrieved proof mismatch: item %d, hash %x", j, vhashes[j])
20532061
}
20542062
} else {

eth/catalyst/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func (api *ConsensusAPI) GetBlobsV1(hashes []common.Hash) ([]*engine.BlobAndProo
495495
if len(hashes) > 128 {
496496
return nil, engine.TooLargeRequest.With(fmt.Errorf("requested blob count too large: %v", len(hashes)))
497497
}
498-
blobs, _, proofs, err := api.eth.BlobTxPool().GetBlobs(hashes, types.BlobSidecarVersion0)
498+
blobs, _, proofs, err := api.eth.BlobTxPool().GetBlobs(hashes, types.BlobSidecarVersion0, false)
499499
if err != nil {
500500
return nil, engine.InvalidParams.With(err)
501501
}
@@ -555,7 +555,7 @@ func (api *ConsensusAPI) GetBlobsV2(hashes []common.Hash) ([]*engine.BlobAndProo
555555
return nil, nil
556556
}
557557

558-
blobs, _, proofs, err := api.eth.BlobTxPool().GetBlobs(hashes, types.BlobSidecarVersion1)
558+
blobs, _, proofs, err := api.eth.BlobTxPool().GetBlobs(hashes, types.BlobSidecarVersion1, false)
559559
if err != nil {
560560
return nil, engine.InvalidParams.With(err)
561561
}

0 commit comments

Comments
 (0)