mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 22:11:06 +00:00
Use consts rather than direct names.
This commit is contained in:
@@ -31,10 +31,18 @@ use support::storage;
|
||||
use primitives::{AccountID, Hash, BlockNumber, Proposal};
|
||||
use runtime::{staking, system, session};
|
||||
|
||||
/*macro_rules! db_name {
|
||||
( $( $name:ident )+ ) => concat!("gov:", $( stringify!($name) ),+ )
|
||||
}*/
|
||||
|
||||
const APPROVALS_REQUIRED: &[u8] = b"gov:apr";
|
||||
const CURRENT_PROPOSAL: &[u8] = b"gov:pro";
|
||||
const APPROVAL_OF: &[u8] = b"gov:app:";
|
||||
|
||||
/// The proportion of validators required for a propsal to be approved measured as the number out
|
||||
/// of 1000.
|
||||
pub fn approval_ppm_required() -> u32 {
|
||||
storage::get_or(b"gov:apr", 1000)
|
||||
storage::get_or(APPROVALS_REQUIRED, 1000)
|
||||
}
|
||||
|
||||
/// The number of concrete validator approvals required for a proposal to pass.
|
||||
@@ -49,10 +57,10 @@ pub mod public {
|
||||
/// Proposal is by the `transactor` and will automatically count as an approval. Transactor must
|
||||
/// be a current validator. It is illegal to propose when there is already a proposal in effect.
|
||||
pub fn propose(validator: &AccountID, proposal: &Proposal) {
|
||||
if storage::exists(b"gov:pro") {
|
||||
if storage::exists(CURRENT_PROPOSAL) {
|
||||
panic!("there may only be one proposal per era.");
|
||||
}
|
||||
storage::put(b"gov:pro", proposal);
|
||||
storage::put(CURRENT_PROPOSAL, proposal);
|
||||
approve(validator, staking::current_era());
|
||||
}
|
||||
|
||||
@@ -62,13 +70,13 @@ pub mod public {
|
||||
if era_index != staking::current_era() {
|
||||
panic!("approval vote applied on non-current era.")
|
||||
}
|
||||
if !storage::exists(b"gov:pro") {
|
||||
if !storage::exists(CURRENT_PROPOSAL) {
|
||||
panic!("there must be a proposal in order to approve.");
|
||||
}
|
||||
if session::validators().into_iter().position(|v| &v == validator).is_none() {
|
||||
panic!("transactor must be a validator to approve.");
|
||||
}
|
||||
let key = validator.to_keyed_vec(b"gov:app:");
|
||||
let key = validator.to_keyed_vec(APPROVAL_OF);
|
||||
if storage::exists(&key) {
|
||||
panic!("transactor may not approve a proposal twice in one era.");
|
||||
}
|
||||
@@ -84,7 +92,7 @@ pub mod privileged {
|
||||
/// validator. `1000` would require the approval of all validators; `667` would require two-thirds
|
||||
/// (or there abouts) of validators.
|
||||
pub fn set_approval_ppm_required(ppm: u32) {
|
||||
storage::put(b"gov:apr", &ppm);
|
||||
storage::put(APPROVALS_REQUIRED, &ppm);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,10 +102,10 @@ pub mod internal {
|
||||
/// Current era is ending; we should finish up any proposals.
|
||||
pub fn end_of_an_era() {
|
||||
// tally up votes for the current proposal, if any. enact if there are sufficient approvals.
|
||||
if let Some(proposal) = storage::take::<Proposal>(b"gov:pro") {
|
||||
if let Some(proposal) = storage::take::<Proposal>(CURRENT_PROPOSAL) {
|
||||
let approvals_required = approvals_required();
|
||||
let approved = session::validators().into_iter()
|
||||
.filter_map(|v| storage::take::<bool>(&v.to_keyed_vec(b"gov:app:")))
|
||||
.filter_map(|v| storage::take::<bool>(&v.to_keyed_vec(APPROVAL_OF)))
|
||||
.take(approvals_required as usize)
|
||||
.count() as u32;
|
||||
if approved == approvals_required {
|
||||
@@ -122,7 +130,7 @@ mod tests {
|
||||
let three = [3u8; 32];
|
||||
|
||||
TestExternalities { storage: map![
|
||||
twox_128(b"gov:apr").to_vec() => vec![].join(&667u32),
|
||||
twox_128(APPROVALS_REQUIRED).to_vec() => vec![].join(&667u32),
|
||||
twox_128(b"ses:len").to_vec() => vec![].join(&1u64),
|
||||
twox_128(b"ses:val:len").to_vec() => vec![].join(&3u32),
|
||||
twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => one.to_vec(),
|
||||
|
||||
@@ -30,7 +30,7 @@ pub fn validators() -> Vec<AccountID> {
|
||||
|
||||
/// The number of blocks in each session.
|
||||
pub fn length() -> BlockNumber {
|
||||
storage::get_or(b"ses:len", 0)
|
||||
storage::get_or(SESSION_LENGTH, 0)
|
||||
}
|
||||
|
||||
/// The number of validators currently.
|
||||
@@ -40,12 +40,12 @@ pub fn validator_count() -> usize {
|
||||
|
||||
/// The current era index.
|
||||
pub fn current_index() -> BlockNumber {
|
||||
storage::get_or(b"ses:ind", 0)
|
||||
storage::get_or(CURRENT_INDEX, 0)
|
||||
}
|
||||
|
||||
/// The block number at which the era length last changed.
|
||||
pub fn last_length_change() -> BlockNumber {
|
||||
storage::get_or(b"ses:llc", 0)
|
||||
storage::get_or(LAST_LENGTH_CHANGE, 0)
|
||||
}
|
||||
|
||||
pub mod public {
|
||||
@@ -55,7 +55,7 @@ pub mod public {
|
||||
/// session.
|
||||
pub fn set_key(validator: &AccountID, key: &SessionKey) {
|
||||
// set new value for next session
|
||||
storage::put(&validator.to_keyed_vec(b"ses:nxt:"), key);
|
||||
storage::put(&validator.to_keyed_vec(NEXT_KEY_FOR), key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ pub mod privileged {
|
||||
|
||||
/// Set a new era length. Won't kick in until the next era change (at current length).
|
||||
pub fn set_length(new: BlockNumber) {
|
||||
storage::put(b"ses:nln", &new);
|
||||
storage::put(NEXT_SESSION_LENGTH, &new);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +93,12 @@ pub mod internal {
|
||||
}
|
||||
}
|
||||
|
||||
const SESSION_LENGTH: &[u8] = b"ses:len";
|
||||
const CURRENT_INDEX: &[u8] = b"ses:ind";
|
||||
const LAST_LENGTH_CHANGE: &[u8] = b"ses:llc";
|
||||
const NEXT_KEY_FOR: &[u8] = b"ses:nxt:";
|
||||
const NEXT_SESSION_LENGTH: &[u8] = b"ses:nln";
|
||||
|
||||
struct ValidatorStorageVec {}
|
||||
impl StorageVec for ValidatorStorageVec {
|
||||
type Item = AccountID;
|
||||
@@ -102,18 +108,18 @@ impl StorageVec for ValidatorStorageVec {
|
||||
/// Move onto next session: register the new authority set.
|
||||
fn rotate_session() {
|
||||
// Increment current session index.
|
||||
storage::put(b"ses:ind", &(current_index() + 1));
|
||||
storage::put(CURRENT_INDEX, &(current_index() + 1));
|
||||
|
||||
// Enact era length change.
|
||||
if let Some(next_len) = storage::get::<u64>(b"ses:nln") {
|
||||
storage::put(b"ses:len", &next_len);
|
||||
storage::put(b"ses:llc", &system::block_number());
|
||||
storage::kill(b"ses:nln");
|
||||
if let Some(next_len) = storage::get::<u64>(NEXT_SESSION_LENGTH) {
|
||||
storage::put(SESSION_LENGTH, &next_len);
|
||||
storage::put(LAST_LENGTH_CHANGE, &system::block_number());
|
||||
storage::kill(NEXT_SESSION_LENGTH);
|
||||
}
|
||||
|
||||
// Update any changes in session keys.
|
||||
validators().iter().enumerate().for_each(|(i, v)| {
|
||||
let k = v.to_keyed_vec(b"ses:nxt:");
|
||||
let k = v.to_keyed_vec(NEXT_KEY_FOR);
|
||||
if let Some(n) = storage::take(&k) {
|
||||
consensus::internal::set_authority(i as u32, &n);
|
||||
}
|
||||
@@ -134,11 +140,11 @@ mod tests {
|
||||
|
||||
fn simple_setup() -> TestExternalities {
|
||||
TestExternalities { storage: map![
|
||||
twox_128(b"ses:len").to_vec() => vec![].join(&2u64),
|
||||
twox_128(SESSION_LENGTH).to_vec() => vec![].join(&2u64),
|
||||
// the validators (10, 20, ...)
|
||||
twox_128(b"ses:val:len").to_vec() => vec![].join(&2u32),
|
||||
twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![10; 32],
|
||||
twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![20; 32],
|
||||
twox_128(&0u32.to_keyed_vec(ValidatorStorageVec::PREFIX)).to_vec() => vec![10; 32],
|
||||
twox_128(&1u32.to_keyed_vec(ValidatorStorageVec::PREFIX)).to_vec() => vec![20; 32],
|
||||
// initial session keys (11, 21, ...)
|
||||
twox_128(b"con:aut:len").to_vec() => vec![].join(&2u32),
|
||||
twox_128(&0u32.to_keyed_vec(b"con:aut:")).to_vec() => vec![11; 32],
|
||||
|
||||
@@ -31,12 +31,12 @@ pub type Bondage = u64;
|
||||
|
||||
/// The length of the bonding duration in eras.
|
||||
pub fn bonding_duration() -> BlockNumber {
|
||||
storage::get_default(b"sta:loc")
|
||||
storage::get_default(BONDING_DURATION)
|
||||
}
|
||||
|
||||
/// The length of a staking era in sessions.
|
||||
pub fn validator_count() -> usize {
|
||||
storage::get_default::<u32>(b"sta:vac") as usize
|
||||
storage::get_default::<u32>(VALIDATOR_COUNT) as usize
|
||||
}
|
||||
|
||||
/// The length of a staking era in blocks.
|
||||
@@ -46,27 +46,27 @@ pub fn era_length() -> BlockNumber {
|
||||
|
||||
/// The length of a staking era in sessions.
|
||||
pub fn sessions_per_era() -> BlockNumber {
|
||||
storage::get_default(b"sta:spe")
|
||||
storage::get_default(SESSIONS_PER_ERA)
|
||||
}
|
||||
|
||||
/// The current era index.
|
||||
pub fn current_era() -> BlockNumber {
|
||||
storage::get_default(b"sta:era")
|
||||
storage::get_default(CURRENT_ERA)
|
||||
}
|
||||
|
||||
/// The block number at which the era length last changed.
|
||||
pub fn last_era_length_change() -> BlockNumber {
|
||||
storage::get_default(b"sta:lec")
|
||||
storage::get_default(LAST_ERA_LENGTH_CHANGE)
|
||||
}
|
||||
|
||||
/// The balance of a given account.
|
||||
pub fn balance(who: &AccountID) -> Balance {
|
||||
storage::get_default(&who.to_keyed_vec(b"sta:bal:"))
|
||||
storage::get_default(&who.to_keyed_vec(BALANCE_OF))
|
||||
}
|
||||
|
||||
/// The liquidity-state of a given account.
|
||||
pub fn bondage(who: &AccountID) -> Bondage {
|
||||
storage::get_default(&who.to_keyed_vec(b"sta:bon:"))
|
||||
storage::get_default(&who.to_keyed_vec(BONDAGE_OF))
|
||||
}
|
||||
|
||||
// Each identity's stake may be in one of three bondage states, given by an integer:
|
||||
@@ -80,10 +80,10 @@ pub mod public {
|
||||
|
||||
/// Transfer some unlocked staking balance to another staker.
|
||||
pub fn transfer(transactor: &AccountID, dest: &AccountID, value: Balance) {
|
||||
let from_key = transactor.to_keyed_vec(b"sta:bal:");
|
||||
let from_key = transactor.to_keyed_vec(BALANCE_OF);
|
||||
let from_balance = storage::get_default::<Balance>(&from_key);
|
||||
assert!(from_balance >= value);
|
||||
let to_key = dest.to_keyed_vec(b"sta:bal:");
|
||||
let to_key = dest.to_keyed_vec(BALANCE_OF);
|
||||
let to_balance: Balance = storage::get_default(&to_key);
|
||||
assert!(bondage(transactor) <= bondage(dest));
|
||||
assert!(to_balance + value > to_balance); // no overflow
|
||||
@@ -100,7 +100,7 @@ pub mod public {
|
||||
assert!(intentions.iter().find(|t| *t == transactor).is_none(), "Cannot stake if already staked.");
|
||||
intentions.push(transactor.clone());
|
||||
IntentionStorageVec::set_items(&intentions);
|
||||
storage::put(&transactor.to_keyed_vec(b"sta:bon:"), &u64::max_value());
|
||||
storage::put(&transactor.to_keyed_vec(BONDAGE_OF), &u64::max_value());
|
||||
}
|
||||
|
||||
/// Retract the desire to stake for the transactor.
|
||||
@@ -114,7 +114,7 @@ pub mod public {
|
||||
panic!("Cannot unstake if not already staked.");
|
||||
}
|
||||
IntentionStorageVec::set_items(&intentions);
|
||||
storage::put(&transactor.to_keyed_vec(b"sta:bon:"), &(current_era() + bonding_duration()));
|
||||
storage::put(&transactor.to_keyed_vec(BONDAGE_OF), &(current_era() + bonding_duration()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,17 +123,17 @@ pub mod privileged {
|
||||
|
||||
/// Set the number of sessions in an era.
|
||||
pub fn set_sessions_per_era(new: BlockNumber) {
|
||||
storage::put(b"sta:nse", &new);
|
||||
storage::put(NEXT_SESSIONS_PER_ERA, &new);
|
||||
}
|
||||
|
||||
/// The length of the bonding duration in eras.
|
||||
pub fn set_bonding_duration(new: BlockNumber) {
|
||||
storage::put(b"sta:loc", &new);
|
||||
storage::put(BONDING_DURATION, &new);
|
||||
}
|
||||
|
||||
/// The length of a staking era in sessions.
|
||||
pub fn set_validator_count(new: usize) {
|
||||
storage::put(b"sta:vac", &(new as u32));
|
||||
storage::put(VALIDATOR_COUNT, &(new as u32));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,6 +155,15 @@ impl StorageVec for IntentionStorageVec {
|
||||
const PREFIX: &'static[u8] = b"sta:wil:";
|
||||
}
|
||||
|
||||
const BONDING_DURATION: &[u8] = b"sta:loc";
|
||||
const VALIDATOR_COUNT: &[u8] = b"sta:vac";
|
||||
const SESSIONS_PER_ERA: &[u8] = b"sta:spe";
|
||||
const NEXT_SESSIONS_PER_ERA: &[u8] = b"sta:nse";
|
||||
const CURRENT_ERA: &[u8] = b"sta:era";
|
||||
const LAST_ERA_LENGTH_CHANGE: &[u8] = b"sta:lec";
|
||||
const BALANCE_OF: &[u8] = b"sta:bal:";
|
||||
const BONDAGE_OF: &[u8] = b"sta:bon:";
|
||||
|
||||
/// The era has changed - enact new staking set.
|
||||
///
|
||||
/// NOTE: This always happens immediately before a session change to ensure that new validators
|
||||
@@ -164,13 +173,13 @@ fn new_era() {
|
||||
governance::internal::end_of_an_era();
|
||||
|
||||
// Increment current era.
|
||||
storage::put(b"sta:era", &(current_era() + 1));
|
||||
storage::put(CURRENT_ERA, &(current_era() + 1));
|
||||
|
||||
// Enact era length change.
|
||||
let next_spe: u64 = storage::get_default(b"sta:nse");
|
||||
let next_spe: u64 = storage::get_default(NEXT_SESSIONS_PER_ERA);
|
||||
if next_spe > 0 && next_spe != sessions_per_era() {
|
||||
storage::put(b"sta:spe", &next_spe);
|
||||
storage::put(b"sta:lec", &system::block_number());
|
||||
storage::put(SESSIONS_PER_ERA, &next_spe);
|
||||
storage::put(LAST_ERA_LENGTH_CHANGE, &system::block_number());
|
||||
}
|
||||
|
||||
// evaluate desired staking amounts and nominations and optimise to find the best
|
||||
@@ -215,13 +224,13 @@ mod tests {
|
||||
twox_128(b"ses:val:len").to_vec() => vec![].join(&2u32),
|
||||
twox_128(&0u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![10; 32],
|
||||
twox_128(&1u32.to_keyed_vec(b"ses:val:")).to_vec() => vec![20; 32],
|
||||
twox_128(b"sta:spe").to_vec() => vec![].join(&2u64),
|
||||
twox_128(b"sta:vac").to_vec() => vec![].join(&2u32),
|
||||
twox_128(b"sta:loc").to_vec() => vec![].join(&3u64),
|
||||
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&10u64),
|
||||
twox_128(&two.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&20u64),
|
||||
twox_128(&three.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&30u64),
|
||||
twox_128(&four.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&40u64)
|
||||
twox_128(SESSIONS_PER_ERA).to_vec() => vec![].join(&2u64),
|
||||
twox_128(VALIDATOR_COUNT).to_vec() => vec![].join(&2u32),
|
||||
twox_128(BONDING_DURATION).to_vec() => vec![].join(&3u64),
|
||||
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&10u64),
|
||||
twox_128(&two.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&20u64),
|
||||
twox_128(&three.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&30u64),
|
||||
twox_128(&four.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&40u64)
|
||||
], };
|
||||
|
||||
with_externalities(&mut t, || {
|
||||
@@ -281,7 +290,7 @@ mod tests {
|
||||
fn staking_eras_work() {
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(b"ses:len").to_vec() => vec![].join(&1u64),
|
||||
twox_128(b"sta:spe").to_vec() => vec![].join(&2u64)
|
||||
twox_128(SESSIONS_PER_ERA).to_vec() => vec![].join(&2u64)
|
||||
], };
|
||||
with_externalities(&mut t, || {
|
||||
assert_eq!(era_length(), 2u64);
|
||||
@@ -347,7 +356,7 @@ mod tests {
|
||||
let two = two();
|
||||
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&42u64)
|
||||
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&42u64)
|
||||
], };
|
||||
|
||||
with_externalities(&mut t, || {
|
||||
@@ -362,7 +371,7 @@ mod tests {
|
||||
let two = two();
|
||||
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&111u64)
|
||||
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&111u64)
|
||||
], };
|
||||
|
||||
with_externalities(&mut t, || {
|
||||
@@ -379,7 +388,7 @@ mod tests {
|
||||
let two = two();
|
||||
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(&one.to_keyed_vec(b"sta:bal:")).to_vec() => vec![].join(&111u64)
|
||||
twox_128(&one.to_keyed_vec(BALANCE_OF)).to_vec() => vec![].join(&111u64)
|
||||
], };
|
||||
|
||||
with_externalities(&mut t, || {
|
||||
|
||||
@@ -31,7 +31,7 @@ pub fn block_number() -> BlockNumber {
|
||||
|
||||
/// Get the block hash of a given block (uses storage).
|
||||
pub fn block_hash(number: BlockNumber) -> Hash {
|
||||
storage::get_default(&number.to_keyed_vec(b"sys:old:"))
|
||||
storage::get_default(&number.to_keyed_vec(BLOCK_HASH_AT))
|
||||
}
|
||||
|
||||
pub mod privileged {
|
||||
@@ -39,7 +39,7 @@ pub mod privileged {
|
||||
|
||||
/// Set the new code.
|
||||
pub fn set_code(new: &[u8]) {
|
||||
storage::put_raw(b":code", new);
|
||||
storage::put_raw(CODE, new);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ pub mod internal {
|
||||
// so will wait until a little later.
|
||||
|
||||
// store the header hash in storage.
|
||||
let header_hash_key = header.number.to_keyed_vec(b"sys:old:");
|
||||
let header_hash_key = header.number.to_keyed_vec(BLOCK_HASH_AT);
|
||||
storage::put(&header_hash_key, &header.blake2_256());
|
||||
|
||||
// execute transactions
|
||||
@@ -119,6 +119,9 @@ fn final_checks(_block: &Block) {
|
||||
});
|
||||
}
|
||||
|
||||
const BLOCK_HASH_AT: &[u8] = b"sys:old:";
|
||||
const CODE: &[u8] = b"sys:cod";
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -23,7 +23,7 @@ pub type Timestamp = u64;
|
||||
|
||||
/// Get the current time.
|
||||
pub fn get() -> Timestamp {
|
||||
storage::get_default(b"tim:val")
|
||||
storage::get_default(CURRENT_TIMESTAMP)
|
||||
}
|
||||
|
||||
pub mod public {
|
||||
@@ -31,10 +31,12 @@ pub mod public {
|
||||
|
||||
/// Set the current time.
|
||||
pub fn set(now: Timestamp) {
|
||||
storage::put(b"tim:val", &now);
|
||||
storage::put(CURRENT_TIMESTAMP, &now);
|
||||
}
|
||||
}
|
||||
|
||||
const CURRENT_TIMESTAMP: &[u8] = b"tim:val";
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -48,7 +50,7 @@ mod tests {
|
||||
#[test]
|
||||
fn timestamp_works() {
|
||||
let mut t = TestExternalities { storage: map![
|
||||
twox_128(b"tim:val").to_vec() => vec![].join(&42u64)
|
||||
twox_128(CURRENT_TIMESTAMP).to_vec() => vec![].join(&42u64)
|
||||
], };
|
||||
|
||||
with_externalities(&mut t, || {
|
||||
|
||||
Reference in New Issue
Block a user