5

When writing smart contracts it's important to make sure that all data structures used are deterministic.

Specifically, if HashMap or HashSet are used - is there a possible non determinism coming from Rust standard library?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
ilblackdragon
  • 1,834
  • 12
  • 12

2 Answers2

3

Since Wasm runtime doesn't have access to non-deterministic inputs, the entire execution is deterministic. HashSet and HashMap use seed from the available source, in case of Wasm compilation there are no available source, the execution will always be the same. It should be easy to confirm.

Evgeny Kuzyakov
  • 1,078
  • 3
  • 5
  • Keep in mind that WASI has `random_get` and both WASI and emscripten have I/O operations. Rust's std uses non-det hasher by default when targeting those platforms. So unless you're using `wasm32-unknown-unknown` target your `HashMap`s will still be non-det by default. – sinan Aug 12 '21 at 10:23
2

To add to what @evgeny-kuzyakov said, the only sources of non-determinism are OS-level features, like threads, clock, OS randomness, networking, devices, file system, etc. If contract is compiled with the code that tries to access these features it will not be executed on our blockchain (though one can deploy it, because one can deploy any sequence of bytes as a contract) and will fail with an error before the execution starts.