Skip to content

Commit 2dbe761

Browse files
authored
Change Call- and BlockConfirmation to ProposedSidechainblock and ProcessedParentchainblock (#11)
* initial commit * rename ConfirmedCalls to ExecutedCalls * [unshield-funds] rename info to be more informative * add merkle root to parentchainblock processed event * fix bencharmking * fix unit test * fix unit test * update cargo.lock * cargo.lock update * update comments * update comments * remove ipfs hash from block confirmations * fully remove ipfs hash from pallet * remove ipfs hash from storage * rename proposed sidechain block * rename processed parentchain block * update comment * remove shard from parentchain block processed * comment: change produced to proposed * make comments a bit nicer * [test] remove extra conirm from function name * [tests] rename verify .. fails to ... errs
1 parent eb54aed commit 2dbe761

File tree

5 files changed

+66
-112
lines changed

5 files changed

+66
-112
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

teerex/src/benchmarking.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,7 @@ fn add_enclaves_to_registry<T: Config>(accounts: &[T::AccountId]) {
5151
}
5252
}
5353

54-
fn assert_latest_worker_update<T: Config>(
55-
sender: &T::AccountId,
56-
shard: &ShardIdentifier,
57-
ipfs_hash: Vec<u8>,
58-
) {
59-
assert_eq!(Teerex::<T>::latest_ipfs_hash(shard), ipfs_hash);
60-
54+
fn assert_latest_worker_update<T: Config>(sender: &T::AccountId, shard: &ShardIdentifier) {
6155
assert_eq!(Teerex::<T>::worker_for_shard(shard), Teerex::<T>::enclave_index(sender));
6256
}
6357

@@ -111,34 +105,29 @@ benchmarks! {
111105
let req = Request { shard:H256::from_slice(&TEST4_SETUP.mrenclave), cyphertext: vec![1u8; 2000]};
112106
}: _(RawOrigin::Signed(accounts[0].clone()), req)
113107

114-
// Benchmark `confirm_call` with the worst possible conditions:
108+
// Benchmark `confirm_processed_parentchainblock` with the worst possible conditions:
115109
// * sender enclave is registered
116-
confirm_call {
110+
confirm_processed_parentchainblock {
117111
let accounts: Vec<T::AccountId> = generate_accounts::<T>(1);
118112
add_enclaves_to_registry::<T>(&accounts);
119113

120-
let shard: ShardIdentifier = H256::from_slice(&TEST4_SETUP.mrenclave);
121114
let block_hash: H256 = [2; 32].into();
122-
let ipfs_hash: Vec<u8> = [3; 32].to_vec();
115+
let merkle_root: H256 = [4; 32].into();
123116

124-
}: _(RawOrigin::Signed(accounts[0].clone()), shard, block_hash, ipfs_hash.clone())
125-
verify {
126-
assert_latest_worker_update::<T>(&accounts[0], &shard, ipfs_hash)
127-
}
117+
}: _(RawOrigin::Signed(accounts[0].clone()), block_hash, merkle_root)
128118

129-
// Benchmark `confirm_block` with the worst possible conditions:
119+
// Benchmark `confirm_proposed_sidechainblock` with the worst possible conditions:
130120
// * sender enclave is registered
131-
confirm_block {
121+
confirm_proposed_sidechainblock {
132122
let accounts: Vec<T::AccountId> = generate_accounts::<T>(1);
133123
add_enclaves_to_registry::<T>(&accounts);
134124

135125
let shard: ShardIdentifier = H256::from_slice(&TEST4_SETUP.mrenclave);
136126
let block_hash: H256 = [2; 32].into();
137-
let ipfs_hash: Vec<u8> = [3; 32].to_vec();
138127

139-
}: _(RawOrigin::Signed(accounts[0].clone()), shard, block_hash, ipfs_hash.clone())
128+
}: _(RawOrigin::Signed(accounts[0].clone()), shard, block_hash)
140129
verify {
141-
assert_latest_worker_update::<T>(&accounts[0], &shard, ipfs_hash)
130+
assert_latest_worker_update::<T>(&accounts[0], &shard)
142131
}
143132
}
144133

teerex/src/lib.rs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ decl_event!(
8787
{
8888
AddedEnclave(AccountId, Vec<u8>),
8989
RemovedEnclave(AccountId),
90-
UpdatedIpfsHash(ShardIdentifier, u64, Vec<u8>),
9190
Forwarded(ShardIdentifier),
9291
ShieldFunds(Vec<u8>),
9392
UnshieldedFunds(AccountId),
94-
CallConfirmed(AccountId, H256),
95-
BlockConfirmed(AccountId, H256),
93+
ProcessedParentchainBlock(AccountId, H256, H256),
94+
ProposedSidechainBlock(AccountId, H256),
9695
}
9796
);
9897

@@ -107,11 +106,9 @@ decl_storage! {
107106
pub EnclaveRegistry get(fn enclave): map hasher(blake2_128_concat) u64 => Enclave<T::AccountId, Vec<u8>>;
108107
pub EnclaveCount get(fn enclave_count): u64;
109108
pub EnclaveIndex get(fn enclave_index): map hasher(blake2_128_concat) T::AccountId => u64;
110-
pub LatestIpfsHash get(fn latest_ipfs_hash) : map hasher(blake2_128_concat) ShardIdentifier => Vec<u8>;
111109
// enclave index of the worker that recently committed an update
112110
pub WorkerForShard get(fn worker_for_shard) : map hasher(blake2_128_concat) ShardIdentifier => u64;
113-
pub ConfirmedCalls get(fn confirmed_calls): map hasher(blake2_128_concat) H256 => u64;
114-
//pub ConfirmedBlocks get(fn confirmed_blocks): map hasher(blake2_128_concat) H256 => u64;
111+
pub ExecutedCalls get(fn confirmed_calls): map hasher(blake2_128_concat) H256 => u64;
115112
pub AllowSGXDebugMode get(fn allow_sgx_debug_mode) config(allow_sgx_debug_mode): bool;
116113
}
117114
}
@@ -178,33 +175,26 @@ decl_module! {
178175
Ok(())
179176
}
180177

181-
// the integritee-service calls this function for every processed call to confirm a state update
182-
#[weight = (<T as Config>::WeightInfo::confirm_call(), DispatchClass::Normal, Pays::Yes)]
183-
pub fn confirm_call(origin, shard: ShardIdentifier, call_hash: H256, ipfs_hash: Vec<u8>) -> DispatchResult {
178+
/// The integritee worker calls this function for every processed parentchainblock to confirm a state update.
179+
#[weight = (<T as Config>::WeightInfo::confirm_processed_parentchainblock(), DispatchClass::Normal, Pays::Yes)]
180+
pub fn confirm_processed_parentchainblock(origin, block_hash: H256, trusted_calls_merkle_root: H256) -> DispatchResult {
184181
let sender = ensure_signed(origin)?;
185182
Self::is_registered_enclave(&sender)?;
186-
let sender_index = Self::enclave_index(&sender);
187-
ensure!(<EnclaveRegistry::<T>>::get(sender_index).mr_enclave.encode() == shard.encode(), <Error<T>>::WrongMrenclaveForShard);
188-
<LatestIpfsHash>::insert(shard, ipfs_hash.clone());
189-
<WorkerForShard>::insert(shard, sender_index);
190-
log::debug!("call confirmed with shard {:?}, call hash {:?}, ipfs_hash {:?}", shard, call_hash, ipfs_hash);
191-
Self::deposit_event(RawEvent::CallConfirmed(sender, call_hash));
192-
Self::deposit_event(RawEvent::UpdatedIpfsHash(shard, sender_index, ipfs_hash));
183+
log::debug!("Processed parentchain block confirmed for mrenclave {:?}, block hash {:?}", sender, block_hash);
184+
Self::deposit_event(RawEvent::ProcessedParentchainBlock(sender, block_hash, trusted_calls_merkle_root));
193185
Ok(())
194186
}
195187

196-
// the integritee-service calls this function for every processed block to confirm a state update
197-
#[weight = (<T as Config>::WeightInfo::confirm_block(), DispatchClass::Normal, Pays::Yes)]
198-
pub fn confirm_block(origin, shard: ShardIdentifier, block_hash: H256, ipfs_hash: Vec<u8>) -> DispatchResult {
188+
/// The integritee worker calls this function for every proposed sidechainblock.
189+
#[weight = (<T as Config>::WeightInfo::confirm_proposed_sidechainblock(), DispatchClass::Normal, Pays::Yes)]
190+
pub fn confirm_proposed_sidechainblock(origin, shard_id: ShardIdentifier, block_hash: H256) -> DispatchResult {
199191
let sender = ensure_signed(origin)?;
200192
Self::is_registered_enclave(&sender)?;
201193
let sender_index = Self::enclave_index(&sender);
202-
ensure!(<EnclaveRegistry::<T>>::get(sender_index).mr_enclave.encode() == shard.encode(),<Error<T>>::WrongMrenclaveForShard);
203-
<LatestIpfsHash>::insert(shard, ipfs_hash.clone());
204-
<WorkerForShard>::insert(shard, sender_index);
205-
log::debug!("block confirmed with shard {:?}, block hash {:?}, ipfs_hash {:?}", shard, block_hash, ipfs_hash);
206-
Self::deposit_event(RawEvent::BlockConfirmed(sender, block_hash));
207-
Self::deposit_event(RawEvent::UpdatedIpfsHash(shard, sender_index, ipfs_hash));
194+
ensure!(<EnclaveRegistry::<T>>::get(sender_index).mr_enclave.encode() == shard_id.encode(),<Error<T>>::WrongMrenclaveForShard);
195+
<WorkerForShard>::insert(shard_id, sender_index);
196+
log::debug!("Proposed sidechain block confirmed with shard {:?}, block hash {:?}", shard_id, block_hash);
197+
Self::deposit_event(RawEvent::ProposedSidechainBlock(sender, block_hash));
208198
Ok(())
209199
}
210200

@@ -227,16 +217,16 @@ decl_module! {
227217
let sender_index = <EnclaveIndex<T>>::get(sender);
228218
ensure!(<EnclaveRegistry::<T>>::get(sender_index).mr_enclave.encode() == bonding_account.encode(),<Error<T>>::WrongMrenclaveForBondingAccount);
229219

230-
if !<ConfirmedCalls>::contains_key(call_hash) {
231-
log::info!("First confirmation for call: {:?}", call_hash);
220+
if !<ExecutedCalls>::contains_key(call_hash) {
221+
log::info!("Executing unshielding call: {:?}", call_hash);
232222
T::Currency::transfer(&bonding_account, &public_account, amount, ExistenceRequirement::AllowDeath)?;
233-
<ConfirmedCalls>::insert(call_hash, 0);
223+
<ExecutedCalls>::insert(call_hash, 0);
234224
Self::deposit_event(RawEvent::UnshieldedFunds(public_account));
235225
} else {
236-
log::info!("Second confirmation for call: {:?}", call_hash);
226+
log::info!("Already executed unshielding call: {:?}", call_hash);
237227
}
238228

239-
<ConfirmedCalls>::mutate(call_hash, |confirmations| {*confirmations += 1 });
229+
<ExecutedCalls>::mutate(call_hash, |confirmations| {*confirmations += 1 });
240230
Ok(())
241231
}
242232
}

teerex/src/tests/tests.rs

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
//use super::*;
1919
use crate::{
20-
mock::*, ConfirmedCalls, Enclave, EnclaveRegistry, Error, RawEvent, Request, ShardIdentifier,
20+
mock::*, Enclave, EnclaveRegistry, Error, ExecutedCalls, RawEvent, Request, ShardIdentifier,
2121
};
2222
use frame_support::{assert_err, assert_ok, IterableStorageMap, StorageMap};
2323
use ias_verify::SgxBuildMode;
@@ -239,10 +239,8 @@ fn update_enclave_url_works() {
239239
fn update_ipfs_hash_works() {
240240
new_test_ext().execute_with(|| {
241241
Timestamp::set_timestamp(TEST4_TIMESTAMP);
242-
243-
let ipfs_hash = "QmYY9U7sQzBYe79tVfiMyJ4prEJoJRWCD8t85j9qjssS9y";
244-
let shard = H256::from_slice(&TEST4_MRENCLAVE);
245-
let request_hash = H256::default();
242+
let block_hash = H256::default();
243+
let merkle_root = H256::default();
246244
let signer = get_signer(TEST4_SIGNER_PUB);
247245

248246
assert_ok!(Teerex::register_enclave(
@@ -251,38 +249,27 @@ fn update_ipfs_hash_works() {
251249
URL.to_vec(),
252250
));
253251
assert_eq!(Teerex::enclave_count(), 1);
254-
assert_ok!(Teerex::confirm_call(
252+
assert_ok!(Teerex::confirm_processed_parentchainblock(
255253
Origin::signed(signer.clone()),
256-
shard.clone(),
257-
request_hash.clone(),
258-
ipfs_hash.as_bytes().to_vec()
259-
));
260-
assert_eq!(Teerex::latest_ipfs_hash(shard.clone()), ipfs_hash.as_bytes().to_vec());
261-
assert_eq!(Teerex::worker_for_shard(shard.clone()), 1u64);
262-
263-
let expected_event = Event::Teerex(RawEvent::UpdatedIpfsHash(
264-
shard.clone(),
265-
1,
266-
ipfs_hash.as_bytes().to_vec(),
254+
block_hash.clone(),
255+
merkle_root.clone(),
267256
));
268-
assert!(System::events().iter().any(|a| a.event == expected_event));
269257

270-
let expected_event = Event::Teerex(RawEvent::CallConfirmed(signer.clone(), request_hash));
258+
let expected_event =
259+
Event::Teerex(RawEvent::ProcessedParentchainBlock(signer, block_hash, merkle_root));
271260
assert!(System::events().iter().any(|a| a.event == expected_event));
272261
})
273262
}
274263

275264
#[test]
276265
fn ipfs_update_from_unregistered_enclave_fails() {
277266
new_test_ext().execute_with(|| {
278-
let ipfs_hash = "QmYY9U7sQzBYe79tVfiMyJ4prEJoJRWCD8t85j9qjssS9y";
279267
let signer = get_signer(TEST4_SIGNER_PUB);
280268
assert_err!(
281-
Teerex::confirm_call(
269+
Teerex::confirm_processed_parentchainblock(
282270
Origin::signed(signer),
283271
H256::default(),
284272
H256::default(),
285-
ipfs_hash.as_bytes().to_vec()
286273
),
287274
Error::<Test>::EnclaveIsNotRegistered
288275
);
@@ -340,7 +327,7 @@ fn unshield_is_only_executed_once_for_the_same_call_hash() {
340327
)
341328
.is_ok());
342329

343-
assert_eq!(<ConfirmedCalls>::get(call_hash), 2)
330+
assert_eq!(<ExecutedCalls>::get(call_hash), 2)
344331
})
345332
}
346333
#[test]
@@ -560,7 +547,7 @@ fn verify_unshield_funds_works() {
560547
}
561548

562549
#[test]
563-
fn verify_unshield_funds_from_not_registered_enclave_fails() {
550+
fn unshield_funds_from_not_registered_enclave_errs() {
564551
new_test_ext().execute_with(|| {
565552
Timestamp::set_timestamp(TEST4_TIMESTAMP);
566553
let signer4 = get_signer(TEST4_SIGNER_PUB);
@@ -582,7 +569,7 @@ fn verify_unshield_funds_from_not_registered_enclave_fails() {
582569
}
583570

584571
#[test]
585-
fn verify_unshield_funds_from_enclave_not_bonding_account_fails() {
572+
fn unshield_funds_from_enclave_neq_bonding_account_errs() {
586573
new_test_ext().execute_with(|| {
587574
Timestamp::set_timestamp(TEST7_TIMESTAMP);
588575
let signer4 = get_signer(TEST4_SIGNER_PUB);
@@ -632,38 +619,36 @@ fn verify_unshield_funds_from_enclave_not_bonding_account_fails() {
632619
}
633620

634621
#[test]
635-
fn verify_call_confirmation_from_shards_not_enclave_fails() {
622+
fn confirm_processed_parentchainblock_works() {
636623
new_test_ext().execute_with(|| {
637624
Timestamp::set_timestamp(TEST7_TIMESTAMP);
638-
let ipfs_hash = "QmYY9U7sQzBYe79tVfiMyJ4prEJoJRWCD8t85j9qjssS9y";
639-
let request_hash = H256::default();
625+
let block_hash = H256::default();
626+
let merkle_root = H256::default();
640627
let signer7 = get_signer(TEST7_SIGNER_PUB);
641-
let shard4 = H256::from_slice(&TEST4_MRENCLAVE);
642-
643628
//Ensure that enclave is registered
644629
assert_ok!(Teerex::register_enclave(
645630
Origin::signed(signer7.clone()),
646631
TEST7_CERT.to_vec(),
647632
URL.to_vec(),
648633
));
634+
assert_eq!(Teerex::enclave_count(), 1);
649635

650-
assert_err!(
651-
Teerex::confirm_call(
652-
Origin::signed(signer7.clone()),
653-
shard4.clone(),
654-
request_hash.clone(),
655-
ipfs_hash.as_bytes().to_vec()
656-
),
657-
Error::<Test>::WrongMrenclaveForShard
658-
);
636+
assert_ok!(Teerex::confirm_processed_parentchainblock(
637+
Origin::signed(signer7.clone()),
638+
block_hash.clone(),
639+
merkle_root.clone(),
640+
));
641+
642+
let expected_event =
643+
Event::Teerex(RawEvent::ProcessedParentchainBlock(signer7, block_hash, merkle_root));
644+
assert!(System::events().iter().any(|a| a.event == expected_event));
659645
})
660646
}
661647

662648
#[test]
663-
fn update_block_confirmation_works() {
649+
fn confirm_proposed_sidechainblock_works_for_correct_shard() {
664650
new_test_ext().execute_with(|| {
665651
Timestamp::set_timestamp(TEST7_TIMESTAMP);
666-
let ipfs_hash = "QmYY9U7sQzBYe79tVfiMyJ4prEJoJRWCD8t85j9qjssS9y";
667652
let block_hash = H256::default();
668653
let signer7 = get_signer(TEST7_SIGNER_PUB);
669654
let shard7 = H256::from_slice(&TEST7_MRENCLAVE);
@@ -676,30 +661,21 @@ fn update_block_confirmation_works() {
676661
));
677662
assert_eq!(Teerex::enclave_count(), 1);
678663

679-
assert_ok!(Teerex::confirm_block(
664+
assert_ok!(Teerex::confirm_proposed_sidechainblock(
680665
Origin::signed(signer7.clone()),
681666
shard7.clone(),
682667
block_hash.clone(),
683-
ipfs_hash.as_bytes().to_vec()
684-
));
685-
686-
let expected_event = Event::Teerex(RawEvent::UpdatedIpfsHash(
687-
shard7.clone(),
688-
1,
689-
ipfs_hash.as_bytes().to_vec(),
690668
));
691-
assert!(System::events().iter().any(|a| a.event == expected_event));
692669

693-
let expected_event = Event::Teerex(RawEvent::BlockConfirmed(signer7.clone(), block_hash));
670+
let expected_event = Event::Teerex(RawEvent::ProposedSidechainBlock(signer7, block_hash));
694671
assert!(System::events().iter().any(|a| a.event == expected_event));
695672
})
696673
}
697674

698675
#[test]
699-
fn verify_block_confirmation_from_shards_not_enclave_fails() {
676+
fn confirm_proposed_sidechainblock_from_shard_neq_mrenclave_errs() {
700677
new_test_ext().execute_with(|| {
701678
Timestamp::set_timestamp(TEST7_TIMESTAMP);
702-
let ipfs_hash = "QmYY9U7sQzBYe79tVfiMyJ4prEJoJRWCD8t85j9qjssS9y";
703679
let block_hash = H256::default();
704680
let signer7 = get_signer(TEST7_SIGNER_PUB);
705681
let shard4 = H256::from_slice(&TEST4_MRENCLAVE);
@@ -713,11 +689,10 @@ fn verify_block_confirmation_from_shards_not_enclave_fails() {
713689
assert_eq!(Teerex::enclave_count(), 1);
714690

715691
assert_err!(
716-
Teerex::confirm_block(
692+
Teerex::confirm_proposed_sidechainblock(
717693
Origin::signed(signer7.clone()),
718694
shard4.clone(),
719695
block_hash.clone(),
720-
ipfs_hash.as_bytes().to_vec()
721696
),
722697
Error::<Test>::WrongMrenclaveForShard
723698
);

teerex/src/weights.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ pub trait WeightInfo {
5353
fn register_enclave() -> Weight;
5454
fn unregister_enclave() -> Weight;
5555
fn call_worker() -> Weight;
56-
fn confirm_call() -> Weight;
57-
fn confirm_block() -> Weight;
56+
fn confirm_processed_parentchainblock() -> Weight;
57+
fn confirm_proposed_sidechainblock() -> Weight;
5858
}
5959

6060
/// Weights for pallet_teerex using the Integritee parachain node and recommended hardware.
@@ -73,12 +73,12 @@ impl<T: frame_system::Config> WeightInfo for IntegriteeWeight<T> {
7373
fn call_worker() -> Weight {
7474
(57_200_000 as Weight)
7575
}
76-
fn confirm_call() -> Weight {
76+
fn confirm_processed_parentchainblock() -> Weight {
7777
(46_900_000 as Weight)
7878
.saturating_add(T::DbWeight::get().reads(1 as Weight))
7979
.saturating_add(T::DbWeight::get().writes(2 as Weight))
8080
}
81-
fn confirm_block() -> Weight {
81+
fn confirm_proposed_sidechainblock() -> Weight {
8282
(46_200_000 as Weight)
8383
.saturating_add(T::DbWeight::get().reads(1 as Weight))
8484
.saturating_add(T::DbWeight::get().writes(2 as Weight))
@@ -100,12 +100,12 @@ impl WeightInfo for () {
100100
fn call_worker() -> Weight {
101101
(57_200_000 as Weight)
102102
}
103-
fn confirm_call() -> Weight {
103+
fn confirm_processed_parentchainblock() -> Weight {
104104
(46_900_000 as Weight)
105105
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
106106
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
107107
}
108-
fn confirm_block() -> Weight {
108+
fn confirm_proposed_sidechainblock() -> Weight {
109109
(46_200_000 as Weight)
110110
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
111111
.saturating_add(RocksDbWeight::get().writes(2 as Weight))

0 commit comments

Comments
 (0)