Skip to content

Commit

Permalink
Select random investor
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Oct 25, 2023
1 parent b394bde commit bbdf4ea
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 9 deletions.
50 changes: 46 additions & 4 deletions Cargo.lock

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

44 changes: 43 additions & 1 deletion pallets/bidding/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
pub use super::*;
pub use sp_runtime::{BoundedVec,Percent};
pub use frame_support::pallet_prelude::ConstU32;
pub use frame_support::{
dispatch::{DispatchResult, GetDispatchInfo},
ensure,
pallet_prelude::MaxEncodedLen,
traits::{Currency, ExistenceRequirement::KeepAlive, Get, Randomness, ReservableCurrency},
PalletId,
};
pub use Onboarding::Zero;
pub use pallet_roles::vec;

Expand Down Expand Up @@ -36,6 +42,42 @@ impl<T: Config> Pallet<T> {
}


/// Randomly choose an investor from among an investors list.
/// Returns `None` if there are no investors in the list.
fn choose_ticket(mut investors: Vec<AccountIdOf<T>>) -> (Option<AccountIdOf<T>>,usize) {
let total = investors.len() as u32;
if total == 0 {
return (None,0)
}
let mut random_number = Self::generate_random_number(0);

// Best effort attempt to remove bias from modulus operator.
for i in 1..T::MaxGenerateRandom::get() {
if random_number < u32::MAX - u32::MAX % total && ( 0..total-1).contains(&(random_number%total)) {
break
}

random_number = Self::generate_random_number(i);
}
let num = random_number % total;
let inv = investors[num as usize].clone();
(Some(inv),num as usize)
}


/// Generate a random number from a given seed.
/// Note that there is potential bias introduced by using modulus operator.
/// You should call this function with different seed values until the random
/// number lies within `u32::MAX - u32::MAX % n`.
/// TODO: deal with randomness freshness
/// https://github.com/paritytech/substrate/issues/8311
fn generate_random_number(seed: u32) -> u32 {
let (random_seed, _) = T::Randomness::random(&(T::PalletId::get(), seed).encode());
let random_number = <u32>::decode(&mut random_seed.as_ref())
.expect("secure hashes should always be bigger than u32; qed");
random_number
}


}

7 changes: 6 additions & 1 deletion pallets/bidding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub use pallet::*;
//pub use weights::*;
mod functions;
mod types;
pub use functions::*;
pub use types::*;
pub use pallet_onboarding as Onboarding;
pub use pallet_roles as Roles;
pub use pallet_housing_fund as Houses;
Expand Down Expand Up @@ -50,7 +52,10 @@ pub mod pallet {
+ Share::Config {
/// The overarching runtime event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Something that provides randomness in the runtime.
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;
#[pallet::constant]
type MaxGenerateRandom: Get<u32>;
}

/// A storage item for this pallet.
Expand Down
9 changes: 6 additions & 3 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pallet-assets = { git = "https://github.com/paritytech/polkadot-sdk", tag = "po
pallet-identity = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-v1.1.0", default-features = false }
pallet-nfts = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-v1.1.0", default-features = false }
pallet-nfts-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-v1.1.0", default-features = false }
pallet-insecure-randomness-collective-flip = { git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-v1.1.0", default-features = false }
sp-storage = { version = "13.0.0", default-features = false, git = "https://github.com/paritytech/polkadot-sdk", tag = "polkadot-v1.1.0" }


Expand Down Expand Up @@ -124,7 +125,8 @@ std = [
"pallet-nfts/std",
"pallet-nfts-runtime-api/std",
"pallet-share_distributor/std",
"pallet-bidding/std"
"pallet-bidding/std",
"pallet-insecure-randomness-collective-flip/std",
### add new std
]
runtime-benchmarks = [
Expand Down Expand Up @@ -152,7 +154,7 @@ runtime-benchmarks = [
"pallet-democracy/runtime-benchmarks",
"pallet-nfts/runtime-benchmarks",
"pallet-share_distributor/runtime-benchmarks",
"pallet-bidding/runtime-benchmarks"
"pallet-bidding/runtime-benchmarks",
### add new runtime-benchmarks
]
try-runtime = [
Expand All @@ -179,7 +181,8 @@ try-runtime = [
"pallet-democracy/try-runtime",
"pallet-nfts/try-runtime",
"pallet-share_distributor/try-runtime",
"pallet-bidding/try-runtime"
"pallet-bidding/try-runtime",
"pallet-insecure-randomness-collective-flip/try-runtime",

### add new try-runtime
]
9 changes: 9 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,23 @@ impl pallet_roles::Config for Runtime {
//type WeightInfo = pallet_roles::weights::SubstrateWeight<Runtime>;
}

parameter_types!{
pub const MaxGenerateRandom:u32 =60;
}
impl pallet_bidding::Config for Runtime{
type RuntimeEvent = RuntimeEvent;
type MaxGenerateRandom = MaxGenerateRandom;
type Randomness = RandomnessCollectiveFlip;

}

impl pallet_finalizer::Config for Runtime{
type RuntimeEvent = RuntimeEvent;
//type WeightInfo =
}

impl pallet_insecure_randomness_collective_flip::Config for Runtime {}

impl pallet_council::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
Expand Down Expand Up @@ -696,6 +704,7 @@ construct_runtime!(
FinalizerModule: pallet_finalizer,
ShareDistributorModule: pallet_share_distributor,
BiddingModule: pallet_bidding,
RandomnessCollectiveFlip: pallet_insecure_randomness_collective_flip,
// flag add pallet runtime
}
);
Expand Down

0 comments on commit bbdf4ea

Please sign in to comment.