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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions contracts/campaign/src/fuzz_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ proptest! {
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1a, 0x1b, 0x1c]);
client.set_merkle_root(&admin, &0, &root, &None, &Vec::new(&env));
client.set_merkle_root(&admin, &0, &root, &Vec::new(&env));

let leaf = BytesN::from_array(&env, &leaf_bytes);
let mut proof: Vec<BytesN<32>> = Vec::new(&env);
Expand Down Expand Up @@ -573,7 +573,7 @@ proptest! {
let mut root_bytes = [0u8; 32];
root_bytes[0] = 0xff;
let root = BytesN::from_array(&env, &root_bytes);
client.set_merkle_root(&admin, &0, &root, &None, &Vec::new(&env));
client.set_merkle_root(&admin, &0, &root, &Vec::new(&env));

// Build a bit-flipped proof node.
let mut node = [0u8; 32];
Expand Down
46 changes: 27 additions & 19 deletions contracts/campaign/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ fn log_activity(env: &Env, kind: ActivityKind, actor: Address, amount: Option<u6

fn require_admin_with_nonce(env: &Env, admin: &Address, nonce: u64) -> Result<(), Error> {
admin.require_auth();
let stored: Address = env.storage().instance().get(&ADMIN).unwrap();
let stored: Address = env.storage().instance().get(&ADMIN).ok_or(Error::Unauthorized)?;
if &stored != admin {
return Err(Error::Unauthorized);
}
Expand Down Expand Up @@ -540,7 +540,7 @@ impl CampaignContract {
/// serves as an idempotent migration hook for upgrade workflows.
pub fn migrate(env: Env, admin: Address, target_version: u32) -> Result<u32, Error> {
admin.require_auth();
let stored: Address = env.storage().instance().get(&ADMIN).unwrap();
let stored: Address = env.storage().instance().get(&ADMIN).ok_or(Error::Unauthorized)?;
if stored != admin {
return Err(Error::Unauthorized);
}
Expand Down Expand Up @@ -1220,13 +1220,19 @@ impl CampaignContract {
if cursor >= len {
cursor = 0;
}
let addr = registry.get(cursor).unwrap();
let addr = if let Some(a) = registry.get(cursor) {
a
} else {
cursor = 0;
continue;
};
let key = (PARTICIPANT, addr);
if !env.storage().persistent().has(&key) {
let last_idx = len - 1;
if cursor != last_idx {
let last = registry.get(last_idx).unwrap();
registry.set(cursor, last);
if let Some(last) = registry.get(last_idx) {
registry.set(cursor, last);
}
}
registry.pop_back();
len -= 1;
Expand Down Expand Up @@ -1275,12 +1281,13 @@ impl CampaignContract {
let mut checked = 0u32;
let mut idx = cursor;
while checked < len && pruned < max_entries {
let nonce = registry.get(idx).unwrap();
let key = (NONCE_USED, nonce);
if let Some(used_at) = env.storage().instance().get::<_, u32>(&key) {
if now.saturating_sub(used_at) > NONCE_TTL_LEDGERS {
env.storage().instance().remove(&key);
pruned += 1;
if let Some(nonce) = registry.get(idx) {
let key = (NONCE_USED, nonce);
if let Some(used_at) = env.storage().instance().get::<_, u32>(&key) {
if now.saturating_sub(used_at) > NONCE_TTL_LEDGERS {
env.storage().instance().remove(&key);
pruned += 1;
}
}
}
idx = (idx + 1) % len;
Expand Down Expand Up @@ -1571,11 +1578,12 @@ impl CampaignContract {
.unwrap_or(Vec::new(&env));
let mut found = false;
for i in 0..co_admins.len() {
let (addr, _) = co_admins.get(i).unwrap();
if addr == co_admin {
co_admins.set(i, (co_admin.clone(), pubkey.clone()));
found = true;
break;
if let Some((addr, _)) = co_admins.get(i) {
if addr == co_admin {
co_admins.set(i, (co_admin.clone(), pubkey.clone()));
found = true;
break;
}
}
}
if !found {
Expand Down Expand Up @@ -1650,7 +1658,7 @@ impl CampaignContract {

/// Return the current admin address.
pub fn admin(env: Env) -> Address {
env.storage().instance().get(&ADMIN).unwrap()
env.storage().instance().get(&ADMIN).unwrap_or_else(|| panic!("Admin not initialized"))
}

/// Return the pending admin address proposed by the current admin, if any.
Expand All @@ -1666,7 +1674,7 @@ impl CampaignContract {
new_admin: Address,
) -> Result<(), Error> {
current_admin.require_auth();
let stored_admin: Address = env.storage().instance().get(&ADMIN).unwrap();
let stored_admin: Address = env.storage().instance().get(&ADMIN).ok_or(Error::Unauthorized)?;
if stored_admin != current_admin {
return Err(Error::Unauthorized);
}
Expand Down Expand Up @@ -1704,7 +1712,7 @@ impl CampaignContract {
/// Cancel an in-flight admin transfer (current admin only).
pub fn cancel_admin_transfer(env: Env, current_admin: Address) -> Result<(), Error> {
current_admin.require_auth();
let stored_admin: Address = env.storage().instance().get(&ADMIN).unwrap();
let stored_admin: Address = env.storage().instance().get(&ADMIN).ok_or(Error::Unauthorized)?;
if stored_admin != current_admin {
return Err(Error::Unauthorized);
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/campaign/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ fn test_referral_loop_direct_cycle_rejected() {
// we need to deregister Bob first to exercise the loop-detection branch.
client.deregister(&bob);
let result = client.try_register(&bob, &leaf, &proof, &None, &Some(alice.clone()));
assert_eq!(result, Ok(Err(Error::ReferralLoop)));
assert_eq!(result, Err(Ok(Error::ReferralLoop)));
}

#[test]
Expand All @@ -1588,7 +1588,7 @@ fn test_referral_loop_indirect_cycle_rejected() {
// Alice tries to register with carol as referrer → alice→carol→bob→alice cycle.
client.deregister(&alice);
let result = client.try_register(&alice, &leaf, &proof, &None, &Some(carol.clone()));
assert_eq!(result, Ok(Err(Error::ReferralLoop)));
assert_eq!(result, Err(Ok(Error::ReferralLoop)));
}

#[test]
Expand All @@ -1611,7 +1611,7 @@ fn test_referral_locked_prevents_referrer_switch_on_reregister() {
// Alice deregisters and tries to re-register with carol as new referrer.
client.deregister(&alice);
let result = client.try_register(&alice, &leaf, &proof, &None, &Some(carol.clone()));
assert_eq!(result, Ok(Err(Error::ReferralLocked)));
assert_eq!(result, Err(Ok(Error::ReferralLocked)));
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,58 @@
]
],
[],
[
[
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
{
"function": {
"contract_fn": {
"contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"function_name": "set_active",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
},
{
"u64": "11"
},
{
"bool": true
}
]
}
},
"sub_invocations": []
}
]
],
[],
[
[
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
{
"function": {
"contract_fn": {
"contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"function_name": "set_active",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
},
{
"u64": "12"
},
{
"bool": true
}
]
}
},
"sub_invocations": []
}
]
],
[],
[],
[]
],
Expand Down Expand Up @@ -343,7 +395,7 @@
"symbol": "anonce"
},
"val": {
"u64": "11"
"u64": "13"
}
},
{
Expand Down Expand Up @@ -467,6 +519,26 @@
},
"live_until": 6311999
},
{
"entry": {
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
"key": {
"ledger_key_nonce": {
"nonce": "1301173170172112462"
}
},
"durability": "temporary",
"val": "void"
}
},
"ext": "v0"
},
"live_until": 6311999
},
{
"entry": {
"last_modified_ledger_seq": 0,
Expand All @@ -487,6 +559,26 @@
},
"live_until": 6311999
},
{
"entry": {
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
"key": {
"ledger_key_nonce": {
"nonce": "3126073502131104533"
}
},
"durability": "temporary",
"val": "void"
}
},
"ext": "v0"
},
"live_until": 6311999
},
{
"entry": {
"last_modified_ledger_seq": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,6 @@
]
],
[],
[
[
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
{
"function": {
"contract_fn": {
"contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"function_name": "set_active",
"args": [
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
},
{
"u64": "5"
},
{
"bool": true
}
]
}
},
"sub_invocations": []
}
]
],
[],
[],
[]
],
Expand Down Expand Up @@ -213,7 +187,7 @@
"symbol": "anonce"
},
"val": {
"u64": "6"
"u64": "5"
}
},
{
Expand Down Expand Up @@ -317,26 +291,6 @@
},
"live_until": 6311999
},
{
"entry": {
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
"key": {
"ledger_key_nonce": {
"nonce": "4270020994084947596"
}
},
"durability": "temporary",
"val": "void"
}
},
"ext": "v0"
},
"live_until": 6311999
},
{
"entry": {
"last_modified_ledger_seq": 0,
Expand Down
Loading