Skip to content

Commit

Permalink
adds vouch tests and refactors vouch queries
Browse files Browse the repository at this point in the history
  • Loading branch information
soaresa committed Aug 12, 2024
1 parent 028057a commit 05d7284
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 29 deletions.
110 changes: 110 additions & 0 deletions framework/libra-framework/sources/ol_sources/tests/vouch.test.move
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,42 @@ module ol_framework::test_vouch {
assert!(vouch::get_vouch_price() == 9_500, 73570027);
}

#[test(root = @ol_framework, alice = @0x1000a, bob = @0x1000b)]
fun update_vouch(root: &signer, alice: &signer) {
// create vals without vouches
mock::create_vals(root, 2, false);
vouch::set_vouch_price(root, 0);

// alice vouches for bob
vouch::vouch_for(alice, @0x1000b);

// check alice
let (given_vouches, given_epochs) = vouch::get_given_vouches(@0x1000a);
assert!(given_vouches == vector[@0x1000b], 73570005);
assert!(given_epochs == vector[0], 73570006);

// check bob
let (received_vouches, received_epochs) = vouch::get_received_vouches(@0x1000b);
assert!(received_vouches == vector[@0x1000a], 73570007);
assert!(received_epochs == vector[0], 73570008);

// fast forward to epoch 1
mock::trigger_epoch(root);

// alice vouches for bob again
vouch::vouch_for(alice, @0x1000b);

// check alice
let (given_vouches, given_epochs) = vouch::get_given_vouches(@0x1000a);
assert!(given_vouches == vector[@0x1000b], 73570005);
assert!(given_epochs == vector[1], 73570006);

// check bob
let (received_vouches, received_epochs) = vouch::get_received_vouches(@0x1000b);
assert!(received_vouches == vector[@0x1000a], 73570007);
assert!(received_epochs == vector[1], 73570008);
}

// Sad Day scenarios

#[test(root = @ol_framework, alice = @0x1000a)]
Expand Down Expand Up @@ -223,4 +259,78 @@ module ol_framework::test_vouch {
assert!(received_epochs == vector::empty(), 73570024);
}

#[test(root = @ol_framework, alice = @0x1000a)]
#[expected_failure(abort_code = 0x30006, location = ol_framework::vouch)]
fun vouch_without_init(alice: &signer) {
// alice vouches for bob without init
vouch::vouch_for(alice, @0x1000b);
}

#[test(root = @ol_framework, alice = @0x1000a)]
#[expected_failure(abort_code = 0x30002, location = ol_framework::vouch)]
fun vouch_for_account_not_init(root: &signer, alice: &signer) {
mock::create_vals(root, 1, false);

// alice vouches for bob without init
vouch::vouch_for(alice, @0x1000b);
}

#[test(root = @ol_framework, alice = @0x1000a, bob = @0x1000b)]
#[expected_failure(abort_code = 0x30006, location = ol_framework::vouch)]
fun revoke_without_init(alice: &signer) {
// alice try to revoke bob without vouch
vouch::revoke(alice, @0x1000b);
}

#[test(root = @ol_framework, alice = @0x1000a)]
#[expected_failure(abort_code = 0x10005, location = ol_framework::vouch)]
fun revoke_account_not_vouched(root: &signer, alice: &signer) {
mock::create_vals(root, 2, false);

// alice try to revoke bob without vouch
vouch::revoke(alice, @0x1000b);
}


#[test(root = @ol_framework, alice = @0x1000a)]
fun true_friends_not_init() {
// alice try to get true friends in list without init
let result = vouch::true_friends(@0x1000a);

assert!(result == vector::empty(), 73570028);
}

#[test(root = @ol_framework, alice = @0x1000a)]
fun true_friends_in_list_not_init() {
// alice try to get true friends in list without init
let (list, size) = vouch::true_friends_in_list(@0x1000a, &vector::singleton(@0x1000b));

assert!(list == vector::empty(), 73570028);
assert!(size == 0, 73570029);
}

#[test(root = @ol_framework, alice = @0x1000a)]
fun get_received_vouches_not_init() {
// alice try to get received vouches without init
let (received_vouches, received_epochs) = vouch::get_received_vouches(@0x1000a);

assert!(received_vouches == vector::empty(), 73570030);
assert!(received_epochs == vector::empty(), 73570031);
}

#[test(root = @ol_framework, alice = @0x1000a)]
#[expected_failure(abort_code = 0x30003, location = ol_framework::vouch)]
fun get_given_vouches_not_init() {
// alice try to get given vouches without init
vouch::get_given_vouches(@0x1000a);
}

#[test(root = @ol_framework, alice = @0x1000a)]
fun get_all_vouchers_not_init() {
// alice try to get all vouchers without init
let all_vouchers = vouch::all_vouchers(@0x1000a);

assert!(all_vouchers == vector::empty(), 73570034);
}

}
52 changes: 23 additions & 29 deletions framework/libra-framework/sources/ol_sources/vouch.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module ol_framework::vouch {
use ol_framework::ol_account;
use ol_framework::epoch_helper;

use diem_framework::account;
use diem_framework::system_addresses;
use diem_framework::transaction_fee;

Expand Down Expand Up @@ -123,14 +122,18 @@ module ol_framework::vouch {
};
}

fun vouch_impl(grantor: &signer, friend_acc: address) acquires ReceivedVouches, GivenVouches, VouchPrice {
fun vouch_impl(grantor: &signer, friend_acc: address, check_unrelated: bool) acquires ReceivedVouches, GivenVouches, VouchPrice {
let grantor_acc = signer::address_of(grantor);
assert!(grantor_acc != friend_acc, error::invalid_argument(ETRY_SELF_VOUCH_REALLY));

// check if structures are initialized
assert!(is_init(grantor_acc), error::invalid_state(EGRANTOR_NOT_INIT));
assert!(is_init(friend_acc), error::invalid_state(ERECEIVER_NOT_INIT));

if (check_unrelated) {
ancestry::assert_unrelated(grantor_acc, friend_acc);
};

// check if the grantor has already reached the limit of vouches
let (given_vouches, _) = get_given_vouches(grantor_acc);
assert!(
Expand Down Expand Up @@ -190,14 +193,13 @@ module ol_framework::vouch {
/// prevents spending a vouch that would not be counted.
/// to add a vouch and ignore this check use insist_vouch
public entry fun vouch_for(grantor: &signer, friend_acc: address) acquires ReceivedVouches, GivenVouches, VouchPrice {
ancestry::assert_unrelated(signer::address_of(grantor), friend_acc);
vouch_impl(grantor, friend_acc);
vouch_impl(grantor, friend_acc, true);
}

/// you may want to add people who are related to you
/// there are no known use cases for this at the moment.
public entry fun insist_vouch_for(grantor: &signer, friend_acc: address) acquires ReceivedVouches, GivenVouches, VouchPrice {
vouch_impl(grantor, friend_acc);
vouch_impl(grantor, friend_acc, false);
}

public entry fun revoke(grantor: &signer, friend_acc: address) acquires ReceivedVouches, GivenVouches {
Expand Down Expand Up @@ -347,28 +349,29 @@ module ol_framework::vouch {
#[view]
/// gets all buddies, including expired ones
public fun all_vouchers(val: address): vector<address> acquires ReceivedVouches {

if (!exists<ReceivedVouches>(val)) return vector::empty<address>();
let state = borrow_global<ReceivedVouches>(val);
*&state.incoming_vouches
let (incoming_vouches, _) = get_received_vouches(val);
incoming_vouches
}

#[view]
/// gets the buddies and checks if they are expired
/// gets the received vouches not expired
public fun all_not_expired(addr: address): vector<address> acquires ReceivedVouches {
let valid_vouches = vector::empty<address>();
if (is_init(addr)) {
let state = borrow_global<ReceivedVouches>(addr);
vector::for_each(state.incoming_vouches, |buddy_acc| {
// account might have dropped
if (account::exists_at(buddy_acc)){
if (is_not_expired(buddy_acc, state)) {
vector::push_back(&mut valid_vouches, buddy_acc)
}
}

})
let (all_received, epoch_vouched) = get_received_vouches(addr);
let current_epoch = epoch_helper::get_current_epoch();

let i = 0;
while (i < vector::length(&all_received)) {
let vouch_received = vector::borrow(&all_received, i);
let when_vouched = *vector::borrow(&epoch_vouched, i);
// check if the vouch is expired
if ((when_vouched + EXPIRATION_ELAPSED_EPOCHS) > current_epoch) {
vector::push_back(&mut valid_vouches, *vouch_received)
};
i = i + 1;
};

valid_vouches
}

Expand All @@ -389,15 +392,6 @@ module ol_framework::vouch {
vector::contains(&list, &voucher)
}

fun is_not_expired(voucher: address, state: &ReceivedVouches): bool {
let (found, i) = vector::index_of(&state.incoming_vouches, &voucher);
if (found) {
let when_vouched = vector::borrow(&state.epoch_vouched, i);
return (*when_vouched + EXPIRATION_ELAPSED_EPOCHS) > epoch_helper::get_current_epoch()
};
false
}

/// for a given list find and count any of my vouchers
public(friend) fun true_friends_in_list(addr: address, list: &vector<address>): (vector<address>, u64) acquires ReceivedVouches {
if (!exists<ReceivedVouches>(addr)) return (vector::empty(), 0);
Expand Down

0 comments on commit 05d7284

Please sign in to comment.