From 62f3bee9c1ff0a1a0b3753e7f3e7205dd742ae1d Mon Sep 17 00:00:00 2001 From: Gav <gavin@parity.io> Date: Sat, 10 Mar 2018 19:23:10 +0100 Subject: [PATCH] Public function dispatching. --- substrate/demo/primitives/src/transaction.rs | 96 +++++++++++++--- substrate/demo/runtime/src/runtime/council.rs | 105 ++++++++++-------- substrate/demo/runtime/src/runtime/system.rs | 33 ++++++ 3 files changed, 169 insertions(+), 65 deletions(-) diff --git a/substrate/demo/primitives/src/transaction.rs b/substrate/demo/primitives/src/transaction.rs index 3af7dbdcf4d..e75b67d7457 100644 --- a/substrate/demo/primitives/src/transaction.rs +++ b/substrate/demo/primitives/src/transaction.rs @@ -18,6 +18,7 @@ use rstd::vec::Vec; use codec::{Input, Slicable}; +use {AccountId, SessionKey}; #[cfg(feature = "std")] use std::fmt; @@ -153,16 +154,27 @@ impl Slicable for Proposal { #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] #[repr(u8)] enum FunctionId { - /// Set the timestamp. TimestampSet = 0x00, - /// Set temporary session key as a validator. + SessionSetKey = 0x10, - /// Staking subsystem: begin staking. + StakingStake = 0x20, - /// Staking subsystem: stop staking. StakingUnstake = 0x21, - /// Staking subsystem: transfer stake. StakingTransfer = 0x22, + + CouncilVotePropose = 0x30, + CouncilVoteVote = 0x31, + CouncilVoteVeto = 0x32, + + CouncilSetApprovals = 0x40, + CouncilReapInactiveVoter = 0x41, + CouncilRetractVoter = 0x42, + CouncilSubmitCandidacy = 0x43, + CouncilPresentWinner = 0x44, + + DemocracyPropose = 0x50, + DemocracySecond = 0x51, + DemocracyVote = 0x52, } impl FunctionId { @@ -179,17 +191,29 @@ impl FunctionId { /// Functions on the runtime. #[derive(Clone, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))] +#[allow(missing_docs)] pub enum Function { - /// Set the timestamp. TimestampSet(u64), - /// Set temporary session key as a validator. - SessionSetKey(::SessionKey), - /// Staking subsystem: begin staking. + + SessionSetKey(SessionKey), + StakingStake, - /// Staking subsystem: stop staking. StakingUnstake, - /// Staking subsystem: transfer stake. - StakingTransfer(::AccountId, u64), + StakingTransfer(AccountId, u64), + + CouncilVotePropose(Proposal), + CouncilVoteVote([u8; 32], bool), + CouncilVoteVeto([u8; 32]), + + CouncilSetApprovals(Vec<bool>, u32), + CouncilReapInactiveVoter(u32, AccountId, u32, u32), + CouncilRetractVoter(u32), + CouncilSubmitCandidacy(u32), + CouncilPresentWinner(AccountId, u64, u32), + + DemocracyPropose(Proposal, u64), + DemocracySecond(u32), + DemocracyVote(u32, bool), } impl Slicable for Function { @@ -197,17 +221,54 @@ impl Slicable for Function { let id = try_opt!(u8::decode(input).and_then(FunctionId::from_u8)); Some(match id { FunctionId::TimestampSet => - Function::TimestampSet(try_opt!(Slicable::decode(input))), + Function::TimestampSet(Slicable::decode(input)?), FunctionId::SessionSetKey => - Function::SessionSetKey(try_opt!(Slicable::decode(input))), + Function::SessionSetKey(Slicable::decode(input)?), FunctionId::StakingStake => Function::StakingStake, FunctionId::StakingUnstake => Function::StakingUnstake, FunctionId::StakingTransfer => { - let to = try_opt!(Slicable::decode(input)); - let amount = try_opt!(Slicable::decode(input)); - + let to = Slicable::decode(input)?; + let amount = Slicable::decode(input)?; Function::StakingTransfer(to, amount) } + FunctionId::CouncilVotePropose => Function::CouncilVotePropose(Slicable::decode(input)?), + FunctionId::CouncilVoteVote => { + let a = Slicable::decode(input)?; + let b = Slicable::decode(input)?; + Function::CouncilVoteVote(a, b) + } + FunctionId::CouncilVoteVeto => Function::CouncilVoteVeto(Slicable::decode(input)?), + FunctionId::CouncilSetApprovals => { + let a = Slicable::decode(input)?; + let b = Slicable::decode(input)?; + Function::CouncilSetApprovals(a, b) + } + FunctionId::CouncilReapInactiveVoter => { + let a = Slicable::decode(input)?; + let b = Slicable::decode(input)?; + let c = Slicable::decode(input)?; + let d = Slicable::decode(input)?; + Function::CouncilReapInactiveVoter(a, b, c, d) + } + FunctionId::CouncilRetractVoter => Function::CouncilRetractVoter(Slicable::decode(input)?), + FunctionId::CouncilSubmitCandidacy => Function::CouncilSubmitCandidacy(Slicable::decode(input)?), + FunctionId::CouncilPresentWinner => { + let a = Slicable::decode(input)?; + let b = Slicable::decode(input)?; + let c = Slicable::decode(input)?; + Function::CouncilPresentWinner(a, b, c) + } + FunctionId::DemocracyPropose => { + let a = Slicable::decode(input)?; + let b = Slicable::decode(input)?; + Function::DemocracyPropose(a, b) + } + FunctionId::DemocracySecond => Function::DemocracySecond(Slicable::decode(input)?), + FunctionId::DemocracyVote => { + let a = Slicable::decode(input)?; + let b = Slicable::decode(input)?; + Function::DemocracyVote(a, b) + } }) } @@ -233,6 +294,7 @@ impl Slicable for Function { to.using_encoded(|s| v.extend(s)); amount.using_encoded(|s| v.extend(s)); } + _ => {} } v diff --git a/substrate/demo/runtime/src/runtime/council.rs b/substrate/demo/runtime/src/runtime/council.rs index a9e217ac97c..1a6ad815f90 100644 --- a/substrate/demo/runtime/src/runtime/council.rs +++ b/substrate/demo/runtime/src/runtime/council.rs @@ -258,6 +258,11 @@ pub fn leaderboard() -> Option<Vec<(Balance, AccountId)>> { pub mod public { use super::*; + // CouncilVotePropose(AccountId, Proposal) + // CouncilVoteVote(AccountId, [u8; 32], bool) + // CouncilVoteVeto(AccountId, [u8; 32]) + // CouncilSetApprovals(AccountId, Vec<bool>, u32) + // CouncilReapInactiveVoter(AccountId, u32, AccountId, u32, u32) /// Set candidate approvals. Approval slots stay valid as long as candidates in those slots /// are registered. pub fn set_approvals(signed: &AccountId, votes: &Vec<bool>, index: VoteIndex) { @@ -354,10 +359,14 @@ pub mod public { storage::put(&signed.to_keyed_vec(REGISTER_INFO_OF), &(vote_index(), slot)); } + // CouncilRetractVoter(AccountId, u32) + // CouncilSubmitCandidacy(AccountId, u32) + // CouncilPresent(AccountId, u32) + /// Claim that `signed` is one of the top carry_count() + current_vote().1 candidates. /// Only works if the block number >= current_vote().0 and < current_vote().0 + presentation_duration() /// `signed` should have at least - pub fn present(signed: &AccountId, candidate: &AccountId, total: Balance, index: VoteIndex) { + pub fn present_winner(signed: &AccountId, candidate: &AccountId, total: Balance, index: VoteIndex) { assert_eq!(index, vote_index(), "index not current"); let (_, _, expiring): (BlockNumber, u32, Vec<AccountId>) = storage::get(NEXT_FINALISE) .expect("cannot present outside of presentation period"); @@ -881,8 +890,8 @@ mod tests { with_env(|e| e.block_number = 6); assert!(presentation_active()); - public::present(&Dave, &Bob, 11, 0); - public::present(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Eve, 41, 0); assert_eq!(leaderboard(), Some(vec![(0, AccountId::default()), (0, AccountId::default()), (11, Bob.into()), (41, Eve.into())])); internal::end_block(); @@ -911,9 +920,9 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); - public::present(&Dave, &Eve, 41, 0); - public::present(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Eve, 41, 0); internal::end_block(); assert_eq!(active_council(), vec![(Eve.to_raw_public(), 11), (Bob.into(), 11)]); @@ -930,7 +939,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Bob, 11, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -939,7 +948,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Eve, 41, 1); + public::present_winner(&Dave, &Eve, 41, 1); internal::end_block(); public::reap_inactive_voter( @@ -965,7 +974,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Bob, 11, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -974,7 +983,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Bob, 11, 1); + public::present_winner(&Dave, &Bob, 11, 1); }); } @@ -987,7 +996,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Bob, 11, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -996,7 +1005,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Eve, 41, 1); + public::present_winner(&Dave, &Eve, 41, 1); internal::end_block(); with_env(|e| e.block_number = 11); @@ -1025,7 +1034,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 8, 0); + public::present_winner(&Dave, &Bob, 8, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -1034,7 +1043,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Eve, 38, 1); + public::present_winner(&Dave, &Eve, 38, 1); internal::end_block(); public::reap_inactive_voter( @@ -1055,7 +1064,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 8, 0); + public::present_winner(&Dave, &Bob, 8, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -1064,7 +1073,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Eve, 38, 1); + public::present_winner(&Dave, &Eve, 38, 1); internal::end_block(); public::reap_inactive_voter( @@ -1090,10 +1099,10 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); - public::present(&Dave, &Charlie, 21, 0); - public::present(&Dave, &Dave, 31, 0); - public::present(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Charlie, 21, 0); + public::present_winner(&Dave, &Dave, 31, 0); + public::present_winner(&Dave, &Eve, 41, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -1101,8 +1110,8 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Bob, 11, 1); - public::present(&Dave, &Charlie, 21, 1); + public::present_winner(&Dave, &Bob, 11, 1); + public::present_winner(&Dave, &Charlie, 21, 1); internal::end_block(); public::reap_inactive_voter( @@ -1127,7 +1136,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Bob, 11, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -1136,7 +1145,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Eve, 41, 1); + public::present_winner(&Dave, &Eve, 41, 1); internal::end_block(); public::reap_inactive_voter( @@ -1165,11 +1174,11 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Alice, 60, 0); - public::present(&Dave, &Charlie, 21, 0); - public::present(&Dave, &Dave, 31, 0); - public::present(&Dave, &Eve, 41, 0); - public::present(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Alice, 60, 0); + public::present_winner(&Dave, &Charlie, 21, 0); + public::present_winner(&Dave, &Dave, 31, 0); + public::present_winner(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Bob, 11, 0); }); } @@ -1190,11 +1199,11 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 0); - public::present(&Dave, &Alice, 60, 0); - public::present(&Dave, &Charlie, 21, 0); - public::present(&Dave, &Dave, 31, 0); - public::present(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Bob, 11, 0); + public::present_winner(&Dave, &Alice, 60, 0); + public::present_winner(&Dave, &Charlie, 21, 0); + public::present_winner(&Dave, &Dave, 31, 0); + public::present_winner(&Dave, &Eve, 41, 0); assert_eq!(leaderboard(), Some(vec![ (21, Charlie.into()), @@ -1211,7 +1220,7 @@ mod tests { with_externalities(&mut new_test_ext(), || { with_env(|e| e.block_number = 4); assert!(!presentation_active()); - public::present(&Eve, &Eve, 1, 0); + public::present_winner(&Eve, &Eve, 1, 0); }); } @@ -1227,7 +1236,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 11, 1); + public::present_winner(&Dave, &Bob, 11, 1); }); } @@ -1246,7 +1255,7 @@ mod tests { with_env(|e| e.block_number = 6); assert_eq!(staking::balance(&Alice), 1); - public::present(&Alice, &Alice, 30, 0); + public::present_winner(&Alice, &Alice, 30, 0); }); } @@ -1264,7 +1273,7 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Bob, 80, 0); + public::present_winner(&Dave, &Bob, 80, 0); assert_eq!(staking::balance(&Dave), 38); }); @@ -1291,16 +1300,16 @@ mod tests { with_env(|e| e.block_number = 6); assert!(presentation_active()); - public::present(&Dave, &Alice, 60, 0); + public::present_winner(&Dave, &Alice, 60, 0); assert_eq!(leaderboard(), Some(vec![ (0, AccountId::default()), (0, AccountId::default()), (0, AccountId::default()), (60, Alice.to_raw_public()) ])); - public::present(&Dave, &Charlie, 21, 0); - public::present(&Dave, &Dave, 31, 0); - public::present(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Charlie, 21, 0); + public::present_winner(&Dave, &Dave, 31, 0); + public::present_winner(&Dave, &Eve, 41, 0); assert_eq!(leaderboard(), Some(vec![ (21, Charlie.into()), (31, Dave.into()), @@ -1346,10 +1355,10 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 6); - public::present(&Dave, &Alice, 60, 0); - public::present(&Dave, &Charlie, 21, 0); - public::present(&Dave, &Dave, 31, 0); - public::present(&Dave, &Eve, 41, 0); + public::present_winner(&Dave, &Alice, 60, 0); + public::present_winner(&Dave, &Charlie, 21, 0); + public::present_winner(&Dave, &Dave, 31, 0); + public::present_winner(&Dave, &Eve, 41, 0); internal::end_block(); with_env(|e| e.block_number = 8); @@ -1358,8 +1367,8 @@ mod tests { internal::end_block(); with_env(|e| e.block_number = 10); - public::present(&Dave, &Charlie, 81, 1); - public::present(&Dave, &Dave, 31, 1); + public::present_winner(&Dave, &Charlie, 81, 1); + public::present_winner(&Dave, &Dave, 31, 1); internal::end_block(); assert!(!presentation_active()); diff --git a/substrate/demo/runtime/src/runtime/system.rs b/substrate/demo/runtime/src/runtime/system.rs index ede71a69d18..8cb06c34bec 100644 --- a/substrate/demo/runtime/src/runtime/system.rs +++ b/substrate/demo/runtime/src/runtime/system.rs @@ -144,6 +144,39 @@ pub mod internal { Function::TimestampSet(t) => { ::runtime::timestamp::public::set(t); } + Function::CouncilVotePropose(ref a) => { + ::runtime::council_vote::public::propose(transactor, a); + } + Function::CouncilVoteVote(ref a, b) => { + ::runtime::council_vote::public::vote(transactor, a, b); + } + Function::CouncilVoteVeto(ref a) => { + ::runtime::council_vote::public::veto(transactor, a); + } + Function::CouncilSetApprovals(ref a, b) => { + ::runtime::council::public::set_approvals(transactor, a, b); + } + Function::CouncilReapInactiveVoter(a, ref b, c, d) => { + ::runtime::council::public::reap_inactive_voter(transactor, a, b, c, d); + } + Function::CouncilRetractVoter(a) => { + ::runtime::council::public::retract_voter(transactor, a); + } + Function::CouncilSubmitCandidacy(a) => { + ::runtime::council::public::submit_candidacy(transactor, a); + } + Function::CouncilPresentWinner(ref a, b, c) => { + ::runtime::council::public::present_winner(transactor, a, b, c); + } + Function::DemocracyPropose(ref a, b) => { + ::runtime::democracy::public::propose(transactor, a, b); + } + Function::DemocracySecond(a) => { + ::runtime::democracy::public::second(transactor, a); + } + Function::DemocracyVote(a, b) => { + ::runtime::democracy::public::vote(transactor, a, b); + } } } } -- GitLab