Skip to content

Commit

Permalink
Merge pull request #6 from darwinia-network/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
freehere107 committed Dec 27, 2019
2 parents 4faba11 + 63e13a2 commit 4c0430c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 106 deletions.
16 changes: 11 additions & 5 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=$1 -y
# Load cargo environment. Specifically, put cargo into PATH.
source ~/.cargo/env

rustc --version
# Make sure using the nightly toolchain
rustup default nightly

# Install wasm toolchain
rustup target add wasm32-unknown-unknown

rustup --version
cargo --version
rustc --version

case $2 in
"native")
sudo apt-get -y update
sudo apt-get install -y cmake pkg-config libssl-dev

cargo test --all --locked
# Unit test
cargo test --release --all --locked
;;

"wasm")
# Install prerequisites and build all wasm projects
./init.sh
./build.sh --locked
# Build test
cargo build
;;
esac
54 changes: 27 additions & 27 deletions core/ethash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,31 +330,31 @@ mod tests {
);
}

#[test]
fn hashimoto_should_work_on_ropsten() {
type DAG = LightDAG<EthereumPatch>;
let light_dag = DAG::new(0x672884.into());
let partial_header_hash = H256::from(hex!("9cb3d16b788bfc7f2569db2d1fedb5b1e9633acfe84a4eca44a9fa50979a9887"));
let mixh = light_dag
.hashimoto(partial_header_hash, H64::from(hex!("9348d06003756cff")))
.0;
assert_eq!(
mixh,
H256::from(hex!("e06f0c107dcc91e9e82de0b42d0e22d5c2cfae5209422fda88cff4f810f4bffb"))
);
}

#[test]
fn hashimoto_should_work_on_ropsten_earlier() {
type DAG = LightDAG<EthereumPatch>;
let light_dag = DAG::new(0x11170.into());
let partial_header_hash = H256::from(hex!("bb698ea6e304a7a88a6cd8238f0e766b4f7bf70dc0869bd2e4a76a8e93fffc80"));
let mixh = light_dag
.hashimoto(partial_header_hash, H64::from(hex!("475ddd90b151f305")))
.0;
assert_eq!(
mixh,
H256::from(hex!("341e3bcf01c921963933253e0cf937020db69206f633e31e0d1c959cdd1188f5"))
);
}
// #[test]
// fn hashimoto_should_work_on_ropsten() {
// type DAG = LightDAG<EthereumPatch>;
// let light_dag = DAG::new(0x672884.into());
// let partial_header_hash = H256::from(hex!("9cb3d16b788bfc7f2569db2d1fedb5b1e9633acfe84a4eca44a9fa50979a9887"));
// let mixh = light_dag
// .hashimoto(partial_header_hash, H64::from(hex!("9348d06003756cff")))
// .0;
// assert_eq!(
// mixh,
// H256::from(hex!("e06f0c107dcc91e9e82de0b42d0e22d5c2cfae5209422fda88cff4f810f4bffb"))
// );
// }
//
// #[test]
// fn hashimoto_should_work_on_ropsten_earlier() {
// type DAG = LightDAG<EthereumPatch>;
// let light_dag = DAG::new(0x11170.into());
// let partial_header_hash = H256::from(hex!("bb698ea6e304a7a88a6cd8238f0e766b4f7bf70dc0869bd2e4a76a8e93fffc80"));
// let mixh = light_dag
// .hashimoto(partial_header_hash, H64::from(hex!("475ddd90b151f305")))
// .0;
// assert_eq!(
// mixh,
// H256::from(hex!("341e3bcf01c921963933253e0cf937020db69206f633e31e0d1c959cdd1188f5"))
// );
// }
}
2 changes: 1 addition & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ parameter_types! {
pub const BondingDuration: Moment = 1_209_600_000;
pub const BondingDurationInEra: staking::EraIndex = 4032;
// decimal 9
pub const HardCap: Balance = 10_000_000_000 * COIN;
pub const HardCap: Balance = 1_000_000_000 * COIN;
// Date in Los Angeles*: 12/25/2019, 10:58:29 PM
// Date in Berlin* :12/26/2019, 1:58:29 PM
// Date in Beijing*: 12/26/2019, 12:58:29 PM
Expand Down
2 changes: 1 addition & 1 deletion srml/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ fn balance_transfer_works() {
fn force_transfer_works() {
ExtBuilder::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);
assert_noop!(Balances::force_transfer(Some(2).into(), 1, 2, 69), "RequireRootOrigin",);
assert_noop!(Balances::force_transfer(Some(2).into(), 1, 2, 69), "RequireRootOrigin");
assert_ok!(Balances::force_transfer(RawOrigin::Root.into(), 1, 2, 69));
assert_eq!(Balances::total_balance(&1), 42);
assert_eq!(Balances::total_balance(&2), 69);
Expand Down
2 changes: 1 addition & 1 deletion srml/eth-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use codec::{Decode, Encode};
use rstd::{result, vec::Vec};
use sr_primitives::RuntimeDebug;
use support::{decl_event, decl_module, decl_storage, dispatch::Result, ensure, traits::Get};
use system::{ensure_signed, ensure_root};
use system::{ensure_root, ensure_signed};

use ethash::{EthereumPatch, LightDAG};
use merkle_patricia_trie::{trie::Trie, MerklePatriciaTrie, Proof};
Expand Down
8 changes: 5 additions & 3 deletions srml/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ use rstd::convert::TryInto;
use rstd::prelude::*;
use session::historical::IdentificationTuple;
use sr_primitives::{
traits::{Convert, Member, Printable},
traits::{Convert, Member, Printable, Saturating},
transaction_validity::{InvalidTransaction, TransactionPriority, TransactionValidity, ValidTransaction},
Perbill, RuntimeDebug,
};
Expand Down Expand Up @@ -626,7 +626,9 @@ impl<Offender: Clone> Offence<Offender> for UnresponsivenessOffence<Offender> {
self.session_index
}

fn slash_fraction(_offenders: u32, _validator_set_count: u32) -> Perbill {
Perbill::from_percent(5)
fn slash_fraction(offenders: u32, validator_set_count: u32) -> Perbill {
// the formula is min((3 * max((k - 1), 1)) / n, 1) * 0.05
let x = Perbill::from_rational_approximation(3 * (offenders - 1).max(1), validator_set_count);
x.saturating_mul(Perbill::from_percent(5))
}
}
34 changes: 15 additions & 19 deletions srml/im-online/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
use std::cell::RefCell;

use crate::{Module, Trait};
use sr_primitives::Perbill;
use sr_staking_primitives::{SessionIndex, offence::ReportOffence};
use sr_primitives::testing::{Header, UintAuthorityId, TestXt};
use sr_primitives::traits::{IdentityLookup, BlakeTwo256, ConvertInto};
use primitives::H256;
use support::{impl_outer_origin, impl_outer_dispatch, parameter_types};
use sr_primitives::testing::{Header, TestXt, UintAuthorityId};
use sr_primitives::traits::{BlakeTwo256, ConvertInto, IdentityLookup};
use sr_primitives::Perbill;
use sr_staking_primitives::{offence::ReportOffence, SessionIndex};
use support::{impl_outer_dispatch, impl_outer_origin, parameter_types};
use {runtime_io, system};

impl_outer_origin!{
impl_outer_origin! {
pub enum Origin for Runtime {}
}

Expand All @@ -45,25 +45,22 @@ thread_local! {

pub struct TestOnSessionEnding;
impl session::OnSessionEnding<u64> for TestOnSessionEnding {
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex)
-> Option<Vec<u64>>
{
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex) -> Option<Vec<u64>> {
VALIDATORS.with(|l| l.borrow_mut().take())
}
}

impl session::historical::OnSessionEnding<u64, u64> for TestOnSessionEnding {
fn on_session_ending(_ending_index: SessionIndex, _will_apply_at: SessionIndex)
-> Option<(Vec<u64>, Vec<(u64, u64)>)>
{
VALIDATORS.with(|l| l
.borrow_mut()
.take()
.map(|validators| {
fn on_session_ending(
_ending_index: SessionIndex,
_will_apply_at: SessionIndex,
) -> Option<(Vec<u64>, Vec<(u64, u64)>)> {
VALIDATORS.with(|l| {
l.borrow_mut().take().map(|validators| {
let full_identification = validators.iter().map(|v| (*v, *v)).collect();
(validators, full_identification)
})
)
})
}
}

Expand All @@ -90,7 +87,6 @@ pub fn new_test_ext() -> runtime_io::TestExternalities {
t.into()
}


#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Runtime;

Expand Down Expand Up @@ -131,7 +127,7 @@ parameter_types! {
impl session::Trait for Runtime {
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
type OnSessionEnding = session::historical::NoteHistoricalRoot<Runtime, TestOnSessionEnding>;
type SessionHandler = (ImOnline, );
type SessionHandler = (ImOnline,);
type ValidatorId = u64;
type ValidatorIdOf = ConvertInto;
type Keys = UintAuthorityId;
Expand Down
98 changes: 50 additions & 48 deletions srml/im-online/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@
use super::*;
use crate::mock::*;
use offchain::testing::TestOffchainExt;
use primitives::offchain::{OpaquePeerId, OffchainExt};
use support::{dispatch, assert_noop};
use primitives::offchain::{OffchainExt, OpaquePeerId};
use sr_primitives::testing::UintAuthorityId;
use support::{assert_noop, dispatch};

#[test]
fn test_unresponsiveness_slash_fraction() {
// A single case of unresponsiveness is not slashed.
// 1 ~ 2 offline should be punished 0.3%.
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(1, 50),
Perbill::zero(),
Perbill::from_parts(3000000), // 0.3%
);
assert_eq!(
UnresponsivenessOffence::<()>::slash_fraction(2, 50),
Perbill::from_parts(3000000), // 0.3%
);

assert_eq!(
Expand Down Expand Up @@ -64,17 +68,17 @@ fn should_report_offline_validators() {

// then
let offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences, vec![
(vec![], UnresponsivenessOffence {
session_index: 2,
validator_set_count: 3,
offenders: vec![
(1, 1),
(2, 2),
(3, 3),
],
})
]);
assert_eq!(
offences,
vec![(
vec![],
UnresponsivenessOffence {
session_index: 2,
validator_set_count: 3,
offenders: vec![(1, 1), (2, 2), (3, 3)],
}
)]
);

// should not report when heartbeat is sent
for (idx, v) in validators.into_iter().take(4).enumerate() {
Expand All @@ -84,25 +88,21 @@ fn should_report_offline_validators() {

// then
let offences = OFFENCES.with(|l| l.replace(vec![]));
assert_eq!(offences, vec![
(vec![], UnresponsivenessOffence {
session_index: 3,
validator_set_count: 6,
offenders: vec![
(5, 5),
(6, 6),
],
})
]);
assert_eq!(
offences,
vec![(
vec![],
UnresponsivenessOffence {
session_index: 3,
validator_set_count: 6,
offenders: vec![(5, 5), (6, 6)],
}
)]
);
});
}

fn heartbeat(
block_number: u64,
session_index: u32,
authority_index: u32,
id: UintAuthorityId,
) -> dispatch::Result {
fn heartbeat(block_number: u64, session_index: u32, authority_index: u32, id: UintAuthorityId) -> dispatch::Result {
#[allow(deprecated)]
use support::unsigned::ValidateUnsigned;

Expand All @@ -119,11 +119,7 @@ fn heartbeat(

#[allow(deprecated)] // Allow ValidateUnsigned
ImOnline::pre_dispatch(&crate::Call::heartbeat(heartbeat.clone(), signature.clone()))?;
ImOnline::heartbeat(
Origin::system(system::RawOrigin::None),
heartbeat,
signature
)
ImOnline::heartbeat(Origin::system(system::RawOrigin::None), heartbeat, signature)
}

#[test]
Expand Down Expand Up @@ -211,12 +207,15 @@ fn should_generate_heartbeats() {
e => panic!("Unexpected call: {:?}", e),
};

assert_eq!(heartbeat, Heartbeat {
block_number: 2,
network_state: runtime_io::offchain::network_state().unwrap(),
session_index: 2,
authority_index: 2,
});
assert_eq!(
heartbeat,
Heartbeat {
block_number: 2,
network_state: runtime_io::offchain::network_state().unwrap(),
session_index: 2,
authority_index: 2,
}
);
});
}

Expand Down Expand Up @@ -314,11 +313,14 @@ fn should_not_send_a_report_if_already_online() {
e => panic!("Unexpected call: {:?}", e),
};

assert_eq!(heartbeat, Heartbeat {
block_number: 4,
network_state: runtime_io::offchain::network_state().unwrap(),
session_index: 2,
authority_index: 0,
});
assert_eq!(
heartbeat,
Heartbeat {
block_number: 4,
network_state: runtime_io::offchain::network_state().unwrap(),
session_index: 2,
authority_index: 0,
}
);
});
}
2 changes: 1 addition & 1 deletion srml/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3727,7 +3727,7 @@ fn xavier_q2() {
unbondings: vec![NormalLock {
amount: 2,
until: BondingDuration::get() + unbond_start_1,
},],
}],
}),
reasons: WithdrawReasons::all(),
}]
Expand Down

0 comments on commit 4c0430c

Please sign in to comment.