Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions EVENT_PAYLOADS.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ Every `publish(...)` call in the contract, with its topics, data shape, and the
| Field | Value |
|---------|------------------------------------------------------------|
| Topics | `("campaigns_bulk_verified",)` |
| Data | `(verified_count: u32, total: u32)` |
| Source | `lib.rs:1175` — `verify_campaigns()` |
| Data | `(verified_count: u32, failed_count: u32, total: u32)` |
| Source | `lib.rs:256` — `verify_campaigns()` |

---

Expand Down
46 changes: 35 additions & 11 deletions src/contributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ fn check_contribution_caps(
amount: i128,
) -> Result<(), Error> {
if campaign.max_contribution_per_user > 0
&& current_lifetime_contribution + amount > campaign.max_contribution_per_user
&& current_lifetime_contribution
.checked_add(amount)
.ok_or(Error::Overflow)?
> campaign.max_contribution_per_user
{
return Err(Error::ContributionCapExceeded);
}
Expand Down Expand Up @@ -97,19 +100,40 @@ fn update_contribution_accounting(
current: i128,
lifetime: i128,
amount: i128,
) {
campaign.amount_raised += amount;
campaign.effective_amount_raised += amount;
) -> Result<(), Error> {
campaign.amount_raised = campaign
.amount_raised
.checked_add(amount)
.ok_or(Error::Overflow)?;
campaign.effective_amount_raised = campaign
.effective_amount_raised
.checked_add(amount)
.ok_or(Error::Overflow)?;
set_campaign(env, campaign_id, campaign);
set_contribution(env, campaign_id, contributor, current + amount);
set_lifetime_contribution(env, campaign_id, contributor, lifetime + amount);
set_contribution(
env,
campaign_id,
contributor,
current.checked_add(amount).ok_or(Error::Overflow)?,
);
set_lifetime_contribution(
env,
campaign_id,
contributor,
lifetime.checked_add(amount).ok_or(Error::Overflow)?,
);

if lifetime == 0 {
increment_contributor_count(env, campaign_id);
}

let total_raised = get_total_raised_global(env);
set_total_raised_global(env, total_raised + amount);
set_total_raised_global(
env,
total_raised.checked_add(amount).ok_or(Error::Overflow)?,
);

Ok(())
}

pub(crate) fn contribute(
Expand Down Expand Up @@ -145,7 +169,7 @@ pub(crate) fn contribute(
check_contribution_caps(&campaign, lifetime, amount)?;

if let Some(cap) = get_personal_cap(env, campaign_id, &contributor) {
if current + amount > cap {
if current.checked_add(amount).ok_or(Error::Overflow)? > cap {
return Err(Error::ContributionCapExceeded);
}
}
Expand All @@ -161,7 +185,7 @@ pub(crate) fn contribute(
current,
lifetime,
amount,
);
)?;

let client = token_client(env);
client.transfer(&contributor, &env.current_contract_address(), &amount);
Expand Down Expand Up @@ -217,7 +241,7 @@ pub(crate) fn batch_contribute(
check_contribution_caps(&campaign, lifetime, amount)?;

if let Some(cap) = get_personal_cap(env, campaign_id, &contributor) {
if current + amount > cap {
if current.checked_add(amount).ok_or(Error::Overflow)? > cap {
return Err(Error::ContributionCapExceeded);
}
}
Expand All @@ -232,7 +256,7 @@ pub(crate) fn batch_contribute(
current,
lifetime,
amount,
);
)?;

total = total.checked_add(amount).ok_or(Error::Overflow)?;

Expand Down
34 changes: 20 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,24 @@ impl ProofOfHeart {
voting::admin_verify(&env, campaign_id)
}

pub fn verify_campaigns(env: Env, campaign_ids: soroban_sdk::Vec<u32>) -> Result<u32, Error> {
/// Batch-verifies up to 50 campaigns in a single call. Each campaign is
/// independently verified; failures are collected rather than aborting the
/// batch. Returns `Ok((verified_ids, failed_ids))` so callers can distinguish
/// partial success from total failure (#442). Auth / paused checks still
/// return `Err` and abort the entire batch.
pub fn verify_campaigns(
env: Env,
campaign_ids: soroban_sdk::Vec<u32>,
) -> Result<(soroban_sdk::Vec<u32>, soroban_sdk::Vec<u32>), Error> {
let admin = get_admin(&env);
assert_admin(&env, &admin)?;
lifecycle::require_not_paused(&env)?;

const MAX_BATCH_SIZE: u32 = 50;
let batch_size = campaign_ids.len().min(MAX_BATCH_SIZE);

let mut verified_count = 0u32;
let mut first_error: Option<Error> = None;
let mut verified_ids = soroban_sdk::Vec::new(&env);
let mut failed_ids = soroban_sdk::Vec::new(&env);

bump_instance_ttl(&env);

Expand All @@ -240,27 +248,25 @@ impl ProofOfHeart {
storage::extend_voting_state_ttl(&env, campaign_id);
match voting::admin_verify(&env, campaign_id) {
Ok(()) => {
verified_count += 1;
verified_ids.push_back(campaign_id);
}
Err(e) => {
if first_error.is_none() {
first_error = Some(e);
}
Err(_e) => {
failed_ids.push_back(campaign_id);
}
}
}
}

env.events().publish(
("campaigns_bulk_verified",),
(verified_count, campaign_ids.len()),
(
verified_ids.len(),
failed_ids.len(),
batch_size,
),
);

if let Some(err) = first_error {
Err(err)
} else {
Ok(verified_count)
}
Ok((verified_ids, failed_ids))
}

pub fn verify_campaign_with_votes(env: Env, campaign_id: u32) -> Result<(), Error> {
Expand Down
14 changes: 12 additions & 2 deletions src/revenue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ pub(crate) fn deposit_revenue(env: &Env, campaign_id: u32, amount: i128) -> Resu
bump_instance_ttl(env);

let current_pool = get_revenue_pool(env, campaign_id);
set_revenue_pool(env, campaign_id, current_pool + amount);
set_revenue_pool(
env,
campaign_id,
current_pool.checked_add(amount).ok_or(Error::Overflow)?,
);

let token_addr = get_token(env);
let client = token::Client::new(env, &token_addr);
Expand Down Expand Up @@ -134,7 +138,13 @@ pub(crate) fn claim_revenue(
// Track the running sum paid out to contributors, and the count of
// distinct contributors who have claimed at least once, so future claims
// can detect the last claimant (#526).
set_contributor_revenue_distributed(env, campaign_id, distributed_so_far + claimable);
set_contributor_revenue_distributed(
env,
campaign_id,
distributed_so_far
.checked_add(claimable)
.ok_or(Error::Overflow)?,
);
if is_first_claim {
set_contributor_revenue_claimants(env, campaign_id, claimants_so_far + 1);
}
Expand Down
12 changes: 7 additions & 5 deletions src/tests/test_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,15 @@ fn test_verify_campaigns_extends_ttl_on_failure() {

// 3. Verify the campaign successfully first.
let ids = soroban_sdk::Vec::from_array(&env, [campaign_id]);
let first_res = client.verify_campaigns(&ids);
assert_eq!(first_res, 1);
let (first_verified, first_failed) = client.verify_campaigns(&ids);
assert_eq!(first_verified.len(), 1);
assert_eq!(first_failed.len(), 0);

// Now try to verify the campaign again.
// Since it's already verified, it will fail verification.
let second_res = client.try_verify_campaigns(&ids);
assert!(second_res.is_err()); // verification failed (AdminVerificationConflict error)
// Since it's already verified, it will fail verification and land in failed_ids.
let (second_verified, second_failed) = client.verify_campaigns(&ids);
assert_eq!(second_verified.len(), 0);
assert_eq!(second_failed.len(), 1);

// 4. Despite the failure, the voting state TTL should have been extended.
let current_ledger = env.ledger().sequence();
Expand Down
16 changes: 10 additions & 6 deletions src/tests/test_voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ fn test_verify_campaigns_extends_voting_state_ttl() {
));

// Bulk verify the campaign
let count = client.verify_campaigns(&soroban_sdk::Vec::from_array(&env, [campaign_id]));
assert_eq!(count, 1);
let (verified, failed) = client.verify_campaigns(&soroban_sdk::Vec::from_array(&env, [campaign_id]));
assert_eq!(verified.len(), 1);
assert_eq!(failed.len(), 0);

// Verify campaign is verified (confirming it worked)
let campaign = client.get_campaign(&campaign_id);
Expand Down Expand Up @@ -353,7 +354,7 @@ fn test_vote_on_campaign_after_deadline_returns_deadline_passed() {
}

#[test]
fn test_verify_campaigns_partial_failure_returns_err() {
fn test_verify_campaigns_partial_failure_returns_ids() {
let (env, _admin, creator, _, _, _, _, client) = setup_env();

let campaign_id = client.create_campaign(&make_params(
Expand All @@ -368,10 +369,13 @@ fn test_verify_campaigns_partial_failure_returns_err() {
0i128,
));

// 999 does not exist — will produce CampaignNotFound
// 999 does not exist — will produce CampaignNotFound, collected as failed_id
let ids = soroban_sdk::Vec::from_array(&env, [campaign_id, 999u32]);
let res = client.try_verify_campaigns(&ids);
assert!(res.unwrap_err().is_ok()); // Err variant, inner Ok means contract error
let (verified, failed) = client.verify_campaigns(&ids);
assert_eq!(verified.len(), 1);
assert_eq!(failed.len(), 1);
assert_eq!(verified.get(0).unwrap(), campaign_id);
assert_eq!(failed.get(0).unwrap(), 999u32);
}

// ── verification via votes ──────────────────────────────────────────────────────
Expand Down
Loading