Context: Logic and Actor State
Context is the most important concept in MOI blockchain and means the data, stored with actors. Logics operate on data that can be stored in logics themselves (logic state) or the data that the participants bring into interations (actor state). While logic state works as a regular smart-contract storage, known in other blockchains, actor state is the key feature that enables massive parallelism when executing logic calls. Actor state enables logics to store their data on actors, so when actors participate in an interaction with logic, they bring their own data, while the same logic can be used in other interactions with different actors at the same time.
As every logic can write the data to actors, each actor holds the data of multiple logics in its storage. Even logics themselves can act as actors, they can contain their own storage (logic state) and also the data of other logics. Logic can write (mutate) it's own data (either on it's own logic state or on any participating actor, e.g. on Sender) and it can read (observe) any data of any other logic. But it can't write (mutate) to other logic's data, not even in its own context.
In short, Coco supports two kinds of states:
- logic state — data stored under the logic itself (requires locking the logic to mutate).
- actor state — data stored under an actor’s context (the logic can mutate its state on multiple actors in parallel).
In the diagram above we see how storage ("context") of each actor (logic is also an actor for other logics) contains the data of different logics. Logic stores its own data (state logic) at the beginning of the storage while it can access its actor storage on actor context. Logic can observe and mutate its own data on actors, but only when actors participate in an interaction. E.g., Sender always participates so logic can read and write its data, stored in the Sender's context.
In the Logic box above one can notice some data outside of boxes (0xabdd...), this represents a single ("atomic") value of a complex object, e.g. each key of the map "balances" in slot 1 is stored in a different place, what's explained in the next section.
Storing Complex Objects
While primitive types (numbers, strings etc.) are stored in individual slots, complex types - maps, arrays and classes - have their contents scattered through the storage, what is called in MOI atomic storage. This enables very high efficiency for data management as we usually only access a few elements of such large structures. E.g. we can have a map with thousands of objects, but we only deal with the one that matches the current Sender. This implementation of storage enables MOI to only transfer a small share of data between the network storage and runtime (PISA) what drastically reduces gas costs.
Implementation of atomic storage is largely invisible to Coco with two exceptions. Transfer of complete complex objects (e.g. a complete map) to and from the state is not allowed by default, but if the developer explicitly instructs to compiler to allow such potentially costly operations, they can use micro keywords gather and disperse to observe or mutate complex objects in the storage. The use of both is in general discouraged, except for limited cases of e.g. initializing data structures that we know are empty or small, like in the example.
In the image above, an example of stored map balances is show where slot 1 only holds the length of the map and values are stored individually in places identified by key hashes. Actual key hash is a combination of logic identifier, slot and key, so there are no collisions between elements of different logics even if they are exactly the same.
Iterating Through Maps Not Possible
While atomic storage of maps where values are scattered and accessible by a hash of a key enables efficient transfer of individual map values, it's not possible to iterate over map values as keys are not stored in the map. If iteration is needed, map keys need to be stored in a separate varray over which we can iterate while map holds the values for these keys.