Proposal: stateless architecture
Current Sia architecture (stateful)
In existing implementation modules are stateful, i.e. many of them have their own states and write to / read from the database. When new blocks are accepted, consensus changes (like contract diffs or Coin outputs diffs) are pushed to all the "subscriber" modules (for example, explorer
or host
) from the consensus module. This approach has the following disadvantages:
- If the program crashes at the moment of deploying updates, it may result in synchronization issues, because different modules will correspond to different blockchain states. This could be solved by manually checking ID of last block and reverting all the modules to one with oldest data, but this is not optimal solution both in terms of performance and code clarity.
- This architecture leads to huge performance overhead when accepting new blocks, because data has to be passed to all the independent subscriber modules, and each of these modules produces its own extra DB writes (= more disk seeks), slowing down blockchain import a lot.
- Code itself is not clean, because logic of writing changes to DB is spread among all the subscriber modules. This makes it really hard to work on further blockchain import speed optimizations, because all the modules would have to be modified. This also means that code is overcomplicated and has way more lines than stateless implementation would have.
- If there is any common data that is needed in several stateful modules, there is no sense in storing it twice in different modules.
Stateless architecture
The idea is to develop consensus module to make it contain overall node's state, with any data that any of other modules might need. Other modules, that currently all have their own states, will ask main module for the exact data they need instead. Actually many states that are spread across many modules are replaced with the main state module. This solution fixes all of the above disadvantages, but requires global codebase changes.