Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added delay #136

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions emitter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl Emitter for EmitterContract {

storage::set_backstop(&e, &backstop);
storage::set_blend_id(&e, &blnd_token_id);
storage::set_last_fork(&e, 0); // We set the block 45 days in the past to allow for an immediate initial drop
storage::set_drop_status(&e, false);
// TODO: Determine if setting the last distro time here is appropriate, since it means tokens immediately start being distributed
storage::set_last_distro_time(&e, &(e.ledger().timestamp() - 7 * 24 * 60 * 60));
}
Expand Down
23 changes: 17 additions & 6 deletions emitter/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn execute_swap_backstop(e: &Env, new_backstop_id: Address) {
if new_backstop_balance > backstop_balance {
storage::set_backstop(e, &new_backstop_id);
storage::set_drop_status(e, false);
storage::set_last_fork(e, e.ledger().sequence());
} else {
panic_with_error!(e, EmitterError::InsufficientBackstopSize);
}
Expand All @@ -42,6 +43,10 @@ pub fn execute_drop(e: &Env) -> Map<Address, i128> {
if storage::get_drop_status(e) {
panic_with_error!(e, EmitterError::BadDrop);
}
if storage::get_last_fork(e) + 777600 > e.ledger().sequence() {
// Check that the last fork was at least 45 days ago
panic_with_error!(e, EmitterError::BadDrop);
}
let backstop = storage::get_backstop(e);
let backstop_client = BackstopClient::new(e, &backstop);
let backstop_token = backstop_client.backstop_token();
Expand All @@ -65,6 +70,7 @@ pub fn execute_drop(e: &Env) -> Map<Address, i128> {

#[cfg(test)]
mod tests {

use crate::{
storage,
testutils::{create_backstop, create_emitter},
Expand Down Expand Up @@ -208,7 +214,7 @@ mod tests {
e.ledger().set(LedgerInfo {
timestamp: 12345,
protocol_version: 20,
sequence_number: 50,
sequence_number: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 10,
Expand Down Expand Up @@ -242,6 +248,7 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_drop_status(&e, false);
storage::set_last_fork(&e, 4000000);

let list = execute_drop(&e);
assert_eq!(storage::get_drop_status(&e), true);
Expand All @@ -263,7 +270,7 @@ mod tests {
e.ledger().set(LedgerInfo {
timestamp: 12345,
protocol_version: 20,
sequence_number: 50,
sequence_number: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 10,
Expand Down Expand Up @@ -296,6 +303,7 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_drop_status(&e, true);
storage::set_last_fork(&e, 4000000);

execute_drop(&e);
assert_eq!(storage::get_drop_status(&e), true);
Expand All @@ -311,7 +319,7 @@ mod tests {
e.ledger().set(LedgerInfo {
timestamp: 12345,
protocol_version: 20,
sequence_number: 50,
sequence_number: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 10,
Expand Down Expand Up @@ -344,22 +352,23 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_drop_status(&e, false);
storage::set_last_fork(&e, 4000000);

execute_drop(&e);
assert_eq!(storage::get_drop_status(&e), false);
});
}

#[test]
#[should_panic(expected = "Error(Storage, MissingValue)")]
fn test_drop_no_status() {
#[should_panic(expected = "Error(Contract, #40)")]
fn test_drop_bad_block() {
let e = Env::default();
e.mock_all_auths_allowing_non_root_auth();

e.ledger().set(LedgerInfo {
timestamp: 12345,
protocol_version: 20,
sequence_number: 50,
sequence_number: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 10,
Expand Down Expand Up @@ -391,6 +400,8 @@ mod tests {
e.as_contract(&emitter, || {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_last_fork(&e, 5000000);
storage::set_drop_status(&e, false);

execute_drop(&e);
});
Expand Down
36 changes: 24 additions & 12 deletions emitter/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum EmitterDataKey {
LastDistro,
// The drop status for the current backstop
DropStatus,
// The last block emissions were forked
LastFork,
}

/// Bump the instance rent for the contract. Bumps for 10 days due to the 7-day cycle window of this contract
Expand Down Expand Up @@ -117,13 +119,8 @@ pub fn set_last_distro_time(e: &Env, last_distro: &u64) {
///
/// Returns true if the emitter has dropped
pub fn get_drop_status(e: &Env) -> bool {
e.storage().persistent().bump(
&EmitterDataKey::DropStatus,
LEDGER_THRESHOLD_SHARED,
LEDGER_BUMP_SHARED,
);
e.storage()
.persistent()
.instance()
.get(&EmitterDataKey::DropStatus)
.unwrap_optimized()
}
Expand All @@ -134,11 +131,26 @@ pub fn get_drop_status(e: &Env) -> bool {
/// * `new_status` - new drop status
pub fn set_drop_status(e: &Env, new_status: bool) {
e.storage()
.persistent()
.instance()
.set::<EmitterDataKey, bool>(&EmitterDataKey::DropStatus, &new_status);
e.storage().persistent().bump(
&EmitterDataKey::DropStatus,
LEDGER_THRESHOLD_SHARED,
LEDGER_BUMP_SHARED,
);
}

/// Get the last block an emission fork was executed
///
/// Returns true if the emitter has dropped
pub fn get_last_fork(e: &Env) -> u32 {
e.storage()
.instance()
.get(&EmitterDataKey::LastFork)
.unwrap_optimized()
}

/// Set whether the emitter has performed the drop distribution or not for the current backstop
///
/// ### Arguments
/// * `new_status` - new drop status
pub fn set_last_fork(e: &Env, block: u32) {
e.storage()
.instance()
.set::<EmitterDataKey, u32>(&EmitterDataKey::LastFork, &block);
}
4 changes: 2 additions & 2 deletions test-suites/src/liquidity_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub(crate) fn create_lp_pool<'a>(
let token_2_client = TokenClient::new(e, token_2);
token_1_client.mint(&admin, &1_000_0000000);
token_2_client.mint(&admin, &25_0000000);
token_1_client.approve(&admin, &contract_address, &1_000_0000000, &535670);
token_2_client.approve(&admin, &contract_address, &1_000_0000000, &535670);
token_1_client.approve(&admin, &contract_address, &1_000_0000000, &5356700);
token_2_client.approve(&admin, &contract_address, &1_000_0000000, &5356700);

client.init(&Address::random(e), &admin);
client.bundle_bind(
Expand Down
2 changes: 1 addition & 1 deletion test-suites/src/test_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl TestFixture<'_> {
e.ledger().set(LedgerInfo {
timestamp: 1441065600, // Sept 1st, 2015 (backstop epoch)
protocol_version: 20,
sequence_number: 100,
sequence_number: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 999999,
Expand Down