Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I116 unapprove validators #117

Merged
merged 13 commits into from
Mar 20, 2023
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ substrate-frame-rpc-system = { version = "15.0.0", default-features = false }
pallet-aura = { version = "14.0.0", default-features = false }
pallet-balances = { version = "15.0.0", default-features = false }
pallet-grandpa = { version = "15.0.0", default-features = false }
pallet-im-online = { version = "14.0.0", default-features = false }
pallet-randomness-collective-flip = { version = "15.0.0", default-features = false }
pallet-session = { version = "15.0.0", default-features = false }
pallet-sudo = { version = "15.0.0", default-features = false }
Expand Down
25 changes: 20 additions & 5 deletions gn-client/examples/guild/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod common;
mod fund;
mod join;
mod key;
mod register;
mod sudo;
mod token;

use gn_client::tx;
Expand All @@ -20,9 +20,13 @@ enum Example {
Join,
Key(Key),
Token,
Register {
Sudo {
#[structopt(long, short)]
operator: Option<String>,
pallet: String,
#[structopt(long, short)]
method: String,
#[structopt(long, short)]
account: Option<String>,
},
}

Expand Down Expand Up @@ -79,8 +83,19 @@ async fn main() {
key::generate(&curve, password.as_deref())
}
Example::Key(Key::Rotate) => key::rotate(api).await,
Example::Register { operator } => {
register::register(api, signer, operator.as_deref()).await
Example::Sudo {
pallet,
method,
account,
} => {
sudo::sudo(
api,
signer,
&pallet.to_lowercase(),
&method.to_lowercase(),
account.as_deref(),
)
.await
}
Example::Token => token::token(api, signer).await,
}
Expand Down
21 changes: 0 additions & 21 deletions gn-client/examples/guild/register.rs

This file was deleted.

34 changes: 34 additions & 0 deletions gn-client/examples/guild/sudo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use gn_client::{
tx::{self, Signer},
AccountId, Api,
};

use std::str::FromStr;
use std::sync::Arc;

pub async fn sudo(
api: Api,
root: Arc<Signer>,
pallet: &str,
method: &str,
maybe_operator: Option<&str>,
) {
let account_id = if let Some(operator) = maybe_operator {
AccountId::from_str(operator).expect("invalid operator id string")
} else {
root.account_id().clone()
};

let payload = match (pallet, method) {
("oracle", "register") => tx::register_operator(&account_id),
("oracle", "deregister") => tx::deregister_operator(&account_id),
("validator", "add") => tx::add_validator(&account_id),
("validator", "remove") => tx::remove_validator(&account_id),
_ => unimplemented!(),
};

tx::send_tx_in_block(api, &tx::sudo(payload), root)
.await
.unwrap();
println!("{pallet} {method} {account_id} submitted successfully");
}
36 changes: 30 additions & 6 deletions gn-client/src/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ pub fn sudo<'a>(call: DynamicTxPayload<'_>) -> DynamicTxPayload<'a> {
subxt::dynamic::tx("Sudo", "sudo", vec![("call", call.into_value())])
}

pub fn fund_account(account: &AccountId, amount: u128) -> impl TxPayload {
runtime::tx()
.balances()
.transfer(MultiAddress::Id(account.clone()), amount)
}

pub fn register_operator<'a>(operator: &AccountId) -> DynamicTxPayload<'a> {
subxt::dynamic::tx(
"Oracle",
Expand All @@ -53,6 +47,36 @@ pub fn register_operator<'a>(operator: &AccountId) -> DynamicTxPayload<'a> {
)
}

pub fn deregister_operator<'a>(operator: &AccountId) -> DynamicTxPayload<'a> {
subxt::dynamic::tx(
"Oracle",
"deregister_operator",
vec![("operator", Value::from_bytes(operator))],
)
}

pub fn add_validator<'a>(validator: &AccountId) -> DynamicTxPayload<'a> {
subxt::dynamic::tx(
"ValidatorManager",
"add_validator",
vec![("validator_id", Value::from_bytes(validator))],
)
}

pub fn remove_validator<'a>(validator: &AccountId) -> DynamicTxPayload<'a> {
subxt::dynamic::tx(
"ValidatorManager",
"remove_validator",
vec![("validator_id", Value::from_bytes(validator))],
)
}

pub fn fund_account(account: &AccountId, amount: u128) -> impl TxPayload {
runtime::tx()
.balances()
.transfer(MultiAddress::Id(account.clone()), amount)
}

pub fn activate_operator() -> impl TxPayload {
runtime::tx().oracle().activate_operator()
}
Expand Down
1 change: 0 additions & 1 deletion gn-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ frame-system = { workspace = true, optional = true }
substrate-frame-rpc-system = { workspace = true }

# substrate pallets
pallet-im-online = { workspace = true }
pallet-transaction-payment = { workspace = true, optional = true }
pallet-transaction-payment-rpc = { workspace = true }

Expand Down
9 changes: 2 additions & 7 deletions gn-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use gn_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, ImOnlineConfig,
SessionConfig, Signature, SudoConfig, SystemConfig, ValidatorManagerConfig, WASM_BINARY,
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, SessionConfig, Signature,
SudoConfig, SystemConfig, ValidatorManagerConfig, WASM_BINARY,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use sc_service::ChainType;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{sr25519, Pair, Public};
Expand Down Expand Up @@ -36,7 +35,6 @@ struct AuthorityKeys {
account_id: AccountId,
aura_id: AuraId,
grandpa_id: GrandpaId,
im_online_id: ImOnlineId,
}

impl AuthorityKeys {
Expand All @@ -45,15 +43,13 @@ impl AuthorityKeys {
account_id: get_account_id_from_seed::<sr25519::Public>(seed),
aura_id: get_from_seed::<AuraId>(seed),
grandpa_id: get_from_seed::<GrandpaId>(seed),
im_online_id: get_from_seed::<ImOnlineId>(seed),
}
}

pub fn to_session_keys(&self) -> gn_runtime::opaque::SessionKeys {
gn_runtime::opaque::SessionKeys {
aura: self.aura_id.clone(),
grandpa: self.grandpa_id.clone(),
im_online: self.im_online_id.clone(),
}
}
}
Expand Down Expand Up @@ -192,7 +188,6 @@ fn testnet_genesis(
grandpa: GrandpaConfig {
authorities: vec![],
},
im_online: ImOnlineConfig { keys: vec![] },
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
Expand Down
2 changes: 2 additions & 0 deletions gn-pallets/pallet-validator-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#![deny(clippy::dbg_macro)]
#![deny(unused_crate_dependencies)]

pub mod migration;
#[cfg(test)]
mod mock;
#[cfg(test)]
Expand Down Expand Up @@ -238,6 +239,7 @@ impl<T: Config> Pallet<T> {
fn unapprove_validator(validator_id: T::AccountId) -> DispatchResult {
let mut approved_set = <ApprovedValidators<T>>::get();
approved_set.retain(|v| *v != validator_id);
<ApprovedValidators<T>>::set(approved_set);
Ok(())
}

Expand Down
15 changes: 15 additions & 0 deletions gn-pallets/pallet-validator-manager/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::*;

pub fn on_runtime_upgrade<T: Config>() {
let validators = Validators::<T>::get();
log::info!("# validators: {}", validators.len());
log::info!(
"# approved validators: {}",
ApprovedValidators::<T>::get().len()
);
ApprovedValidators::<T>::set(validators);
log::info!(
"# approved validators post-migration: {}",
ApprovedValidators::<T>::get().len()
);
}
16 changes: 11 additions & 5 deletions gn-pallets/pallet-validator-manager/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ fn simple_setup_should_work() {
authorities(),
vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]
);
assert_eq!(ValidatorSet::validators(), vec![1u64, 2u64, 3u64]);
assert_eq!(Session::validators(), vec![1, 2, 3]);
assert_eq!(ValidatorSet::validators(), &[1, 2, 3]);
assert_eq!(Session::validators(), &[1, 2, 3]);
});
}

#[test]
fn add_validator_updates_validators_list() {
new_test_ext().execute_with(|| {
assert_ok!(ValidatorSet::add_validator(RuntimeOrigin::root(), 4));
assert_eq!(ValidatorSet::validators(), vec![1u64, 2u64, 3u64, 4u64])
assert_eq!(ValidatorSet::validators(), &[1, 2, 3, 4]);
assert_eq!(ValidatorSet::approved_validators(), &[1, 2, 3, 4]);
});
}

#[test]
fn remove_validator_updates_validators_list() {
new_test_ext().execute_with(|| {
assert_ok!(ValidatorSet::remove_validator(RuntimeOrigin::root(), 2));
assert_eq!(ValidatorSet::validators(), vec![1u64, 3u64]);
assert_eq!(ValidatorSet::validators(), &[1, 3]);
assert_eq!(ValidatorSet::approved_validators(), &[1, 3]);
// add again
assert_ok!(ValidatorSet::add_validator(RuntimeOrigin::root(), 2));
assert_eq!(ValidatorSet::validators(), &[1, 3, 2]);
assert_eq!(ValidatorSet::approved_validators(), &[1, 3, 2]);
});
}

Expand Down Expand Up @@ -57,7 +63,7 @@ fn remove_validator_fails_with_invalid_origin() {
fn duplicate_check() {
new_test_ext().execute_with(|| {
assert_ok!(ValidatorSet::add_validator(RuntimeOrigin::root(), 4));
assert_eq!(ValidatorSet::validators(), vec![1u64, 2u64, 3u64, 4u64]);
assert_eq!(ValidatorSet::validators(), &[1, 2, 3, 4]);
assert_noop!(
ValidatorSet::add_validator(RuntimeOrigin::root(), 4),
Error::<Test>::Duplicate
Expand Down
4 changes: 0 additions & 4 deletions gn-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ std = [
"pallet-balances/std",
"pallet-grandpa/std",
"pallet-guild/std",
"pallet-im-online/std",
"pallet-oracle/std",
"pallet-randomness-collective-flip/std",
"pallet-session/std",
Expand Down Expand Up @@ -64,7 +63,6 @@ try-runtime = [
"pallet-balances/try-runtime",
"pallet-grandpa/try-runtime",
"pallet-guild/try-runtime",
"pallet-im-online/try-runtime",
"pallet-oracle/try-runtime",
"pallet-randomness-collective-flip/try-runtime",
"pallet-sudo/try-runtime",
Expand All @@ -81,7 +79,6 @@ pallet-validator-manager = { version = "0.0.0-alpha", path = "../gn-pallets/pall

# general
hex-literal = { version = "0.3.4", optional = true }
log = { version = "0.4.17", default-features = false }
parity-scale-codec = { workspace = true, features = ["derive"] }
scale-info = { workspace = true, features = ["derive"] }

Expand All @@ -98,7 +95,6 @@ frame-try-runtime = { workspace = true, optional = true }
pallet-aura = { workspace = true }
pallet-balances = { workspace = true }
pallet-grandpa = { workspace = true }
pallet-im-online = { workspace = true }
pallet-randomness-collective-flip = { workspace = true }
pallet-session = { workspace = true }
pallet-sudo = { workspace = true }
Expand Down
Loading