Skip to content

Commit

Permalink
Update bag thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Oct 23, 2023
1 parent a5e540d commit 8a6bd6b
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 26 deletions.
71 changes: 59 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"test/client",
"test/runtime-utils",
"test/traced-test",
'runtime/generate-bags',
]

resolver = "2"
Expand Down Expand Up @@ -152,3 +153,4 @@ pallet-proxy = { branch = "pos-keep-history-polkadot-v0.9.41", git = "https://gi
pallet-fast-unstake = { branch = "pos-keep-history-polkadot-v0.9.41", git = "https://github.com/gluwa/substrate.git", default-features = false }
pallet-nomination-pools = { branch = "pos-keep-history-polkadot-v0.9.41", git = "https://github.com/gluwa/substrate.git", default-features = false }
pallet-identity = { branch = "pos-keep-history-polkadot-v0.9.41", git = "https://github.com/gluwa/substrate.git", default-features = false }
generate-bags = { branch = "pos-keep-history-polkadot-v0.9.41", git = "https://github.com/gluwa/substrate.git" } # Utils
2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sp-staking = { workspace = true }
pallet-staking-reward-fn = { workspace = true }
pallet-offences = { workspace = true }
log = { workspace = true }
sp-io = { workspace = true }

# Pallets necessary for use with the Staking Dashboard
# CSUB-503 https://gluwa.atlassian.net/browse/CSUB-503?atlOrigin=eyJpIjoiODY0NGFjOTkzZjE1NGFkYTg1OTNkZmYxZGU1NjlmYjIiLCJwIjoiaiJ9
Expand Down Expand Up @@ -132,6 +133,7 @@ std = [
'sp-block-builder/std',
'sp-core/std',
'sp-inherents/std',
'sp-io/std',
'sp-offchain/std',
'sp-runtime/std',
'sp-session/std',
Expand Down
11 changes: 11 additions & 0 deletions runtime/generate-bags/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "generate-bags"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
creditcoin-node-runtime = { path = "../" }
generate-bags = { workspace = true }
clap = { version = "4.4.6", features = ["derive"] }
55 changes: 55 additions & 0 deletions runtime/generate-bags/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::path::PathBuf;

use clap::Parser;
use generate_bags::generate_thresholds;

#[derive(Parser)]
struct Cli {
/// Total issuance of the currency in millions
#[clap(short, long)]
total_issuance: u128,

/// Minimum account balance
#[clap(short, long, default_value_t = 8456776)]
minimum_balance: u128,

#[clap(long, default_value_t = 200)]
n_bags: usize,

#[clap(long)]
output: PathBuf,
}

/*
pub struct U128CurrencyToVote;
impl U128CurrencyToVote {
fn factor(issuance: u128) -> u128 {
(issuance / u64::MAX as u128).max(1)
}
}
impl CurrencyToVote<u128> for U128CurrencyToVote {
fn to_vote(value: u128, issuance: u128) -> u64 {
(value / Self::factor(issuance)).saturated_into()
}
fn to_currency(value: u128, issuance: u128) -> u128 {
value.saturating_mul(Self::factor(issuance))
}
} */

fn main() -> Result<(), std::io::Error> {
let Cli { total_issuance, minimum_balance, n_bags, output } = Cli::parse();

let issuance_ctc = total_issuance * creditcoin_node_runtime::CTC * 1_000_000;

println!("Issuance ctc = {issuance_ctc}; factor = {}", (issuance_ctc / u64::MAX as u128));

generate_thresholds::<creditcoin_node_runtime::Runtime>(
n_bags,
&output,
issuance_ctc,
minimum_balance,
)
}
37 changes: 24 additions & 13 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use pallet_offchain_task_scheduler::crypto::AuthorityId;
use pallet_session::historical as session_historical;
use pallet_staking::UseValidatorsMap;
pub use pallet_staking_substrate::{self, StakerStatus};
use parity_scale_codec::Decode;
use sp_api::impl_runtime_apis;
use sp_consensus_babe as babe_primitives;
use sp_core::{crypto::KeyTypeId, ConstU64, Encode, OpaqueMetadata, U256};
Expand Down Expand Up @@ -78,6 +79,8 @@ mod mock;
#[cfg(test)]
mod tests;

mod output;

mod version;
pub use version::VERSION;

Expand Down Expand Up @@ -429,20 +432,24 @@ parameter_types! {
pub MinAnnualInflation : Perquintill = Perquintill::from_rational(25u64, 1000u64);
}

pub struct InitBabe;
// NOTE: this should be removed once the runtime has been upgraded on mainnet
// (though there is a check in case we forget)
pub struct MigrateBags;

impl OnRuntimeUpgrade for InitBabe {
impl OnRuntimeUpgrade for MigrateBags {
fn on_runtime_upgrade() -> Weight {
if Babe::epoch_config().is_none() {
log::debug!("Initializing BABE");
let babe_config = pallet_babe::InitConfig {
authorities: Vec::new(),
epoch_config: Some(BABE_GENESIS_EPOCH_CONFIG),
};
Babe::genesis_init(babe_config);
<Runtime as frame_system::Config>::DbWeight::get().writes(3)
} else {
const BAGS_LIST_MIGRATION: &[u8] = b"ctc:bags_list_thresholds_migrated";
const OLD_THRESHOLDS: &[u64] = &[];
let key = sp_core::hashing::blake2_256(BAGS_LIST_MIGRATION);
if let Some(_v) = sp_io::storage::get(&key) {
Weight::zero()
} else {
let num_accts_affected =
pallet_bags_list::List::<Runtime, VoterBagsListInstance>::migrate(OLD_THRESHOLDS);
let each = ParityDbWeight::get().reads_writes(2, 2);
let weight = each.saturating_mul(num_accts_affected as u64);
sp_io::storage::set(&key, &true.encode());
weight
}
}
}
Expand Down Expand Up @@ -486,12 +493,16 @@ impl onchain::Config for OnChainSeqPhragmen {
type TargetsBound = MaxElectableTargets;
}

parameter_types! {
pub const BagThresholds: &'static [u64] = &output::THRESHOLDS;
}

type VoterBagsListInstance = pallet_bags_list::Instance1;
impl pallet_bags_list::Config<VoterBagsListInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type ScoreProvider = Staking;
type WeightInfo = ();
type BagThresholds = ();
type BagThresholds = BagThresholds;
type Score = u64;
}

Expand Down Expand Up @@ -702,7 +713,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(InitBabe,),
(MigrateBags,),
>;

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Loading

0 comments on commit 8a6bd6b

Please sign in to comment.