From b2cc9b7f56e2c644723acf0d825e354d96798917 Mon Sep 17 00:00:00 2001 From: Himanshu Goyal Date: Wed, 7 Feb 2024 12:37:34 -0500 Subject: [PATCH] add signature in api responses --- src/builder_state.rs | 28 ++++++++++++++++++++++++---- src/service.rs | 22 ++++++++++++++++------ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/builder_state.rs b/src/builder_state.rs index b82b5518..7a541733 100644 --- a/src/builder_state.rs +++ b/src/builder_state.rs @@ -26,6 +26,7 @@ use async_std::task; use async_trait::async_trait; use async_compatibility_layer::channel:: UnboundedSender; use async_lock::RwLock; +//use sha2::digest::crypto_common::rand_core::block; use std::collections::hash_map::Entry; use std::collections::{BTreeMap, HashMap, HashSet}; @@ -82,6 +83,8 @@ pub struct ResponseMessage{ pub block_payload: TYPES::BlockPayload, pub metadata: <::BlockPayload as BlockPayload>::Metadata, pub join_handle: Arc::>, + pub signature:<::SignatureKey as SignatureKey>::PureAssembledSignatureType, + pub sender: TYPES::SignatureKey, } pub enum Status { @@ -423,9 +426,9 @@ impl BuilderProgress for BuilderState{ let encoded_txns:Vec = payload.encode().unwrap().into_iter().collect(); - let block_size = encoded_txns.len() as u64; + let block_size: u64 = encoded_txns.len() as u64; - let offered_fee = 0; + let offered_fee: u64 = 0; // get the number of quorum committee members to be used for VID calculation @@ -451,7 +454,20 @@ impl BuilderProgress for BuilderState{ //self.global_state.write().block_hash_to_block.insert(block_hash, (payload, metadata, join_handle)); //let mut global_state = self.global_state.write().unwrap(); //self.global_state.write_arc().await.block_hash_to_block.insert(block_hash.clone(), (payload, metadata, join_handle)); - return Some(ResponseMessage{block_hash: block_hash, block_size: block_size, offered_fee: offered_fee, block_payload: payload, metadata: metadata, join_handle: Arc::new(join_handle)}); + + // to sign combine the block_hash i.e builder commitment, block size and offered fee + let mut combined_bytes: Vec = Vec::new(); + combined_bytes.extend_from_slice(&block_size.to_be_bytes()); + combined_bytes.extend_from_slice(block_hash.as_ref()); + combined_bytes.extend_from_slice(&offered_fee.to_be_bytes()); + + let signature_over_block_info = ::SignatureKey::sign( + &self.builder_keys.1, combined_bytes.as_slice()) + .expect("Failed to sign tx hash"); + //let signature = self.builder_keys.0.sign(&block_hash); + return Some(ResponseMessage{block_hash: block_hash, block_size: block_size, offered_fee: offered_fee, + block_payload: payload, metadata: metadata, join_handle: Arc::new(join_handle), + signature: signature_over_block_info, sender: self.builder_keys.0.clone()}); }; None @@ -467,7 +483,11 @@ impl BuilderProgress for BuilderState{ // send the response back self.response_sender.send(response.clone()).await.unwrap(); //let inner = Arc::unwrap_or_clone(response.join_handle); - self.global_state.write_arc().await.block_hash_to_block.insert(response.block_hash, (response.block_payload, response.metadata, response.join_handle)); + // generate signature over the block payload and metadata and sent it back + + // again sign over the block hash + + self.global_state.write_arc().await.block_hash_to_block.insert(response.block_hash, (response.block_payload, response.metadata, response.join_handle, response.signature, response.sender)); } None => { println!("No response to send"); diff --git a/src/service.rs b/src/service.rs index 4e5004b8..a08a8d12 100644 --- a/src/service.rs +++ b/src/service.rs @@ -27,7 +27,7 @@ use hotshot_types::{ utils::BuilderCommitment, data::VidCommitment }; -use hs_builder_api::{data_source::BuilderDataSource, block_metadata::BlockMetadata, builder::BuildError}; +use hs_builder_api::{data_source::BuilderDataSource, block_info::{AvailableBlockData, AvailableBlockInfo}, builder::BuildError}; use futures::stream::StreamExt; use async_compatibility_layer::channel::UnboundedReceiver; @@ -52,7 +52,8 @@ pub struct Options { #[derive(Debug)] pub struct GlobalState{ //pub block_hash_to_block: HashMap, - pub block_hash_to_block: HashMap::BlockPayload as BlockPayload>::Metadata, Arc>)>, + pub block_hash_to_block: HashMap::BlockPayload as BlockPayload>::Metadata, Arc>, + <::SignatureKey as SignatureKey>::PureAssembledSignatureType,Types::SignatureKey)>, //pub vid_to_potential_builder_state: HashMap>, pub request_sender: BroadcastSender, pub response_receiver: UnboundedReceiver>, @@ -78,7 +79,7 @@ where async fn get_available_blocks( &self, for_parent: &VidCommitment, - ) -> Result>, BuildError> { + ) -> Result>, BuildError> { let req_msg = RequestMessage{ requested_vid_commitment: for_parent.clone(), @@ -88,10 +89,12 @@ where match response_received { Ok(response) =>{ - let block_metadata = BlockMetadata::{ + let block_metadata = AvailableBlockInfo::{ block_hash: response.block_hash, block_size: response.block_size, offered_fee: response.offered_fee, + signature: response.signature, + sender: response.sender, _phantom: Default::default(), }; Ok(vec![block_metadata]) @@ -106,11 +109,18 @@ where &self, block_hash: &BuilderCommitment, signature: &<::SignatureKey as SignatureKey>::PureAssembledSignatureType, - ) -> Result { + ) -> Result, BuildError> { // TODO: Verify the signature?? if let Some(block) = self.block_hash_to_block.get(block_hash){ - Ok(block.0.clone()) + //Ok(block.0.clone()) + let block_data = AvailableBlockData::{ + block_payload: block.0.clone(), + signature: block.3.clone(), + sender: block.4.clone(), + _phantom: Default::default(), + }; + Ok(block_data) } else { Err(BuildError::Error{message: "Block not found".to_string()}) }