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
16 changes: 12 additions & 4 deletions contracts/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ impl EscrowContract {
}
}

Self::remove_live_match(env.clone(), match_id);

m.state = MatchState::Completed;
m.completed_ledger = Some(env.ledger().sequence());
Self::remove_active_match(&env, match_id);
Expand Down Expand Up @@ -749,11 +751,12 @@ impl EscrowContract {
.get(&DataKey::MatchCount)
.unwrap_or(0);

for i in 0..count {
for i in 0..ids.len() {
let match_id = *ids.get(i).unwrap();
if let Ok(m) = env
.storage()
.persistent()
.get::<DataKey, Match>(&DataKey::Match(i))
.get::<DataKey, Match>(&DataKey::Match(match_id))
{
if m.state == state {
matches.push_back(m);
Expand All @@ -779,11 +782,16 @@ impl EscrowContract {
let mut skipped = 0u32;
let mut added = 0u32;

for i in 0..count {
for i in 0..ids.len() {
if skipped < offset {
skipped = skipped.saturating_add(1);
continue;
}
let match_id = *ids.get(i).unwrap();
if let Ok(m) = env
.storage()
.persistent()
.get::<DataKey, Match>(&DataKey::Match(i))
.get::<DataKey, Match>(&DataKey::Match(match_id))
{
if m.state != state {
continue;
Expand Down
33 changes: 33 additions & 0 deletions contracts/escrow/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,4 +1784,37 @@ fn test_get_live_matches_returns_only_fully_funded_matches() {
let live_matches = client.get_live_matches();
assert_eq!(live_matches.len(), 1);
assert_eq!(live_matches.get(0).unwrap().id, active_id);
assert_ne!(live_matches.get(0).unwrap().id, pending_id);
}

#[test]
fn test_get_active_matches_excludes_pending_matches() {
let (env, contract_id, _oracle, player1, player2, token, _admin) = setup();
let client = EscrowContractClient::new(&env, &contract_id);

// Create pending match and active match.
let pending_id = client.create_match(
&player1,
&player2,
&100,
&token,
&String::from_str(&env, "pending_match_exclusion"),
&Platform::Lichess,
);

let active_id = client.create_match(
&player1,
&player2,
&100,
&token,
&String::from_str(&env, "active_match_exclusion"),
&Platform::Lichess,
);
client.deposit(&active_id, &player1);
client.deposit(&active_id, &player2);

let active_matches = client.get_active_matches();
assert_eq!(active_matches.len(), 1);
assert_eq!(active_matches.get(0).unwrap().id, active_id);
assert_ne!(active_matches.get(0).unwrap().id, pending_id);
}
8 changes: 4 additions & 4 deletions contracts/escrow/src/tests/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ fn test_expire_match_refunds_depositor_after_timeout() {
.extend_ttl_for_code(token.clone(), MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS);
env.as_contract(&contract_id, || {
env.storage().persistent().extend_ttl(
&DataKey::ActiveMatches,
&DataKey::LiveMatches,
MATCH_TTL_LEDGERS,
MATCH_TTL_LEDGERS,
);
Expand Down Expand Up @@ -1304,7 +1304,7 @@ fn test_get_match_returns_cancelled_after_expire_match() {
}
env.as_contract(&contract_id, || {
env.storage().persistent().extend_ttl(
&DataKey::ActiveMatches,
&DataKey::LiveMatches,
MATCH_TTL_LEDGERS,
MATCH_TTL_LEDGERS,
);
Expand All @@ -1323,7 +1323,7 @@ fn test_get_match_returns_cancelled_after_expire_match() {
}
env.as_contract(&contract_id, || {
env.storage().persistent().extend_ttl(
&DataKey::ActiveMatches,
&DataKey::LiveMatches,
MATCH_TTL_LEDGERS,
MATCH_TTL_LEDGERS,
);
Expand Down Expand Up @@ -1535,7 +1535,7 @@ fn test_expire_match_refunds_both_players_when_both_deposited_but_still_pending(
.extend_ttl_for_code(token.clone(), MATCH_TTL_LEDGERS, MATCH_TTL_LEDGERS);
env.as_contract(&contract_id, || {
env.storage().persistent().extend_ttl(
&DataKey::ActiveMatches,
&DataKey::LiveMatches,
MATCH_TTL_LEDGERS,
MATCH_TTL_LEDGERS,
);
Expand Down
4 changes: 2 additions & 2 deletions contracts/escrow/src/tests/ttl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn test_active_matches_ttl_refreshed_on_append_and_removal() {
);

let ttl_after_append = env.as_contract(&contract_id, || {
env.storage().persistent().get_ttl(&DataKey::ActiveMatches)
env.storage().persistent().get_ttl(&DataKey::LiveMatches)
});
assert_eq!(ttl_after_append, crate::MATCH_TTL_LEDGERS);

Expand All @@ -122,7 +122,7 @@ fn test_active_matches_ttl_refreshed_on_append_and_removal() {
client.submit_result(&match1, &Winner::Player1);

let ttl_after_removal = env.as_contract(&contract_id, || {
env.storage().persistent().get_ttl(&DataKey::ActiveMatches)
env.storage().persistent().get_ttl(&DataKey::LiveMatches)
});
assert_eq!(ttl_after_removal, crate::MATCH_TTL_LEDGERS);
}
Expand Down
Loading