Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 4c544c6

Browse files
committed
docs: comments re. storing the state in the WASM module
1 parent bc245b2 commit 4c544c6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

rust/src/contract.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ use crate::actions::transfer::transfer;
1010
use crate::js_imports::{Block, Transaction, log, Contract};
1111
use crate::state::{HandlerResult, State};
1212

13+
/*
14+
Note: in order do optimize communication between host and the WASM module,
15+
we're storing the state inside the WASM module (for the time of state evaluation).
16+
This allows to reduce the overhead of passing the state back and forth
17+
between the host and module with each contract interaction.
18+
In case of bigger states this overhead can be huge.
19+
Same approach has been implemented for the AssemblyScript version.
20+
21+
So the flow (from the SDK perspective) is:
22+
1. SDK calls exported WASM module function "initState" (with lastly cached state or initial state,
23+
if cache is empty) - which initializes the state in the WASM module.
24+
2. SDK calls "handle" function for each of the interaction.
25+
If given interaction was modifying the state - it is updated inside the WASM module
26+
- but not returned to host.
27+
3. Whenever SDK needs to know the current state (eg. in order to perform
28+
caching or to simply get its value after evaluating all of the interactions)
29+
- it calls WASM's module "currentState" function.
30+
31+
The handle function by default does not return the new state -
32+
it only updates it in the WASM module.
33+
The handle function returns a value only in case of error
34+
or calling a "view" function.
35+
36+
In the future this might also allow to enhance the inner-contracts communication
37+
- e.g. if the execution network will store the state of the contracts - as the WASM contract module memory
38+
- it would allow to read other contract's state "directly" from WASM module memory.
39+
*/
40+
41+
// inspired by https://github.com/dfinity/examples/blob/master/rust/basic_dao/src/basic_dao/src/lib.rs#L13
1342
thread_local! {
1443
static STATE: RefCell<State> = RefCell::default();
1544
}

0 commit comments

Comments
 (0)