Skip to content

Commit

Permalink
Changed era_time to auction_delay, which includes all eras.
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaplas committed Feb 3, 2025
1 parent 40c9405 commit f286ef0
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 85 deletions.
11 changes: 5 additions & 6 deletions core/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ pub trait HostContext {
/// Advances the block time by the specified time difference and processes auctions.
fn advance_with_auctions(&self, time_diff: u64);

/// Era length in milliseconds.
fn era_length(&self) -> u64;
/// Time between auctions in milliseconds.
fn auction_delay(&self) -> u64;

/// Returns the current block time.
fn block_time(&self) -> u64;
Expand Down Expand Up @@ -371,16 +371,15 @@ impl HostEnv {
}

/// Advances the block time by the specified time difference and processes auctions.
pub fn advance_with_rewards(&self, time_diff: u64) {
pub fn advance_with_auctions(&self, time_diff: u64) {
let backend = self.backend.borrow();
backend.advance_with_auctions(time_diff);
backend.advance_block_time(time_diff);
}

Check warning on line 377 in core/src/host.rs

View check run for this annotation

Codecov / codecov/patch

core/src/host.rs#L374-L377

Added lines #L374 - L377 were not covered by tests

/// Returns the era length in milliseconds.
pub fn era_length(&self) -> u64 {
pub fn auction_delay(&self) -> u64 {
let backend = self.backend.borrow();
backend.era_length()
backend.auction_delay()
}

Check warning on line 383 in core/src/host.rs

View check run for this annotation

Codecov / codecov/patch

core/src/host.rs#L380-L383

Added lines #L380 - L383 were not covered by tests

/// Returns the current block time.
Expand Down
127 changes: 64 additions & 63 deletions examples/src/features/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,67 +57,68 @@ pub enum ValError {
InsufficientBalance = 1
}

#[cfg(test)]
mod tests {

// Validators are now supported only on casper target
#[cfg(target_arch = "wasm32")]
#[test]
fn test_validators() {
use crate::features::validators::{ValidatorsContract, ValidatorsContractInitArgs};
use odra::casper_types::U512;
use odra::host::Deployer;
use odra::host::HostRef;

/// Time in milliseconds for one era. On livenet it's 120 minutes. On local, by default it's 41 seconds.
pub const ERA_DURATION: u64 = 41 * 1000;
let test_env = odra_test::env();
test_env.set_caller(test_env.get_account(0));
let mut staking = ValidatorsContract::deploy(
&test_env,
ValidatorsContractInitArgs {
validator: test_env.get_validator()
}
);

// Setup validators in vm
let inital_account_balance = test_env.balance_of(&test_env.get_account(0));

// Stake some amount
let staking_amount = U512::from(1_000_000_000_000u64);
staking.with_tokens(staking_amount).stake();
assert_eq!(staking.currently_delegated_amount(), staking_amount);
assert_eq!(
test_env.balance_of(&test_env.get_account(0)),
inital_account_balance - staking_amount
);

// Advance time, run auctions and give off rewards
test_env.advance_with_rewards(ERA_DURATION * 10);

// Check that the amount is greater than the staking amount
let staking_with_reward = staking.currently_delegated_amount();
assert!(staking_with_reward > staking_amount);

// Unstake
staking.unstake(staking_with_reward);
assert_eq!(staking.currently_delegated_amount(), U512::from(0));

// Withdraw should first fail, as we need to wait 7 eras
staking.try_withdraw(staking_with_reward).unwrap_err();
// To confirm, the contract balance should be 0
assert_eq!(staking.current_casper_balance(), U512::from(0));

// Advance time, run auctions and give off rewards
test_env.advance_with_rewards(ERA_DURATION * 8);
assert_eq!(staking.current_casper_balance(), staking_with_reward);
staking.withdraw(staking_with_reward);
assert_eq!(staking.current_casper_balance(), U512::from(0));

// The user now should have the tokens
assert_eq!(
test_env.balance_of(&test_env.get_account(0)),
staking_with_reward + inital_account_balance - staking_amount
);
}
// Validators are now supported only on casper target
#[cfg(target_arch = "wasm32")]
#[test]
fn test_validators() {
use crate::features::validators::{ValidatorsContract, ValidatorsContractInitArgs};
use odra::casper_types::U512;
use odra::host::Deployer;
use odra::host::HostRef;

let test_env = odra_test::env();
let auction_delay = test_env.auction_delay();
// Should we put it into host method?
let unbonding_delay = auction_delay * 8;

test_env.set_caller(test_env.get_account(0));
let mut staking = ValidatorsContract::deploy(
&test_env,
ValidatorsContractInitArgs {
validator: test_env.get_validator()
}
);

// Setup validators in vm
let inital_account_balance = test_env.balance_of(&test_env.get_account(0));

// Stake some amount
let staking_amount = U512::from(1_000_000_000_000u64);
staking.with_tokens(staking_amount).stake();
assert_eq!(staking.currently_delegated_amount(), staking_amount);
assert_eq!(
test_env.balance_of(&test_env.get_account(0)),
inital_account_balance - staking_amount
);

// Advance time, run auctions and give off rewards
// TODO: Why it works with auction delay * 2 and not with auction delay?
test_env.advance_with_auctions(auction_delay * 2);

// Check that the amount is greater than the staking amount
let staking_with_reward = staking.currently_delegated_amount();
assert!(staking_with_reward > staking_amount);

// Unstake
staking.unstake(staking_with_reward);
assert_eq!(staking.currently_delegated_amount(), U512::from(0));

// Withdraw should first fail, as we need to wait for auction delay
staking.try_withdraw(staking_with_reward).unwrap_err();
// To confirm, the contract balance should be 0
assert_eq!(staking.current_casper_balance(), U512::from(0));

// Advance time, run auctions and give off rewards
test_env.advance_with_auctions(unbonding_delay);
assert_eq!(staking.current_casper_balance(), staking_with_reward);
staking.withdraw(staking_with_reward);
assert_eq!(staking.current_casper_balance(), U512::from(0));

// The user now should have the tokens
assert_eq!(
test_env.balance_of(&test_env.get_account(0)),
staking_with_reward + inital_account_balance - staking_amount
);
}
#[cfg(test)]
mod tests {}
2 changes: 1 addition & 1 deletion odra-casper/livenet-env/src/livenet_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl HostContext for LivenetHost {
panic!("advance_with_auctions not supported for LivenetHost")
}

fn era_length(&self) -> u64 {
fn auction_delay(&self) -> u64 {
panic!("era_length not yet implemented for LivenetHost")
}
}
Expand Down
8 changes: 4 additions & 4 deletions odra-casper/rpc-client/src/casper_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ use casper_client::{
get_balance, get_deploy, get_transaction, put_transaction, query_global_state, JsonRpcId,
Verbosity
};
use casper_types::bytesrepr::{deserialize_from_slice, Bytes, FromBytes, ToBytes};
use casper_types::bytesrepr::{deserialize_from_slice, Bytes, ToBytes};
use casper_types::contracts::ContractPackageHash;
use casper_types::execution::ExecutionResultV1::{Failure, Success};
use casper_types::StoredValue::CLValue;
use casper_types::{
execution::ExecutionResult, runtime_args, sign, CLTyped, Digest, EntityAddr, Key, PublicKey,
RuntimeArgs, SecretKey, Transaction, TransactionHash, URef, U512
};
use casper_types::{ Deploy, DeployHash, ExecutableDeployItem, StoredValue, TimeDiff, Timestamp};
use casper_types::{Deploy, DeployHash, ExecutableDeployItem, StoredValue, TimeDiff, Timestamp};
use odra_core::casper_event_standard::EVENTS_LENGTH;
use odra_core::consts::{
AMOUNT_ARG, ARGS_ARG, ATTACHED_VALUE_ARG, ENTRY_POINT_ARG, EVENTS, PACKAGE_HASH_ARG,
Expand Down Expand Up @@ -344,7 +344,7 @@ impl CasperClient {
let result = r.map_err(|_| LivenetToDo)?;
let stored_value = result.result.stored_value;
let cl_value = stored_value.into_cl_value().ok_or(LivenetToDo)?;

Check warning on line 346 in odra-casper/rpc-client/src/casper_client.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/rpc-client/src/casper_client.rs#L344-L346

Added lines #L344 - L346 were not covered by tests

// Note: this is for compatibility with CEP18 named keys.
if cl_value.cl_type() == &Vec::<u8>::cl_type() {
let bytes = cl_value.into_t().map_err(|_| LivenetToDo)?;
Expand Down Expand Up @@ -465,7 +465,7 @@ impl CasperClient {
StoredValue::ContractPackage(package) => {
let last_version = package.current_contract_hash().unwrap();
EntityAddr::SmartContract(last_version.value())

Check warning on line 467 in odra-casper/rpc-client/src/casper_client.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/rpc-client/src/casper_client.rs#L451-L467

Added lines #L451 - L467 were not covered by tests
},
}
_ => {
panic!(
"Couldn't get entity addr for address: {:?}",
Expand Down
5 changes: 3 additions & 2 deletions odra-casper/test-vm/src/casper_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ impl HostContext for CasperHost {
self.vm.borrow_mut().transfer(to, amount)
}

fn era_length(&self) -> u64 {
self.vm.borrow().era_length()
fn auction_delay(&self) -> u64 {
let mut backend = self.vm.borrow_mut();
backend.auction_delay()
}

Check warning on line 180 in odra-casper/test-vm/src/casper_host.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/test-vm/src/casper_host.rs#L177-L180

Added lines #L177 - L180 were not covered by tests
}

Expand Down
17 changes: 9 additions & 8 deletions odra-casper/test-vm/src/vm/casper_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,14 @@ impl CasperVm {
/// Advances the block time by the specified time difference and processes auctions
/// Giving the rewards to the validators
pub fn advance_with_auctions(&mut self, time_diff: u64) {
let time_between_auctions = self
.context
.get_auction_delay()
.saturating_mul(self.context.chainspec().core_config.era_duration.millis());
let time_between_auctions = self.auction_delay();
// Calculate how many auctions we can run based on time_diff
let num_auctions = time_diff / time_between_auctions;

// Run auctions for each complete delay period
for _ in 0..num_auctions {
self.context.run_auction(0u64, vec![]);
self.advance_block_time(time_between_auctions);
}

Check warning on line 121 in odra-casper/test-vm/src/vm/casper_vm.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/test-vm/src/vm/casper_vm.rs#L112-L121

Added lines #L112 - L121 were not covered by tests

// Distribute rewards
Expand Down Expand Up @@ -147,11 +145,14 @@ impl CasperVm {

// Run remaining auctions with the leftover time
let remaining_time = time_diff % time_between_auctions;
self.advance_block_time(remaining_time);
}

Check warning on line 149 in odra-casper/test-vm/src/vm/casper_vm.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/test-vm/src/vm/casper_vm.rs#L124-L149

Added lines #L124 - L149 were not covered by tests

/// Gets the era length in milliseconds.
pub fn era_length(&self) -> u64 {
self.context.chainspec().core_config.era_duration.millis()
/// Gets the time between auctions.
pub fn auction_delay(&mut self) -> u64 {
self.context
.get_auction_delay()
.saturating_mul(self.context.chainspec().core_config.era_duration.millis())
}

Check warning on line 156 in odra-casper/test-vm/src/vm/casper_vm.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/test-vm/src/vm/casper_vm.rs#L152-L156

Added lines #L152 - L156 were not covered by tests

/// Gets the current block time.
Expand Down Expand Up @@ -624,7 +625,7 @@ impl CasperVm {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/chainspec.toml");
let mut builder = LmdbWasmTestBuilder::new_temporary_with_chainspec(chainspec_path);
// let mut builder = LmdbWasmTestBuilder::default();

builder.run_genesis(genesis_request).commit();

Check warning on line 629 in odra-casper/test-vm/src/vm/casper_vm.rs

View check run for this annotation

Codecov / codecov/patch

odra-casper/test-vm/src/vm/casper_vm.rs#L626-L629

Added lines #L626 - L629 were not covered by tests

// crank the auction
Expand Down
2 changes: 1 addition & 1 deletion odra-vm/src/odra_vm_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl HostContext for OdraVmHost {
.checked_transfer_tokens(&caller, &to, &amount)
}

fn era_length(&self) -> u64 {
fn auction_delay(&self) -> u64 {
1_000u64
}

Check warning on line 162 in odra-vm/src/odra_vm_host.rs

View check run for this annotation

Codecov / codecov/patch

odra-vm/src/odra_vm_host.rs#L160-L162

Added lines #L160 - L162 were not covered by tests
}
Expand Down

0 comments on commit f286ef0

Please sign in to comment.