Skip to content

Commit

Permalink
Whitelist can be turned on and off
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Dec 24, 2019
1 parent e75d14a commit d39ee69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
4 changes: 2 additions & 2 deletions node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn darwinia_genesis(
]
});

let eth_relay_whitelist: Vec<AccountId> = vec![
let eth_relay_authorities: Vec<AccountId> = vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
];
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn darwinia_genesis(
..Default::default()
}),
eth_relay: Some(EthRelayConfig {
whitelist: eth_relay_whitelist,
authorities: eth_relay_authorities,
..Default::default()
}),
eth_backing: Some(EthBackingConfig {
Expand Down
47 changes: 41 additions & 6 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 @@ -73,7 +73,8 @@ decl_storage! {
// pub HeaderForIndex get(header_for_index): map H256 => Vec<(u64, T::Hash)>;
// pub UnverifiedHeader get(unverified_header): map PrevHash => Vec<Header>;

pub Whitelist get(fn whitelist) config(): Vec<T::AccountId>;
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 @@ -100,8 +101,9 @@ decl_module! {

pub fn reset_genesis_header(origin, header: EthHeader, genesis_difficulty: u64) {
let relayer = ensure_signed(origin)?;
ensure!(Self::whitelist().contains(&relayer), "Your account is not on the whitelist!");
// 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 @@ -111,7 +113,9 @@ decl_module! {

pub fn relay_header(origin, header: EthHeader) {
let relayer = ensure_signed(origin)?;
ensure!(Self::whitelist().contains(&relayer), "Your account is not on the whitelist!");
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 @@ -124,7 +128,9 @@ decl_module! {

pub fn check_receipt(origin, proof_record: EthReceiptProof) {
let relayer = ensure_signed(origin)?;
ensure!(Self::whitelist().contains(&relayer), "Your account is not on the whitelist!");
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 @@ -138,6 +144,35 @@ 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 Self::authorities().contains(&who) {
let index = Self::authorities().iter().position(|x| *x == who).ok_or("Authority - NOT EXISTED")?;
<Authorities<T>>::mutate(|l| l.remove(index));
}

Ok(())
}

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

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

Ok(())
}
}
}

Expand Down

0 comments on commit d39ee69

Please sign in to comment.