Skip to content

Commit 81fb6fe

Browse files
author
Felix Müller
committed
move peer_block_sync and block production suspension to 'common' crate
also rename 'attemp_block_sync' to just 'block_sync'
1 parent 341cdf6 commit 81fb6fe

File tree

11 files changed

+127
-28
lines changed

11 files changed

+127
-28
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,11 +2786,13 @@ dependencies = [
27862786
"its-consensus-aura",
27872787
"its-primitives",
27882788
"its-state",
2789+
"its-test",
27892790
"log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
27902791
"parity-scale-codec",
27912792
"sgx-externalities",
27922793
"sgx_tstd",
27932794
"sgx_types",
2795+
"sp-core",
27942796
"sp-keyring",
27952797
"sp-runtime",
27962798
"thiserror 1.0.30",

sidechain/consensus/aura/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ use sp_runtime::{
4848
use std::{string::ToString, sync::Arc, time::Duration, vec::Vec};
4949

5050
pub mod block_importer;
51-
pub mod block_production_suspension;
52-
pub mod peer_block_sync;
5351
pub mod proposer_factory;
5452
pub mod slot_proposer;
5553
mod verifier;

sidechain/consensus/aura/src/test/mocks/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
1616
*/
1717

18-
pub mod block_importer_mock;
1918
pub mod environment_mock;
2019
pub mod proposer_mock;
2120
pub mod state_mock;

sidechain/consensus/common/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ thiserror-sgx = { package = "thiserror", git = "https://github.com/mesalock-linu
4848
sp-runtime = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "master"}
4949

5050
[dev-dependencies]
51-
its-consensus-aura = { path = "../aura" }
51+
# local
5252
itp-test = { path = "../../../core-primitives/test" }
53+
its-consensus-aura = { path = "../aura" }
54+
its-test = { path = "../../test" }
55+
# substrate
56+
sp-core = { version = "4.1.0-dev", default-features = false, features = ["full_crypto"], git = "https://github.com/paritytech/substrate.git", branch = "master" }
5357
sp-keyring = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "master"}
58+
# integritee / scs
5459
sgx-externalities = { git = "https://github.com/integritee-network/sgx-runtime", branch = "master" }

sidechain/consensus/aura/src/block_production_suspension.rs renamed to sidechain/consensus/common/src/block_production_suspension.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::sync::SgxRwLock as RwLock;
2323
#[cfg(feature = "std")]
2424
use std::sync::RwLock;
2525

26-
use its_consensus_common::{Error, Result};
26+
use crate::error::{Error, Result};
2727
use log::*;
2828

2929
/// Trait to suspend the production of sidechain blocks.

sidechain/consensus/common/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! Common stuff that could be shared across multiple consensus engines
1919
2020
#![cfg_attr(not(feature = "std"), no_std)]
21+
#![cfg_attr(test, feature(assert_matches))]
2122

2223
#[cfg(all(feature = "std", feature = "sgx"))]
2324
compile_error!("feature \"std\" and feature \"sgx\" cannot be enabled at the same time");
@@ -32,10 +33,17 @@ use sp_runtime::traits::Block as ParentchainBlockTrait;
3233
use std::{time::Duration, vec::Vec};
3334

3435
mod block_import;
36+
mod block_production_suspension;
3537
mod error;
38+
mod peer_block_sync;
39+
40+
#[cfg(test)]
41+
mod test;
3642

3743
pub use block_import::*;
44+
pub use block_production_suspension::*;
3845
pub use error::*;
46+
pub use peer_block_sync::*;
3947

4048
pub trait Verifier<ParentchainBlock, SignedSidechainBlock>: Send + Sync
4149
where

sidechain/consensus/aura/src/peer_block_sync.rs renamed to sidechain/consensus/common/src/peer_block_sync.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
1616
*/
1717

18-
use crate::block_production_suspension::{IsBlockProductionSuspended, SuspendBlockProduction};
18+
use crate::{
19+
block_production_suspension::{IsBlockProductionSuspended, SuspendBlockProduction},
20+
BlockImport, Error, Result,
21+
};
1922
use core::marker::PhantomData;
20-
use its_consensus_common::{BlockImport, Error, Result};
2123
use its_primitives::{traits::SignedBlock as SignedSidechainBlockTrait, types::BlockHash};
2224
use log::*;
2325
use sp_runtime::traits::{Block as ParentchainBlockTrait, Header as ParentchainHeaderTrait};
@@ -32,7 +34,7 @@ where
3234
ParentchainHeader: ParentchainHeaderTrait,
3335
SignedSidechainBlock: SignedSidechainBlockTrait,
3436
{
35-
fn attempt_block_sync(
37+
fn sync_block(
3638
&self,
3739
sidechain_block: SignedSidechainBlock,
3840
last_imported_parentchain_header: &ParentchainHeader,
@@ -91,7 +93,7 @@ where
9193
BlockImporter: BlockImport<ParentchainBlock, SignedSidechainBlock>,
9294
BlockProductionSuspender: SuspendBlockProduction + IsBlockProductionSuspended,
9395
{
94-
fn attempt_block_sync(
96+
fn sync_block(
9597
&self,
9698
sidechain_block: SignedSidechainBlock,
9799
last_imported_parentchain_header: &ParentchainBlock::Header,
@@ -160,9 +162,7 @@ mod tests {
160162

161163
block_import_suspender.suspend().unwrap();
162164

163-
peer_syncer
164-
.attempt_block_sync(signed_sidechain_block, &parentchain_header)
165-
.unwrap();
165+
peer_syncer.sync_block(signed_sidechain_block, &parentchain_header).unwrap();
166166

167167
assert!(block_import_suspender.is_suspended().unwrap());
168168
assert!(block_importer_mock.get_imported_blocks().is_empty());
@@ -181,9 +181,7 @@ mod tests {
181181
let parentchain_header = ParentchainHeaderBuilder::default().build();
182182
let signed_sidechain_block = SidechainBlockBuilder::default().build_signed();
183183

184-
peer_syncer
185-
.attempt_block_sync(signed_sidechain_block, &parentchain_header)
186-
.unwrap();
184+
peer_syncer.sync_block(signed_sidechain_block, &parentchain_header).unwrap();
187185

188186
assert!(!block_import_suspender.is_suspended().unwrap());
189187
assert_eq!(1, block_importer_mock.get_imported_blocks().len());
@@ -204,8 +202,7 @@ mod tests {
204202
let parentchain_header = ParentchainHeaderBuilder::default().build();
205203
let signed_sidechain_block = SidechainBlockBuilder::default().build_signed();
206204

207-
let sync_result =
208-
peer_syncer.attempt_block_sync(signed_sidechain_block, &parentchain_header);
205+
let sync_result = peer_syncer.sync_block(signed_sidechain_block, &parentchain_header);
209206

210207
assert_matches!(sync_result, Err(Error::Other(_)));
211208
assert!(!block_import_suspender.is_suspended().unwrap());

sidechain/consensus/aura/src/test/mocks/block_importer_mock.rs renamed to sidechain/consensus/common/src/test/mocks/block_importer_mock.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
1616
*/
1717

18-
use crate::AuraVerifier;
18+
use crate::{test::mocks::verifier_mock::VerifierMock, BlockImport, Error, Result};
1919
use core::marker::PhantomData;
2020
use itp_sgx_crypto::aes::Aes;
2121
use itp_test::mock::onchain_mock::OnchainMock;
2222
use itp_types::H256;
23-
use its_consensus_common::{BlockImport, Error, Result};
2423
use its_primitives::traits::{ShardIdentifierFor, SignedBlock as SignedSidechainBlockTrait};
2524
use its_state::SidechainDB;
2625
use sgx_externalities::SgxExternalities;
@@ -41,13 +40,11 @@ where
4140
SignedSidechainBlock:
4241
SignedSidechainBlockTrait<Public = <sp_core::ed25519::Pair as Pair>::Public> + 'static,
4342
{
44-
#[allow(unused)]
4543
pub fn with_import_result(mut self, result: Result<()>) -> Self {
4644
self.import_result = Some(result);
4745
self
4846
}
4947

50-
#[allow(unused)]
5148
pub fn get_imported_blocks(&self) -> Vec<SignedSidechainBlock> {
5249
(*self.imported_blocks.read().unwrap()).clone()
5350
}
@@ -72,13 +69,8 @@ where
7269
SignedSidechainBlock:
7370
SignedSidechainBlockTrait<Public = <sp_core::ed25519::Pair as Pair>::Public> + 'static,
7471
{
75-
type Verifier = AuraVerifier<
76-
sp_core::ed25519::Pair,
77-
ParentchainBlock,
78-
SignedSidechainBlock,
79-
SidechainDB<SignedSidechainBlock::Block, SgxExternalities>,
80-
OnchainMock,
81-
>;
72+
type Verifier =
73+
VerifierMock<ParentchainBlock, SignedSidechainBlock, SignedSidechainBlock, OnchainMock>;
8274
type SidechainState = SidechainDB<SignedSidechainBlock::Block, SgxExternalities>;
8375
type StateCrypto = Aes;
8476
type Context = OnchainMock;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2021 Integritee AG and Supercomputing Systems AG
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
*/
17+
18+
pub mod block_importer_mock;
19+
pub mod verifier_mock;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2021 Integritee AG and Supercomputing Systems AG
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
*/
17+
18+
use crate::{Result, Verifier};
19+
use itp_types::H256;
20+
use its_primitives::traits::SignedBlock as SignedSidechainBlockTrait;
21+
use sp_core::Pair;
22+
use sp_runtime::traits::Block as ParentchainBlockTrait;
23+
use std::marker::PhantomData;
24+
25+
/// Verifier mock implementation.
26+
pub struct VerifierMock<
27+
ParentchainBlock,
28+
SignedSidechainBlock,
29+
BlockImportParameters,
30+
VerifierContext,
31+
> {
32+
_phantom: PhantomData<(
33+
ParentchainBlock,
34+
SignedSidechainBlock,
35+
BlockImportParameters,
36+
VerifierContext,
37+
)>,
38+
}
39+
40+
impl<ParentchainBlock, SignedSidechainBlock, BlockImportParameters, VerifierContext>
41+
Verifier<ParentchainBlock, SignedSidechainBlock>
42+
for VerifierMock<ParentchainBlock, SignedSidechainBlock, BlockImportParameters, VerifierContext>
43+
where
44+
ParentchainBlock: ParentchainBlockTrait<Hash = H256>,
45+
SignedSidechainBlock:
46+
SignedSidechainBlockTrait<Public = <sp_core::ed25519::Pair as Pair>::Public> + 'static,
47+
BlockImportParameters: Send + Sync,
48+
VerifierContext: Send + Sync,
49+
{
50+
type BlockImportParams = BlockImportParameters;
51+
type Context = VerifierContext;
52+
53+
fn verify(
54+
&mut self,
55+
_block: SignedSidechainBlock,
56+
_parentchain_header: &ParentchainBlock::Header,
57+
_ctx: &Self::Context,
58+
) -> Result<Self::BlockImportParams> {
59+
todo!()
60+
}
61+
}

0 commit comments

Comments
 (0)