Skip to content

Commit

Permalink
Implement highest processed block count logic based on gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
samdealy committed Oct 17, 2022
1 parent b644a93 commit 11b965a
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions fog/view/enclave/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ use mc_oblivious_traits::ORAMStorageCreator;
use mc_sgx_compat::sync::Mutex;
use mc_sgx_report_cache_api::{ReportableEnclave, Result as ReportableEnclaveResult};

/// Helper struct that contains the decrypted `QueryResponse` and the
/// `BlockRange` the shard is responsible for.
#[derive(Clone)]
struct DecryptedMultiViewStoreQueryResponse {
/// Decrypted `QueryResponse`
pub query_response: QueryResponse,
/// The `BlockRange` that the shard is meant to process.
pub block_range: BlockRange,
}

pub struct ViewEnclave<OSC>
where
OSC: ORAMStorageCreator<StorageDataSize, StorageMetaSize>,
Expand Down Expand Up @@ -316,34 +326,32 @@ where
.map(|multi_view_store_query_response| {
let plaintext_bytes = self.ake.backend_decrypt(
&multi_view_store_query_response.store_responder_id,
&multi_view_store_query_response
.encrypted_query_response
.into(),
&multi_view_store_query_response.encrypted_query_response,
)?;
let query_response: QueryResponse = mc_util_serial::decode(&plaintext_bytes)?;

Ok((query_response, multi_view_store_query_response.block_range))
Ok(DecryptedMultiViewStoreQueryResponse {
query_response,
block_range: multi_view_store_query_response.block_range,
})
})
.collect::<Result<Vec<(QueryResponse, BlockRange)>>>()?;
.collect::<Result<Vec<DecryptedMultiViewStoreQueryResponse>>>()?;

shard_query_response.tx_out_search_results = self.get_collated_tx_out_search_results(
client_query_request,
shard_query_responses.clone(),
)?;
shard_query_response.tx_out_search_results =
Self::get_collated_tx_out_search_results(client_query_request, &shard_query_responses)?;
shard_query_response.highest_processed_block_count =
self.get_minimum_highest_processed_block_count(shard_query_responses);
Self::get_minimum_highest_processed_block_count(&shard_query_responses);

Ok(shard_query_response)
}

fn get_collated_tx_out_search_results(
&self,
client_query_request: QueryRequest,
shard_query_responses: Vec<(QueryResponse, BlockRange)>,
shard_query_responses: &[DecryptedMultiViewStoreQueryResponse],
) -> Result<Vec<TxOutSearchResult>> {
let plaintext_search_results = shard_query_responses
.into_iter()
.flat_map(|response| response.0.tx_out_search_results)
.iter()
.flat_map(|response| response.query_response.tx_out_search_results.clone())
.collect::<Vec<TxOutSearchResult>>();

oblivious_utils::collate_shard_tx_out_search_results(
Expand All @@ -352,10 +360,31 @@ where
)
}

// Takes each MultiViewStoreResponseQuery
fn get_minimum_highest_processed_block_count(
&self,
_shard_query_responses: Vec<(QueryResponse, BlockRange)>,
responses: &[DecryptedMultiViewStoreQueryResponse],
) -> u64 {
todo!()
let responses_with_gaps = responses
.iter()
.cloned()
.filter(|response| {
response.query_response.highest_processed_block_count
!= response.block_range.end_block
})
.collect::<Vec<DecryptedMultiViewStoreQueryResponse>>();

let highest_processed_block_count = if !responses_with_gaps.is_empty() {
responses_with_gaps
.iter()
.min_by_key(|response| response.query_response.highest_processed_block_count)
} else {
responses
.iter()
.max_by_key(|response| response.query_response.highest_processed_block_count)
};

highest_processed_block_count.map_or(u64::MIN, |response| {
response.query_response.highest_processed_block_count
})
}
}

0 comments on commit 11b965a

Please sign in to comment.