From 941e5a618158aa91f708af1a20695cb1748e9c43 Mon Sep 17 00:00:00 2001 From: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:59:57 -0400 Subject: [PATCH] check for current bidding, and save historical --- .../sources/ol_sources/secret_bid.move | 102 ++++++++++++------ 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/framework/libra-framework/sources/ol_sources/secret_bid.move b/framework/libra-framework/sources/ol_sources/secret_bid.move index 12e403bbb..8e47750af 100644 --- a/framework/libra-framework/sources/ol_sources/secret_bid.move +++ b/framework/libra-framework/sources/ol_sources/secret_bid.move @@ -36,6 +36,7 @@ module ol_framework::secret_bid { struct CommittedBid has key { reveal_entry_fee: u64, + entry_fee_history: vector, // keep previous 7 days bids commit_digest: vector, commit_epoch: u64, } @@ -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(account) - } - + /// Transaction entry function for committing bid public entry fun commit(user: &signer, digest: vector) 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, entry_fee: u64, signed_msg: vector) acquires CommittedBid { // don't allow commiting within reveal window assert!(in_reveal_window(), error::invalid_state(ENOT_IN_REVEAL_WINDOW)); @@ -89,14 +66,33 @@ module ol_framework::secret_bid { if (!is_init(signer::address_of(user))) { move_to(user, CommittedBid { reveal_entry_fee: 0, + entry_fee_history: vector::empty(), commit_digest: vector::empty(), commit_epoch: 0, }); }; let state = borrow_global_mut(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 @@ -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(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 { @@ -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; } @@ -158,6 +150,54 @@ 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(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. + // should also fail gracefully if this is called on epoch boundary. + if (in_reveal_window()) return 0; + + let state = borrow_global(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 acquires CommittedBid { + let state = borrow_global(user); + state.entry_fee_history + } + //////// TESTS //////// #[test] fun test_sign_message() {