Skip to content

Commit

Permalink
feat: update beacon jsonrpc endpoints (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita authored Dec 11, 2024
1 parent 580a72b commit e18427e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
12 changes: 12 additions & 0 deletions ethportal-api/src/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use crate::{
consensus::header::BeaconBlockHeader,
light_client::store::LightClientStore,
types::{
consensus::light_client::{
finality_update::LightClientFinalityUpdate,
optimistic_update::LightClientOptimisticUpdate,
},
content_key::beacon::BeaconContentKey,
enr::Enr,
portal::{
Expand Down Expand Up @@ -60,6 +64,14 @@ pub trait BeaconNetworkApi {
#[method(name = "beaconFinalizedHeader")]
async fn finalized_header(&self) -> RpcResult<BeaconBlockHeader>;

/// Get the latest finality update
#[method(name = "beaconFinalityUpdate")]
async fn finality_update(&self) -> RpcResult<LightClientFinalityUpdate>;

/// Get the latest optimistic update
#[method(name = "beaconOptimisticUpdate")]
async fn optimistic_update(&self) -> RpcResult<LightClientOptimisticUpdate>;

/// Send a FINDNODES request for nodes that fall within the given set of distances, to the
/// designated peer and wait for a response
#[method(name = "beaconFindNodes")]
Expand Down
4 changes: 4 additions & 0 deletions ethportal-api/src/types/jsonrpc/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ pub enum BeaconEndpoint {
FinalizedHeader,
/// params: None
FinalizedStateRoot,
/// params: None
FinalityUpdate,
/// params: None
OptimisticUpdate,
/// params: node_id
GetEnr(NodeId),
/// params: None
Expand Down
16 changes: 15 additions & 1 deletion light-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::{anyhow, Result};
use ethportal_api::{consensus::header::BeaconBlockHeader, light_client::store::LightClientStore};
use ethportal_api::{
consensus::header::BeaconBlockHeader,
light_client::store::LightClientStore,
types::consensus::light_client::{
finality_update::LightClientFinalityUpdate, optimistic_update::LightClientOptimisticUpdate,
},
};
use log::{error, info, warn};
use tokio::{spawn, sync::RwLock, time::sleep};

Expand Down Expand Up @@ -459,6 +465,14 @@ impl<DB: Database, R: ConsensusRpc + 'static> Client<DB, R> {
self.node.read().await.get_finalized_header()
}

pub async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdate> {
self.node.read().await.get_optimistic_update().await
}

pub async fn get_finality_update(&self) -> Result<LightClientFinalityUpdate> {
self.node.read().await.get_finality_update().await
}

pub async fn get_light_client_store(&self) -> Result<LightClientStore> {
self.node.read().await.get_light_client_store()
}
Expand Down
18 changes: 16 additions & 2 deletions light-client/src/consensus/consensus_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use ethportal_api::{
consensus::{header::BeaconBlockHeader, signature::BlsSignature},
light_client::{
bootstrap::CurrentSyncCommitteeProofLen,
finality_update::LightClientFinalityUpdateDeneb,
optimistic_update::LightClientOptimisticUpdateDeneb,
finality_update::{LightClientFinalityUpdate, LightClientFinalityUpdateDeneb},
optimistic_update::{LightClientOptimisticUpdate, LightClientOptimisticUpdateDeneb},
store::LightClientStore,
update::{FinalizedRootProofLen, LightClientUpdateDeneb},
},
Expand Down Expand Up @@ -91,6 +91,20 @@ impl<R: ConsensusRpc> ConsensusLightClient<R> {
&self.store.finalized_header
}

pub async fn get_finality_update(&self) -> Result<LightClientFinalityUpdate> {
self.rpc
.get_finality_update()
.await
.map(|update| update.into())
}

pub async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdate> {
self.rpc
.get_optimistic_update()
.await
.map(|update| update.into())
}

pub fn get_light_client_store(&self) -> &LightClientStore {
&self.store
}
Expand Down
16 changes: 15 additions & 1 deletion light-client/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::{sync::Arc, time::Duration};

use anyhow::{Error, Result};
use ethportal_api::{consensus::header::BeaconBlockHeader, light_client::store::LightClientStore};
use ethportal_api::{
consensus::header::BeaconBlockHeader,
light_client::store::LightClientStore,
types::consensus::light_client::{
finality_update::LightClientFinalityUpdate, optimistic_update::LightClientOptimisticUpdate,
},
};

use crate::{
config::client_config::Config,
Expand Down Expand Up @@ -93,6 +99,14 @@ impl<R: ConsensusRpc> Node<R> {
self.consensus.last_checkpoint.clone()
}

pub async fn get_optimistic_update(&self) -> Result<LightClientOptimisticUpdate> {
self.consensus.get_optimistic_update().await
}

pub async fn get_finality_update(&self) -> Result<LightClientFinalityUpdate> {
self.consensus.get_finality_update().await
}

fn check_head_age(&self) -> Result<(), NodeError> {
let synced_slot = self.consensus.get_header().slot;
let expected_slot = self.consensus.expected_current_slot();
Expand Down
16 changes: 16 additions & 0 deletions rpc/src/beacon_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use ethportal_api::{
consensus::header::BeaconBlockHeader,
light_client::store::LightClientStore,
types::{
consensus::light_client::{
finality_update::LightClientFinalityUpdate,
optimistic_update::LightClientOptimisticUpdate,
},
enr::Enr,
jsonrpc::{endpoints::BeaconEndpoint, request::BeaconJsonRpcRequest},
portal::{
Expand Down Expand Up @@ -116,6 +120,18 @@ impl BeaconNetworkApiServer for BeaconNetworkApi {
Ok(proxy_to_subnet(&self.network, endpoint).await?)
}

/// Get the latest optimistic update.
async fn optimistic_update(&self) -> RpcResult<LightClientOptimisticUpdate> {
let endpoint = BeaconEndpoint::OptimisticUpdate;
Ok(proxy_to_subnet(&self.network, endpoint).await?)
}

/// Get the latest finality update.
async fn finality_update(&self) -> RpcResult<LightClientFinalityUpdate> {
let endpoint = BeaconEndpoint::FinalityUpdate;
Ok(proxy_to_subnet(&self.network, endpoint).await?)
}

/// Send FINDCONTENT message to get the content with a content key.
async fn find_content(
&self,
Expand Down
32 changes: 29 additions & 3 deletions trin-beacon/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn complete_request(network: Arc<BeaconNetwork>, request: BeaconJsonRpcReq
Some(client) => {
let header = client.get_header().await;
match header {
Ok(header) => Ok(json!((header.state_root))),
Ok(header) => Ok(json!(header.state_root)),
Err(err) => Err(err.to_string()),
}
}
Expand All @@ -99,7 +99,7 @@ async fn complete_request(network: Arc<BeaconNetwork>, request: BeaconJsonRpcReq
Some(client) => {
let header = client.get_finalized_header().await;
match header {
Ok(header) => Ok(json!((header.state_root))),
Ok(header) => Ok(json!(header.state_root)),
Err(err) => Err(err.to_string()),
}
}
Expand All @@ -112,7 +112,33 @@ async fn complete_request(network: Arc<BeaconNetwork>, request: BeaconJsonRpcReq
Some(client) => {
let header = client.get_finalized_header().await;
match header {
Ok(header) => Ok(json!((header))),
Ok(header) => Ok(json!(header)),
Err(err) => Err(err.to_string()),
}
}
None => Err("Beacon client not initialized".to_string()),
}
}
BeaconEndpoint::FinalityUpdate => {
let beacon_client = network.beacon_client.lock().await;
match beacon_client.as_ref() {
Some(client) => {
let update = client.get_finality_update().await;
match update {
Ok(update) => Ok(json!(update)),
Err(err) => Err(err.to_string()),
}
}
None => Err("Beacon client not initialized".to_string()),
}
}
BeaconEndpoint::OptimisticUpdate => {
let beacon_client = network.beacon_client.lock().await;
match beacon_client.as_ref() {
Some(client) => {
let update = client.get_optimistic_update().await;
match update {
Ok(update) => Ok(json!(update)),
Err(err) => Err(err.to_string()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion trin-utils/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn setup_data_dir(
/// - Unix-like: `$HOME/.local/share/{app_name}`
///
/// It returns `None` if no valid home directory path could be retrieved from the operating system.
pub fn get_default_data_dir_path(app_name: &str) -> Option<PathBuf> {
fn get_default_data_dir_path(app_name: &str) -> Option<PathBuf> {
ProjectDirs::from("", "", app_name).map(|proj_dirs| proj_dirs.data_local_dir().to_path_buf())
}

Expand Down

0 comments on commit e18427e

Please sign in to comment.