Skip to content

Commit

Permalink
added delay
Browse files Browse the repository at this point in the history
  • Loading branch information
markuspluna committed Oct 22, 2023
1 parent 83d6267 commit f2f905d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
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, e.ledger().sequence() - 777600); // 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
62 changes: 59 additions & 3 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,6 +352,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);

execute_drop(&e);
assert_eq!(storage::get_drop_status(&e), false);
Expand Down Expand Up @@ -392,6 +401,53 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);

execute_drop(&e);
});
}
#[test]
#[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: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_expiration: 10,
min_persistent_entry_expiration: 10,
max_entry_expiration: 2000000,
});

let bombadil = Address::random(&e);
let frodo = Address::random(&e);
let samwise = Address::random(&e);
let emitter = create_emitter(&e);
let (backstop, backstop_client) = create_backstop(&e);

let backstop_token = e.register_stellar_asset_contract(bombadil.clone());
let drop_list = map![
&e,
(frodo.clone(), 20_000_000 * SCALAR_7),
(samwise.clone(), 30_000_000 * SCALAR_7)
];

backstop_client.initialize(
&backstop_token,
&Address::random(&e),
&Address::random(&e),
&Address::random(&e),
&drop_list,
);

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
32 changes: 32 additions & 0 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 @@ -142,3 +144,33 @@ pub fn set_drop_status(e: &Env, new_status: bool) {
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().persistent().bump(
&EmitterDataKey::DropStatus,
LEDGER_THRESHOLD_SHARED,
LEDGER_BUMP_SHARED,
);
e.storage()
.persistent()
.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()
.persistent()
.set::<EmitterDataKey, u32>(&EmitterDataKey::LastFork, &block);
e.storage().persistent().bump(
&EmitterDataKey::LastFork,
LEDGER_THRESHOLD_SHARED,
LEDGER_BUMP_SHARED,
);
}
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

0 comments on commit f2f905d

Please sign in to comment.