Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add offchain gas_price test
  • Loading branch information
ascjones committed Jul 2, 2020
commit a6668b7b5528a885801f5a86b4a6291b051f345f
8 changes: 8 additions & 0 deletions core/src/env/engine/off_chain/db/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ impl ChainSpec {
self.gas_price.decode().map_err(Into::into)
}

/// Set the gas price for the chain.
pub fn set_gas_price<T>(&mut self, gas_price: T::Balance)
where
T: EnvTypes
{
self.gas_price = OffBalance::new(&gas_price)
}

/// Returns the minimum balance for an account on the chain.
pub fn minimum_balance<T>(&self) -> Result<T::Balance>
where
Expand Down
4 changes: 4 additions & 0 deletions core/src/env/engine/off_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ impl EnvInstance {
.last_mut()
.ok_or_else(|| OffChainError::UninitializedBlocks)
}

fn chain_spec_mut(&mut self) -> &mut ChainSpec {
&mut self.chain_spec
}
}

impl OnInstance for EnvInstance {
Expand Down
11 changes: 11 additions & 0 deletions core/src/env/engine/off_chain/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use super::{
use crate::env::{
EnvTypes,
Result,
engine::off_chain::db::ChainSpec,
};
use ink_prelude::string::String;

Expand Down Expand Up @@ -210,6 +211,16 @@ where
.map_err(Into::into)
}

/// Update the [ChainSpec](`crate::env::engine::off_chain::db::ChainSpec`) for the test environment
pub fn update_chain_spec<F>(f: F) -> Result<()>
where
F: FnOnce(&mut ChainSpec) -> ()
{
Ok(<EnvInstance as OnInstance>::on_instance(|instance| {
f(instance.chain_spec_mut())
}))
}

/// Returns the contents of the past performed environmental `println` in order.
pub fn recorded_printlns() -> impl Iterator<Item = String> {
<EnvInstance as OnInstance>::on_instance(|instance| {
Expand Down
16 changes: 16 additions & 0 deletions core/src/env/engine/off_chain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ fn key_add_sub() -> Result<()> {
Ok(())
})
}

#[test]
fn gas_price() -> env::Result<()> {
env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
let gas_price= 2u32;
env::test::update_chain_spec(|chain_spec| {
chain_spec.set_gas_price::<env::DefaultEnvTypes>(gas_price.into())
})?;

assert_eq!(2u128, env::gas_price::<env::DefaultEnvTypes>(1).unwrap());
assert_eq!(20u128, env::gas_price::<env::DefaultEnvTypes>(10).unwrap());
assert_eq!(6u128, env::gas_price::<env::DefaultEnvTypes>(3).unwrap());

Ok(())
})
}