diff --git a/contracts/escrow/src/lib.rs b/contracts/escrow/src/lib.rs index 5a25941..564dd34 100644 --- a/contracts/escrow/src/lib.rs +++ b/contracts/escrow/src/lib.rs @@ -256,6 +256,11 @@ impl EscrowContract { env.storage() .persistent() .set(&DataKey::ActiveMatches, &active); + env.storage().persistent().extend_ttl( + &DataKey::ActiveMatches, + MATCH_TTL_LEDGERS, + MATCH_TTL_LEDGERS, + ); env.events().publish( (Symbol::new(&env, "match"), symbol_short!("created")), @@ -714,10 +719,19 @@ impl EscrowContract { /// Return all currently active (non-cancelled, non-completed) match IDs. pub fn get_active_matches(env: Env) -> Vec { - env.storage() + let active: Vec = env + .storage() .persistent() .get(&DataKey::ActiveMatches) - .unwrap_or_else(|| vec![&env]) + .unwrap_or_else(|| vec![&env]); + if !active.is_empty() { + env.storage().persistent().extend_ttl( + &DataKey::ActiveMatches, + MATCH_TTL_LEDGERS, + MATCH_TTL_LEDGERS, + ); + } + active } /// Add a token to the allowlist. Requires admin auth. @@ -750,6 +764,11 @@ impl EscrowContract { env.storage() .persistent() .set(&DataKey::ActiveMatches, &active); + env.storage().persistent().extend_ttl( + &DataKey::ActiveMatches, + MATCH_TTL_LEDGERS, + MATCH_TTL_LEDGERS, + ); } } } diff --git a/contracts/escrow/src/tests.rs b/contracts/escrow/src/tests.rs index 3f9efac..ad68601 100644 --- a/contracts/escrow/src/tests.rs +++ b/contracts/escrow/src/tests.rs @@ -1371,6 +1371,67 @@ fn test_ttl_extended_on_deposit() { assert_eq!(ttl, crate::MATCH_TTL_LEDGERS); } +#[test] +fn test_active_matches_ttl_refreshed_on_append_and_removal() { + let (env, contract_id, _oracle, player1, player2, token, _admin) = setup(); + let client = EscrowContractClient::new(&env, &contract_id); + + let match1 = client.create_match( + &player1, + &player2, + &100, + &token, + &String::from_str(&env, "ttl_active_append_remove_1"), + &Platform::Lichess, + ); + + env.ledger().set(soroban_sdk::testutils::LedgerInfo { + sequence_number: env.ledger().sequence() + 1000, + timestamp: env.ledger().timestamp() + 5000, + protocol_version: 22, + network_id: Default::default(), + base_reserve: 10, + min_temp_entry_ttl: 1, + min_persistent_entry_ttl: 1, + max_entry_ttl: crate::MATCH_TTL_LEDGERS + 2000, + }); + + let _match2 = client.create_match( + &player1, + &player2, + &100, + &token, + &String::from_str(&env, "ttl_active_append_remove_2"), + &Platform::Lichess, + ); + + let ttl_after_append = env.as_contract(&contract_id, || { + env.storage().persistent().get_ttl(&DataKey::ActiveMatches) + }); + assert_eq!(ttl_after_append, crate::MATCH_TTL_LEDGERS); + + client.deposit(&match1, &player1); + client.deposit(&match1, &player2); + + env.ledger().set(soroban_sdk::testutils::LedgerInfo { + sequence_number: env.ledger().sequence() + 1000, + timestamp: env.ledger().timestamp() + 5000, + protocol_version: 22, + network_id: Default::default(), + base_reserve: 10, + min_temp_entry_ttl: 1, + min_persistent_entry_ttl: 1, + max_entry_ttl: crate::MATCH_TTL_LEDGERS + 2000, + }); + + client.submit_result(&match1, &Winner::Player1); + + let ttl_after_removal = env.as_contract(&contract_id, || { + env.storage().persistent().get_ttl(&DataKey::ActiveMatches) + }); + assert_eq!(ttl_after_removal, crate::MATCH_TTL_LEDGERS); +} + #[test] fn test_ttl_extended_on_submit_result() { let (env, contract_id, _oracle, player1, player2, token, _admin) = setup(); diff --git a/task1.md b/task1.md index b6fa4e0..206c508 100644 --- a/task1.md +++ b/task1.md @@ -1,14 +1,14 @@ -#618 Add Test: escrow instance TTL is refreshed after create_match -Repo Avatar -StellarCheckMate/Checkmate-Escrow -Status: Open - unassigned -Labels: testing, storage -Priority: Medium - -Description: -Coverage for issue #3 should include creation paths. - -Tasks: - -Inspect instance TTL before and after create_match -Assert refresh \ No newline at end of file +#618 Add Test: escrow instance TTL is refreshed after create_match +Repo Avatar +StellarCheckMate/Checkmate-Escrow +Status: Open - unassigned +Labels: testing, storage +Priority: Medium + +Description: +Coverage for issue #3 should include creation paths. + +Tasks: + +Inspect instance TTL before and after create_match +Assert refresh