Skip to content

Commit cbd5efc

Browse files
committed
feat: featch blobs via /eth/v1/beacon/blobs from beacon node
1 parent 0697268 commit cbd5efc

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

params/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
const (
2525
VersionMajor = 5 // Major version component of the current release
2626
VersionMinor = 9 // Minor version component of the current release
27-
VersionPatch = 5 // Patch version component of the current release
27+
VersionPatch = 6 // Patch version component of the current release
2828
VersionMeta = "mainnet" // Version metadata to append to the version string
2929
)
3030

rollup/da_syncer/blob_client/beacon_node_client.go

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package blob_client
22

33
import (
44
"context"
5-
"crypto/sha256"
65
"encoding/json"
76
"fmt"
87
"io"
@@ -29,7 +28,7 @@ type BeaconNodeClient struct {
2928
var (
3029
beaconNodeGenesisEndpoint = "/eth/v1/beacon/genesis"
3130
beaconNodeSpecEndpoint = "/eth/v1/config/spec"
32-
beaconNodeBlobEndpoint = "/eth/v1/beacon/blob_sidecars"
31+
beaconNodeBlobEndpoint = "/eth/v1/beacon/blobs"
3332
)
3433

3534
func NewBeaconNodeClient(apiEndpoint string) (*BeaconNodeClient, error) {
@@ -110,7 +109,7 @@ func (c *BeaconNodeClient) GetBlobByVersionedHashAndBlockTime(ctx context.Contex
110109
slot := (blockTime - c.genesisTime) / c.secondsPerSlot
111110

112111
// get blob sidecar for slot
113-
blobSidecarPath, err := url.JoinPath(c.apiEndpoint, beaconNodeBlobEndpoint, fmt.Sprintf("%d", slot))
112+
blobSidecarPath, err := url.JoinPath(c.apiEndpoint, beaconNodeBlobEndpoint, fmt.Sprintf("%d?versioned_hashes=[%s]", slot, versionedHash))
114113
if err != nil {
115114
return nil, fmt.Errorf("failed to join path, err: %w", err)
116115
}
@@ -133,35 +132,27 @@ func (c *BeaconNodeClient) GetBlobByVersionedHashAndBlockTime(ctx context.Contex
133132
return nil, fmt.Errorf("beacon node request failed, status: %s, body: %s", resp.Status, bodyStr)
134133
}
135134

136-
var blobSidecarResp BlobSidecarResp
137-
err = json.NewDecoder(resp.Body).Decode(&blobSidecarResp)
135+
var blobsResp BlobsResp
136+
err = json.NewDecoder(resp.Body).Decode(&blobsResp)
138137
if err != nil {
139138
return nil, fmt.Errorf("failed to decode result into struct, err: %w", err)
140139
}
141140

142-
// find blob with desired versionedHash
143-
for _, blob := range blobSidecarResp.Data {
144-
// calculate blob hash from commitment and check it with desired
145-
commitmentBytes := common.FromHex(blob.KzgCommitment)
146-
if len(commitmentBytes) != lenKZGCommitment {
147-
return nil, fmt.Errorf("len of kzg commitment is not correct, expected: %d, got: %d", lenKZGCommitment, len(commitmentBytes))
148-
}
149-
commitment := kzg4844.Commitment(commitmentBytes)
150-
blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &commitment)
151-
152-
if blobVersionedHash == versionedHash {
153-
// found desired blob
154-
blobBytes := common.FromHex(blob.Blob)
155-
if len(blobBytes) != lenBlobBytes {
156-
return nil, fmt.Errorf("len of blob data is not correct, expected: %d, got: %d", lenBlobBytes, len(blobBytes))
157-
}
158-
159-
b := kzg4844.Blob(blobBytes)
160-
return &b, nil
161-
}
141+
// sanity check response length
142+
if len(blobsResp.Data) == 0 {
143+
return nil, fmt.Errorf("missing blob %v in slot %d", versionedHash, slot)
144+
}
145+
if len(blobsResp.Data) > 1 {
146+
return nil, fmt.Errorf("more than 1 blob returned from beacon node for slot %d, requested blob hash: %s, expected 1, got: %d", slot, versionedHash.Hex(), len(blobsResp.Data))
162147
}
163148

164-
return nil, fmt.Errorf("missing blob %v in slot %d", versionedHash, slot)
149+
blobBytes := common.FromHex(blobsResp.Data[0])
150+
if len(blobBytes) != lenBlobBytes {
151+
return nil, fmt.Errorf("len of blob data is not correct, expected: %d, got: %d", lenBlobBytes, len(blobBytes))
152+
}
153+
154+
b := kzg4844.Blob(blobBytes)
155+
return &b, nil
165156
}
166157

167158
type GenesisResp struct {
@@ -176,22 +167,6 @@ type SpecResp struct {
176167
} `json:"data"`
177168
}
178169

179-
type BlobSidecarResp struct {
180-
Data []struct {
181-
Index string `json:"index"`
182-
Blob string `json:"blob"`
183-
KzgCommitment string `json:"kzg_commitment"`
184-
KzgProof string `json:"kzg_proof"`
185-
SignedBlockHeader struct {
186-
Message struct {
187-
Slot string `json:"slot"`
188-
ProposerIndex string `json:"proposer_index"`
189-
ParentRoot string `json:"parent_root"`
190-
StateRoot string `json:"state_root"`
191-
BodyRoot string `json:"body_root"`
192-
} `json:"message"`
193-
Signature string `json:"signature"`
194-
} `json:"signed_block_header"`
195-
KzgCommitmentInclusionProof []string `json:"kzg_commitment_inclusion_proof"`
196-
} `json:"data"`
170+
type BlobsResp struct {
171+
Data []string `json:"data"` // array of blobs as hex strings
197172
}

0 commit comments

Comments
 (0)