Skip to content

Commit

Permalink
change rewards_should_work() tests: 11 validator has 1/2 of the total…
Browse files Browse the repository at this point in the history
… rewards instead of 2/3
  • Loading branch information
readlnh committed Dec 26, 2019
2 parents 194b181 + 368a8d3 commit ebc8b61
Show file tree
Hide file tree
Showing 18 changed files with 1,664 additions and 427 deletions.
338 changes: 169 additions & 169 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ members = [
"srml/balances",
"srml/eth-relay",
"srml/eth-backing",
"srml/im-online",
"srml/kton",
"srml/staking",
"srml/support"
"srml/support",
]
2 changes: 1 addition & 1 deletion node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ contracts = { package = "srml-contracts", git = "https://github.com/darwinia-net
system = { package = "srml-system", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
transaction-payment = { package = "srml-transaction-payment", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
support = { package = "srml-support", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
im_online = { package = "srml-im-online", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }

balances = { package = "darwinia-balances", path = "../../srml/balances" }
im-online = { package = "srml-im-online", path = "../../srml/im-online" }

# node-specific dependencies
node-runtime = { path = "../runtime" }
Expand Down
10 changes: 9 additions & 1 deletion node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ pub fn darwinia_genesis(
]
});

let eth_relay_authorities: Vec<AccountId> = vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
];

const ENDOWMENT: Balance = 1_000_000 * COIN;
const STASH: Balance = 100 * COIN;

Expand Down Expand Up @@ -263,7 +268,10 @@ pub fn darwinia_genesis(
slash_reward_fraction: Perbill::from_percent(10),
..Default::default()
}),
eth_relay: Some(EthRelayConfig { ..Default::default() }),
eth_relay: Some(EthRelayConfig {
authorities: eth_relay_authorities,
..Default::default()
}),
eth_backing: Some(EthBackingConfig {
ring_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(),
kton_redeem_address: hex!["dbc888d701167cbfb86486c516aafbefc3a4de6e"].into(),
Expand Down
4 changes: 2 additions & 2 deletions node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ contracts-rpc-runtime-api = { package = "srml-contracts-rpc-runtime-api", git =
executive = { package = "srml-executive", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
finality-tracker = { package = "srml-finality-tracker", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
grandpa = { package = "srml-grandpa", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
im-online = { package = "srml-im-online", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
indices = { package = "srml-indices", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
inherents = { package = "substrate-inherents", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
offchain-primitives = { package = "substrate-offchain-primitives", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
Expand Down Expand Up @@ -53,6 +52,7 @@ version = { package = "sr-version", git = "https://github.com/darwinia-network/s
balances = { package = "darwinia-balances", path = '../../srml/balances', default-features = false }
eth-relay = { package = "darwinia-eth-relay", path = "../../srml/eth-relay", default-features = false }
eth-backing = { package = "darwinia-eth-backing", path = "../../srml/eth-backing", default-features = false }
im-online = { package = "srml-im-online", path = "../../srml/im-online", default-features = false }
kton = { package = "darwinia-kton", path = '../../srml/kton', default-features = false }
node-primitives = { path = "../primitives", default-features = false }
staking = { package = "darwinia-staking", path = "../../srml/staking", default-features = false }
Expand Down Expand Up @@ -81,7 +81,6 @@ std = [
"executive/std",
"finality-tracker/std",
"grandpa/std",
"im-online/std",
"indices/std",
"inherents/std",
"offchain-primitives/std",
Expand Down Expand Up @@ -110,6 +109,7 @@ std = [
"balances/std",
"eth-relay/std",
"eth-backing/std",
"im-online/std",
"kton/std",
"node-primitives/std",
"staking/std",
Expand Down
2 changes: 1 addition & 1 deletion node/runtime/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub mod time {
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);

// Develop
pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = MINUTES;
pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10;
// Production
// pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10 * MINUTES;
pub const EPOCH_DURATION_IN_SLOTS: u64 = {
Expand Down
2 changes: 1 addition & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ construct_runtime!(
Sudo: sudo,
Utility: utility::{Module, Call, Event},

EthRelay: eth_relay::{Module, Call, Storage, Event<T>, Config},
EthRelay: eth_relay::{Module, Call, Storage, Event<T>, Config<T>},
EthBacking: eth_backing,
}
);
Expand Down
2 changes: 1 addition & 1 deletion srml/eth-backing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ authorship = { package = "srml-authorship", git = "https://github.com/darwinia-n
session = { package = "srml-session",git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
sr-staking-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
phragmen = { package = "substrate-phragmen", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop"}
runtime_io = { package = "sr-io", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
runtime-io = { package = "sr-io", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }
rlp = { package = "rlp", git = "https://github.com/darwinia-network/parity-common.git"}


Expand Down
35 changes: 16 additions & 19 deletions srml/eth-backing/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use sr_eth_primitives::{
Bloom, EthAddress, H64, U128,
};
use std::str::FromStr;
use support::{assert_ok, assert_err};
use support::{assert_err, assert_ok};

use sr_primitives::{AccountId32, traits::Dispatchable};
use sr_primitives::{traits::Dispatchable, AccountId32};

use staking::{
RewardDestination, StakingBalances, StakingLedger, TimeDepositItem
};
use staking::{RewardDestination, StakingBalances, StakingLedger, TimeDepositItem};

use darwinia_support::StakingLock;

Expand Down Expand Up @@ -167,27 +165,27 @@ fn verify_redeem_kton() {
// totalDifficulty
assert_ok!(EthRelay::init_genesis_header(&header, 0x68e4ea361f7a78_u64));

let expect_account_id = <Test as Trait>::DetermineAccountId::account_id_for(&hex!("2a92ae5b41feba5ee68a61449c557efa9e3b894a6461c058ec2de45429adb44546")).unwrap();
let expect_account_id = <Test as Trait>::DetermineAccountId::account_id_for(&hex!("2a92ae5b41feba5ee68a61449c557efa9e3b894a6461c058ec2de45429adb44546")).unwrap();

// 0.123456789123456789 KTON
assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens"), Ok((expect_account_id.clone(), 123456789)));
assert_eq!(EthBacking::parse_token_redeem_proof(&proof_record, "KtonBurndropTokens"), Ok((expect_account_id.clone(), 123456789)));

let id1 = AccountId32::from([0; 32]);
// If expect_account_id doesn't exist, redeem should fail
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist");
// If expect_account_id doesn't exist, redeem should fail
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "beneficiary account must pre-exist");

let kton_locked_before = EthBacking::kton_locked();

let _ = KtonModule::deposit_creating(&expect_account_id, 1);
assert_ok!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()));

assert_eq!(KtonModule::free_balance(&expect_account_id), 123456789 + 1);
let _ = KtonModule::deposit_creating(&expect_account_id, 1);
assert_ok!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()));

assert_eq!(KtonModule::free_balance(&expect_account_id), 123456789 + 1);

let kton_locked_after = EthBacking::kton_locked();
assert_eq!(kton_locked_after + 123456789, kton_locked_before);

// shouldn't redeem twice
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "Kton For This Proof - ALREADY BEEN REDEEMED");
// shouldn't redeem twice
assert_err!(EthBacking::redeem_kton(Origin::signed(id1.clone()), proof_record.clone()), "Kton For This Proof - ALREADY BEEN REDEEMED");
});
}

Expand Down Expand Up @@ -248,15 +246,15 @@ fn verify_redeem_deposit() {
let id1 = AccountId32::from([0; 32]);

let controller = AccountId32::from([1; 32]);

let _ = RingModule::deposit_creating(&expect_account_id, 1);
assert_ok!(
staking::Call::<Test>::bond(
controller.clone(),
StakingBalances::Ring(1),
RewardDestination::Controller,
0).dispatch(Origin::signed(expect_account_id.clone()))
);
);
assert_ok!(EthBacking::redeem_deposit(Origin::signed(id1.clone()), proof_record.clone()));

assert_eq!(RingModule::free_balance(&expect_account_id), 1234000000000 + 1);
Expand Down Expand Up @@ -284,4 +282,3 @@ fn verify_redeem_deposit() {
fn verify_insurficient_backing_assets() {
// TODO
}

59 changes: 51 additions & 8 deletions 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;
use system::{ensure_signed, ensure_root};

use ethash::{EthereumPatch, LightDAG};
use merkle_patricia_trie::{trie::Trie, MerklePatriciaTrie, Proof};
Expand Down Expand Up @@ -72,6 +72,9 @@ decl_storage! {

// pub HeaderForIndex get(header_for_index): map H256 => Vec<(u64, T::Hash)>;
// pub UnverifiedHeader get(unverified_header): map PrevHash => Vec<Header>;

pub CheckAuthorities get(fn check_authorities) config(): bool = true;
pub Authorities get(fn authorities) config(): Vec<T::AccountId>;
}
add_extra_genesis {
config(header): Option<Vec<u8>>;
Expand All @@ -98,7 +101,9 @@ decl_module! {

pub fn reset_genesis_header(origin, header: EthHeader, genesis_difficulty: u64) {
let relayer = ensure_signed(origin)?;
// TODO: Check authority
if Self::check_authorities() {
ensure!(Self::authorities().contains(&relayer), "Your account is not on the authorities!");
}

// TODO: Just for easy testing.
Self::init_genesis_header(&header, genesis_difficulty)?;
Expand All @@ -108,6 +113,9 @@ decl_module! {

pub fn relay_header(origin, header: EthHeader) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
ensure!(Self::authorities().contains(&relayer), "Your account is not on the authorities!");
}
// 1. There must be a corresponding parent hash
// 2. Update best hash if the current block number is larger than current best block's number (Chain reorg)

Expand All @@ -120,6 +128,9 @@ decl_module! {

pub fn check_receipt(origin, proof_record: EthReceiptProof) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
ensure!(Self::authorities().contains(&relayer), "Your account is not on the authorities!");
}

let verified_receipt = Self::verify_receipt(&proof_record)?;

Expand All @@ -133,6 +144,36 @@ decl_module! {
// if header confirmed then return
// if header in unverified header then challenge
}

pub fn add_authority(origin, who: T::AccountId) -> Result {
let _me = ensure_root(origin)?;

if !Self::authorities().contains(&who) {
<Authorities<T>>::mutate(|l| l.push(who));
}

Ok(())
}

pub fn remove_authority(origin, who: T::AccountId) -> Result {
let _me = ensure_root(origin)?;

if let Some(i) = Self::authorities()
.into_iter()
.position(|who_| who_ == who) {
<Authorities<T>>::mutate(|l| l.remove(i));
}

Ok(())
}

pub fn toggle_check_authorities(origin) -> Result {
let _me = ensure_root(origin)?;

CheckAuthorities::put(!Self::check_authorities());

Ok(())
}
}
}

Expand Down Expand Up @@ -243,9 +284,15 @@ impl<T: Trait> Module<T> {
let header_hash = header.hash();
let block_number = header.number();

HeaderOf::insert(header_hash, header);
let prev_total_difficulty = Self::header_details_of(header.parent_hash())
.ok_or("Previous Header Detail - NOT EXISTED")?
.total_difficulty;
let best_header_hash = Self::best_header_hash();
// let best_header = Self::header_of(best_header_hash).ok_or("Can not find best header.");
let best_header_details =
Self::header_details_of(best_header_hash).ok_or("Best Header Detail - NOT EXISTED")?;

let prev_total_difficulty = Self::header_details_of(header.parent_hash()).unwrap().total_difficulty;
HeaderOf::insert(header_hash, header);

HeaderDetailsOf::insert(
header_hash,
Expand All @@ -256,10 +303,6 @@ impl<T: Trait> Module<T> {
},
);

let best_header_hash = Self::best_header_hash();
// let best_header = Self::header_of(best_header_hash).ok_or("Can not find best header.");
let best_header_details = Self::header_details_of(best_header_hash).unwrap();

// TODO: Check total difficulty and reorg if necessary.
if prev_total_difficulty + header.difficulty() > best_header_details.total_difficulty {
BestHeaderHash::mutate(|hash| {
Expand Down
43 changes: 43 additions & 0 deletions srml/im-online/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = "srml-im-online"

This comment has been minimized.

Copy link
@hackfisher

hackfisher Dec 26, 2019

Contributor

What is the modification in im online compared to substrate im online.

version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
# crates.io
codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.101", optional = true }

# github.com
app-crypto = { package = "substrate-application-crypto", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
authorship = { package = "srml-authorship", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
primitives = { package="substrate-primitives", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
rstd = { package = "sr-std", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
runtime-io = { package = "sr-io", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
session = { package = "srml-session", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
sr-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
sr-staking-primitives = { git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
support = { package = "srml-support", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }
system = { package = "srml-system", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop", default-features = false }

[dev-dependencies]
offchain = { package = "substrate-offchain", git = "https://github.com/darwinia-network/substrate.git", branch = "darwinia-develop" }

[features]
default = ["std", "session/historical"]
std = [
"codec/std",
"serde",

"app-crypto/std",
"authorship/std",
"primitives/std",
"rstd/std",
"runtime-io/std",
"session/std",
"sr-primitives/std",
"sr-staking-primitives/std",
"support/std",
"system/std",
]
Loading

0 comments on commit ebc8b61

Please sign in to comment.