diff --git a/control/Cargo.toml b/control/Cargo.toml index 3a70e9d45..4ffb24f1b 100755 --- a/control/Cargo.toml +++ b/control/Cargo.toml @@ -5,7 +5,7 @@ # ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ [package] name = "gamedao-control" -version = "1.1.0" +version = "1.1.1" authors = ["zero.io","gamedao.co"] repository = "https://github.com/gamedaoco/gamedao-protocol" edition = "2018" diff --git a/flow/Cargo.toml b/flow/Cargo.toml index 4654f59b8..5cbd5ee5d 100644 --- a/flow/Cargo.toml +++ b/flow/Cargo.toml @@ -5,7 +5,7 @@ # ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ [package] name = "gamedao-flow" -version = "1.1.0" +version = "1.1.1" authors = ["zero.io","gamedao.co"] edition = "2018" license = "GPL-3.0-or-later" diff --git a/flow/src/lib.rs b/flow/src/lib.rs index be02e6a5c..d12e2a735 100644 --- a/flow/src/lib.rs +++ b/flow/src/lib.rs @@ -511,7 +511,9 @@ pub mod pallet { fn on_initialize(block_number: T::BlockNumber) -> Weight { let mut contributors: u32 = 0; Self::process_campaigns(&block_number, FlowState::Finalizing, &mut contributors); - Self::process_campaigns(&block_number, FlowState::Reverting, &mut contributors); + if contributors < T::MaxContributorsProcessing::get() { + Self::process_campaigns(&block_number, FlowState::Reverting, &mut contributors); + } T::WeightInfo::on_initialize(contributors) } @@ -944,13 +946,13 @@ impl Pallet { } fn finalize_campaign( - block_number: &T::BlockNumber, processed: &mut u32, + block_number: &T::BlockNumber, total_processed: &mut u32, campaign: &Campaign>, campaign_balance: &T::Balance, org_treasury: &T::AccountId, contributors: &Vec, owner: &T::AccountId ) { - let processed_offset = ContributorsFinalized::::get(campaign.id); - let offset: usize = usize::try_from(processed_offset).unwrap(); + let mut processed = ContributorsFinalized::::get(campaign.id); + let offset: usize = usize::try_from(processed).unwrap(); for contributor in &contributors[offset..] { if contributor == owner { continue; @@ -973,13 +975,14 @@ impl Pallet { }, Ok(_) => { } } - *processed += 1; - if *processed >= T::MaxContributorsProcessing::get() { - ContributorsFinalized::::insert(campaign.id, processed_offset + *processed); + *total_processed += 1; + processed += 1; + if *total_processed >= T::MaxContributorsProcessing::get() { + ContributorsFinalized::::insert(campaign.id, processed); return } } - ContributorsFinalized::::insert(campaign.id, processed_offset + *processed); + ContributorsFinalized::::insert(campaign.id, processed); // TODO: This doesn't make sense without "transfer_amount" error handling if *campaign_balance < campaign.cap { let _ = Self::set_state(campaign.id, FlowState::Reverting, &campaign.org); @@ -1009,24 +1012,25 @@ impl Pallet { } fn revert_campaign( - block_number: &T::BlockNumber, processed: &mut u32, + block_number: &T::BlockNumber, total_processed: &mut u32, campaign: &Campaign>, campaign_balance: &T::Balance, org_treasury: &T::AccountId, contributors: &Vec ) { - let processed_offset = ContributorsReverted::::get(campaign.id); - let offset: usize = usize::try_from(processed_offset).unwrap(); + let mut processed = ContributorsReverted::::get(campaign.id); + let offset: usize = usize::try_from(processed).unwrap(); for account in &contributors[offset..] { let contribution = CampaignContribution::::get((campaign.id, account.clone())); T::Currency::unreserve(T::PaymentTokenId::get(), &account, contribution); - *processed += 1; - if *processed >= T::MaxContributorsProcessing::get() { - ContributorsReverted::::insert(campaign.id, processed_offset + *processed); + *total_processed += 1; + processed += 1; + if *total_processed >= T::MaxContributorsProcessing::get() { + ContributorsReverted::::insert(campaign.id, processed); return } } - ContributorsReverted::::insert(campaign.id, processed_offset + *processed); + ContributorsReverted::::insert(campaign.id, processed); // Unreserve Initial deposit T::Currency::unreserve(T::ProtocolTokenId::get(), &org_treasury, campaign.deposit); diff --git a/flow/src/mock.rs b/flow/src/mock.rs index 6fe7b5e91..261db27ef 100644 --- a/flow/src/mock.rs +++ b/flow/src/mock.rs @@ -216,8 +216,8 @@ impl gamedao_control::Config for Test { parameter_types! { pub const MinNameLength: u32 = 2; - pub const MaxCampaignsPerAddress: u32 = 3; - pub const MaxCampaignsPerBlock: u32 = 1; + pub const MaxCampaignsPerAddress: u32 = 2; + pub const MaxCampaignsPerBlock: u32 = 2; pub const MaxCampaignsPerOrg: u32 = 64; pub const MaxContributionsPerBlock: u32 = 3; pub const MaxContributorsProcessing: u32 = 4; diff --git a/flow/src/tests.rs b/flow/src/tests.rs index 1404422e9..9cf1e3704 100644 --- a/flow/src/tests.rs +++ b/flow/src/tests.rs @@ -128,6 +128,14 @@ fn flow_create_errors() { Ok(()) } )); + + assert_ok!(Flow::create_campaign( + Origin::signed(BOB), org, BOB, name.clone(), 20 * DOLLARS, 10 * DOLLARS, + current_block + 1, FlowProtocol::Raise, FlowGovernance::No, + empty_vec.clone(), + empty_vec.clone(), + empty_vec.clone(), + )); let block_campaigns_cnt = CampaignsByBlock::::get(current_block+1).len() as u32; println!("Current limit is {:?} and max is {:?}", block_campaigns_cnt, ::MaxCampaignsPerBlock::get()); assert_noop!( @@ -361,6 +369,7 @@ fn flow_on_finalize_campaign_succeess() { let deposit = 10 * DOLLARS; let contribution = 60 * DOLLARS; let target = 500 * DOLLARS; + let init_acc_balance = 100 * DOLLARS; // Create Campaign let nonce = Nonce::::get(); @@ -389,6 +398,21 @@ fn flow_on_finalize_campaign_succeess() { // Contribute (600/500) assert_ok!(Flow::contribute(Origin::signed(ACC_10), campaign_id, contribution)); + // Create second Campaign + let nonce = Nonce::::get(); + let campaign_id_rev: H256 = ::Hashing::hash_of(&nonce); + assert_ok!(Flow::create_campaign( + Origin::signed(BOB), org.clone(), BOB, + BoundedVec::truncate_from(vec![1, 2]), + target, deposit, expiry, + FlowProtocol::Raise, FlowGovernance::No, + BoundedVec::truncate_from(vec![1, 2]), + BoundedVec::truncate_from(vec![]), + BoundedVec::truncate_from(vec![]), + )); + // Contribute (10/500) + assert_ok!(Flow::contribute(Origin::signed(ACC_1), campaign_id_rev, 10 * DOLLARS)); + // --------- Block 0 (expiry): Schedule settlements --------- System::set_block_number(expiry); Flow::on_finalize(expiry); @@ -399,6 +423,12 @@ fn flow_on_finalize_campaign_succeess() { block_number: expiry, })); + System::assert_has_event(Event::Flow(crate::Event::CampaignReverting { + campaign_id: campaign_id_rev, + campaign_balance: CampaignBalance::::get(campaign_id_rev), + block_number: expiry, + })); + // Ensure that campaign was scheduled to be finalized assert_eq!(CampaignsByState::::get(&FlowState::Finalizing, &org), vec![campaign_id]); // Ensure that campaign will be finalize in 3 blocks: 4 + 4 + 2 @@ -410,6 +440,7 @@ fn flow_on_finalize_campaign_succeess() { Flow::on_initialize(expiry + 1); assert_eq!(ContributorsFinalized::::get(campaign_id), batch_size as u32); + assert_eq!(ContributorsReverted::::get(campaign_id_rev), 0 as u32); assert_eq!( ::Currency::total_balance(PAYMENT_TOKEN_ID, &treasury), batch_size * contribution @@ -424,6 +455,7 @@ fn flow_on_finalize_campaign_succeess() { Flow::on_initialize(expiry + 2); assert_eq!(ContributorsFinalized::::get(campaign_id), batch_size as u32 * 2); + assert_eq!(ContributorsReverted::::get(campaign_id), 0 as u32); assert_eq!( ::Currency::total_balance(PAYMENT_TOKEN_ID, &treasury), 2 * batch_size * contribution @@ -433,11 +465,14 @@ fn flow_on_finalize_campaign_succeess() { 0 ); - // --------- Block 3: process last 2 contributors and finalize Campaign --------- + // --------- Block 3: process last 2 contributors and finalize Campaign1, process 1 contributor and revert Campaign2 --------- System::set_block_number(expiry + 3); Flow::on_initialize(expiry + 3); assert_eq!(ContributorsFinalized::::get(campaign_id), total_contributors as u32); + assert_eq!(ContributorsReverted::::get(campaign_id_rev), 1 as u32); + + // Campaign finalized: let commission = ::CampaignFee::get().mul_floor(contribution * 10); // The balance was transfered and locked in the org treasury assert_eq!( diff --git a/sense/Cargo.toml b/sense/Cargo.toml index d54891aa7..3f17d16f1 100755 --- a/sense/Cargo.toml +++ b/sense/Cargo.toml @@ -5,7 +5,7 @@ # ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ [package] name = "gamedao-sense" -version = "1.1.0" +version = "1.1.1" authors = ["zero.io","gamedao.co"] edition = "2018" license = "GPL-3.0-or-later" diff --git a/signal/Cargo.toml b/signal/Cargo.toml index af47a6d84..f69643add 100644 --- a/signal/Cargo.toml +++ b/signal/Cargo.toml @@ -6,7 +6,7 @@ [package] name = "gamedao-signal" -version = "1.1.0" +version = "1.1.1" authors = ["zero.io","gamedao.co"] repository = "https://github.com/gamedaoco/gamedao-protocol" edition = "2018" diff --git a/traits/Cargo.toml b/traits/Cargo.toml index 6d0462ad8..6c8e0f239 100644 --- a/traits/Cargo.toml +++ b/traits/Cargo.toml @@ -6,7 +6,7 @@ [package] name = "gamedao-traits" -version = "1.1.0" +version = "1.1.1" authors = ["zero.io","gamedao.co"] edition = "2018" license = "GPL-3.0-or-later"