Skip to content
Merged
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
28 changes: 16 additions & 12 deletions contracts/analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,20 @@ impl Analytics {
.get(points_key)
.unwrap_or_else(|| Vec::new(env));

if points.len() >= max_capacity {
let summary = env
.storage()
.persistent()
.get::<DataKey, MetricSummary>(summary_key);
let current_count = summary.as_ref().map(|s| s.total_count).unwrap_or(0);
if current_count >= max_capacity {
return Err(ContractError::StoreFull);
}

points.push_back(DataPoint { timestamp, value });
env.storage().persistent().set(points_key, &points);

// Update summary
let summary = if let Some(mut s) = env
.storage()
.persistent()
.get::<DataKey, MetricSummary>(summary_key)
{
s.total_count += 1;
let next_summary = if let Some(mut s) = summary {
s.total_count = s
.total_count
.checked_add(1)
.ok_or(ContractError::Overflow)?;
s.total_sum = s
.total_sum
.checked_add(value)
Expand All @@ -419,7 +419,11 @@ impl Analytics {
}
};

env.storage().persistent().set(summary_key, &summary);
points.push_back(DataPoint { timestamp, value });
env.storage().persistent().set(points_key, &points);
env.storage()
.persistent()
.set(summary_key, &next_summary);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions contracts/analytics/tests/time_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ fn test_capacity_limits() {
client.try_record_metric_at(&metric, &40, &400),
Err(Ok(ContractError::StoreFull))
);
assert_eq!(client.get_metric_summary(&metric).total_count, 3);
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions contracts/disputes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ path = "tests/err_stab.rs"
name = "dispute_edge_cases"
path = "tests/dispute_edge_cases.rs"

[[test]]
name = "dispute_storage"
path = "tests/dispute_storage.rs"

[[test]]
name = "xcontract"
path = "tests/xcontract.rs"
67 changes: 58 additions & 9 deletions contracts/disputes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum DisputeStatus {
#[contracttype]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Dispute {
pub id: u32,
pub market_id: Symbol,
pub creator: Symbol,
pub status: DisputeStatus,
Expand Down Expand Up @@ -161,6 +162,26 @@ public struct EscalationRecord {
pub resolved: bool,
}

#[contracttype]
#[derive(Clone)]
enum DataKey {
DisputeCounter,
Dispute(u32),
}

fn load_dispute(env: &Env, id: u32) -> Dispute {
env.storage()
.persistent()
.get(&DataKey::Dispute(id))
.unwrap_or_else(|| panic!("dispute not found"))
}

fn store_dispute(env: &Env, dispute: &Dispute) {
env.storage()
.persistent()
.set(&DataKey::Dispute(dispute.id), dispute);
}

#[contract]
pub struct DisputesContract;

Expand Down Expand Up @@ -253,24 +274,48 @@ impl DisputesContract {
}
}

/// Returns a dispute by its persistent ID, if it exists.
pub fn get_dispute(env: Env, id: u32) -> Option<Dispute> {
env.storage().persistent().get(&DataKey::Dispute(id))
}

/// Open a new dispute for a market. The dispute begins in the
/// `Open` status; community voting has not yet started.
pub fn open_dispute(_env: Env, market_id: Symbol, creator: Symbol) -> Dispute {
Dispute {
/// `Open` status; community voting has not yet started. Its allocated ID
/// can be used to retrieve the persisted dispute and identify it in later
/// lifecycle transitions.
pub fn open_dispute(env: Env, market_id: Symbol, creator: Symbol) -> Dispute {
let previous_id: u32 = env
.storage()
.persistent()
.get(&DataKey::DisputeCounter)
.unwrap_or(0);
let id = previous_id
.checked_add(1)
.unwrap_or_else(|| panic!("dispute ID overflow"));
let dispute = Dispute {
id,
market_id,
creator,
status: DisputeStatus::Open,
outcome: DisputeOutcome::Pending,
}
};
env.storage()
.persistent()
.set(&DataKey::DisputeCounter, &id);
store_dispute(&env, &dispute);
dispute
}

/// Transition a dispute from `Open` to `Voting`. Once voting has
/// begun the dispute can no longer be withdrawn by its creator.
pub fn start_voting(_env: Env, mut dispute: Dispute) -> Dispute {
/// begun the dispute can no longer be withdrawn by its creator. The
/// persisted record is authoritative; only the supplied ID is used.
pub fn start_voting(env: Env, dispute: Dispute) -> Dispute {
let mut dispute = load_dispute(&env, dispute.id);
if dispute.status != DisputeStatus::Open {
panic!("dispute is not open");
}
dispute.status = DisputeStatus::Voting;
store_dispute(&env, &dispute);
dispute
}

Expand All @@ -279,24 +324,27 @@ impl DisputesContract {
/// before community voting has begun). This prevents a creator from
/// opening a frivolous dispute to delay resolution and then
/// cancelling it mid-vote to avoid an unfavourable ruling.
pub fn withdraw_dispute(_env: Env, mut dispute: Dispute, caller: Symbol) -> Dispute {
pub fn withdraw_dispute(env: Env, dispute: Dispute, caller: Symbol) -> Dispute {
let mut dispute = load_dispute(&env, dispute.id);
if caller != dispute.creator {
panic!("only the dispute creator may withdraw");
}
if dispute.status != DisputeStatus::Open {
panic!"cannot withdraw a dispute once voting has begun");
}
dispute.status = DisputeStatus::Withdrawn;
store_dispute(&env, &dispute);
dispute
}

/// Record a binding resolution for a dispute. Only permitted while
/// the dispute is in the `Voting` status.
pub fn resolve_dispute(
_env: Env,
mut dispute: Dispute,
env: Env,
dispute: Dispute,
outcome: DisputeOutcome,
) -> Dispute {
let mut dispute = load_dispute(&env, dispute.id);
if dispute.status != DisputeStatus::Voting {
panic!("dispute is not in voting");
}
Expand All @@ -305,6 +353,7 @@ impl DisputesContract {
}
dispute.status = DisputeStatus::Resolved;
dispute.outcome = outcome;
store_dispute(&env, &dispute);
dispute
}
}
36 changes: 36 additions & 0 deletions contracts/disputes/tests/dispute_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use disputes::{
DisputeOutcome, DisputeStatus, DisputesContract, DisputesContractClient,
};
use soroban_sdk::{Env, Symbol};

#[test]
fn dispute_lifecycle_is_persisted_and_uses_unique_ids() {
let env = Env::default();
let contract_id = env.register(DisputesContract, ());
let client = DisputesContractClient::new(&env, &contract_id);
let market_id = Symbol::new(&env, "MARKET1");
let creator = Symbol::new(&env, "CREATOR");

let opened = client.open_dispute(&market_id, &creator);
assert_eq!(opened.id, 1);
assert_eq!(opened.status, DisputeStatus::Open);
assert_eq!(client.get_dispute(&opened.id), Some(opened.clone()));

let mut stale_input = opened.clone();
stale_input.creator = Symbol::new(&env, "FORGED");
let voting = client.start_voting(&stale_input);
assert_eq!(voting.creator, creator);
assert_eq!(voting.status, DisputeStatus::Voting);
assert_eq!(client.get_dispute(&opened.id), Some(voting.clone()));

let resolved = client.resolve_dispute(&voting, &DisputeOutcome::ChallengerWins);
assert_eq!(resolved.status, DisputeStatus::Resolved);
assert_eq!(resolved.outcome, DisputeOutcome::ChallengerWins);
assert_eq!(client.get_dispute(&opened.id), Some(resolved));

let second = client.open_dispute(&Symbol::new(&env, "MARKET2"), &creator);
assert_eq!(second.id, 2);
let withdrawn = client.withdraw_dispute(&second, &creator);
assert_eq!(withdrawn.status, DisputeStatus::Withdrawn);
assert_eq!(client.get_dispute(&second.id), Some(withdrawn));
}
3 changes: 3 additions & 0 deletions contracts/fees/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,9 @@ impl FeesContract {
if config.min_fee_amount > config.max_fee_amount {
return Err(ContractError::InvalidInput);
}
if config.fees_enabled && config.min_fee_amount == 0 {
return Err(ContractError::InvalidInput);
}
Ok(())
}
}
Expand Down
17 changes: 17 additions & 0 deletions contracts/fees/tests/auth_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ fn test_update_fee_config_rejects_negative_values() {
}
}

#[test]
fn test_update_fee_config_rejects_zero_minimum_when_fees_enabled() {
let env = Env::default();
let admin = Address::generate(&env);
let client = register_and_initialize(&env, &admin);

let mut config = default_fee_config();
config.min_fee_amount = 0;
config.max_fee_amount = 0;

let result = client.try_update_fee_config(&admin, &config);
assert!(matches!(result, Err(Ok(ContractError::InvalidInput))));

config.fees_enabled = false;
client.update_fee_config(&admin, &config);
}

// ============================================================
// Auth Boundary Tests — set_platform_fee
// ============================================================
Expand Down
Loading
Loading