Skip to content

Commit

Permalink
Merge pull request #4 from garbageslam/block-version-aware-with-oncecell
Browse files Browse the repository at this point in the history
add once-cell for config in block-version-aware PR
  • Loading branch information
Chris Beck authored Feb 19, 2022
2 parents aed2e61 + 0af895f commit 191020e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion consensus/enclave/api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ pub enum Error {
FeeMap(FeeMapError),

/// Enclave not initialized
NotInited,
NotInitialized,

/// Block Version Error: {0}
BlockVersion(String),
}

impl From<MessageCipherError> for Error {
Expand Down
1 change: 1 addition & 0 deletions consensus/enclave/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mc-util-from-random = { path = "../../../util/from-random" }
mc-util-serial = { path = "../../../util/serial" }

mbedtls = { version = "0.8.1", default-features = false, features = ["no_std_deps"] }
once_cell = { version = "1.9", default-features = false, features = ["alloc", "race"] }
prost = { version = "0.9", default-features = false, features = ["prost-derive"] }
rand_core = { version = "0.6", default-features = false }

Expand Down
50 changes: 26 additions & 24 deletions consensus/enclave/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod identity;
// Include autogenerated constants.rs
include!(concat!(env!("OUT_DIR"), "/constants.rs"));

use alloc::{collections::BTreeSet, format, string::String, vec::Vec};
use alloc::{boxed::Box, collections::BTreeSet, format, string::String, vec::Vec};
use core::convert::TryFrom;
use identity::Ed25519Identity;
use mc_account_keys::PublicAddress;
Expand Down Expand Up @@ -52,6 +52,9 @@ use mc_transaction_core::{
validation::TransactionValidationError,
Block, BlockContents, BlockSignature, TokenId,
};
// Race here refers to, this is thread-safe, first-one-wins behavior, without
// blocking
use once_cell::race::OnceBox;
use prost::Message;
use rand_core::{CryptoRng, RngCore};

Expand Down Expand Up @@ -114,7 +117,7 @@ pub struct SgxConsensusEnclave {
/// This is configuration data that affects whether or not a transaction
/// is valid. To ensure that it is uniform across the network, it's hash
/// gets appended to responder id.
blockchain_config: Mutex<Option<BlockchainConfigWithDigest>>,
blockchain_config: OnceBox<BlockchainConfigWithDigest>,
}

impl SgxConsensusEnclave {
Expand All @@ -126,7 +129,7 @@ impl SgxConsensusEnclave {
&mut McRng::default(),
)),
logger,
blockchain_config: Mutex::new(None),
blockchain_config: Default::default(),
}
}

Expand Down Expand Up @@ -183,7 +186,9 @@ impl ConsensusEnclave for SgxConsensusEnclave {
// Inject the fee map and block version into the peer ResponderId.
let peer_self_id = blockchain_config.responder_id(peer_self_id);

*self.blockchain_config.lock().unwrap() = Some(blockchain_config);
self.blockchain_config
.set(Box::new(blockchain_config))
.expect("enclave already initialized");

// Init AKE.
self.ake.init(peer_self_id, client_self_id.clone())?;
Expand Down Expand Up @@ -217,9 +222,8 @@ impl ConsensusEnclave for SgxConsensusEnclave {
fn get_minimum_fee(&self, token_id: &TokenId) -> Result<Option<u64>> {
Ok(self
.blockchain_config
.lock()?
.as_ref()
.ok_or(Error::NotInited)?
.get()
.ok_or(Error::NotInitialized)?
.get_config()
.fee_map
.get_fee_for_token(token_id))
Expand Down Expand Up @@ -263,9 +267,8 @@ impl ConsensusEnclave for SgxConsensusEnclave {
// Inject the blockchain config hash, passing off to the AKE
let peer_id = self
.blockchain_config
.lock()?
.as_ref()
.ok_or(Error::NotInited)?
.get()
.ok_or(Error::NotInitialized)?
.responder_id(peer_id);

Ok(self.ake.peer_init(&peer_id)?)
Expand All @@ -283,9 +286,8 @@ impl ConsensusEnclave for SgxConsensusEnclave {
// Inject the blockchain config hash before passing off to the AKE
let peer_id = self
.blockchain_config
.lock()?
.as_ref()
.ok_or(Error::NotInited)?
.get()
.ok_or(Error::NotInitialized)?
.responder_id(peer_id);

Ok(self.ake.peer_connect(&peer_id, msg)?)
Expand Down Expand Up @@ -364,10 +366,10 @@ impl ConsensusEnclave for SgxConsensusEnclave {
block_index: u64,
proofs: Vec<TxOutMembershipProof>,
) -> Result<(WellFormedEncryptedTx, WellFormedTxContext)> {
let blockchain_config = self.blockchain_config.lock()?;
let config = blockchain_config
.as_ref()
.ok_or(Error::NotInited)?
let config = self
.blockchain_config
.get()
.ok_or(Error::NotInitialized)?
.get_config();

// Enforce that all membership proofs provided by the untrusted system for
Expand Down Expand Up @@ -453,14 +455,14 @@ impl ConsensusEnclave for SgxConsensusEnclave {
encrypted_txs_with_proofs: &[(WellFormedEncryptedTx, Vec<TxOutMembershipProof>)],
root_element: &TxOutMembershipElement,
) -> Result<(Block, BlockContents, BlockSignature)> {
let blockchain_config = self.blockchain_config.lock()?;
let config = blockchain_config
.as_ref()
.expect("enclave was not initialized")
let config = self
.blockchain_config
.get()
.ok_or(Error::NotInitialized)?
.get_config();

if parent_block.version > *config.block_version {
return Err(Error::FormBlock(format!("Block version cannot decrease: parent_block.version = {}, config.block_version = {}", parent_block.version, config.block_version)));
return Err(Error::BlockVersion(format!("Block version cannot decrease: parent_block.version = {}, config.block_version = {}", parent_block.version, config.block_version)));
}

// This implicitly converts Vec<Result<(Tx Vec<TxOutMembershipProof>),_>> into
Expand Down Expand Up @@ -1534,9 +1536,9 @@ mod tests {

// Check if we get a form block error as expected
match form_block_result {
Err(Error::FormBlock(_)) => {}
Err(Error::BlockVersion(_)) => {}
_ => panic!(
"Expected a FormBlock error due to config.block_version being less than parent"
"Expected a BlockVersion error due to config.block_version being less than parent"
),
}
}
Expand Down
7 changes: 7 additions & 0 deletions consensus/enclave/trusted/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 191020e

Please sign in to comment.