From 408bf06f633083932b4fb310db14d864553982b6 Mon Sep 17 00:00:00 2001 From: OnyxSkyscape Date: Mon, 20 Mar 2023 18:04:20 +0100 Subject: [PATCH] Revert "Merge branch 'main' into I110-im-online-fix" This reverts commit 1accee1948b3f65fc756c54caa4bcf59d3955b46, reversing changes made to a45502a1586f96e5385ae7da97155d62f0f8664a. --- Cargo.toml | 1 + gn-client/examples/guild/main.rs | 25 +--- gn-client/examples/guild/register.rs | 21 ++++ gn-client/examples/guild/sudo.rs | 34 ------ gn-client/src/tx/mod.rs | 36 +----- gn-node/Cargo.toml | 1 + gn-node/src/chain_spec.rs | 9 +- .../pallet-validator-manager/src/lib.rs | 2 - .../pallet-validator-manager/src/migration.rs | 15 --- .../pallet-validator-manager/src/test.rs | 16 +-- gn-runtime/Cargo.toml | 4 + gn-runtime/src/lib.rs | 11 +- gn-wasm/Cargo.toml | 1 - gn-wasm/src/lib.rs | 113 ++---------------- integr_test.py | 2 +- 15 files changed, 72 insertions(+), 219 deletions(-) create mode 100644 gn-client/examples/guild/register.rs delete mode 100644 gn-client/examples/guild/sudo.rs delete mode 100644 gn-pallets/pallet-validator-manager/src/migration.rs diff --git a/Cargo.toml b/Cargo.toml index cd650b86..74f04609 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ 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 } diff --git a/gn-client/examples/guild/main.rs b/gn-client/examples/guild/main.rs index cbbe0aa1..c47ce2d0 100644 --- a/gn-client/examples/guild/main.rs +++ b/gn-client/examples/guild/main.rs @@ -2,7 +2,7 @@ mod common; mod fund; mod join; mod key; -mod sudo; +mod register; mod token; use gn_client::tx; @@ -20,13 +20,9 @@ enum Example { Join, Key(Key), Token, - Sudo { + Register { #[structopt(long, short)] - pallet: String, - #[structopt(long, short)] - method: String, - #[structopt(long, short)] - account: Option, + operator: Option, }, } @@ -83,19 +79,8 @@ async fn main() { key::generate(&curve, password.as_deref()) } Example::Key(Key::Rotate) => key::rotate(api).await, - Example::Sudo { - pallet, - method, - account, - } => { - sudo::sudo( - api, - signer, - &pallet.to_lowercase(), - &method.to_lowercase(), - account.as_deref(), - ) - .await + Example::Register { operator } => { + register::register(api, signer, operator.as_deref()).await } Example::Token => token::token(api, signer).await, } diff --git a/gn-client/examples/guild/register.rs b/gn-client/examples/guild/register.rs new file mode 100644 index 00000000..966a7e78 --- /dev/null +++ b/gn-client/examples/guild/register.rs @@ -0,0 +1,21 @@ +use gn_client::{ + tx::{self, Signer}, + AccountId, Api, +}; + +use std::str::FromStr; +use std::sync::Arc; + +pub async fn register(api: Api, root: Arc, 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 = tx::register_operator(&account_id); + tx::send_tx_in_block(api, &tx::sudo(payload), root) + .await + .unwrap(); + println!("{account_id} registered as operator"); +} diff --git a/gn-client/examples/guild/sudo.rs b/gn-client/examples/guild/sudo.rs deleted file mode 100644 index 3358be16..00000000 --- a/gn-client/examples/guild/sudo.rs +++ /dev/null @@ -1,34 +0,0 @@ -use gn_client::{ - tx::{self, Signer}, - AccountId, Api, -}; - -use std::str::FromStr; -use std::sync::Arc; - -pub async fn sudo( - api: Api, - root: Arc, - 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"); -} diff --git a/gn-client/src/tx/mod.rs b/gn-client/src/tx/mod.rs index 6164c33a..26713872 100644 --- a/gn-client/src/tx/mod.rs +++ b/gn-client/src/tx/mod.rs @@ -39,44 +39,20 @@ pub fn sudo<'a>(call: DynamicTxPayload<'_>) -> DynamicTxPayload<'a> { subxt::dynamic::tx("Sudo", "sudo", vec![("call", call.into_value())]) } -pub fn register_operator<'a>(operator: &AccountId) -> DynamicTxPayload<'a> { - subxt::dynamic::tx( - "Oracle", - "register_operator", - vec![("operator", Value::from_bytes(operator))], - ) +pub fn fund_account(account: &AccountId, amount: u128) -> impl TxPayload { + runtime::tx() + .balances() + .transfer(MultiAddress::Id(account.clone()), amount) } -pub fn deregister_operator<'a>(operator: &AccountId) -> DynamicTxPayload<'a> { +pub fn register_operator<'a>(operator: &AccountId) -> DynamicTxPayload<'a> { subxt::dynamic::tx( "Oracle", - "deregister_operator", + "register_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() } diff --git a/gn-node/Cargo.toml b/gn-node/Cargo.toml index 1fc2ec15..e33c4541 100644 --- a/gn-node/Cargo.toml +++ b/gn-node/Cargo.toml @@ -41,6 +41,7 @@ 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 } diff --git a/gn-node/src/chain_spec.rs b/gn-node/src/chain_spec.rs index d49868c7..3377c11d 100644 --- a/gn-node/src/chain_spec.rs +++ b/gn-node/src/chain_spec.rs @@ -1,7 +1,8 @@ use gn_runtime::{ - AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, SessionConfig, Signature, - SudoConfig, SystemConfig, ValidatorManagerConfig, WASM_BINARY, + AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, ImOnlineConfig, + 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}; @@ -35,6 +36,7 @@ struct AuthorityKeys { account_id: AccountId, aura_id: AuraId, grandpa_id: GrandpaId, + im_online_id: ImOnlineId, } impl AuthorityKeys { @@ -43,6 +45,7 @@ impl AuthorityKeys { account_id: get_account_id_from_seed::(seed), aura_id: get_from_seed::(seed), grandpa_id: get_from_seed::(seed), + im_online_id: get_from_seed::(seed), } } @@ -50,6 +53,7 @@ impl AuthorityKeys { gn_runtime::opaque::SessionKeys { aura: self.aura_id.clone(), grandpa: self.grandpa_id.clone(), + im_online: self.im_online_id.clone(), } } } @@ -188,6 +192,7 @@ fn testnet_genesis( grandpa: GrandpaConfig { authorities: vec![], }, + im_online: ImOnlineConfig { keys: vec![] }, sudo: SudoConfig { // Assign network admin rights. key: Some(root_key), diff --git a/gn-pallets/pallet-validator-manager/src/lib.rs b/gn-pallets/pallet-validator-manager/src/lib.rs index 68acfa7b..5e84e2eb 100644 --- a/gn-pallets/pallet-validator-manager/src/lib.rs +++ b/gn-pallets/pallet-validator-manager/src/lib.rs @@ -16,7 +16,6 @@ #![deny(clippy::dbg_macro)] #![deny(unused_crate_dependencies)] -pub mod migration; #[cfg(test)] mod mock; #[cfg(test)] @@ -239,7 +238,6 @@ impl Pallet { fn unapprove_validator(validator_id: T::AccountId) -> DispatchResult { let mut approved_set = >::get(); approved_set.retain(|v| *v != validator_id); - >::set(approved_set); Ok(()) } diff --git a/gn-pallets/pallet-validator-manager/src/migration.rs b/gn-pallets/pallet-validator-manager/src/migration.rs deleted file mode 100644 index 4ee34011..00000000 --- a/gn-pallets/pallet-validator-manager/src/migration.rs +++ /dev/null @@ -1,15 +0,0 @@ -use super::*; - -pub fn on_runtime_upgrade() { - let validators = Validators::::get(); - log::info!("# validators: {}", validators.len()); - log::info!( - "# approved validators: {}", - ApprovedValidators::::get().len() - ); - ApprovedValidators::::set(validators); - log::info!( - "# approved validators post-migration: {}", - ApprovedValidators::::get().len() - ); -} diff --git a/gn-pallets/pallet-validator-manager/src/test.rs b/gn-pallets/pallet-validator-manager/src/test.rs index f00036db..71f8aca1 100644 --- a/gn-pallets/pallet-validator-manager/src/test.rs +++ b/gn-pallets/pallet-validator-manager/src/test.rs @@ -12,8 +12,8 @@ fn simple_setup_should_work() { authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)] ); - assert_eq!(ValidatorSet::validators(), &[1, 2, 3]); - assert_eq!(Session::validators(), &[1, 2, 3]); + assert_eq!(ValidatorSet::validators(), vec![1u64, 2u64, 3u64]); + assert_eq!(Session::validators(), vec![1, 2, 3]); }); } @@ -21,8 +21,7 @@ fn simple_setup_should_work() { fn add_validator_updates_validators_list() { new_test_ext().execute_with(|| { assert_ok!(ValidatorSet::add_validator(RuntimeOrigin::root(), 4)); - assert_eq!(ValidatorSet::validators(), &[1, 2, 3, 4]); - assert_eq!(ValidatorSet::approved_validators(), &[1, 2, 3, 4]); + assert_eq!(ValidatorSet::validators(), vec![1u64, 2u64, 3u64, 4u64]) }); } @@ -30,12 +29,7 @@ fn add_validator_updates_validators_list() { fn remove_validator_updates_validators_list() { new_test_ext().execute_with(|| { assert_ok!(ValidatorSet::remove_validator(RuntimeOrigin::root(), 2)); - 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]); + assert_eq!(ValidatorSet::validators(), vec![1u64, 3u64]); }); } @@ -63,7 +57,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(), &[1, 2, 3, 4]); + assert_eq!(ValidatorSet::validators(), vec![1u64, 2u64, 3u64, 4u64]); assert_noop!( ValidatorSet::add_validator(RuntimeOrigin::root(), 4), Error::::Duplicate diff --git a/gn-runtime/Cargo.toml b/gn-runtime/Cargo.toml index 67723a6c..101459ab 100644 --- a/gn-runtime/Cargo.toml +++ b/gn-runtime/Cargo.toml @@ -35,6 +35,7 @@ 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", @@ -64,6 +65,7 @@ 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", @@ -80,6 +82,7 @@ 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"] } @@ -96,6 +99,7 @@ frame-try-runtime = { workspace = true, features = ["try-runtime"], optional = t 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 } diff --git a/gn-runtime/src/lib.rs b/gn-runtime/src/lib.rs index 67585529..6e79aacd 100644 --- a/gn-runtime/src/lib.rs +++ b/gn-runtime/src/lib.rs @@ -14,6 +14,8 @@ pub use pallet_balances::Call as BalancesCall; use pallet_grandpa::{ fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList, }; +use pallet_im_online::sr25519::AuthorityId as ImOnlineId; +use parity_scale_codec::Encode; use sp_api::impl_runtime_apis; use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; @@ -24,7 +26,7 @@ use sp_runtime::{ Verify, }, transaction_validity::{TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, MultiSignature, + ApplyExtrinsicResult, MultiSignature, SaturatedConversion, }; use sp_std::prelude::*; #[cfg(feature = "std")] @@ -90,6 +92,7 @@ pub mod opaque { pub struct SessionKeys { pub aura: Aura, pub grandpa: Grandpa, + pub im_online: ImOnline, } } } @@ -156,6 +159,11 @@ parameter_types! { pub const MinAuthorities: u32 = 2; pub const Period: u32 = 2 * MINUTES; pub const Offset: u32 = 0; + + pub const ImOnlineUnsignedPriority: TransactionPriority = TransactionPriority::max_value(); + pub const MaxKeys: u32 = 10_000; + pub const MaxPeerInHeartbeats: u32 = 10_000; + pub const MaxPeerDataEncodingSize: u32 = 1_000; } // Configure FRAME pallets to include in runtime. @@ -420,6 +428,7 @@ construct_runtime!( Timestamp: pallet_timestamp, ValidatorManager: pallet_validator_manager, Session: pallet_session, + ImOnline: pallet_im_online, Aura: pallet_aura, Grandpa: pallet_grandpa, diff --git a/gn-wasm/Cargo.toml b/gn-wasm/Cargo.toml index e87ce7bc..9d582091 100644 --- a/gn-wasm/Cargo.toml +++ b/gn-wasm/Cargo.toml @@ -13,7 +13,6 @@ queries = [] # local gn-client = { version = "0.0.0-alpha", path = "../gn-client", default-features = false, features = ["wasm"] } gn-common = { version = "0.0.0-alpha", path = "../gn-common", default-features = false } -gn-engine = { version = "0.0.0-alpha", path = "../gn-engine", default-features = false } # general serde-wasm-bindgen = "0.4.5" diff --git a/gn-wasm/src/lib.rs b/gn-wasm/src/lib.rs index 63b2aeef..fe3fa6ca 100644 --- a/gn-wasm/src/lib.rs +++ b/gn-wasm/src/lib.rs @@ -6,9 +6,7 @@ use gn_client::{query, AccountId, Api}; use gn_common::filter::Guild as GuildFilter; use gn_common::identity::Identity; use gn_common::merkle::Proof; -use gn_common::SerializedRequirements; use gn_common::{pad::pad_to_n_bytes, GuildName, RoleName}; -use gn_engine::RequirementsWithLogic; use serde_wasm_bindgen::{from_value as deserialize_from_value, to_value as serialize_to_value}; use wasm_bindgen::prelude::*; @@ -128,36 +126,10 @@ pub fn verification_msg(address: String) -> Result { Ok(gn_common::utils::verification_msg(account_id)) } -#[wasm_bindgen(js_name = "serializeRequirements")] -pub fn serialize_requirements(requirements: JsValue) -> Result { - let req = deserialize_from_value::(requirements) - .map_err(|error| error.to_string())?; - - let req = req - .into_serialized_tuple() - .map_err(|error| error.to_string())?; - - serialize_to_value(&req).map_err(|error| error.to_string()) -} - -#[wasm_bindgen(js_name = "deserializeRequirements")] -pub fn deserialize_requirements(requirements: JsValue) -> Result { - let req = deserialize_from_value::(requirements) - .map_err(|error| error.to_string())?; - - let req = - RequirementsWithLogic::from_serialized_tuple(req).map_err(|error| error.to_string())?; - - serialize_to_value(&req).map_err(|error| error.to_string()) -} - #[cfg(test)] mod test { use super::*; - use gn_engine::balance::{Balance, Relation, TokenType}; - use gn_engine::chains::EvmChain; - use gn_engine::{EvmAddress, Requirement, U256}; - use gn_test_data as _; + use gn_test_data::*; use wasm_bindgen_test::*; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); @@ -168,9 +140,18 @@ mod test { } #[wasm_bindgen_test] - async fn test_verification_msg_wrapper() { + async fn test_query_chain() { init_tracing(); + let api = Api::from_url(URL).await.unwrap(); + + let chain = api.rpc().system_chain().await.unwrap(); + + assert_eq!(chain, "Development"); + } + + #[wasm_bindgen_test] + async fn test_verification_msg_wrapper() { let account_id_str = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"; let account_id = AccountId::from_str(account_id_str).unwrap(); assert_eq!(account_id.to_string(), account_id_str); @@ -180,68 +161,6 @@ mod test { assert_eq!(msg, expected_msg); } - #[wasm_bindgen_test] - async fn test_serialization_roundtrip() { - let tokens: Vec>> = vec![ - None, - Some(TokenType::Fungible { - address: [99u8; 20], - }), - Some(TokenType::NonFungible { - address: [0u8; 20], - id: None, - }), - Some(TokenType::NonFungible { - address: [1u8; 20], - id: Some([223u8; 32]), - }), - ]; - - let relations = vec![ - Relation::EqualTo([11u8; 32]), - Relation::GreaterThan([12u8; 32]), - Relation::LessOrEqualTo([13u8; 32]), - Relation::Between([50u8; 32]..[100u8; 32]), - ]; - - let logic = "(0 AND 2) OR (1 AND 3)".to_string(); - - let requirements = tokens - .into_iter() - .zip(relations.into_iter()) - .map(|(token_type, relation)| { - Requirement::EvmBalance(Balance { - token_type, - relation, - chain: EvmChain::Ethereum, - }) - }) - .collect(); - - let requirements_with_logic = RequirementsWithLogic { - requirements, - logic, - }; - - let js_requirements_expected = serialize_to_value(&requirements_with_logic) - .map_err(|error| error.to_string()) - .unwrap(); - - let js_requirements_serialized = - serialize_requirements(js_requirements_expected.clone()).unwrap(); - let js_requirements = deserialize_requirements(js_requirements_serialized).unwrap(); - - assert_eq!( - js_requirements.as_string(), - js_requirements_expected.as_string() - ); - - let requirements_from_js: RequirementsWithLogic = - deserialize_from_value(js_requirements).unwrap(); - - assert_eq!(requirements_with_logic.logic, requirements_from_js.logic); - } - // NOTE these only work after the guild/join example // was successfully run #[cfg(feature = "queries")] @@ -251,16 +170,6 @@ mod test { use gn_common::filter::{Filter, Logic as FilterLogic}; use gn_common::identity::Identity; use gn_common::Guild; - use gn_test_data::*; - - #[wasm_bindgen_test] - async fn test_query_chain() { - let api = Api::from_url(URL).await.unwrap(); - - let chain = api.rpc().system_chain().await.unwrap(); - - assert_eq!(chain, "Development"); - } #[wasm_bindgen_test] async fn test_query_members() { diff --git a/integr_test.py b/integr_test.py index 4946c166..46f8d553 100644 --- a/integr_test.py +++ b/integr_test.py @@ -74,7 +74,7 @@ def run_tests(*commands, timeout=300): def main(): try: node = start_node() - command = "cargo run --release --example guild -- sudo --pallet oracle --method register" + command = "cargo run --release --example guild -- register" run_tests(command, timeout=90) oracle = start_oracle() oracle_monitor = Thread(target=monitor_oracle, args=(oracle, node,))