Skip to content

Commit

Permalink
fix: simulator doesn't fail with long chains (#385)
Browse files Browse the repository at this point in the history
* fix: simulator doesn't fail with long chains

The core issue is that Block is essentially a linked list, so:

* dropping it may SO
* cloning it is linear and may SO

Fix the drop manually and optimize the clone to be O(1)

closes #379

* Update version

Co-authored-by: Evgeny Kuzyakov <ek@nearprotocol.com>
  • Loading branch information
matklad and Evgeny Kuzyakov authored Apr 29, 2021
1 parent 42107dc commit 1951284
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 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 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.0"
version = "3.1.1"
authors = ["Near Inc <hello@near.org>"]
edition = "2018"
license = "GPL-3.0"
Expand Down
14 changes: 10 additions & 4 deletions near-sdk-sim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ As a first step, you can use this library! With it, you can:

To view this documentation locally, clone this repo and from this folder run `cargo doc --open`.

## Changelog

### `3.1.1`

* Fixed stack overflow for deep chain [PR #385](https://github.com/near/near-sdk-rs/pull/385).

# Getting started

This section will guide you through our suggested approach to adding simulation tests to your project. Want an example? Check out the [Fungible Token Example](https://github.com/near/near-sdk-rs/tree/master/examples/fungible-token).
Expand All @@ -22,15 +28,15 @@ Currently this crate depends on a the GitHub repo of [nearcore](https://github.c

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

```

And update `near-sdk` too:

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

```

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

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

[workspace]
Expand Down
21 changes: 19 additions & 2 deletions near-sdk-sim/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl GenesisConfig {

#[derive(Debug, Default, Clone)]
pub struct Block {
prev_block: Option<Box<Block>>,
prev_block: Option<Arc<Block>>,
state_root: CryptoHash,
gas_burnt: Gas,
pub epoch_height: EpochHeight,
Expand All @@ -97,6 +97,17 @@ pub struct Block {
pub gas_limit: Gas,
}

impl Drop for Block {
fn drop(&mut self) {
// Blocks form a liked list, so the generated recursive drop overflows
// the stack. Let's use an explicit loop to avoid that.
let mut curr = self.prev_block.take();
while let Some(mut next) = curr.and_then(|it| Arc::try_unwrap(it).ok()) {
curr = next.prev_block.take();
}
}
}

impl Block {
pub fn genesis(genesis_config: &GenesisConfig) -> Self {
Self {
Expand All @@ -116,7 +127,7 @@ impl Block {
gas_price: self.gas_price,
gas_limit: self.gas_limit,
block_timestamp: self.block_timestamp + 1_000_000_000,
prev_block: Some(Box::new(self.clone())),
prev_block: Some(Arc::new(self.clone())),
state_root: new_state_root,
block_height: self.block_height + 1,
epoch_height: (self.block_height + 1) / epoch_length,
Expand Down Expand Up @@ -517,4 +528,10 @@ mod tests {
runtime.force_account_update("root".into(), &bob_account);
assert_eq!(runtime.view_account(&"root").unwrap().locked, 10000);
}

#[test]
fn can_produce_many_blocks_without_stack_overflow() {
let (mut runtime, _signer, _) = init_runtime(None);
runtime.produce_blocks(20_000).unwrap();
}
}

0 comments on commit 1951284

Please sign in to comment.