Skip to content

Commit

Permalink
check for current bidding, and save historical
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Sep 26, 2024
1 parent a0dc6e2 commit 247a9d1
Showing 1 changed file with 70 additions and 31 deletions.
101 changes: 70 additions & 31 deletions framework/libra-framework/sources/ol_sources/secret_bid.move
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module ol_framework::secret_bid {

struct CommittedBid has key {
reveal_entry_fee: u64,
entry_fee_history: vector<u64>, // keep previous 7 days bids
commit_digest: vector<u8>,
commit_epoch: u64,
}
Expand All @@ -45,38 +46,14 @@ module ol_framework::secret_bid {
epoch: u64,
}

/// check if we are within the reveal window
/// do not allow bids within the reveal window
/// allow reveal transaction to be submitted
fun in_reveal_window(): bool {
// get the timestamp
// NOTE: this might cause a dependency cycle issue in the future
let remaining_secs = block::get_remaining_epoch_secs();
let window = if (testnet::is_testnet()) {
// ten secs
10
} else {
// five mins
60*5
};

if (remaining_secs > window) {
return false
};
true
}

fun is_init(account: address): bool {
exists<CommittedBid>(account)
}

/// Transaction entry function for committing bid
public entry fun commit(user: &signer, digest: vector<u8>) acquires CommittedBid {
// don't allow commiting within reveal window
assert!(!in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW));
commit_entry_fee_impl(user, digest);
}

// TODO: the public key could be the consensus_pubkey that is already registered for the validator. That way the operator does not need the root key for bidding strategy. Note it's a BLS key.
/// Transaction entry function for revealing bid
public entry fun reveal(user: &signer, pk: vector<u8>, entry_fee: u64, signed_msg: vector<u8>) acquires CommittedBid {
// don't allow commiting within reveal window
assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW));
Expand All @@ -89,14 +66,33 @@ module ol_framework::secret_bid {
if (!is_init(signer::address_of(user))) {
move_to<CommittedBid>(user, CommittedBid {
reveal_entry_fee: 0,
entry_fee_history: vector::empty(),
commit_digest: vector::empty(),
commit_epoch: 0,
});
};

let state = borrow_global_mut<CommittedBid>(signer::address_of(user));
// if first commit in an epoch reset the counters
maybe_reset_bids(state);

state.commit_digest = digest;
state.commit_epoch = epoch_helper::get_current_epoch();
}

/// if this is the first commit in the epoch then we can reset bids
fun maybe_reset_bids(state: &mut CommittedBid) {
if (epoch_helper::get_current_epoch() > state.commit_epoch) {
// restart bidding
state.commit_epoch = epoch_helper::get_current_epoch();

vector::push_back(&mut state.entry_fee_history, state.reveal_entry_fee);

if (vector::length(&state.entry_fee_history) > 7) {
vector::trim(&mut state.entry_fee_history, 7);
};

state.reveal_entry_fee = 0;
}
}

/// The hashing protocol which the client will be submitting commitments
Expand All @@ -121,13 +117,10 @@ module ol_framework::secret_bid {
assert!(is_init(signer::address_of(user)), error::invalid_state(ECOMMIT_BID_NOT_INITIALIZED));

let state = borrow_global_mut<CommittedBid>(signer::address_of(user));


// must reveal within the current epoch of the bid
let epoch = epoch_helper::get_current_epoch();
assert!(epoch == state.commit_epoch, error::invalid_state(EMISMATCH_EPOCH));


let commitment = make_hash(entry_fee, epoch, signed_msg);

let bid = Bid {
Expand All @@ -139,7 +132,6 @@ module ol_framework::secret_bid {

assert!(comparator::is_equal(&comparator::compare(&commitment, &state.commit_digest)), error::invalid_argument(ECOMMIT_DIGEST_NOT_EQUAL));


state.reveal_entry_fee = entry_fee;
}

Expand All @@ -158,6 +150,53 @@ module ol_framework::secret_bid {
////////
}

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

#[view]
/// check if we are within the reveal window
/// do not allow bids within the reveal window
/// allow reveal transaction to be submitted
public fun in_reveal_window(): bool {
// get the timestamp
// NOTE: using block:: might cause a dependency cycle issue in the future
let remaining_secs = block::get_remaining_epoch_secs();
let window = if (testnet::is_testnet()) {
// ten secs
10
} else {
// five mins
60*5
};

if (remaining_secs > window) {
return false
};
true
}

#[view]
public fun is_init(account: address): bool {
exists<CommittedBid>(account)
}

#[view]
/// get the current bid, and exclude bids that are stale
public fun current_revealed_bid(user: address): u64 acquires CommittedBid {
// 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
}

#[view]
/// get the current bid, and exclude bids that are stale
public fun historical_bids(user: address): vector<u64> acquires CommittedBid {
let state = borrow_global<CommittedBid>(user);
state.entry_fee_history
}

//////// TESTS ////////
#[test]
fun test_sign_message() {
Expand Down

0 comments on commit 247a9d1

Please sign in to comment.