Unverified Commit 577a5a2d authored by Gavin Wood's avatar Gavin Wood Committed by GitHub
Browse files

Crowdfund parachain deposit (#315)



* Initial crowdfund stuff

* Make `communication_for` exit when we end a round (#313)

* Make `communication_for` exit when we end a round

* Fix compilation

* Add file

* Rest of logic.

* Consts to Getters

* Cleanups

* Trying to get things to compile

* More patchwork

* Patch dissolve

* Patch `fix_deploy_data`, add getters to `NewBidder`

* Dispatchable functions compile... with warnings

* Fix some warnings and typos

* Whitespace to Tabs

* Update to use `into_sub_account`

* Add events

* Basic fixes to runtime logic and checking

* Check that auction in progress when creating

* Automatically assign end for crowdfund

* Update runtime/src/crowdfund.rs
Co-Authored-By: default avatarAmar Singh <asinghchrony@protonmail.com>

* Update runtime/src/crowdfund.rs
Co-Authored-By: default avatarAmar Singh <asinghchrony@protonmail.com>

* Update runtime/src/crowdfund.rs
Co-Authored-By: default avatarAmar Singh <asinghchrony@protonmail.com>

* Update crowdfund.rs

* Patch `NewRaise` logic

* Test compiles

* Make `NewRaised` logic even better

* Fix trie id generation, start to add some tests

* More tests

* Add more tests

* Finish tests

* Formatting nits

* Use `into_iter`

* Fix for latest Substrate updates

* Cleanup

* Fixes.

* Copyright header.

* Remove dead code.

* Reinstate actually alive code.

* Fix tests

Still have to write some new follow up tests though

* Make funds work before auction

* Test a fund which spans 2 auctions.

* Docs.

* Update doc
parent 04255f19
Pipeline #51991 failed with stages
in 24 minutes and 5 seconds
This diff is collapsed.
...@@ -25,6 +25,7 @@ mod claims; ...@@ -25,6 +25,7 @@ mod claims;
mod parachains; mod parachains;
mod slot_range; mod slot_range;
mod slots; mod slots;
mod crowdfund;
use rstd::prelude::*; use rstd::prelude::*;
use codec::{Encode, Decode}; use codec::{Encode, Decode};
......
...@@ -65,10 +65,10 @@ pub type AuctionIndex = u32; ...@@ -65,10 +65,10 @@ pub type AuctionIndex = u32;
#[cfg_attr(feature = "std", derive(Debug))] #[cfg_attr(feature = "std", derive(Debug))]
pub struct NewBidder<AccountId> { pub struct NewBidder<AccountId> {
/// The bidder's account ID; this is the account that funds the bid. /// The bidder's account ID; this is the account that funds the bid.
who: AccountId, pub who: AccountId,
/// An additional ID to allow the same account ID (and funding source) to have multiple /// An additional ID to allow the same account ID (and funding source) to have multiple
/// logical bidders. /// logical bidders.
sub: SubId, pub sub: SubId,
} }
/// The desired target of a bidder in an auction. /// The desired target of a bidder in an auction.
...@@ -246,8 +246,7 @@ decl_module! { ...@@ -246,8 +246,7 @@ decl_module! {
/// called by the root origin. Accepts the `duration` of this auction and the /// called by the root origin. Accepts the `duration` of this auction and the
/// `lease_period_index` of the initial lease period of the four that are to be auctioned. /// `lease_period_index` of the initial lease period of the four that are to be auctioned.
#[weight = SimpleDispatchInfo::FixedOperational(100_000)] #[weight = SimpleDispatchInfo::FixedOperational(100_000)]
fn new_auction( pub fn new_auction(origin,
origin,
#[compact] duration: T::BlockNumber, #[compact] duration: T::BlockNumber,
#[compact] lease_period_index: LeasePeriodOf<T> #[compact] lease_period_index: LeasePeriodOf<T>
) { ) {
...@@ -282,8 +281,7 @@ decl_module! { ...@@ -282,8 +281,7 @@ decl_module! {
/// - `amount` is the amount to bid to be held as deposit for the parachain should the /// - `amount` is the amount to bid to be held as deposit for the parachain should the
/// bid win. This amount is held throughout the range. /// bid win. This amount is held throughout the range.
#[weight = SimpleDispatchInfo::FixedNormal(500_000)] #[weight = SimpleDispatchInfo::FixedNormal(500_000)]
fn bid( fn bid(origin,
origin,
#[compact] sub: SubId, #[compact] sub: SubId,
#[compact] auction_index: AuctionIndex, #[compact] auction_index: AuctionIndex,
#[compact] first_slot: LeasePeriodOf<T>, #[compact] first_slot: LeasePeriodOf<T>,
...@@ -310,9 +308,8 @@ decl_module! { ...@@ -310,9 +308,8 @@ decl_module! {
/// absolute lease period index value, not an auction-specific offset. /// absolute lease period index value, not an auction-specific offset.
/// - `amount` is the amount to bid to be held as deposit for the parachain should the /// - `amount` is the amount to bid to be held as deposit for the parachain should the
/// bid win. This amount is held throughout the range. /// bid win. This amount is held throughout the range.
#[weight = SimpleDispatchInfo::FixedNormal(500_000)] #[weight = SimpleDispatchInfo::FixedNormal(500_000)]
fn bid_renew( fn bid_renew(origin,
origin,
#[compact] auction_index: AuctionIndex, #[compact] auction_index: AuctionIndex,
#[compact] first_slot: LeasePeriodOf<T>, #[compact] first_slot: LeasePeriodOf<T>,
#[compact] last_slot: LeasePeriodOf<T>, #[compact] last_slot: LeasePeriodOf<T>,
...@@ -346,9 +343,8 @@ decl_module! { ...@@ -346,9 +343,8 @@ decl_module! {
/// - `para_id` is the parachain ID allotted to the winning bidder. /// - `para_id` is the parachain ID allotted to the winning bidder.
/// - `code_hash` is the hash of the parachain's Wasm validation function. /// - `code_hash` is the hash of the parachain's Wasm validation function.
/// - `initial_head_data` is the parachain's initial head data. /// - `initial_head_data` is the parachain's initial head data.
#[weight = SimpleDispatchInfo::FixedNormal(500_000)] #[weight = SimpleDispatchInfo::FixedNormal(500_000)]
fn fix_deploy_data( pub fn fix_deploy_data(origin,
origin,
#[compact] sub: SubId, #[compact] sub: SubId,
#[compact] para_id: ParaIdOf<T>, #[compact] para_id: ParaIdOf<T>,
code_hash: T::Hash, code_hash: T::Hash,
...@@ -408,13 +404,13 @@ impl<T: Trait> Module<T> { ...@@ -408,13 +404,13 @@ impl<T: Trait> Module<T> {
} }
/// True if an auction is in progress. /// True if an auction is in progress.
fn is_in_progress() -> bool { pub fn is_in_progress() -> bool {
<AuctionInfo<T>>::exists() <AuctionInfo<T>>::exists()
} }
/// Returns `Some(n)` if the now block is part of the ending period of an auction, where `n` /// Returns `Some(n)` if the now block is part of the ending period of an auction, where `n`
/// represents how far into the ending period this block is. Otherwise, returns `None`. /// represents how far into the ending period this block is. Otherwise, returns `None`.
fn is_ending(now: T::BlockNumber) -> Option<T::BlockNumber> { pub fn is_ending(now: T::BlockNumber) -> Option<T::BlockNumber> {
if let Some((_, early_end)) = <AuctionInfo<T>>::get() { if let Some((_, early_end)) = <AuctionInfo<T>>::get() {
if let Some(after_early_end) = now.checked_sub(&early_end) { if let Some(after_early_end) = now.checked_sub(&early_end) {
if after_early_end < T::EndingPeriod::get() { if after_early_end < T::EndingPeriod::get() {
...@@ -496,7 +492,7 @@ impl<T: Trait> Module<T> { ...@@ -496,7 +492,7 @@ impl<T: Trait> Module<T> {
} }
// Add para IDs of any chains that will be newly deployed to our set of managed // Add para IDs of any chains that will be newly deployed to our set of managed
// IDs // IDs.
<ManagedIds<T>>::mutate(|m| m.push(para_id)); <ManagedIds<T>>::mutate(|m| m.push(para_id));
Self::deposit_event(RawEvent::WonDeploy(bidder.clone(), range, para_id, amount)); Self::deposit_event(RawEvent::WonDeploy(bidder.clone(), range, para_id, amount));
...@@ -506,6 +502,8 @@ impl<T: Trait> Module<T> { ...@@ -506,6 +502,8 @@ impl<T: Trait> Module<T> {
let begin_offset = <LeasePeriodOf<T>>::from(range.as_pair().0 as u32); let begin_offset = <LeasePeriodOf<T>>::from(range.as_pair().0 as u32);
let begin_lease_period = auction_lease_period_index + begin_offset; let begin_lease_period = auction_lease_period_index + begin_offset;
<OnboardQueue<T>>::mutate(begin_lease_period, |starts| starts.push(para_id)); <OnboardQueue<T>>::mutate(begin_lease_period, |starts| starts.push(para_id));
// Add a default off-boarding account which matches the original bidder
<Offboarding<T>>::insert(&para_id, &bidder.who);
let entry = (begin_lease_period, IncomingParachain::Unset(bidder)); let entry = (begin_lease_period, IncomingParachain::Unset(bidder));
<Onboarding<T>>::insert(&para_id, entry); <Onboarding<T>>::insert(&para_id, entry);
} }
...@@ -645,7 +643,7 @@ impl<T: Trait> Module<T> { ...@@ -645,7 +643,7 @@ impl<T: Trait> Module<T> {
/// - `first_slot`: The first lease period index of the range to be bid on. /// - `first_slot`: The first lease period index of the range to be bid on.
/// - `last_slot`: The last lease period index of the range to be bid on (inclusive). /// - `last_slot`: The last lease period index of the range to be bid on (inclusive).
/// - `amount`: The total amount to be the bid for deposit over the range. /// - `amount`: The total amount to be the bid for deposit over the range.
fn handle_bid( pub fn handle_bid(
bidder: Bidder<T::AccountId, ParaIdOf<T>>, bidder: Bidder<T::AccountId, ParaIdOf<T>>,
auction_index: u32, auction_index: u32,
first_slot: LeasePeriodOf<T>, first_slot: LeasePeriodOf<T>,
...@@ -1046,6 +1044,24 @@ mod tests { ...@@ -1046,6 +1044,24 @@ mod tests {
#[test] #[test]
fn offboarding_works() { fn offboarding_works() {
with_externalities(&mut new_test_ext(), || {
run_to_block(1);
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Slots::bid(Origin::signed(1), 0, 1, 1, 4, 1));
assert_eq!(Balances::free_balance(&1), 9);
run_to_block(9);
assert_eq!(Slots::deposit_held(&0.into()), 1);
assert_eq!(Slots::deposits(&0.into())[0], 0);
run_to_block(50);
assert_eq!(Slots::deposit_held(&0.into()), 0);
assert_eq!(Balances::free_balance(&1), 10);
});
}
#[test]
fn set_offboarding_works() {
with_externalities(&mut new_test_ext(), || { with_externalities(&mut new_test_ext(), || {
run_to_block(1); run_to_block(1);
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1)); assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment