Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(beacon-network): add json-rpc method to get the latest finalized state root #1364

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ethportal-api/src/beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub trait BeaconNetworkApi {
#[method(name = "beaconPing")]
async fn ping(&self, enr: Enr) -> RpcResult<PongInfo>;

/// Get the finalized state root of the finalized beacon header.
#[method(name = "beaconFinalizedStateRoot")]
async fn finalized_state_root(&self) -> RpcResult<B256>;

/// 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
2 changes: 2 additions & 0 deletions ethportal-api/src/types/jsonrpc/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ pub enum BeaconEndpoint {
FindContent(Enr, BeaconContentKey),
/// params: [enr, distances]
FindNodes(Enr, Vec<u16>),
/// params: None
FinalizedStateRoot,
/// params: node_id
GetEnr(NodeId),
/// params: content_key
Expand Down
4 changes: 4 additions & 0 deletions light-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,8 @@ impl<DB: Database, R: ConsensusRpc + 'static> Client<DB, R> {
pub async fn get_header(&self) -> Result<BeaconBlockHeader> {
self.node.read().await.get_header()
}

pub async fn get_finalized_header(&self) -> Result<BeaconBlockHeader> {
self.node.read().await.get_finalized_header()
}
}
4 changes: 4 additions & 0 deletions light-client/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl<R: ConsensusRpc> Node<R> {
self.config.chain.chain_id
}

pub fn get_finalized_header(&self) -> Result<BeaconBlockHeader> {
Ok(self.consensus.get_finalized_header().clone())
}

pub fn get_header(&self) -> Result<BeaconBlockHeader> {
self.check_head_age()?;
Ok(self.consensus.get_header().clone())
Expand Down
9 changes: 9 additions & 0 deletions rpc/src/beacon_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl BeaconNetworkApiServer for BeaconNetworkApi {
Ok(result)
}

/// Get the optimistic state root of the optimistic beacon header.
async fn optimistic_state_root(&self) -> RpcResult<B256> {
let endpoint = BeaconEndpoint::OptimisticStateRoot;
let result = self.proxy_query_to_beacon_subnet(endpoint).await?;
Expand All @@ -170,6 +171,14 @@ impl BeaconNetworkApiServer for BeaconNetworkApi {
Ok(result)
}

/// Get the finalized state root of the finalized beacon header.
async fn finalized_state_root(&self) -> RpcResult<B256> {
let endpoint = BeaconEndpoint::FinalizedStateRoot;
let result = self.proxy_query_to_beacon_subnet(endpoint).await?;
let result: B256 = from_value(result)?;
Ok(result)
}

/// Lookup a target content key in the network
async fn recursive_find_content(
&self,
Expand Down
13 changes: 13 additions & 0 deletions trin-beacon/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ async fn complete_request(network: Arc<BeaconNetwork>, request: BeaconJsonRpcReq
None => Err("Beacon client not initialized".to_string()),
}
}
BeaconEndpoint::FinalizedStateRoot => {
let beacon_client = network.beacon_client.lock().await;
match beacon_client.as_ref() {
Some(client) => {
let header = client.get_finalized_header().await;
match header {
Ok(header) => Ok(json!((header.state_root))),
Err(err) => Err(err.to_string()),
}
}
None => Err("Beacon client not initialized".to_string()),
}
}
};
let _ = request.resp.send(response);
}
Expand Down
Loading