From 77fab988f9a18defa6df3b022a9837061919c14a Mon Sep 17 00:00:00 2001 From: Keyao Shen Date: Mon, 4 Nov 2024 10:02:08 -0800 Subject: [PATCH] [PoS] - Add a legacy builder API endpoint that includes `num_nodes` (#3834) * Add num_nodes api * Adjust type and comments * Replace with usize * Fix version * Fix route * Restore Cargo.lock * Fix fmt --- crates/builder-api/api/v0_1/builder.toml | 13 +++++++++++ crates/builder-api/src/v0_1/builder.rs | 23 +++++++++++++++++++ crates/builder-api/src/v0_1/data_source.rs | 13 ++++++++++- crates/libp2p-networking/src/network/cbor.rs | 3 ++- crates/task-impls/src/builder.rs | 24 ++++++++++++++++++++ crates/testing/src/block_builder/random.rs | 12 ++++++++++ crates/testing/src/block_builder/simple.rs | 12 ++++++++++ 7 files changed, 98 insertions(+), 2 deletions(-) diff --git a/crates/builder-api/api/v0_1/builder.toml b/crates/builder-api/api/v0_1/builder.toml index f16911a948..7e7ad9d853 100644 --- a/crates/builder-api/api/v0_1/builder.toml +++ b/crates/builder-api/api/v0_1/builder.toml @@ -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" diff --git a/crates/builder-api/src/v0_1/builder.rs b/crates/builder-api/src/v0_1/builder.rs index 0e1066a8ba..ee70f6b34b 100644 --- a/crates/builder-api/src/v0_1/builder.rs +++ b/crates/builder-api/src/v0_1/builder.rs @@ -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")?; diff --git a/crates/builder-api/src/v0_1/data_source.rs b/crates/builder-api/src/v0_1/data_source.rs index c36b457623..1c7ee643bf 100644 --- a/crates/builder-api/src/v0_1/data_source.rs +++ b/crates/builder-api/src/v0_1/data_source.rs @@ -28,7 +28,7 @@ pub trait BuilderDataSource { signature: &::PureAssembledSignatureType, ) -> Result>, 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, @@ -37,6 +37,17 @@ pub trait BuilderDataSource { signature: &::PureAssembledSignatureType, ) -> Result, 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: &::PureAssembledSignatureType, + num_nodes: usize, + ) -> Result, BuildError>; + /// To claim a block header input async fn claim_block_header_input( &self, diff --git a/crates/libp2p-networking/src/network/cbor.rs b/crates/libp2p-networking/src/network/cbor.rs index a289b998b5..4a5685624b 100644 --- a/crates/libp2p-networking/src/network/cbor.rs +++ b/crates/libp2p-networking/src/network/cbor.rs @@ -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::*; @@ -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 = request_response::Behaviour>; diff --git a/crates/task-impls/src/builder.rs b/crates/task-impls/src/builder.rs index fba215217b..1762c91680 100644 --- a/crates/task-impls/src/builder.rs +++ b/crates/task-impls/src/builder.rs @@ -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: &<::SignatureKey as SignatureKey>::PureAssembledSignatureType, + num_nodes: usize, + ) -> Result, 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) + } } } diff --git a/crates/testing/src/block_builder/random.rs b/crates/testing/src/block_builder/random.rs index cea9e40328..0d6d767ff4 100644 --- a/crates/testing/src/block_builder/random.rs +++ b/crates/testing/src/block_builder/random.rs @@ -304,6 +304,18 @@ impl BuilderDataSource for RandomBuilderSource { Ok(payload) } + async fn claim_block_with_num_nodes( + &self, + block_hash: &BuilderCommitment, + view_number: u64, + sender: TYPES::SignatureKey, + signature: &::PureAssembledSignatureType, + _num_nodes: usize, + ) -> Result, BuildError> { + self.claim_block(block_hash, view_number, sender, signature) + .await + } + async fn claim_block_header_input( &self, block_hash: &BuilderCommitment, diff --git a/crates/testing/src/block_builder/simple.rs b/crates/testing/src/block_builder/simple.rs index e1207a4fc2..63a28d854c 100644 --- a/crates/testing/src/block_builder/simple.rs +++ b/crates/testing/src/block_builder/simple.rs @@ -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: &::PureAssembledSignatureType, + _num_nodes: usize, + ) -> Result, BuildError> { + self.claim_block(block_hash, view_number, sender, signature) + .await + } + async fn claim_block_header_input( &self, block_hash: &BuilderCommitment,