-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathbeacon.rs
More file actions
146 lines (121 loc) · 4.18 KB
/
beacon.rs
File metadata and controls
146 lines (121 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::str::FromStr;
use ethers::core::k256::sha2::{Digest, Sha256};
use reqwest::{Client, Url};
use serde::{Deserialize, Serialize};
use serde_json::Value;
// See https://eips.ethereum.org/EIPS/eip-4844#parameters
pub const KZG_VERSIONED_HASH: u8 = 0x1;
pub struct BeaconClient {
beacon_client_url: String,
api_client: Client,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum BeaconAPIResponse {
Success { data: Value },
Error { code: u64, message: String },
}
#[derive(Debug)]
pub enum BeaconClientError {
Url(url::ParseError),
ReqwestError(reqwest::Error),
APIError { code: u64, message: String },
Deserialization(serde_json::Error),
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
// https://ethereum.github.io/beacon-APIs/#/Beacon/getBlobSidecars
pub struct BlobData {
pub index: String,
pub blob: String,
pub kzg_commitment: String,
pub kzg_proof: String,
pub kzg_commitment_inclusion_proof: Vec<String>,
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
// https://ethereum.github.io/beacon-APIs/#/Beacon/getBlockHeaders
pub struct BeaconBlock {
pub root: String,
pub canonical: bool,
pub header: BeaconBlockHeader,
}
#[derive(Deserialize, Debug)]
pub struct BeaconBlockHeader {
pub message: BeaconBlockMessage,
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
pub struct BeaconBlockMessage {
pub slot: String,
pub proposer_index: String,
pub parent_root: String,
pub state_root: String,
pub body_root: String,
}
impl BeaconClient {
pub fn new(beacon_client_url: String) -> Self {
Self {
api_client: Client::new(),
beacon_client_url,
}
}
pub async fn get_block_header_from_parent_hash(
&self,
parent_block_hash: [u8; 32],
) -> Result<Option<BeaconBlock>, BeaconClientError> {
let parent_block_hash_hex = format!("0x{}", hex::encode(parent_block_hash));
let data = self
.beacon_get(&format!(
"/eth/v1/beacon/headers?parent_root={}",
parent_block_hash_hex
))
.await?;
let res =
Vec::<BeaconBlock>::deserialize(data).map_err(BeaconClientError::Deserialization)?;
let block = res
.into_iter()
.find(|block| block.header.message.parent_root == parent_block_hash_hex);
Ok(block)
}
pub async fn get_blobs_from_slot(&self, slot: u64) -> Result<Vec<BlobData>, BeaconClientError> {
let data = self
.beacon_get(&format!("/eth/v1/beacon/blob_sidecars/{}", slot))
.await?;
Vec::<BlobData>::deserialize(data).map_err(BeaconClientError::Deserialization)
}
pub async fn get_blob_by_versioned_hash(
&self,
slot: u64,
blob_versioned_hash: [u8; 32],
) -> Result<Option<BlobData>, BeaconClientError> {
let res = self.get_blobs_from_slot(slot).await?;
let blob = res.into_iter().find(|blob| {
let kzg_commitment_bytes =
hex::decode(blob.kzg_commitment.replace("0x", "")).expect("A valid commitment");
let mut hasher = Sha256::new();
hasher.update(&kzg_commitment_bytes);
let mut versioned_hash: [u8; 32] = hasher.finalize().into();
versioned_hash[0] = KZG_VERSIONED_HASH;
versioned_hash == blob_versioned_hash
});
Ok(blob)
}
async fn beacon_get(&self, path: &str) -> Result<Value, BeaconClientError> {
let url = Url::from_str(&format!("{}{}", self.beacon_client_url, path))
.map_err(BeaconClientError::Url)?;
let req = self
.api_client
.get(url)
.header("content-type", "application/json")
.header("accept", "application/json");
let res = req.send().await.map_err(BeaconClientError::ReqwestError)?;
let beacon_response = res.json().await.map_err(BeaconClientError::ReqwestError)?;
match beacon_response {
BeaconAPIResponse::Success { data } => Ok(data),
BeaconAPIResponse::Error { code, message } => {
Err(BeaconClientError::APIError { code, message })
}
}
}
}