Skip to content

Commit

Permalink
Merge remote-tracking branch 'beacon-api-client/main' into merge-beac…
Browse files Browse the repository at this point in the history
…on-api-client
  • Loading branch information
ralexstokes committed Sep 15, 2023
2 parents 742607d + ea58502 commit b45cb5c
Show file tree
Hide file tree
Showing 11 changed files with 1,667 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ To add support for a new network, you can:
1. add a new module under `ethereum_consensus::configs` using an existing network as a template
2. add the network's `genesis_time` and support for a `Clock` for that network in `ethereum_consensus::clock`
3. there are convenience methods on `ethereum_consensus::state_transition::Context` for each networkd and these should also be updated for the new network

# beacon-api-client

A client for the Ethereum beacon node APIs:

https://ethereum.github.io/beacon-APIs
13 changes: 13 additions & 0 deletions examples/post.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use beacon_api_client::mainnet::Client;
use ethereum_consensus::builder::SignedValidatorRegistration;
use url::Url;

#[tokio::main]
async fn main() {
let client = Client::new(Url::parse("http://localhost:8080").unwrap());
let data = SignedValidatorRegistration::default();
let response = client.http_post("/eth/v1/builder/validators", &data).await.unwrap();
dbg!(&response);
dbg!(&response.status());
dbg!(&response.text().await);
}
66 changes: 66 additions & 0 deletions examples/sketch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use beacon_api_client::{ApiError, ApiResult, Value, VersionedValue};
use ethereum_consensus::{bellatrix::mainnet as bellatrix, capella::mainnet as capella};
use std::collections::HashMap;

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(tag = "version", content = "data")]
#[serde(rename_all = "lowercase")]
enum BlindedBeaconBlock {
Bellatrix(bellatrix::BlindedBeaconBlock),
Capella(capella::BlindedBeaconBlock),
}

fn main() {
let block = Value { meta: HashMap::new(), data: bellatrix::BlindedBeaconBlock::default() };
let block_repr = serde_json::to_string(&block).unwrap();
println!("{block_repr}");

let version = serde_json::to_value("bellatrix").unwrap();
let block_with_version = Value {
meta: HashMap::from_iter([("version".to_string(), version)]),
data: bellatrix::BlindedBeaconBlock::default(),
};
let block_with_version_repr = serde_json::to_string(&block_with_version).unwrap();
println!("{block_with_version_repr}");

let block = BlindedBeaconBlock::Bellatrix(Default::default());
let block_with_version_repr = serde_json::to_string(&block).unwrap();
println!("{block_with_version_repr}");
let recovered_block: BlindedBeaconBlock =
serde_json::from_str(&block_with_version_repr).unwrap();
println!("{recovered_block:#?}");

let block = BlindedBeaconBlock::Capella(Default::default());
let block_with_version_repr = serde_json::to_string(&block).unwrap();
println!("{block_with_version_repr}");

let full_success_response = ApiResult::Ok(block.clone());
let str_repr = serde_json::to_string(&full_success_response).unwrap();
println!("{str_repr}");

let recovered_success: ApiResult<VersionedValue<BlindedBeaconBlock>> =
serde_json::from_str(&str_repr).unwrap();
println!("{recovered_success:#?}");

let full_success_response = ApiResult::Ok(VersionedValue {
payload: block,
meta: HashMap::from_iter([(
String::from("finalized_root"),
serde_json::Value::String("0xdeadbeefcafe".to_string()),
)]),
});
let str_repr = serde_json::to_string(&full_success_response).unwrap();
println!("{str_repr}");

let recovered_success: ApiResult<VersionedValue<BlindedBeaconBlock>> =
serde_json::from_str(&str_repr).unwrap();
println!("{recovered_success:#?}");

let full_error_response: ApiResult<Value<bellatrix::BlindedBeaconBlock>> =
ApiResult::Err(ApiError::try_from((404, "some failure")).unwrap());
let str_repr = serde_json::to_string(&full_error_response).unwrap();
println!("{str_repr}");

let recovered_error: ApiResult<String> = serde_json::from_str(&str_repr).unwrap();
println!("{recovered_error:#?}");
}
Loading

0 comments on commit b45cb5c

Please sign in to comment.