Skip to content

Commit

Permalink
Cleanup/updates. (#107)
Browse files Browse the repository at this point in the history
* Cleanup/updates.

* Add in migrations.
  • Loading branch information
firke authored Dec 15, 2021
1 parent 084a905 commit ec928ee
Show file tree
Hide file tree
Showing 11 changed files with 732 additions and 426 deletions.
142 changes: 73 additions & 69 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions node/opportunity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ runtime-benchmarks = [
]

[dependencies]
rand = "0.7.2"
structopt = { version = "0.3.8" }
codec = { package = "parity-scale-codec", version = "2.0.0" }

Expand Down
284 changes: 141 additions & 143 deletions node/opportunity/spec/opportunity_raw.json

Large diffs are not rendered by default.

66 changes: 38 additions & 28 deletions node/opportunity/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_staking::Forcing;
use sc_chain_spec::{ChainSpecExtension, ChainType};
use sc_client_api::{BadBlocks, ForkBlocks};
use serde::{Deserialize, Serialize};
Expand All @@ -10,13 +9,13 @@ use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{IdentifyAccount, Verify};

use opportunity_runtime::{
AccountId, AssetRegistryConfig, BabeConfig, BalancesConfig, Block, CouncilConfig,
wasm_binary_unwrap, MAX_NOMINATIONS, AssetRegistryConfig, BabeConfig, BalancesConfig, Block, CouncilConfig,
DemocracyConfig, ElectionsConfig, GrandpaConfig, ImOnlineConfig, OracleConfig, Perbill,
SessionConfig, SessionKeys, Signature, StakerStatus, StakingConfig, SudoConfig, SystemConfig,
TechnicalCommitteeConfig, TechnicalMembershipConfig, TreasuryConfig,
SessionConfig, SessionKeys, StakerStatus, StakingConfig, SudoConfig, SystemConfig,
TechnicalCommitteeConfig, TechnicalMembershipConfig, TreasuryConfig
};
use primitives::{AssetId, Signature, AccountId, Balance};

use primitives::AssetId;
pub const CORE_ASSET_ID: AssetId = 1;

// Node `ChainSpec` extensions.
Expand Down Expand Up @@ -92,7 +91,6 @@ pub fn opportunity_config() -> Result<ChainSpec, String> {
}

pub fn opportunity_standalone_config() -> Result<ChainSpec, String> {
let wasm_binary = opportunity_runtime::WASM_BINARY.ok_or("Development wasm not available")?;
let boot_nodes = vec![];

Ok(ChainSpec::from_genesis(
Expand All @@ -104,10 +102,10 @@ pub fn opportunity_standalone_config() -> Result<ChainSpec, String> {
ChainType::Live,
move || {
opportunity_testnet_config_genesis(
// Opportunity Runtime WASM binary
wasm_binary,
// Initial authorities
vec![authority_keys_from_seed("Alice")],
// Initial nominators
vec![],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
Expand Down Expand Up @@ -144,7 +142,6 @@ pub fn opportunity_standalone_config() -> Result<ChainSpec, String> {
}

pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = opportunity_runtime::WASM_BINARY.ok_or("Development wasm not available")?;
let boot_nodes = vec![];

Ok(ChainSpec::from_genesis(
Expand All @@ -156,9 +153,10 @@ pub fn development_config() -> Result<ChainSpec, String> {
ChainType::Local,
move || {
opportunity_testnet_config_genesis(
wasm_binary,
// Initial authorities
vec![authority_keys_from_seed("Alice")],
// Initial nominators
vec![],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
Expand All @@ -184,7 +182,6 @@ pub fn development_config() -> Result<ChainSpec, String> {
}

pub fn local_testnet_config() -> Result<ChainSpec, String> {
let wasm_binary = opportunity_runtime::WASM_BINARY.ok_or("Development wasm not available")?;
let boot_nodes = vec![];

Ok(ChainSpec::from_genesis(
Expand All @@ -195,9 +192,10 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
ChainType::Local,
move || {
opportunity_testnet_config_genesis(
wasm_binary,
// Initial authorities
vec![authority_keys_from_seed("Alice")],
// Initial nominators
vec![],
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
// Pre-funded accounts
Expand Down Expand Up @@ -231,7 +229,6 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
}

fn opportunity_testnet_config_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(
AccountId,
AccountId,
Expand All @@ -240,13 +237,37 @@ fn opportunity_testnet_config_genesis(
ImOnlineId,
AuthorityDiscoveryId,
)>,
initial_nominators: Vec<AccountId>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
) -> opportunity_runtime::GenesisConfig {
const MILLICENTS: Balance = 1_000_000_000;
const CENTS: Balance = 1_000 * MILLICENTS;
const DOLLARS: Balance = 100 * CENTS;
const ENDOWMENT: Balance = 10_000_000 * DOLLARS;
const STASH: Balance = ENDOWMENT / 1000;
// stakers: all validators and nominators.
let mut rng = rand::thread_rng();
let stakers = initial_authorities
.iter()
.map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator))
.chain(initial_nominators.iter().map(|x| {
use rand::{seq::SliceRandom, Rng};
let limit = (MAX_NOMINATIONS as usize).min(initial_authorities.len());
let count = rng.gen::<usize>() % limit;
let nominations = initial_authorities
.as_slice()
.choose_multiple(&mut rng, count)
.into_iter()
.map(|choice| choice.0.clone())
.collect::<Vec<_>>();
(x.clone(), x.clone(), STASH, StakerStatus::Nominator(nominations))
}))
.collect::<Vec<_>>();

opportunity_runtime::GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
code: wasm_binary_unwrap().to_vec(),
changes_trie_config: Default::default(),
},
balances: BalancesConfig {
Expand Down Expand Up @@ -274,21 +295,10 @@ fn opportunity_testnet_config_genesis(
},
staking: StakingConfig {
validator_count: initial_authorities.len() as u32,
minimum_validator_count: 1,
stakers: initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.1.clone(),
100_000_000_000_000_000_u128,
StakerStatus::Validator,
)
})
.collect(),
minimum_validator_count: initial_authorities.len() as u32,
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
force_era: Forcing::NotForcing,
slash_reward_fraction: Perbill::from_percent(10),
stakers,
..Default::default()
},
asset_registry: AssetRegistryConfig {
Expand Down
5 changes: 1 addition & 4 deletions node/opportunity/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@

use std::sync::Arc;

use opportunity_runtime::{AccountId, BlockNumber, Hash, Index};
use primitives::{Balance, Block};
use primitives::{Balance, Block, AccountId, BlockNumber, Hash, Index};
use sc_client_api::AuxStore;
use sc_consensus_babe::{Config, Epoch};
use sc_consensus_babe_rpc::BabeRpcHandler;
Expand Down Expand Up @@ -138,7 +137,6 @@ where
// more context: https://github.com/paritytech/substrate/pull/3480
// These RPCs should use an asynchronous caller instead.
io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone())));

io.extend_with(sc_consensus_babe_rpc::BabeApi::to_delegate(BabeRpcHandler::new(
client.clone(),
shared_epoch_changes.clone(),
Expand All @@ -147,7 +145,6 @@ where
select_chain,
deny_unsafe,
)));

io.extend_with(sc_finality_grandpa_rpc::GrandpaApi::to_delegate(GrandpaRpcHandler::new(
shared_authority_set.clone(),
shared_voter_state,
Expand Down
42 changes: 21 additions & 21 deletions node/opportunity/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
#![warn(unused_extern_crates)]

//! Service implementation. Specialized wrapper over substrate service.
use crate::rpc::{create_full, BabeDeps, FullDeps, GrandpaDeps, IoHandler};
use opportunity_runtime::{self, RuntimeApi};
use opportunity_runtime::{RuntimeApi};
use primitives::Block;
use crate::rpc::{create_full, BabeDeps, FullDeps, GrandpaDeps, IoHandler};

use futures::prelude::*;
use sc_client_api::{ExecutorProvider};
use sc_consensus_babe::{self, SlotProportion};
use sc_executor::NativeElseWasmExecutor;
use sc_network::{Event, NetworkService};
pub use sc_rpc_api::DenyUnsafe;
use sc_service::{config::Configuration, error::Error as ServiceError, TaskManager};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_runtime::{traits::Block as BlockT};
use std::sync::Arc;
use sc_rpc_api::DenyUnsafe;

// Declare an instance of the native executor named `ExecutorDispatch`. Include the wasm binary as
// the equivalent wasm code.
Expand Down Expand Up @@ -81,6 +80,7 @@ pub fn new_partial(
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>,
sc_consensus_babe::BabeLink<Block>,
),
sc_finality_grandpa::SharedVoterState,
Option<Telemetry>,
),
>,
Expand Down Expand Up @@ -132,7 +132,6 @@ pub fn new_partial(
select_chain.clone(),
telemetry.as_ref().map(|x| x.handle()),
)?;

let justification_import = grandpa_block_import.clone();

let (block_import, babe_link) = sc_consensus_babe::block_import(
Expand Down Expand Up @@ -170,13 +169,13 @@ pub fn new_partial(

let import_setup = (block_import, grandpa_link, babe_link);

let rpc_extensions_builder = {
let (rpc_extensions_builder, rpc_setup) = {
let (_, grandpa_link, babe_link) = &import_setup;

let justification_stream = grandpa_link.justification_stream();
let shared_authority_set = grandpa_link.shared_authority_set().clone();
// let shared_voter_state = sc_finality_grandpa::SharedVoterState::empty();
// let rpc_setup = shared_voter_state.clone();
let shared_voter_state = sc_finality_grandpa::SharedVoterState::empty();
let rpc_setup = shared_voter_state.clone();

let finality_proof_provider = sc_finality_grandpa::FinalityProofProvider::new_for_service(
backend.clone(),
Expand Down Expand Up @@ -205,7 +204,7 @@ pub fn new_partial(
keystore: keystore.clone(),
},
grandpa: GrandpaDeps {
shared_voter_state: sc_finality_grandpa::SharedVoterState::empty(),
shared_voter_state: shared_voter_state.clone(),
shared_authority_set: shared_authority_set.clone(),
justification_stream: justification_stream.clone(),
subscription_executor,
Expand All @@ -216,7 +215,7 @@ pub fn new_partial(
create_full(deps).map_err(Into::into)
};

rpc_extensions_builder
(rpc_extensions_builder, rpc_setup)
};

Ok(sc_service::PartialComponents {
Expand All @@ -227,7 +226,7 @@ pub fn new_partial(
select_chain,
import_queue,
transaction_pool,
other: (rpc_extensions_builder, import_setup, telemetry),
other: (rpc_extensions_builder, import_setup, rpc_setup, telemetry),
})
}

Expand Down Expand Up @@ -259,9 +258,10 @@ pub fn new_full_base(
keystore_container,
select_chain,
transaction_pool,
other: (rpc_extensions_builder, import_setup, mut telemetry),
other: (rpc_extensions_builder, import_setup, rpc_setup, mut telemetry),
} = new_partial(&config)?;

let shared_voter_state = rpc_setup;
let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht;

config.network.extra_sets.push(sc_finality_grandpa::grandpa_peers_set_config());
Expand Down Expand Up @@ -319,7 +319,7 @@ pub fn new_full_base(

(with_startup_data)(&block_import, &babe_link);

if role.is_authority() {
if let sc_service::config::Role::Authority { .. } = &role {
let proposer = sc_basic_authorship::ProposerFactory::new(
task_manager.spawn_handle(),
client.clone(),
Expand Down Expand Up @@ -413,7 +413,8 @@ pub fn new_full_base(
let keystore =
if role.is_authority() { Some(keystore_container.sync_keystore()) } else { None };

let grandpa_config = sc_finality_grandpa::Config {
let config = sc_finality_grandpa::Config {
// FIXME #1578 make this available through chainspec
gossip_duration: std::time::Duration::from_millis(333),
justification_period: 512,
name: Some(name),
Expand All @@ -431,21 +432,20 @@ pub fn new_full_base(
// been tested extensively yet and having most nodes in a network run it
// could lead to finality stalls.
let grandpa_config = sc_finality_grandpa::GrandpaParams {
config: grandpa_config,
config,
link: grandpa_link,
network: network.clone(),
telemetry: telemetry.as_ref().map(|x| x.handle()),
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
prometheus_registry,
shared_voter_state: sc_finality_grandpa::SharedVoterState::empty(),
telemetry: telemetry.as_ref().map(|x| x.handle()),
shared_voter_state,
};

// the GRANDPA voter task is considered infallible, i.e.
// if it fails we take down the service with it.
task_manager.spawn_essential_handle().spawn_blocking(
"grandpa-voter",
sc_finality_grandpa::run_grandpa_voter(grandpa_config)?,
);
task_manager
.spawn_essential_handle()
.spawn_blocking("grandpa-voter", sc_finality_grandpa::run_grandpa_voter(grandpa_config)?);
}

network_starter.start_network();
Expand Down
2 changes: 2 additions & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ edition = "2018"
[dependencies]
sp-std = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.12", default-features = false }
sp-runtime = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }
sp-core = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.12", default-features = false }

[features]
default = ["std"]
std = [
"sp-std/std",
"sp-runtime/std",
"sp-core/std"
]
11 changes: 10 additions & 1 deletion primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![cfg_attr(not(feature = "std"), no_std)]

use sp_runtime::{generic, traits::BlakeTwo256, OpaqueExtrinsic};
use sp_runtime::{generic, OpaqueExtrinsic, MultiSignature, traits::{BlakeTwo256, IdentifyAccount, Verify}};

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
/// An index to a block.
pub type BlockNumber = u32;
/// Balance for and account
Expand All @@ -22,3 +25,9 @@ pub type EraIndex = u64;
pub type SocketIndex = u32;
/// Primary asset ID to use
pub const CORE_ASSET_ID: AssetId = 0;
/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;
/// Index of a transaction in the chain.
pub type Index = u32;
/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;
Loading

0 comments on commit ec928ee

Please sign in to comment.