Skip to content

Commit

Permalink
near-sdk-sim 3.2.0: Expose cur block and fix storage costs (#390)
Browse files Browse the repository at this point in the history
* Expose `cur_block` and `genesis_config` from `RuntimeStandalone`. This allows to manipulate block time, e.g. set to a given timestamp.
* Use `RuntimeConfig::from_protocol_version` that fixes storage costs issue. Fixes: #388 
* Set root account balance to one billion tokens.

## Test plan:
- Modify FT test to verify storage cost fix by limiting initial deposit to 5 NEAR. It is not enough with old storage cost (50kb) and enough with new storage cost (500kb)

* make block time configurable to sim contract tests that depend on env::block_time

* Update near-sdk-sim/src/runtime.rs

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>

* use block_prod_time name to make it matches nearcore

* Expose cur_block. Fix storage cost

* Fix initial supply

* Modify FT test to verify storage cost fix

* Rework RuntimeConfig from genesis

Co-authored-by: Bo Yao <bo@nearprotocol.com>
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
  • Loading branch information
3 people authored May 3, 2021
1 parent 9af51b8 commit 0507deb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/fungible-token/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/fungible-token/tests/sim/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn register_user(user: &near_sdk_sim::UserAccount) {
pub fn init_no_macros(initial_balance: u128) -> (UserAccount, UserAccount, UserAccount) {
let root = init_simulator(None);

let ft = root.deploy(&FT_WASM_BYTES, FT_ID.into(), STORAGE_AMOUNT);
let ft = root.deploy(&FT_WASM_BYTES, FT_ID.into(), to_yocto("5"));

ft.call(
FT_ID.into(),
Expand Down
2 changes: 1 addition & 1 deletion near-sdk-sim/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "near-sdk-sim"
version = "3.1.1"
version = "3.2.0"
authors = ["Near Inc <hello@near.org>"]
edition = "2018"
license = "GPL-3.0"
Expand Down
15 changes: 9 additions & 6 deletions near-sdk-sim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ To view this documentation locally, clone this repo and from this folder run `ca

## Changelog

### `3.1.1`
### `3.2.0`

* Fixed stack overflow for deep chain [PR #385](https://github.com/near/near-sdk-rs/pull/385).
* Introduce `block_prod_time` duration in nanoseconds to `GenesisConfig` that defines the duration between produced blocks.
* Expose `cur_block` and `genesis_config` from `RuntimeStandalone`. This allows to manipulate block time.
* Use `RuntimeConfig::from_protocol_version` that fixes storage costs issue.
* Set root account balance to one billion tokens.

# Getting started

Expand All @@ -28,15 +31,15 @@ Currently this crate depends on a the GitHub repo of [nearcore](https://github.c

```toml
[dev-dependencies]
near-sdk-sim = "3.1.1"
near-sdk-sim = "3.2.0"

```

And update `near-sdk` too:

```toml
[dependencies]
near-sdk = "3.1.1"
near-sdk = "3.1.0"

```

Expand All @@ -61,8 +64,8 @@ Now in the root of the project (`contract-wrap`), create a new `Cargo.toml`. You

```toml
[dev-dependencies]
near-sdk = "3.1.1"
near-sdk-sim = "3.1.1"
near-sdk = "3.1.0"
near-sdk-sim = "3.2.0"
contract = { path = "./contract" }

[workspace]
Expand Down
15 changes: 10 additions & 5 deletions near-sdk-sim/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ pub struct GenesisConfig {

impl Default for GenesisConfig {
fn default() -> Self {
let runtime_config = RuntimeConfig::default();
let runtime_config = RuntimeConfig::from_protocol_version(
&Arc::new(RuntimeConfig::default()),
PROTOCOL_VERSION,
)
.as_ref()
.clone();
Self {
genesis_time: 0,
gas_price: 100_000_000,
Expand All @@ -74,7 +79,7 @@ impl Default for GenesisConfig {
impl GenesisConfig {
pub fn init_root_signer(&mut self, account_id: &str) -> InMemorySigner {
let signer = InMemorySigner::from_seed(account_id, KeyType::ED25519, "test");
let root_account = account_new(std::u128::MAX, CryptoHash::default());
let root_account = account_new(10u128.pow(33), CryptoHash::default());

self.state_records.push(StateRecord::Account {
account_id: account_id.to_string(),
Expand Down Expand Up @@ -146,12 +151,12 @@ impl Block {
}

pub struct RuntimeStandalone {
genesis: GenesisConfig,
pub genesis: GenesisConfig,
tx_pool: TransactionPool,
transactions: HashMap<CryptoHash, SignedTransaction>,
outcomes: HashMap<CryptoHash, ExecutionOutcome>,
profile: HashMap<CryptoHash, ProfileData>,
cur_block: Block,
pub cur_block: Block,
runtime: Runtime,
tries: ShardTries,
pending_receipts: Vec<Receipt>,
Expand Down Expand Up @@ -268,7 +273,7 @@ impl RuntimeStandalone {
random_seed: Default::default(),
epoch_id: EpochId::default(),
current_protocol_version: PROTOCOL_VERSION,
config: Arc::from(self.genesis.runtime_config.clone()),
config: Arc::new(self.genesis.runtime_config.clone()),
#[cfg(feature = "no_contract_cache")]
cache: None,
#[cfg(not(feature = "no_contract_cache"))]
Expand Down

0 comments on commit 0507deb

Please sign in to comment.