@@ -10,6 +10,35 @@ use crate::actions::transfer::transfer;
10
10
use crate :: js_imports:: { Block , Transaction , log, Contract } ;
11
11
use crate :: state:: { HandlerResult , State } ;
12
12
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
13
42
thread_local ! {
14
43
static STATE : RefCell <State > = RefCell :: default ( ) ;
15
44
}
0 commit comments