|
| 1 | +package blob_client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strconv" |
| 12 | + |
| 13 | + "github.com/scroll-tech/go-ethereum/common" |
| 14 | + "github.com/scroll-tech/go-ethereum/crypto/kzg4844" |
| 15 | + "github.com/scroll-tech/go-ethereum/rollup/rollup_sync_service" |
| 16 | +) |
| 17 | + |
| 18 | +type BeaconNodeClient struct { |
| 19 | + apiEndpoint string |
| 20 | + l1Client *rollup_sync_service.L1Client |
| 21 | + genesisTime uint64 |
| 22 | + secondsPerSlot uint64 |
| 23 | +} |
| 24 | + |
| 25 | +var ( |
| 26 | + beaconNodeGenesisEndpoint = "/eth/v1/beacon/genesis" |
| 27 | + beaconNodeSpecEndpoint = "/eth/v1/config/spec" |
| 28 | + beaconNodeBlobEndpoint = "/eth/v1/beacon/blob_sidecars" |
| 29 | +) |
| 30 | + |
| 31 | +func NewBeaconNodeClient(apiEndpoint string, l1Client *rollup_sync_service.L1Client) (*BeaconNodeClient, error) { |
| 32 | + // get genesis time |
| 33 | + genesisPath, err := url.JoinPath(apiEndpoint, beaconNodeGenesisEndpoint) |
| 34 | + if err != nil { |
| 35 | + return nil, fmt.Errorf("failed to join path, err: %w", err) |
| 36 | + } |
| 37 | + resp, err := http.Get(genesisPath) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("cannot do request, err: %w", err) |
| 40 | + } |
| 41 | + defer resp.Body.Close() |
| 42 | + |
| 43 | + if resp.StatusCode != http.StatusOK { |
| 44 | + body, _ := io.ReadAll(resp.Body) |
| 45 | + bodyStr := string(body) |
| 46 | + return nil, fmt.Errorf("beacon node request failed, status: %s, body: %s", resp.Status, bodyStr) |
| 47 | + } |
| 48 | + |
| 49 | + var genesisResp GenesisResp |
| 50 | + err = json.NewDecoder(resp.Body).Decode(&genesisResp) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to decode result into struct, err: %w", err) |
| 53 | + } |
| 54 | + genesisTime, err := strconv.ParseUint(genesisResp.Data.GenesisTime, 10, 64) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("failed to decode genesis time %s, err: %w", genesisResp.Data.GenesisTime, err) |
| 57 | + } |
| 58 | + |
| 59 | + // get seconds per slot from spec |
| 60 | + specPath, err := url.JoinPath(apiEndpoint, beaconNodeSpecEndpoint) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to join path, err: %w", err) |
| 63 | + } |
| 64 | + resp, err = http.Get(specPath) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("cannot do request, err: %w", err) |
| 67 | + } |
| 68 | + defer resp.Body.Close() |
| 69 | + |
| 70 | + if resp.StatusCode != http.StatusOK { |
| 71 | + body, _ := io.ReadAll(resp.Body) |
| 72 | + bodyStr := string(body) |
| 73 | + return nil, fmt.Errorf("beacon node request failed, status: %s, body: %s", resp.Status, bodyStr) |
| 74 | + } |
| 75 | + |
| 76 | + var specResp SpecResp |
| 77 | + err = json.NewDecoder(resp.Body).Decode(&specResp) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("failed to decode result into struct, err: %w", err) |
| 80 | + } |
| 81 | + secondsPerSlot, err := strconv.ParseUint(specResp.Data.SecondsPerSlot, 10, 64) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("failed to decode seconds per slot %s, err: %w", specResp.Data.SecondsPerSlot, err) |
| 84 | + } |
| 85 | + if secondsPerSlot == 0 { |
| 86 | + return nil, fmt.Errorf("failed to make new BeaconNodeClient, secondsPerSlot is 0") |
| 87 | + } |
| 88 | + |
| 89 | + return &BeaconNodeClient{ |
| 90 | + apiEndpoint: apiEndpoint, |
| 91 | + l1Client: l1Client, |
| 92 | + genesisTime: genesisTime, |
| 93 | + secondsPerSlot: secondsPerSlot, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (c *BeaconNodeClient) GetBlobByVersionedHashAndBlockNumber(ctx context.Context, versionedHash common.Hash, blockNumber uint64) (*kzg4844.Blob, error) { |
| 98 | + // get block timestamp to calculate slot |
| 99 | + header, err := c.l1Client.GetHeaderByNumber(blockNumber) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("failed to get header by number, err: %w", err) |
| 102 | + } |
| 103 | + slot := (header.Time - c.genesisTime) / c.secondsPerSlot |
| 104 | + |
| 105 | + // get blob sidecar for slot |
| 106 | + blobSidecarPath, err := url.JoinPath(c.apiEndpoint, beaconNodeBlobEndpoint, fmt.Sprintf("%d", slot)) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("failed to join path, err: %w", err) |
| 109 | + } |
| 110 | + resp, err := http.Get(blobSidecarPath) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("cannot do request, err: %w", err) |
| 113 | + } |
| 114 | + defer resp.Body.Close() |
| 115 | + |
| 116 | + if resp.StatusCode != http.StatusOK { |
| 117 | + body, _ := io.ReadAll(resp.Body) |
| 118 | + bodyStr := string(body) |
| 119 | + return nil, fmt.Errorf("beacon node request failed, status: %s, body: %s", resp.Status, bodyStr) |
| 120 | + } |
| 121 | + |
| 122 | + var blobSidecarResp BlobSidecarResp |
| 123 | + err = json.NewDecoder(resp.Body).Decode(&blobSidecarResp) |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("failed to decode result into struct, err: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + // find blob with desired versionedHash |
| 129 | + for _, blob := range blobSidecarResp.Data { |
| 130 | + // calculate blob hash from commitment and check it with desired |
| 131 | + commitmentBytes := common.FromHex(blob.KzgCommitment) |
| 132 | + if len(commitmentBytes) != lenKZGCommitment { |
| 133 | + return nil, fmt.Errorf("len of kzg commitment is not correct, expected: %d, got: %d", lenKZGCommitment, len(commitmentBytes)) |
| 134 | + } |
| 135 | + commitment := kzg4844.Commitment(commitmentBytes) |
| 136 | + blobVersionedHash := kzg4844.CalcBlobHashV1(sha256.New(), &commitment) |
| 137 | + |
| 138 | + if blobVersionedHash == versionedHash { |
| 139 | + // found desired blob |
| 140 | + blobBytes := common.FromHex(blob.Blob) |
| 141 | + if len(blobBytes) != lenBlobBytes { |
| 142 | + return nil, fmt.Errorf("len of blob data is not correct, expected: %d, got: %d", lenBlobBytes, len(blobBytes)) |
| 143 | + } |
| 144 | + |
| 145 | + b := kzg4844.Blob(blobBytes) |
| 146 | + return &b, nil |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return nil, fmt.Errorf("missing blob %v in slot %d, block number %d", versionedHash, slot, blockNumber) |
| 151 | +} |
| 152 | + |
| 153 | +type GenesisResp struct { |
| 154 | + Data struct { |
| 155 | + GenesisTime string `json:"genesis_time"` |
| 156 | + } `json:"data"` |
| 157 | +} |
| 158 | + |
| 159 | +type SpecResp struct { |
| 160 | + Data struct { |
| 161 | + SecondsPerSlot string `json:"SECONDS_PER_SLOT"` |
| 162 | + } `json:"data"` |
| 163 | +} |
| 164 | + |
| 165 | +type BlobSidecarResp struct { |
| 166 | + Data []struct { |
| 167 | + Index string `json:"index"` |
| 168 | + Blob string `json:"blob"` |
| 169 | + KzgCommitment string `json:"kzg_commitment"` |
| 170 | + KzgProof string `json:"kzg_proof"` |
| 171 | + SignedBlockHeader struct { |
| 172 | + Message struct { |
| 173 | + Slot string `json:"slot"` |
| 174 | + ProposerIndex string `json:"proposer_index"` |
| 175 | + ParentRoot string `json:"parent_root"` |
| 176 | + StateRoot string `json:"state_root"` |
| 177 | + BodyRoot string `json:"body_root"` |
| 178 | + } `json:"message"` |
| 179 | + Signature string `json:"signature"` |
| 180 | + } `json:"signed_block_header"` |
| 181 | + KzgCommitmentInclusionProof []string `json:"kzg_commitment_inclusion_proof"` |
| 182 | + } `json:"data"` |
| 183 | +} |
0 commit comments