Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
add signature in api responses
Browse files Browse the repository at this point in the history
  • Loading branch information
move47 committed Feb 7, 2024
1 parent 6ba41d8 commit b2cc9b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
28 changes: 24 additions & 4 deletions src/builder_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -82,6 +83,8 @@ pub struct ResponseMessage<TYPES: BuilderType>{
pub block_payload: TYPES::BlockPayload,
pub metadata: <<TYPES as BuilderType>::BlockPayload as BlockPayload>::Metadata,
pub join_handle: Arc::<JoinHandle<()>>,
pub signature:<<TYPES as BuilderType>::SignatureKey as SignatureKey>::PureAssembledSignatureType,
pub sender: TYPES::SignatureKey,
}

pub enum Status {
Expand Down Expand Up @@ -423,9 +426,9 @@ impl<TYPES: BuilderType> BuilderProgress<TYPES> for BuilderState<TYPES>{
let encoded_txns:Vec<u8> = 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
Expand All @@ -451,7 +454,20 @@ impl<TYPES: BuilderType> BuilderProgress<TYPES> for BuilderState<TYPES>{
//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<u8> = 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 = <TYPES as BuilderType>::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
Expand All @@ -467,7 +483,11 @@ impl<TYPES: BuilderType> BuilderProgress<TYPES> for BuilderState<TYPES>{
// 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");
Expand Down
22 changes: 16 additions & 6 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,7 +52,8 @@ pub struct Options {
#[derive(Debug)]
pub struct GlobalState<Types: BuilderType>{
//pub block_hash_to_block: HashMap<BuilderCommitment, Types::BlockPayload>,
pub block_hash_to_block: HashMap<BuilderCommitment, (Types::BlockPayload, <<Types as BuilderType>::BlockPayload as BlockPayload>::Metadata, Arc<JoinHandle<()>>)>,
pub block_hash_to_block: HashMap<BuilderCommitment, (Types::BlockPayload, <<Types as BuilderType>::BlockPayload as BlockPayload>::Metadata, Arc<JoinHandle<()>>,
<<Types as BuilderType>::SignatureKey as SignatureKey>::PureAssembledSignatureType,Types::SignatureKey)>,
//pub vid_to_potential_builder_state: HashMap<VidCommitment, BuilderState<Types>>,
pub request_sender: BroadcastSender<RequestMessage>,
pub response_receiver: UnboundedReceiver<ResponseMessage<Types>>,
Expand All @@ -78,7 +79,7 @@ where
async fn get_available_blocks(
&self,
for_parent: &VidCommitment,
) -> Result<Vec<BlockMetadata<Types>>, BuildError> {
) -> Result<Vec<AvailableBlockInfo<Types>>, BuildError> {

let req_msg = RequestMessage{
requested_vid_commitment: for_parent.clone(),
Expand All @@ -88,10 +89,12 @@ where

match response_received {
Ok(response) =>{
let block_metadata = BlockMetadata::<Types>{
let block_metadata = AvailableBlockInfo::<Types>{
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])
Expand All @@ -106,11 +109,18 @@ where
&self,
block_hash: &BuilderCommitment,
signature: &<<Types as BuilderType>::SignatureKey as SignatureKey>::PureAssembledSignatureType,
) -> Result<Types::BlockPayload, BuildError> {
) -> Result<AvailableBlockData<Types>, 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::<Types>{
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()})
}
Expand Down

0 comments on commit b2cc9b7

Please sign in to comment.