|
| 1 | +#[test_only] |
| 2 | +module ol_framework::test_permanent_vouches { |
| 3 | + use std::vector; |
| 4 | + use ol_framework::vouch; |
| 5 | + use ol_framework::mock; |
| 6 | + use ol_framework::epoch_helper; |
| 7 | + |
| 8 | + #[test(root = @ol_framework, alice = @0x1000a, _bob = @0x1000b)] |
| 9 | + fun vouch_persists_across_epochs(root: &signer, alice: &signer, _bob: &signer) { |
| 10 | + // Initialize genesis and validators |
| 11 | + mock::ol_test_genesis(root); |
| 12 | + mock::create_validator_accounts(root, 2, false); |
| 13 | + vouch::set_vouch_price(root, 0); |
| 14 | + |
| 15 | + // Alice vouches for Bob |
| 16 | + vouch::vouch_for(alice, @0x1000b); |
| 17 | + |
| 18 | + // Verify vouch exists |
| 19 | + let received_vouches = vouch::get_received_vouches_not_expired(@0x1000b); |
| 20 | + assert!(vector::contains(&received_vouches, &@0x1000a), 1); |
| 21 | + |
| 22 | + // Simulate multiple epochs passing (more than old expiration limit) |
| 23 | + let i = 0; |
| 24 | + while (i < 50) { // Beyond old EXPIRATION_ELAPSED_EPOCHS |
| 25 | + mock::trigger_epoch(root); |
| 26 | + i = i + 1; |
| 27 | + }; |
| 28 | + |
| 29 | + // Verify vouch still exists (permanent vouches) |
| 30 | + let received_vouches_after = vouch::get_received_vouches_not_expired(@0x1000b); |
| 31 | + assert!(vector::contains(&received_vouches_after, &@0x1000a), 2); |
| 32 | + } |
| 33 | + |
| 34 | + #[test(root = @ol_framework, alice = @0x1000a, _bob = @0x1000b)] |
| 35 | + fun vouch_revocation_still_works(root: &signer, alice: &signer, _bob: &signer) { |
| 36 | + // Initialize genesis and validators |
| 37 | + mock::ol_test_genesis(root); |
| 38 | + mock::create_validator_accounts(root, 2, false); |
| 39 | + vouch::set_vouch_price(root, 0); |
| 40 | + |
| 41 | + // Alice vouches for Bob |
| 42 | + vouch::vouch_for(alice, @0x1000b); |
| 43 | + |
| 44 | + // Verify vouch exists |
| 45 | + let received_vouches = vouch::get_received_vouches_not_expired(@0x1000b); |
| 46 | + assert!(vector::contains(&received_vouches, &@0x1000a), 1); |
| 47 | + |
| 48 | + // Alice revokes vouch for Bob |
| 49 | + vouch::revoke(alice, @0x1000b); |
| 50 | + |
| 51 | + // Verify vouch is removed |
| 52 | + let received_vouches_after = vouch::get_received_vouches_not_expired(@0x1000b); |
| 53 | + assert!(!vector::contains(&received_vouches_after, &@0x1000a), 2); |
| 54 | + |
| 55 | + // Verify given vouches are also updated |
| 56 | + let given_vouches = vouch::get_given_vouches_not_expired(@0x1000a); |
| 57 | + assert!(!vector::contains(&given_vouches, &@0x1000b), 3); |
| 58 | + } |
| 59 | + |
| 60 | + #[test(root = @ol_framework, alice = @0x1000a, _bob = @0x1000b)] |
| 61 | + fun vouch_update_extends_permanently(root: &signer, alice: &signer, _bob: &signer) { |
| 62 | + // Initialize genesis and validators |
| 63 | + mock::ol_test_genesis(root); |
| 64 | + mock::create_validator_accounts(root, 2, false); |
| 65 | + vouch::set_vouch_price(root, 0); |
| 66 | + |
| 67 | + // Alice vouches for Bob initially |
| 68 | + vouch::vouch_for(alice, @0x1000b); |
| 69 | + |
| 70 | + let (_received_vouches, epochs) = vouch::get_received_vouches(@0x1000b); |
| 71 | + let initial_epoch = *vector::borrow(&epochs, 0); |
| 72 | + |
| 73 | + // Advance one epoch to ensure difference |
| 74 | + mock::trigger_epoch(root); |
| 75 | + |
| 76 | + // Alice vouches for Bob again (should update epoch) |
| 77 | + vouch::vouch_for(alice, @0x1000b); |
| 78 | + |
| 79 | + let (received_vouches_after, epochs_after) = vouch::get_received_vouches(@0x1000b); |
| 80 | + let updated_epoch = *vector::borrow(&epochs_after, 0); |
| 81 | + |
| 82 | + // Should still have exactly one vouch from Alice |
| 83 | + assert!(vector::length(&received_vouches_after) == 1, 1); |
| 84 | + assert!(vector::contains(&received_vouches_after, &@0x1000a), 2); |
| 85 | + |
| 86 | + // Epoch should be updated to current |
| 87 | + let current_epoch = epoch_helper::get_current_epoch(); |
| 88 | + assert!(updated_epoch == current_epoch, 3); |
| 89 | + assert!(updated_epoch >= initial_epoch, 4); |
| 90 | + } |
| 91 | + |
| 92 | + #[test(root = @ol_framework, alice = @0x1000a, _bob = @0x1000b, _charlie = @0x1000c)] |
| 93 | + fun lifetime_tracking_with_permanent_vouches(root: &signer, alice: &signer, _bob: &signer, _charlie: &signer) { |
| 94 | + // Initialize genesis and validators |
| 95 | + mock::ol_test_genesis(root); |
| 96 | + mock::create_validator_accounts(root, 3, false); |
| 97 | + vouch::set_vouch_price(root, 0); |
| 98 | + |
| 99 | + // Alice vouches for both Bob and Charlie |
| 100 | + vouch::vouch_for(alice, @0x1000b); |
| 101 | + vouch::vouch_for(alice, @0x1000c); |
| 102 | + |
| 103 | + // Check that Alice's given count is 2 |
| 104 | + let given_vouches = vouch::get_given_vouches_not_expired(@0x1000a); |
| 105 | + assert!(vector::length(&given_vouches) == 2, 1); |
| 106 | + |
| 107 | + // Alice revokes vouch for Bob |
| 108 | + vouch::revoke(alice, @0x1000b); |
| 109 | + |
| 110 | + // Check that Alice's given count is now 1 |
| 111 | + let given_vouches_after = vouch::get_given_vouches_not_expired(@0x1000a); |
| 112 | + assert!(vector::length(&given_vouches_after) == 1, 2); |
| 113 | + assert!(vector::contains(&given_vouches_after, &@0x1000c), 3); |
| 114 | + assert!(!vector::contains(&given_vouches_after, &@0x1000b), 4); |
| 115 | + } |
| 116 | + |
| 117 | + #[test(root = @ol_framework, alice = @0x1000a, _bob = @0x1000b)] |
| 118 | + fun true_friends_with_permanent_vouches(root: &signer, alice: &signer, _bob: &signer) { |
| 119 | + // Initialize genesis and validators |
| 120 | + mock::ol_test_genesis(root); |
| 121 | + mock::create_validator_accounts(root, 2, false); |
| 122 | + vouch::set_vouch_price(root, 0); |
| 123 | + |
| 124 | + // Alice vouches for Bob |
| 125 | + vouch::vouch_for(alice, @0x1000b); |
| 126 | + |
| 127 | + // Test the core permanent vouch functionality |
| 128 | + // 1. Check that the raw vouch exists |
| 129 | + let received_vouches = vouch::get_received_vouches_not_expired(@0x1000b); |
| 130 | + assert!(vector::contains(&received_vouches, &@0x1000a), 1); |
| 131 | + |
| 132 | + // 2. Check that the given vouch exists |
| 133 | + let given_vouches = vouch::get_given_vouches_not_expired(@0x1000a); |
| 134 | + assert!(vector::contains(&given_vouches, &@0x1000b), 2); |
| 135 | + |
| 136 | + // 3. Verify that vouches persist after many epochs (permanent aspect) |
| 137 | + let i = 0; |
| 138 | + while (i < 10) { // Multiple epochs to test persistence |
| 139 | + mock::trigger_epoch(root); |
| 140 | + i = i + 1; |
| 141 | + }; |
| 142 | + |
| 143 | + // Vouches should still exist after many epochs |
| 144 | + let received_vouches_after = vouch::get_received_vouches_not_expired(@0x1000b); |
| 145 | + assert!(vector::contains(&received_vouches_after, &@0x1000a), 3); |
| 146 | + |
| 147 | + let given_vouches_after = vouch::get_given_vouches_not_expired(@0x1000a); |
| 148 | + assert!(vector::contains(&given_vouches_after, &@0x1000b), 4); |
| 149 | + |
| 150 | + // Note: We avoid testing true_friends and is_valid_voucher_for here because |
| 151 | + // they apply ancestry filtering which may filter out vouches between test addresses |
| 152 | + } |
| 153 | +} |
0 commit comments