|
| 1 | +//! Lifecycle transition tests for multi-arbiter (multi-sig) escrows. |
| 2 | +//! |
| 3 | +//! `deposit_with_arbiters` stores its arbiters in `EscrowEntry.arbiters` with |
| 4 | +//! `arbiter: None`, but `dispute()` reads only `EscrowEntry.arbiter`. These |
| 5 | +//! tests pin down that a multi-sig escrow can actually reach `Disputed` and be |
| 6 | +//! resolved, which is the precondition for `vote_for_dispute` and |
| 7 | +//! `resolve_dispute_multi_sig` being reachable at all. |
| 8 | +
|
| 9 | + |
| 10 | +use soroban_sdk::{testutils::Address as _, token, Address, Bytes, Env, Vec}; |
| 11 | + |
| 12 | +use crate::{types::EscrowStatus, RustAcademyContract, RustAcademyContractClient}; |
| 13 | + |
| 14 | +fn setup<'a>() -> (Env, RustAcademyContractClient<'a>, Address) { |
| 15 | + let env = Env::default(); |
| 16 | + env.mock_all_auths(); |
| 17 | + let id = env.register(RustAcademyContract, ()); |
| 18 | + let client = RustAcademyContractClient::new(&env, &id); |
| 19 | + let admin = Address::generate(&env); |
| 20 | + client.initialize(&admin); |
| 21 | + let token = env |
| 22 | + .register_stellar_asset_contract_v2(Address::generate(&env)) |
| 23 | + .address(); |
| 24 | + (env, client, token) |
| 25 | +} |
| 26 | + |
| 27 | +/// A multi-sig escrow must be able to enter `Disputed`. |
| 28 | +/// |
| 29 | +/// Without this, `vote_for_dispute` and `resolve_dispute_multi_sig` are |
| 30 | +/// unreachable: both require `status == Disputed`, and `dispute()` is the only |
| 31 | +/// transition into that state. |
| 32 | +#[test] |
| 33 | +fn multi_sig_escrow_can_be_disputed() { |
| 34 | + let (env, client, token) = setup(); |
| 35 | + let owner = Address::generate(&env); |
| 36 | + let amount: i128 = 3_000; |
| 37 | + let salt = Bytes::from_slice(&env, b"multisig_dispute_lifecycle"); |
| 38 | + |
| 39 | + token::StellarAssetClient::new(&env, &token).mint(&owner, &amount); |
| 40 | + |
| 41 | + let mut arbiters = Vec::new(&env); |
| 42 | + arbiters.push_back(Address::generate(&env)); |
| 43 | + arbiters.push_back(Address::generate(&env)); |
| 44 | + arbiters.push_back(Address::generate(&env)); |
| 45 | + |
| 46 | + let commitment = |
| 47 | + client.deposit_with_arbiters(&token, &amount, &owner, &salt, &0u64, &arbiters, &2u32); |
| 48 | + |
| 49 | + assert_eq!( |
| 50 | + client.get_commitment_state(&commitment), |
| 51 | + Some(EscrowStatus::Pending), |
| 52 | + "escrow should start Pending" |
| 53 | + ); |
| 54 | + |
| 55 | + let result = client.try_dispute(&commitment); |
| 56 | + assert!( |
| 57 | + result.is_ok(), |
| 58 | + "a multi-sig escrow must be disputable; otherwise its arbiters can never act" |
| 59 | + ); |
| 60 | + |
| 61 | + assert_eq!( |
| 62 | + client.get_commitment_state(&commitment), |
| 63 | + Some(EscrowStatus::Disputed), |
| 64 | + "escrow should be Disputed after dispute()" |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +/// Once a multi-sig escrow is disputed, its assigned arbiters must be able to |
| 69 | +/// vote. This is the step that is dead code today. |
| 70 | +#[test] |
| 71 | +fn multi_sig_arbiters_can_vote_once_disputed() { |
| 72 | + let (env, client, token) = setup(); |
| 73 | + let owner = Address::generate(&env); |
| 74 | + let amount: i128 = 3_000; |
| 75 | + let salt = Bytes::from_slice(&env, b"multisig_vote_lifecycle"); |
| 76 | + |
| 77 | + token::StellarAssetClient::new(&env, &token).mint(&owner, &amount); |
| 78 | + |
| 79 | + let a1 = Address::generate(&env); |
| 80 | + let a2 = Address::generate(&env); |
| 81 | + let mut arbiters = Vec::new(&env); |
| 82 | + arbiters.push_back(a1.clone()); |
| 83 | + arbiters.push_back(a2.clone()); |
| 84 | + |
| 85 | + let commitment = |
| 86 | + client.deposit_with_arbiters(&token, &amount, &owner, &salt, &0u64, &arbiters, &2u32); |
| 87 | + |
| 88 | + client.dispute(&commitment); |
| 89 | + |
| 90 | + let vote = client.try_vote_for_dispute(&a1, &commitment, &true); |
| 91 | + assert!( |
| 92 | + vote.is_ok(), |
| 93 | + "an assigned multi-sig arbiter must be able to vote on a disputed escrow" |
| 94 | + ); |
| 95 | +} |
0 commit comments