Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Added
- Campaign comment system via off-chain attestation. Added `add_campaign_comment` to emit comment hashes, and `remove_campaign_comment` to flag comments as removed on-chain for moderation (#542).

### Fixed

- `cancel_campaign` now rejects with `GoalMetCancellationNotAllowed` when `amount_raised >= funding_goal` and funds have not yet been withdrawn, preventing rug-pull-adjacent behaviour where a creator could cancel after reaching the goal and force all contributors to self-serve refunds (#164).
Expand Down
2 changes: 1 addition & 1 deletion src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,4 @@ impl ProofOfHeartContract {
let cap_key = DataKey::CategoryMaxGoalCap(category);
env.storage().persistent().get(&cap_key)
}
}
}
69 changes: 69 additions & 0 deletions src/campaigns/comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use soroban_sdk::{Address, Env, String};

use crate::errors::Error;
use crate::lifecycle::{get_campaign_or_error, require_not_paused};
use crate::storage::{
bump_instance_ttl, get_admin, get_campaign_comment_count, is_comment_removed,
set_campaign_comment_count, set_comment_removed,
};

pub(crate) fn add_campaign_comment(
env: &Env,
campaign_id: u32,
commenter: Address,
comment_hash: String,
) -> Result<u32, Error> {
commenter.require_auth();
require_not_paused(env)?;

// Just verify the campaign exists
let _campaign = get_campaign_or_error(env, campaign_id)?;

bump_instance_ttl(env);

let count = get_campaign_comment_count(env, campaign_id);
let comment_id = count.checked_add(1).ok_or(Error::Overflow)?;

set_campaign_comment_count(env, campaign_id, comment_id);

env.events().publish(
("campaign_comment_added", campaign_id, comment_id),
(commenter, comment_hash),
);

Ok(comment_id)
}

pub(crate) fn remove_campaign_comment(
env: &Env,
campaign_id: u32,
comment_id: u32,
caller: Address,
) -> Result<(), Error> {
caller.require_auth();
require_not_paused(env)?;

let campaign = get_campaign_or_error(env, campaign_id)?;

// Only the campaign creator or the admin can remove comments.
if caller != campaign.creator && caller != get_admin(env) {
return Err(Error::NotAuthorized);
}

let count = get_campaign_comment_count(env, campaign_id);
if comment_id == 0 || comment_id > count {
return Err(Error::InvalidCommentId);
}

if is_comment_removed(env, campaign_id, comment_id) {
return Err(Error::CommentAlreadyRemoved);
}

bump_instance_ttl(env);
set_comment_removed(env, campaign_id, comment_id);

env.events()
.publish(("campaign_comment_removed", campaign_id, comment_id), caller);

Ok(())
}
1 change: 1 addition & 0 deletions src/campaigns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub(crate) mod create;
pub(crate) mod transfer;
pub(crate) mod update;
pub(crate) mod withdraw;
pub(crate) mod comments;
4 changes: 4 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl Error {
Error::InvalidStateTransition => "InvalidStateTransition",
Error::CampaignAlreadyBookmarked => "CampaignAlreadyBookmarked",
Error::CampaignNotBookmarked => "CampaignNotBookmarked",
/// The specified comment ID does not exist.
InvalidCommentId = 46,
/// The specified comment has already been removed.
CommentAlreadyRemoved = 47,
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ impl ProofOfHeart {
campaigns::update::extend_campaign_deadline(&env, campaign_id, additional_days)
}

// ── Comments ──────────────────────────────────────────────────────────────

pub fn add_campaign_comment(
env: Env,
campaign_id: u32,
commenter: Address,
comment_hash: String,
) -> Result<u32, Error> {
campaigns::comments::add_campaign_comment(&env, campaign_id, commenter, comment_hash)
}

pub fn remove_campaign_comment(
env: Env,
campaign_id: u32,
comment_id: u32,
caller: Address,
) -> Result<(), Error> {
campaigns::comments::remove_campaign_comment(&env, campaign_id, comment_id, caller)
}

// ── Campaign ownership transfer ───────────────────────────────────────────

pub fn initiate_campaign_transfer(
Expand Down Expand Up @@ -676,4 +696,4 @@ impl ProofOfHeartContract {
None => all_campaigns,
}
}
}
}
31 changes: 31 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ pub enum BookmarkKey {
SavedCampaigns(Address),
}

/// Keys for campaign comments.
#[contracttype]
pub enum CommentKey {
/// Tracks the total number of comments for a campaign.
CampaignCommentCount(u32),
/// Flags whether a specific comment ID has been removed. Key is (campaign_id, comment_id).
CommentRemoved(u32, u32),
}


// ── Campaign ──────────────────────────────────────────────────────────────────

/// Returns the campaign for the given ID.
Expand Down Expand Up @@ -694,6 +704,27 @@ pub fn get_version(env: &Env) -> u32 {
.unwrap_or(0)
}


// ── Comments ──────────────────────────────────────────────────────────────────

pub fn get_campaign_comment_count(env: &Env, campaign_id: u32) -> u32 {
let key = CommentKey::CampaignCommentCount(campaign_id);
env.storage().persistent().get(&key).unwrap_or(0)
}

pub fn set_campaign_comment_count(env: &Env, campaign_id: u32, count: u32) {
persistent_set!(env, CommentKey::CampaignCommentCount(campaign_id), &count);
}

pub fn is_comment_removed(env: &Env, campaign_id: u32, comment_id: u32) -> bool {
let key = CommentKey::CommentRemoved(campaign_id, comment_id);
env.storage().persistent().get(&key).unwrap_or(false)
}

pub fn set_comment_removed(env: &Env, campaign_id: u32, comment_id: u32) {
persistent_set!(env, CommentKey::CommentRemoved(campaign_id, comment_id), &true);
}

// ── Total raised global ───────────────────────────────────────────────────────

/// Returns the total amount raised across all campaigns.
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ mod test_revenue_deposit;
mod test_voting;
mod test_voting_verify;
mod test_withdrawals;
mod test_comments;
18 changes: 12 additions & 6 deletions src/tests/test_campaign_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ fn test_update_campaign_emits_title_and_description() {

let events = env.events().all();
let last_event = events.last().unwrap();
let payload: (String, String) = soroban_sdk::FromVal::from_val(&env, &last_event.2);
let payload: (String, String, String, String) =
soroban_sdk::FromVal::from_val(&env, &last_event.2);

assert_eq!(payload.0, new_title);
assert_eq!(payload.1, new_desc);
assert_eq!(payload.0, String::from_str(&env, "Original Title"));
assert_eq!(payload.1, String::from_str(&env, "Original Description"));
assert_eq!(payload.2, new_title);
assert_eq!(payload.3, new_desc);
}

#[test]
Expand Down Expand Up @@ -94,9 +97,12 @@ fn test_update_campaign_event_tracks_latest_description() {

let events = env.events().all();
let last_event = events.last().unwrap();
let payload: (String, String) = soroban_sdk::FromVal::from_val(&env, &last_event.2);
assert_eq!(payload.0, String::from_str(&env, "Title V3"));
assert_eq!(payload.1, String::from_str(&env, "Description V3"));
let payload: (String, String, String, String) =
soroban_sdk::FromVal::from_val(&env, &last_event.2);
assert_eq!(payload.0, String::from_str(&env, "Title V2"));
assert_eq!(payload.1, String::from_str(&env, "Description V2"));
assert_eq!(payload.2, String::from_str(&env, "Title V3"));
assert_eq!(payload.3, String::from_str(&env, "Description V3"));
}

#[test]
Expand Down
79 changes: 79 additions & 0 deletions src/tests/test_comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#![cfg(test)]
extern crate std;

use super::helpers::{setup_env, setup_env_with_active_campaign};
use crate::ProofOfHeartContractClient;
use soroban_sdk::{testutils::Events, String};

#[test]
fn test_add_and_remove_comment() {
let (env, contract_id, _, creator, _) = setup_env_with_active_campaign(100);
let client = ProofOfHeartContractClient::new(&env, &contract_id);

let commenter = soroban_sdk::Address::generate(&env);
let comment_hash = String::from_str(&env, "QmHash123");

// Add comment
let comment_id = client.add_campaign_comment(&1, &commenter, &comment_hash);
assert_eq!(comment_id, 1);

// Verify events
let events = env.events().all();
let mut found_added = false;
for (contract, topic, data) in events.iter() {
if contract == contract_id {
if let Ok(t) = topic.clone().try_into_val(&env) {
let t: soroban_sdk::Vec<soroban_sdk::Val> = t;
if t.len() == 3 {
let event_name: String = t.get(0).unwrap().try_into_val(&env).unwrap();
if event_name == String::from_str(&env, "campaign_comment_added") {
found_added = true;
let cid: u32 = t.get(2).unwrap().try_into_val(&env).unwrap();
assert_eq!(cid, 1);
}
}
}
}
}
assert!(found_added);

// Remove comment by creator
client.remove_campaign_comment(&1, &comment_id, &creator);

// Verify remove event
let events = env.events().all();
let mut found_removed = false;
for (contract, topic, data) in events.iter() {
if contract == contract_id {
if let Ok(t) = topic.clone().try_into_val(&env) {
let t: soroban_sdk::Vec<soroban_sdk::Val> = t;
if t.len() == 3 {
let event_name: String = t.get(0).unwrap().try_into_val(&env).unwrap();
if event_name == String::from_str(&env, "campaign_comment_removed") {
found_removed = true;
}
}
}
}
}
assert!(found_removed);

// Ensure double remove fails
let res = client.try_remove_campaign_comment(&1, &comment_id, &creator);
assert!(res.is_err()); // CommentAlreadyRemoved
}

#[test]
fn test_remove_comment_unauthorized() {
let (env, contract_id, _, creator, _) = setup_env_with_active_campaign(100);
let client = ProofOfHeartContractClient::new(&env, &contract_id);

let commenter = soroban_sdk::Address::generate(&env);
let comment_hash = String::from_str(&env, "QmHash123");

let comment_id = client.add_campaign_comment(&1, &commenter, &comment_hash);

let stranger = soroban_sdk::Address::generate(&env);
let res = client.try_remove_campaign_comment(&1, &comment_id, &stranger);
assert!(res.is_err()); // NotAuthorized
}
Loading