Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Mar 26, 2024
1 parent ad3a89a commit dc2143e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
5 changes: 2 additions & 3 deletions driver/txlist_fetcher/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package txlistdecoder
import (
"context"
"crypto/sha256"
"math/big"

"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -36,12 +35,12 @@ func (d *BlobFetcher) Fetch(
}

// Fetch the L1 block sidecars.
sidecars, err := d.rpc.L1Beacon.GetBlobs(ctx, new(big.Int).SetUint64(meta.L1Height+1))
sidecars, err := d.rpc.L1Beacon.GetBlobs(ctx, meta.Timestamp)
if err != nil {
return nil, err
}

log.Info("Fetch sidecars", "slot", meta.L1Height+1, "sidecars", len(sidecars))
log.Info("Fetch sidecars", "blockNumber", meta.L1Height+1, "sidecars", len(sidecars))

// Compare the blob hash with the sidecar's kzg commitment.
for i, sidecar := range sidecars {
Expand Down
89 changes: 61 additions & 28 deletions pkg/rpc/beaconclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package rpc

import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"time"

"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/log"
"github.com/prysmaticlabs/prysm/v4/api/client"
"github.com/prysmaticlabs/prysm/v4/api/client/beacon"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/blob"
Expand All @@ -20,12 +16,25 @@ import (
var (
// Request urls.
sidecarsRequestURL = "eth/v1/beacon/blob_sidecars/%d"
genesisRequestURL = "eth/v1/beacon/genesis"
)

type ConfigSpec struct {
SecondsPerSlot string `json:"SECONDS_PER_SLOT"`
}

type GenesisResponse struct {
Data struct {
GenesisTime string `json:"genesis_time"`
} `json:"data"`
}

type BeaconClient struct {
*beacon.Client

timeout time.Duration
timeout time.Duration
genesisTime uint64
secondsPerSlot uint64
}

// NewBeaconClient returns a new beacon client.
Expand All @@ -34,43 +43,67 @@ func NewBeaconClient(endpoint string, timeout time.Duration) (*BeaconClient, err
if err != nil {
return nil, err
}
return &BeaconClient{cli, timeout}, nil
}

// GetBlobs returns the sidecars for a given slot.
func (c *BeaconClient) GetBlobs(ctx context.Context, slot *big.Int) ([]*blob.Sidecar, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

var sidecars *blob.SidecarsResponse
resBytes, err := c.Get(ctxWithTimeout, fmt.Sprintf(sidecarsRequestURL, slot))
// Get the genesis time.
var genesisDetail *GenesisResponse
resBytes, err := cli.Get(ctx, genesisRequestURL)
if err != nil {
return nil, err
}

return sidecars.Data, json.Unmarshal(resBytes, &sidecars)
if err := json.Unmarshal(resBytes, &genesisDetail); err != nil {
return nil, err
}

genesisTime, err := strconv.Atoi(genesisDetail.Data.GenesisTime)
if err != nil {
return nil, err
}

log.Info("L1 genesis time", "time", genesisTime)

// Get the seconds per slot.
spec, err := cli.GetConfigSpec(ctx)
if err != nil {
return nil, err
}

secondsPerSlot, err := strconv.Atoi(spec.Data.(map[string]interface{})["SECONDS_PER_SLOT"].(string))
if err != nil {
return nil, err
}

log.Info("L1 seconds per slot", "seconds", secondsPerSlot)

return &BeaconClient{cli, timeout, uint64(genesisTime), uint64(secondsPerSlot)}, nil
}

// GetBlobByHash returns the sidecars for a given slot.
func (c *BeaconClient) GetBlobByHash(ctx context.Context, slot *big.Int, blobHash common.Hash) ([]byte, error) {
// GetBlobs returns the sidecars for a given slot.
func (c *BeaconClient) GetBlobs(ctx context.Context, time uint64) ([]*blob.Sidecar, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, c.timeout)
defer cancel()

sidecars, err := c.GetBlobs(ctxWithTimeout, slot)
slot, err := c.timeToSlot(time)
if err != nil {
return nil, err
}

for _, sidecar := range sidecars {
commitment := kzg4844.Commitment(common.FromHex(sidecar.KzgCommitment))
if kzg4844.CalcBlobHashV1(
sha256.New(),
&commitment,
) == blobHash {
blob := eth.Blob(common.FromHex(sidecar.Blob))
return blob.ToData()
}
var sidecars *blob.SidecarsResponse
resBytes, err := c.Get(ctxWithTimeout, fmt.Sprintf(sidecarsRequestURL, slot))
if err != nil {
return nil, err
}

return nil, errors.New("sidecar not found")
return sidecars.Data, json.Unmarshal(resBytes, &sidecars)
}

// TimeToSlot returns the slots of the given timestamp.
func (c *BeaconClient) timeToSlot(timestamp uint64) (uint64, error) {
if timestamp < c.genesisTime {
return 0, fmt.Errorf("provided timestamp (%v) precedes genesis time (%v)", timestamp, c.genesisTime)
}
return (timestamp - c.genesisTime) / c.secondsPerSlot, nil
}

0 comments on commit dc2143e

Please sign in to comment.