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