Skip to content

Commit

Permalink
sorting of list of secret bids
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Sep 26, 2024
1 parent 247a9d1 commit 053fbce
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
67 changes: 63 additions & 4 deletions framework/libra-framework/sources/ol_sources/secret_bid.move
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ module ol_framework::secret_bid {
use diem_framework::block;

use ol_framework::testnet;
use ol_framework::address_utils;

#[test_only]
use diem_framework::account;
// use diem_framework::debug::print;
#[test_only]
use diem_framework::system_addresses;


/// User bidding not initialized
const ECOMMIT_BID_NOT_INITIALIZED: u64 = 1;
Expand Down Expand Up @@ -150,8 +153,32 @@ module ol_framework::secret_bid {
////////
}

/// populate the list of bids, but don't sort
fun get_bids_for_account_list(list: vector<address>): (vector<address>, vector<u64>) acquires CommittedBid {
let bids_vec = vector<u64>[];
vector::for_each_ref(&list, |el| {
let b = get_bid_unchecked(*el);
vector::push_back(&mut bids_vec, b);
});

(list, bids_vec)
}

/// get a sorted list of the addresses and their entry fee
public(friend) fun get_bids_by_account_sort_low_high(list: vector<address>): (vector<address>, vector<u64>) acquires CommittedBid {
let (addr, bids) = get_bids_for_account_list(list);
address_utils::sort_by_values(&mut addr, &mut bids);
(addr, bids)
}

///////// GETTERS ////////

fun get_bid_unchecked(user: address): u64 acquires CommittedBid {
let state = borrow_global<CommittedBid>(user);
if (state.commit_epoch != epoch_helper::get_current_epoch()) return 0;
state.reveal_entry_fee
}

#[view]
/// check if we are within the reveal window
/// do not allow bids within the reveal window
Expand Down Expand Up @@ -185,9 +212,7 @@ module ol_framework::secret_bid {
// if we are not in reveal window this information will be confusing.
assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW));

let state = borrow_global<CommittedBid>(user);
if (state.commit_epoch != epoch_helper::get_current_epoch()) return 0;
state.reveal_entry_fee
get_bid_unchecked(user)
}

#[view]
Expand All @@ -198,6 +223,19 @@ module ol_framework::secret_bid {
}

//////// TESTS ////////

#[test_only]
public(friend) fun mock_revealed_bid(framework: &signer, user: &signer, reveal_entry_fee: u64, commit_epoch: u64) {
system_addresses::assert_diem_framework(framework);
testnet::assert_testnet(framework);
move_to(user, CommittedBid {
reveal_entry_fee,
entry_fee_history: vector[0],
commit_digest: vector[0],
commit_epoch,
});
}

#[test]
fun test_sign_message() {
use diem_std::from_bcs;
Expand Down Expand Up @@ -385,4 +423,25 @@ module ol_framework::secret_bid {

reveal_entry_fee_impl(&alice, pk_bytes, 5, sig_bytes);
}

#[test(framework = @0x1, alice = @0x10001, bob = @0x10002, carol = @0x10003)]
fun test_sorting_secret_bids(framework: &signer, alice: &signer, bob: &signer, carol: &signer) acquires CommittedBid {
testnet::initialize(framework);

let this_epoch = 1;
epoch_helper::test_set_epoch(framework, this_epoch);

mock_revealed_bid(framework, alice, 234, this_epoch);
mock_revealed_bid(framework, bob, 1, this_epoch);
mock_revealed_bid(framework, carol, 30, this_epoch);

let (accounts, bids) = get_bids_for_account_list(vector[@0x10001, @0x10002, @0x10003]);

assert!(*vector::borrow(&accounts, 0) == @0x10001, 713570001);
assert!(*vector::borrow(&bids, 0) == 234, 713570002);

let (sorted_accounts, sorted_bids) = get_bids_by_account_sort_low_high(vector[@0x10001, @0x10002, @0x10003]);
assert!(*vector::borrow(&sorted_accounts, 0) == @0x10002, 713570003);
assert!(*vector::borrow(&sorted_bids, 0) == 1, 713570004);
}
}
7 changes: 7 additions & 0 deletions framework/libra-framework/sources/ol_sources/testnet.move
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ module ol_framework::testnet {
chain_id::get() == 2 // TESTNET named chain
}

#[test_only]
public fun initialize(vm: &signer) {
use diem_framework::system_addresses;
system_addresses::assert_ol(vm);
chain_id::initialize_for_test(vm, 4);
}

#[test_only]
public fun unset(vm: &signer) {
use diem_framework::system_addresses;
Expand Down

0 comments on commit 053fbce

Please sign in to comment.