-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provision light-client in mutual remote attestation #1399
Changes from all commits
e289be6
f8c2922
13125f2
522fbd3
500955b
33db450
4a2d31e
25d0759
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,7 +16,7 @@ | |||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
use crate::{ | ||||||||||||||||||||||||
error::Result, | ||||||||||||||||||||||||
error::{Error, Result}, | ||||||||||||||||||||||||
finality::{Finality, GrandpaFinality, ParachainFinality}, | ||||||||||||||||||||||||
light_client_init_params::{GrandpaParams, SimpleParams}, | ||||||||||||||||||||||||
light_validation::{check_validator_set_proof, LightValidation}, | ||||||||||||||||||||||||
|
@@ -37,6 +37,12 @@ use std::{ | |||||||||||||||||||||||
sync::Arc, | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
#[cfg(feature = "sgx")] | ||||||||||||||||||||||||
use std::sync::SgxRwLock as RwLock; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
#[cfg(feature = "std")] | ||||||||||||||||||||||||
use std::sync::RwLock; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
pub const DB_FILE: &str = "db.bin"; | ||||||||||||||||||||||||
pub const BACKUP_FILE: &str = "db.bin.backup"; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -81,9 +87,11 @@ impl<B, L> LightClientStateSeal<B, L> { | |||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
impl<B: Block, LightClientState: Decode + Encode + Debug> LightClientSealing<LightClientState> | ||||||||||||||||||||||||
impl<B: Block, LightClientState: Decode + Encode + Debug> LightClientSealing | ||||||||||||||||||||||||
for LightClientStateSeal<B, LightClientState> | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
type LightClientState = LightClientState; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
fn seal(&self, unsealed: &LightClientState) -> Result<()> { | ||||||||||||||||||||||||
trace!("Backup light client state"); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -108,6 +116,44 @@ impl<B: Block, LightClientState: Decode + Encode + Debug> LightClientSealing<Lig | |||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/// Same as [LightClientStateSeal], but it ensures that no concurrent write operations are done | ||||||||||||||||||||||||
/// accross different threads. | ||||||||||||||||||||||||
clangenb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
#[derive(Debug)] | ||||||||||||||||||||||||
pub struct LightClientStateSealSync<B, LightClientState> { | ||||||||||||||||||||||||
seal: LightClientStateSeal<B, LightClientState>, | ||||||||||||||||||||||||
_rw_lock: RwLock<()>, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Comment on lines
+121
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think a bit more before we tackle that: #1401 |
||||||||||||||||||||||||
impl<B, LightClientState> LightClientStateSealSync<B, LightClientState> { | ||||||||||||||||||||||||
pub fn new(base_path: PathBuf) -> Result<Self> { | ||||||||||||||||||||||||
Ok(Self { seal: LightClientStateSeal::new(base_path)?, _rw_lock: RwLock::new(()) }) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
impl<B: Block, LightClientState: Decode + Encode + Debug> LightClientSealing | ||||||||||||||||||||||||
for LightClientStateSealSync<B, LightClientState> | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
type LightClientState = LightClientState; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
fn seal(&self, unsealed: &LightClientState) -> Result<()> { | ||||||||||||||||||||||||
let _lock = self._rw_lock.write().map_err(|_| Error::PoisonedLock)?; | ||||||||||||||||||||||||
self.seal.seal(unsealed) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
fn unseal(&self) -> Result<LightClientState> { | ||||||||||||||||||||||||
let _lock = self._rw_lock.read().map_err(|_| Error::PoisonedLock)?; | ||||||||||||||||||||||||
self.seal.unseal() | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
fn exists(&self) -> bool { | ||||||||||||||||||||||||
self.seal.exists() | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
fn path(&self) -> &Path { | ||||||||||||||||||||||||
self.seal.path() | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// FIXME: This is a lot of duplicate code for the initialization of two | ||||||||||||||||||||||||
// different but sameish light clients. Should be tackled with #1081 | ||||||||||||||||||||||||
pub fn read_or_init_grandpa_validator<B, OCallApi, LightClientSeal>( | ||||||||||||||||||||||||
|
@@ -119,7 +165,7 @@ where | |||||||||||||||||||||||
B: Block, | ||||||||||||||||||||||||
NumberFor<B>: finality_grandpa::BlockNumberOps, | ||||||||||||||||||||||||
OCallApi: EnclaveOnChainOCallApi, | ||||||||||||||||||||||||
LightClientSeal: LightClientSealing<LightValidationState<B>>, | ||||||||||||||||||||||||
LightClientSeal: LightClientSealing<LightClientState = LightValidationState<B>>, | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
check_validator_set_proof::<B>( | ||||||||||||||||||||||||
params.genesis_header.state_root(), | ||||||||||||||||||||||||
|
@@ -168,7 +214,7 @@ where | |||||||||||||||||||||||
B: Block, | ||||||||||||||||||||||||
NumberFor<B>: finality_grandpa::BlockNumberOps, | ||||||||||||||||||||||||
OCallApi: EnclaveOnChainOCallApi, | ||||||||||||||||||||||||
LightClientSeal: LightClientSealing<LightValidationState<B>>, | ||||||||||||||||||||||||
LightClientSeal: LightClientSealing<LightClientState = LightValidationState<B>>, | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
if !seal.exists() { | ||||||||||||||||||||||||
info!("[Enclave] ChainRelay DB not found, creating new! {}", seal.path().display()); | ||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,33 +21,61 @@ use sp_runtime::{ | |
traits::{Block as BlockT, Header as HeaderT}, | ||
OpaqueExtrinsic, | ||
}; | ||
use std::{fmt, vec::Vec}; | ||
use std::{collections::VecDeque, fmt, vec::Vec}; | ||
|
||
/// Defines the amount of parentchain headers to keep. | ||
pub const PARENTCHAIN_HEADER_PRUNING: u64 = 1000; | ||
|
||
#[derive(Encode, Decode, Clone, Eq, PartialEq)] | ||
pub struct RelayState<Block: BlockT> { | ||
pub genesis_hash: Block::Hash, | ||
pub last_finalized_block_header: Block::Header, | ||
pub penultimate_finalized_block_header: Block::Header, | ||
pub current_validator_set: AuthorityList, | ||
pub current_validator_set_id: SetId, | ||
pub header_hashes: Vec<Block::Hash>, | ||
header_hashes: VecDeque<Block::Hash>, | ||
pub unjustified_headers: Vec<Block::Hash>, // Finalized headers without grandpa proof | ||
pub verify_tx_inclusion: Vec<OpaqueExtrinsic>, // Transactions sent by the relay | ||
pub scheduled_change: Option<ScheduledChangeAtBlock<Block::Header>>, // Scheduled Authorities change as indicated in the header's digest. | ||
} | ||
|
||
impl<Block: BlockT> RelayState<Block> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Todo: don't define this on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
pub fn push_header_hash(&mut self, header: Block::Hash) { | ||
self.header_hashes.push_back(header); | ||
|
||
if self.header_hashes.len() > PARENTCHAIN_HEADER_PRUNING as usize { | ||
self.header_hashes.pop_front().expect("Tested above that is not empty; qed"); | ||
} | ||
} | ||
|
||
pub fn justify_headers(&mut self) { | ||
self.header_hashes.extend(&mut self.unjustified_headers.iter()); | ||
self.unjustified_headers.clear(); | ||
|
||
while self.header_hashes.len() > PARENTCHAIN_HEADER_PRUNING as usize { | ||
self.header_hashes.pop_front().expect("Tested above that is not empty; qed"); | ||
} | ||
} | ||
|
||
pub fn header_hashes(&self) -> &VecDeque<Block::Hash> { | ||
&self.header_hashes | ||
} | ||
} | ||
|
||
#[derive(Encode, Decode, Clone, Eq, PartialEq)] | ||
pub struct ScheduledChangeAtBlock<Header: HeaderT> { | ||
pub at_block: Header::Number, | ||
pub next_authority_list: AuthorityList, | ||
} | ||
|
||
impl<Block: BlockT> RelayState<Block> { | ||
pub fn new(block_header: Block::Header, validator_set: AuthorityList) -> Self { | ||
pub fn new(genesis: Block::Header, validator_set: AuthorityList) -> Self { | ||
RelayState { | ||
header_hashes: vec![block_header.hash()], | ||
last_finalized_block_header: block_header.clone(), | ||
genesis_hash: genesis.hash(), | ||
header_hashes: vec![genesis.hash()].into(), | ||
last_finalized_block_header: genesis.clone(), | ||
// is it bad to initialize with the same? Header trait does no implement default... | ||
penultimate_finalized_block_header: block_header, | ||
penultimate_finalized_block_header: genesis, | ||
current_validator_set: validator_set, | ||
current_validator_set_id: 0, | ||
unjustified_headers: Vec::new(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,10 @@ use crate::{ | |
error::Result, | ||
initialization::{ | ||
global_components::{ | ||
EnclaveExtrinsicsFactory, EnclaveLightClientSeal, EnclaveNodeMetadataRepository, | ||
EnclaveOCallApi, EnclaveParentchainBlockImportDispatcher, EnclaveStfExecutor, | ||
EnclaveValidatorAccessor, GLOBAL_FULL_PARACHAIN_HANDLER_COMPONENT, | ||
GLOBAL_OCALL_API_COMPONENT, GLOBAL_STATE_HANDLER_COMPONENT, | ||
EnclaveExtrinsicsFactory, EnclaveNodeMetadataRepository, EnclaveOCallApi, | ||
EnclaveParentchainBlockImportDispatcher, EnclaveStfExecutor, EnclaveValidatorAccessor, | ||
GLOBAL_FULL_PARACHAIN_HANDLER_COMPONENT, GLOBAL_OCALL_API_COMPONENT, | ||
GLOBAL_STATE_HANDLER_COMPONENT, | ||
}, | ||
parentchain::common::{ | ||
create_extrinsics_factory, create_offchain_immediate_import_dispatcher, | ||
|
@@ -33,12 +33,10 @@ use crate::{ | |
use codec::Encode; | ||
use itc_parentchain::light_client::{concurrent_access::ValidatorAccess, LightClientState}; | ||
use itp_component_container::{ComponentGetter, ComponentInitializer}; | ||
use itp_settings::{ | ||
files::LIGHT_CLIENT_DB_PATH, | ||
worker_mode::{ProvideWorkerMode, WorkerMode}, | ||
}; | ||
use itp_settings::worker_mode::{ProvideWorkerMode, WorkerMode}; | ||
use std::{path::PathBuf, sync::Arc, vec::Vec}; | ||
|
||
use crate::initialization::global_components::GLOBAL_LIGHT_CLIENT_SEAL; | ||
pub use itc_parentchain::primitives::{ParachainBlock, ParachainHeader, ParachainParams}; | ||
|
||
#[derive(Clone)] | ||
|
@@ -54,7 +52,7 @@ pub struct FullParachainHandler { | |
|
||
impl FullParachainHandler { | ||
pub fn init<WorkerModeProvider: ProvideWorkerMode>( | ||
base_path: PathBuf, | ||
_base_path: PathBuf, | ||
params: ParachainParams, | ||
) -> Result<Vec<u8>> { | ||
let ocall_api = GLOBAL_OCALL_API_COMPONENT.get()?; | ||
|
@@ -63,12 +61,12 @@ impl FullParachainHandler { | |
|
||
let genesis_header = params.genesis_header.clone(); | ||
|
||
let light_client_seal = EnclaveLightClientSeal::new(base_path.join(LIGHT_CLIENT_DB_PATH))?; | ||
let light_client_seal = GLOBAL_LIGHT_CLIENT_SEAL.get()?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is now initialized at an earlier stage and made available as a global component. |
||
let validator = itc_parentchain::light_client::io::read_or_init_parachain_validator::< | ||
ParachainBlock, | ||
EnclaveOCallApi, | ||
_, | ||
>(params, ocall_api.clone(), &light_client_seal)?; | ||
>(params, ocall_api.clone(), &*light_client_seal)?; | ||
let latest_header = validator.latest_finalized_header()?; | ||
let validator_accessor = | ||
Arc::new(EnclaveValidatorAccessor::new(validator, light_client_seal)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Associated type is more flexible, as otherwise I would have to declare an extra generic param for the block when being generic of
LightClientSealing
because the generic type of the state is generic over the block, which in turn means that I would have had to introduce a phantom type for the block, not funny.