Skip to content

Commit

Permalink
implement SBliFF (#255)
Browse files Browse the repository at this point in the history
* implement SBliFF

* adapt tests

* review remarks
  • Loading branch information
brenzi authored Mar 7, 2024
1 parent 2c5e3bc commit f5e4b05
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 151 deletions.
13 changes: 9 additions & 4 deletions sidechain/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ benchmarks! {
add_enclaves_to_registry::<T>(&accounts);

let shard: ShardIdentifier = H256::from_slice(&TEST4_SETUP.mrenclave);
let hash: H256 = [2; 32].into();
let block_number = 1;
let next_finalization_candidate_block_number = 20;
}: _(RawOrigin::Signed(accounts[0].clone()), shard, block_number, next_finalization_candidate_block_number, hash)
let ancestor = SidechainBlockConfirmation {
block_number: 2,
block_header_hash: [2; 32].into()
};
let candidate = SidechainBlockConfirmation {
block_number: 25,
block_header_hash: [25; 32].into()
};
}: _(RawOrigin::Signed(accounts[0].clone()), shard, Some(ancestor), candidate)
verify {
assert_latest_worker_update::<T>(&accounts[0], &shard)
}
Expand Down
45 changes: 29 additions & 16 deletions sidechain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
/// A proposed block is unexpected.
ReceivedUnexpectedSidechainBlock,
/// The value for the next finalization candidate is invalid.
InvalidNextFinalizationCandidateBlockNumber,
/// A proposed finalization candidate block is outdated
FinalizationCandidateIsOutdated,
/// The provided last finalized ancestor block number doesn't match.
/// This can mean a fork happened or the sender validateer is not up to date with L1
AncestorNumberMismatch,
/// The provided last finalized ancestor block hash doesn't match.
/// This can mean a fork happened or the sender validateer is not up to date with L1
AncestorHashMismatch,
/// sender hasn't provided an ancestor although an ancestor has been finalized
AncestorMissing,
}

#[pallet::storage]
Expand All @@ -87,9 +93,8 @@ pub mod pallet {
pub fn confirm_imported_sidechain_block(
origin: OriginFor<T>,
shard: ShardIdentifier,
block_number: u64,
_next_finalization_candidate_block_number: u64, //fixme: can be removed next time we introduce breaking changes
block_header_hash: H256,
latest_finalized_ancestor: Option<SidechainBlockConfirmation>,
finalization_candidate: SidechainBlockConfirmation,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let (_enclave, shard_status) =
Expand All @@ -100,25 +105,33 @@ pub mod pallet {
)?;

// TODO: Simple but robust logic for now:
// accept all blocks from first registered enclave for shard as long as blocknumber monotonically increases.
// https://github.com/integritee-network/pallets/issues/254
// accept only blocks from first validateer in shard_status
if sender != shard_status[0].signer {
log::debug!(
"Ignore block confirmation from registered enclave with index > 1: {:?}",
sender
);
return Ok(().into())
}
if let Some(ancestor) = Self::latest_sidechain_block_confirmation(shard) {

if let Some(known_ancestor) = Self::latest_sidechain_block_confirmation(shard) {
let provided_ancestor =
latest_finalized_ancestor.ok_or(Error::<T>::AncestorMissing)?;
ensure!(
finalization_candidate.block_number > known_ancestor.block_number,
<Error<T>>::FinalizationCandidateIsOutdated
);
ensure!(
known_ancestor.block_number == provided_ancestor.block_number,
<Error<T>>::AncestorNumberMismatch
);
ensure!(
ancestor.block_number < block_number,
<Error<T>>::ReceivedUnexpectedSidechainBlock
known_ancestor.block_header_hash == provided_ancestor.block_header_hash,
<Error<T>>::AncestorHashMismatch
);
}
Self::finalize_block(
shard,
SidechainBlockConfirmation { block_number, block_header_hash },
&sender,
);
Self::finalize_block(shard, finalization_candidate, &sender);
Ok(().into())
}
}
Expand Down
Loading

0 comments on commit f5e4b05

Please sign in to comment.