Skip to content

Commit

Permalink
Merge #3985
Browse files Browse the repository at this point in the history
3985: Fix secure share + sdk r=AurelienFT a=aoudiamoncef

* [ ] document all added functions
* [x] try in sandbox /simulation/labnet
* [x] unit tests on the added/changed features
  * [x] make tests compile
  * [x] make tests pass 
* [ ] add logs allowing easy debugging in case the changes caused problems
* [ ] if the API has changed, update the API specification

Co-authored-by: Moncef AOUDIA <ma@massa.net>
  • Loading branch information
bors[bot] and aoudiamoncef authored May 25, 2023
2 parents ea25d01 + 2028b81 commit ca1a98a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 67 deletions.
20 changes: 10 additions & 10 deletions massa-grpc/src/stream/send_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::server::MassaGrpc;
use futures_util::StreamExt;
use massa_models::block::{BlockDeserializer, BlockDeserializerArgs, SecureShareBlock};
use massa_models::error::ModelsError;
use massa_models::mapping_grpc::secure_share_to_vec;
use massa_models::secure_share::SecureShareDeserializer;
use massa_proto::google::rpc::Status;
use massa_proto::massa::api::v1 as grpc;
Expand Down Expand Up @@ -57,17 +58,16 @@ pub(crate) async fn send_blocks(
continue;
};

let pub_key_b = proto_block.content_creator_pub_key.as_bytes();
// Concatenate signature, public key, and data into a single byte vector
let mut blk_serialized = Vec::with_capacity(
proto_block.signature.len()
+ pub_key_b.len()
+ proto_block.serialized_data.len(),
);
let Ok(blk_serialized) = secure_share_to_vec(proto_block) else {
report_error(
req_content.id.clone(),
tx.clone(),
tonic::Code::InvalidArgument,
"failed to convert block secure share".to_owned(),
).await;
continue;
};

blk_serialized.extend_from_slice(proto_block.signature.as_bytes());
blk_serialized.extend_from_slice(pub_key_b);
blk_serialized.extend_from_slice(&proto_block.serialized_data);
// Create a block deserializer arguments
let args = BlockDeserializerArgs {
thread_count: config.thread_count,
Expand Down
14 changes: 2 additions & 12 deletions massa-grpc/src/stream/send_endorsements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::error::{match_for_io_error, GrpcError};
use crate::server::MassaGrpc;
use futures_util::StreamExt;
use massa_models::endorsement::{EndorsementDeserializer, SecureShareEndorsement};
use massa_models::mapping_grpc::secure_share_to_vec;
use massa_models::secure_share::SecureShareDeserializer;
use massa_proto::massa::api::v1 as grpc;
use massa_serialization::{DeserializeError, Deserializer};
Expand Down Expand Up @@ -74,18 +75,7 @@ pub(crate) async fn send_endorsements(
let verified_eds_res: Result<HashMap<String, SecureShareEndorsement>, GrpcError> = proto_endorsement
.into_iter()
.map(|proto_endorsement| {

let pub_key_b = proto_endorsement.content_creator_pub_key.as_bytes();
// Concatenate signature, public key, and data into a single byte vector
let mut ed_serialized = Vec::with_capacity(
proto_endorsement.signature.len()
+ pub_key_b.len()
+ proto_endorsement.serialized_data.len(),
);
ed_serialized.extend_from_slice(proto_endorsement.signature.as_bytes());
ed_serialized.extend_from_slice(pub_key_b);
ed_serialized.extend_from_slice(&proto_endorsement.serialized_data);

let ed_serialized = secure_share_to_vec(proto_endorsement)?;
let verified_op = match endorsement_deserializer.deserialize::<DeserializeError>(&ed_serialized) {
Ok(tuple) => {
// Deserialize the endorsement and verify its signature
Expand Down
13 changes: 2 additions & 11 deletions massa-grpc/src/stream/send_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::error::{match_for_io_error, GrpcError};
use crate::server::MassaGrpc;
use futures_util::StreamExt;
use massa_models::mapping_grpc::secure_share_to_vec;
use massa_models::operation::{OperationDeserializer, SecureShareOperation};
use massa_models::secure_share::SecureShareDeserializer;
use massa_proto::massa::api::v1 as grpc;
Expand Down Expand Up @@ -77,17 +78,7 @@ pub(crate) async fn send_operations(
let verified_ops_res: Result<HashMap<String, SecureShareOperation>, GrpcError> = req_content.operations
.into_iter()
.map(|proto_operation| {
let pub_key_b = proto_operation.content_creator_pub_key.as_bytes();
// Concatenate signature, public key, and data into a single byte vector
let mut op_serialized = Vec::with_capacity(
proto_operation.signature.len()
+ pub_key_b.len()
+ proto_operation.serialized_data.len(),
);
op_serialized.extend_from_slice(proto_operation.signature.as_bytes());
op_serialized.extend_from_slice(pub_key_b);
op_serialized.extend_from_slice(&proto_operation.serialized_data);

let op_serialized = secure_share_to_vec(proto_operation)?;
// Deserialize the operation and verify its signature
let verified_op_res = match operation_deserializer.deserialize::<DeserializeError>(&op_serialized) {
Ok(tuple) => {
Expand Down
17 changes: 17 additions & 0 deletions massa-models/src/mapping_grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use crate::block::{Block, FilledBlock, SecureShareBlock};
use crate::block_header::{BlockHeader, SecuredHeader};
use crate::denunciation::DenunciationIndex;
use crate::endorsement::{Endorsement, SecureShareEndorsement};
use crate::error::ModelsError;
use crate::execution::EventFilter;
use crate::operation::{Operation, OperationId, OperationType, SecureShareOperation};
use crate::output_event::{EventExecutionContext, SCOutputEvent};
use crate::slot::{IndexedSlot, Slot};
use massa_proto::massa::api::v1 as grpc;
use massa_signature::{PublicKey, Signature};

impl From<Block> for grpc::Block {
fn from(value: Block) -> Self {
Expand Down Expand Up @@ -318,3 +320,18 @@ impl From<DenunciationIndex> for grpc::DenunciationIndex {
}
}
}

/// Converts a gRPC `SecureShare` into a byte vector
pub fn secure_share_to_vec(value: grpc::SecureShare) -> Result<Vec<u8>, ModelsError> {
let pub_key = PublicKey::from_str(&value.content_creator_pub_key)?;
let pub_key_b = pub_key.to_bytes();
// Concatenate signature, public key, and data into a single byte vector
let mut serialized_content =
Vec::with_capacity(value.signature.len() + pub_key_b.len() + value.serialized_data.len());
serialized_content
.extend_from_slice(&Signature::from_str(&value.signature).map(|value| value.to_bytes())?);
serialized_content.extend_from_slice(pub_key_b);
serialized_content.extend_from_slice(&value.serialized_data);

Ok(serialized_content)
}
Loading

0 comments on commit ca1a98a

Please sign in to comment.