Skip to content

Commit 24abaa7

Browse files
curryxbocorey
andauthored
Fix GetBlobSidecarsEnhanced (#825)
Co-authored-by: corey <[email protected]>
1 parent aa7f191 commit 24abaa7

File tree

6 files changed

+805
-66
lines changed

6 files changed

+805
-66
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ update_all_mod:
3030
@$(MAKE) update_mod MODULE=ops/tools
3131
@$(MAKE) update_mod MODULE=oracle
3232
@$(MAKE) update_mod MODULE=tx-submitter
33+
@$(MAKE) update_mod MODULE=token-price-oracle
3334

3435

3536
update:

go.work.sum

Lines changed: 793 additions & 0 deletions
Large diffs are not rendered by default.

node/derivation/beacon.go

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sync"
1414

1515
"github.com/morph-l2/go-ethereum/common"
16-
"github.com/morph-l2/go-ethereum/common/hexutil"
1716
"github.com/morph-l2/go-ethereum/core/types"
1817
"github.com/morph-l2/go-ethereum/crypto/kzg4844"
1918
"github.com/morph-l2/go-ethereum/params"
@@ -131,70 +130,16 @@ func (cl *L1BeaconClient) GetBlobSidecars(ctx context.Context, ref L1BlockRef, h
131130
if err := cl.apiReq(ctx, &resp, builder.String()); err != nil {
132131
return nil, fmt.Errorf("%w: failed to fetch blob sidecars for slot %v block %v", err, slot, ref)
133132
}
134-
if len(hashes) != len(resp.Data) {
135-
return nil, fmt.Errorf("expected %v sidecars but got %v", len(hashes), len(resp.Data))
133+
// Some Beacon nodes may ignore the indices parameter and return all sidecars for the slot.
134+
// We only need to ensure we have at least the number of sidecars we requested.
135+
// Callers are responsible for filtering the correct sidecars by index if needed.
136+
if len(resp.Data) < len(hashes) {
137+
return nil, fmt.Errorf("expected at least %v sidecars but got %v", len(hashes), len(resp.Data))
136138
}
137139

138140
return resp.Data, nil
139141
}
140142

141-
// GetBlobSidecar fetches blob sidecars that were confirmed in the specified L1 block with the
142-
// given indexed hashes. Order of the returned sidecars is not guaranteed, and blob data is not
143-
// checked for validity.
144-
func (cl *L1BeaconClient) GetBlobSidecar(ctx context.Context, ref L1BlockRef, hashes []IndexedBlobHash) (types.BlobTxSidecar, error) {
145-
blobSidecars, err := cl.GetBlobSidecars(ctx, ref, hashes)
146-
if err != nil {
147-
return types.BlobTxSidecar{}, fmt.Errorf("%w: failed to get blob sidecars for L1BlockRef %v", err, ref)
148-
}
149-
return sidecarFromSidecars(blobSidecars, hashes)
150-
}
151-
152-
func indexFunc(s []*BlobSidecar, f func(blobSidecars *BlobSidecar) bool) int {
153-
for i := range s {
154-
if f(s[i]) {
155-
return i
156-
}
157-
}
158-
return -1
159-
}
160-
161-
func sidecarFromSidecars(blobSidecars []*BlobSidecar, hashes []IndexedBlobHash) (types.BlobTxSidecar, error) {
162-
var blobTxSidecar types.BlobTxSidecar
163-
for i, ih := range hashes {
164-
// The beacon node api makes no guarantees on order of the returned blob sidecars, so
165-
// search for the sidecar that matches the current indexed hash to ensure blobs are
166-
// returned in the same order.
167-
scIndex := indexFunc(
168-
blobSidecars,
169-
func(sc *BlobSidecar) bool { return uint64(sc.Index) == ih.Index })
170-
if scIndex == -1 {
171-
return types.BlobTxSidecar{}, fmt.Errorf("no blob in response matches desired index: %v", ih.Index)
172-
}
173-
sidecar := blobSidecars[scIndex]
174-
175-
// make sure the blob's kzg commitment hashes to the expected value
176-
hash := KZGToVersionedHash(kzg4844.Commitment(sidecar.KZGCommitment))
177-
if hash != ih.Hash {
178-
return types.BlobTxSidecar{}, fmt.Errorf("expected hash %s for blob at index %d but got %s", ih.Hash, ih.Index, hash)
179-
}
180-
181-
// confirm blob data is valid by verifying its proof against the commitment
182-
var blob Blob
183-
b, err := hexutil.Decode(sidecar.Blob)
184-
if err != nil {
185-
return types.BlobTxSidecar{}, fmt.Errorf("hexutil.Decode(sidecar.Blob) error:%v", err)
186-
}
187-
copy(blob[:], b)
188-
if err := VerifyBlobProof(&blob, kzg4844.Commitment(sidecar.KZGCommitment), kzg4844.Proof(sidecar.KZGProof)); err != nil {
189-
return types.BlobTxSidecar{}, fmt.Errorf("%w: blob at index %d failed verification", err, i)
190-
}
191-
blobTxSidecar.Blobs = append(blobTxSidecar.Blobs, *blob.KZGBlob())
192-
blobTxSidecar.Commitments = append(blobTxSidecar.Commitments, kzg4844.Commitment(sidecar.KZGCommitment))
193-
blobTxSidecar.Proofs = append(blobTxSidecar.Proofs, kzg4844.Proof(sidecar.KZGProof))
194-
}
195-
return blobTxSidecar, nil
196-
}
197-
198143
// IndexedBlobHash represents a blob hash that commits to a single blob confirmed in a block. The
199144
// index helps us avoid unnecessary blob to blob hash conversions to find the right content in a
200145
// sidecar.

node/derivation/beacon_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func TestGetBlob(t *testing.T) {
4141
fmt.Println(indexedBlobHashes)
4242
header, err := l1Client.HeaderByNumber(context.Background(), big.NewInt(int64(lg.BlockNumber)))
4343
require.NoError(t, err)
44-
var bts eth.BlobTxSidecar
44+
var bts []*BlobSidecar
4545
if len(indexedBlobHashes) != 0 {
46-
bts, err = l1BeaconClient.GetBlobSidecar(context.Background(), L1BlockRef{
46+
bts, err = l1BeaconClient.GetBlobSidecarsEnhanced(context.Background(), L1BlockRef{
4747
Time: header.Time,
4848
}, indexedBlobHashes)
4949
require.NoError(t, err)
5050
}
51-
t.Log(len(bts.Blobs))
51+
t.Log(len(bts))
5252
}
5353

5454
}

token-price-oracle/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ replace (
88
)
99

1010
require (
11-
github.com/morph-l2/go-ethereum v1.10.14-0.20251125061742-69718a9dcab9
11+
github.com/morph-l2/go-ethereum v1.10.14-0.20251119080508-d085f8c79a53
1212
github.com/prometheus/client_golang v1.17.0
1313
github.com/sirupsen/logrus v1.9.3
1414
github.com/urfave/cli v1.22.17

token-price-oracle/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqky
143143
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
144144
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
145145
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
146-
github.com/morph-l2/go-ethereum v1.10.14-0.20251125061742-69718a9dcab9 h1:IiOGoNPhICkQdLjwodT5lp4Vd9Zzfwl6cyk+HPwAeyA=
147-
github.com/morph-l2/go-ethereum v1.10.14-0.20251125061742-69718a9dcab9/go.mod h1:tiFPeidxjoCmLj18ne9H3KQdIGTCvRC30qlef06Fd9M=
146+
github.com/morph-l2/go-ethereum v1.10.14-0.20251119080508-d085f8c79a53 h1:8+qaUTn1/eyS8er4RkibhHMFC/L4IgqIXLtORakBDkI=
147+
github.com/morph-l2/go-ethereum v1.10.14-0.20251119080508-d085f8c79a53/go.mod h1:tiFPeidxjoCmLj18ne9H3KQdIGTCvRC30qlef06Fd9M=
148148
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
149149
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
150150
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=

0 commit comments

Comments
 (0)