From 8e260c5491fa59546c49539fdc2c613d2483170e Mon Sep 17 00:00:00 2001 From: Sam Dealy Date: Thu, 14 Jul 2022 16:33:31 -0700 Subject: [PATCH] Use sharding strategy to tell view what blocks to process --- fog/view/enclave/api/src/lib.rs | 6 ++--- fog/view/enclave/impl/src/lib.rs | 46 ++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/fog/view/enclave/api/src/lib.rs b/fog/view/enclave/api/src/lib.rs index c27b9ba62e..0e839e6b83 100644 --- a/fog/view/enclave/api/src/lib.rs +++ b/fog/view/enclave/api/src/lib.rs @@ -8,7 +8,7 @@ extern crate alloc; -use alloc::{collections::BTreeMap, vec::Vec}; +use alloc::{collections::BTreeMap, string::String, vec::Vec}; use core::result::Result as StdResult; use displaydoc::Display; use mc_attest_core::{Quote, Report, SgxError, TargetInfo, VerificationReport}; @@ -235,8 +235,8 @@ pub enum Error { EnclaveNotInitialized, /// Cipher encryption failed: {0} Cipher(CipherError), - /// Fog View Shard query response collation error - QueryResponseCollation, + /// Fog View Shard query response collation error: {0} + QueryResponseCollation(String), } impl From for Error { diff --git a/fog/view/enclave/impl/src/lib.rs b/fog/view/enclave/impl/src/lib.rs index 61e6158239..e4c7b2bc16 100644 --- a/fog/view/enclave/impl/src/lib.rs +++ b/fog/view/enclave/impl/src/lib.rs @@ -9,7 +9,7 @@ extern crate alloc; mod e_tx_out_store; mod oblivious_utils; -use alloc::collections::BTreeMap; +use alloc::{collections::BTreeMap, string::ToString}; use e_tx_out_store::{ETxOutStore, StorageDataSize, StorageMetaSize}; use alloc::vec::Vec; @@ -266,8 +266,22 @@ where Error::ProstDecode })?; - shard_query_response.tx_out_search_results = - self.get_collated_tx_out_search_results(client_query_request, shard_query_responses)?; + let shard_query_responses: Vec = shard_query_responses + .into_iter() + .map(|(responder_id, enclave_message)| { + let plaintext_bytes = self.ake.backend_decrypt(responder_id, enclave_message)?; + let query_response: QueryResponse = mc_util_serial::decode(&plaintext_bytes)?; + + Ok(query_response) + }) + .collect::>>()?; + + shard_query_response.tx_out_search_results = self.get_collated_tx_out_search_results( + client_query_request, + shard_query_responses.clone(), + )?; + shard_query_response.highest_processed_block_count = + self.get_minimum_highest_processed_block_count(shard_query_responses)?; Ok(shard_query_response) } @@ -275,17 +289,12 @@ where fn get_collated_tx_out_search_results( &self, client_query_request: QueryRequest, - shard_query_responses: BTreeMap>, + shard_query_responses: Vec, ) -> Result> { let plaintext_shard_tx_out_search_results: Vec = shard_query_responses .into_iter() - .map(|(responder_id, enclave_message)| { - let plaintext_bytes = self.ake.backend_decrypt(responder_id, enclave_message)?; - let plaintext_response: QueryResponse = mc_util_serial::decode(&plaintext_bytes)?; - - Ok(plaintext_response.tx_out_search_results) - }) - .collect::>>>()? + .map(|query_response| query_response.tx_out_search_results) + .collect::>>() .iter() .flat_map(|array| array.iter()) .cloned() @@ -296,4 +305,19 @@ where plaintext_shard_tx_out_search_results, ) } + + fn get_minimum_highest_processed_block_count( + &self, + shard_query_responses: Vec, + ) -> Result { + shard_query_responses + .into_iter() + .map(|query_response| query_response.highest_processed_block_count) + .min() + .ok_or_else(|| { + Error::QueryResponseCollation( + "No minimum highest processed block count found.".to_string(), + ) + }) + } }