Skip to content

Commit

Permalink
[PoS] - Add a legacy builder API endpoint that includes num_nodes (#…
Browse files Browse the repository at this point in the history
…3834)

* Add num_nodes api

* Adjust type and comments

* Replace with usize

* Fix version

* Fix route

* Restore Cargo.lock

* Fix fmt
  • Loading branch information
shenkeyao authored Nov 4, 2024
1 parent 54c5d2e commit 77fab98
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
13 changes: 13 additions & 0 deletions crates/builder-api/api/v0_1/builder.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ Get the specified block candidate.
Returns application-specific encoded transactions type
"""

[route.claim_block_with_num_nodes]
PATH = ["claimblockwithnumnodes/:block_hash/:view_number/:sender/:signature/:num_nodes"]
":block_hash" = "TaggedBase64"
":view_number" = "Integer"
":sender" = "TaggedBase64"
":signature" = "TaggedBase64"
":num_nodes" = "Integer"
DOC = """
Get the specified block candidate and provide the number of nodes.
Returns application-specific encoded transactions type
"""

[route.claim_header_input]
PATH = ["claimheaderinput/:block_hash/:view_number/:sender/:signature"]
":block_hash" = "TaggedBase64"
Expand Down
23 changes: 23 additions & 0 deletions crates/builder-api/src/v0_1/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ where
}
.boxed()
})?
.get("claim_block_with_num_nodes", |req, state| {
async move {
let block_hash: BuilderCommitment = req.blob_param("block_hash")?;
let view_number = req.integer_param("view_number")?;
let signature = try_extract_param(&req, "signature")?;
let sender = try_extract_param(&req, "sender")?;
let num_nodes = req.integer_param("num_nodes")?;
state
.claim_block_with_num_nodes(
&block_hash,
view_number,
sender,
&signature,
num_nodes,
)
.await
.map_err(|source| Error::BlockClaim {
source,
resource: block_hash.to_string(),
})
}
.boxed()
})?
.get("claim_header_input", |req, state| {
async move {
let block_hash: BuilderCommitment = req.blob_param("block_hash")?;
Expand Down
13 changes: 12 additions & 1 deletion crates/builder-api/src/v0_1/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait BuilderDataSource<TYPES: NodeType> {
signature: &<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType,
) -> Result<Vec<AvailableBlockInfo<TYPES>>, BuildError>;

/// to claim a block from the list of provided available blocks
/// To claim a block from the list of provided available blocks
async fn claim_block(
&self,
block_hash: &BuilderCommitment,
Expand All @@ -37,6 +37,17 @@ pub trait BuilderDataSource<TYPES: NodeType> {
signature: &<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType,
) -> Result<AvailableBlockData<TYPES>, BuildError>;

/// To claim a block from the list of provided available blocks and provide the number of nodes
/// information to the builder for VID computation.
async fn claim_block_with_num_nodes(
&self,
block_hash: &BuilderCommitment,
view_number: u64,
sender: TYPES::SignatureKey,
signature: &<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType,
num_nodes: usize,
) -> Result<AvailableBlockData<TYPES>, BuildError>;

/// To claim a block header input
async fn claim_block_header_input(
&self,
Expand Down
3 changes: 2 additions & 1 deletion crates/libp2p-networking/src/network/cbor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{collections::TryReserveError, convert::Infallible, io, marker::PhantomData};

use async_trait::async_trait;
use cbor4ii::core::error::DecodeError;
use futures::prelude::*;
Expand All @@ -6,7 +8,6 @@ use libp2p::{
StreamProtocol,
};
use serde::{de::DeserializeOwned, Serialize};
use std::{collections::TryReserveError, convert::Infallible, io, marker::PhantomData};

/// `Behaviour` type alias for the `Cbor` codec
pub type Behaviour<Req, Resp> = request_response::Behaviour<Cbor<Req, Resp>>;
Expand Down
24 changes: 24 additions & 0 deletions crates/task-impls/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ pub mod v0_1 {
.await
.map_err(Into::into)
}

/// Claim block and provide the number of nodes information to the builder for VID
/// computation.
///
/// # Errors
/// - [`BuilderClientError::BlockNotFound`] if block isn't available
/// - [`BuilderClientError::Api`] if API isn't responding or responds incorrectly
pub async fn claim_block_with_num_nodes(
&self,
block_hash: BuilderCommitment,
view_number: u64,
sender: TYPES::SignatureKey,
signature: &<<TYPES as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType,
num_nodes: usize,
) -> Result<AvailableBlockData<TYPES>, BuilderClientError> {
let encoded_signature: TaggedBase64 = signature.clone().into();
self.client
.get(&format!(
"{LEGACY_BUILDER_MODULE}/claimblockwithnumnodes/{block_hash}/{view_number}/{sender}/{encoded_signature}/{num_nodes}"
))
.send()
.await
.map_err(Into::into)
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions crates/testing/src/block_builder/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ impl<TYPES: NodeType> BuilderDataSource<TYPES> for RandomBuilderSource<TYPES> {
Ok(payload)
}

async fn claim_block_with_num_nodes(
&self,
block_hash: &BuilderCommitment,
view_number: u64,
sender: TYPES::SignatureKey,
signature: &<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType,
_num_nodes: usize,
) -> Result<AvailableBlockData<TYPES>, BuildError> {
self.claim_block(block_hash, view_number, sender, signature)
.await
}

async fn claim_block_header_input(
&self,
block_hash: &BuilderCommitment,
Expand Down
12 changes: 12 additions & 0 deletions crates/testing/src/block_builder/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ where
Ok(payload)
}

async fn claim_block_with_num_nodes(
&self,
block_hash: &BuilderCommitment,
view_number: u64,
sender: TYPES::SignatureKey,
signature: &<TYPES::SignatureKey as SignatureKey>::PureAssembledSignatureType,
_num_nodes: usize,
) -> Result<AvailableBlockData<TYPES>, BuildError> {
self.claim_block(block_hash, view_number, sender, signature)
.await
}

async fn claim_block_header_input(
&self,
block_hash: &BuilderCommitment,
Expand Down

0 comments on commit 77fab98

Please sign in to comment.