-
Notifications
You must be signed in to change notification settings - Fork 216
The philosophy of RChain node architecture
This is a quick guide to RChain node (RNode) architecture and overview of the source code. It is meant for developers who wants to contribute in the core development but also for everyone who wants to know deeper understanding of Rholang implementation and the use of Scala in the functional style.
DRAFT NOTICE: Some parts of the document are incomplete or work in progress.
Rholang is higher-order concurrent language based on rho-calculus.
TODO: more about Rholang with simple examples.
This repository contains the source code of Rholang implementation in Scala. It consist of three main projects Rholang, RSpace and Casper.
Rholang syntax is defined with BNFC grammar from which Java parser is generated in ./rholang/main/src/java/
directory (defined as sbt build task bnfc:generate
).
Rholang source code supplied in a deploy is parsed and AST is processed in normalize definitions and transformed to Rholang types defined currently in protobuf.
The main file for the evaluation of normalized Rholang term, the Par
top level object, is in Reduce
implementation with eval as the entry point.
Reducer is directly dependent on RSpace to read data needed for evaluation and to save the result back.
The main operations in reducer are substitution, sorting and cost accounting of Rholang terms.
RSpace is an implementation of tuple space. It has two basic operations, produce and consume called as a result of the reducer (Rholang) execution.
The implementation of RSpace is split in three files. Base abstract definition is in RSpaceOps class with two uses, RSpace for the main execution and ReplayRSpace used for execution validation in replay and it's constrained by the execution trace produced with RSpace
variant.
RSpace uses databases for Produce and Consume operations but it also include database for channels used in multiple consumes (Joins). Together with these three operations, RSpace has in memory cache (HotStore), pattern matching, serialization, hashing and history store (Merkle key-value store).
Casper project contains a blockchain implementation with the Casper consensus protocol. The consensus uses Proof of Stake algorithm written as a smart contract in Rholang.
Block processing (validation) is in MultiParentCasperImpl class which contains the main logic for block processing (validation). Block creation is in BlockCreator class together with the Proposer.
Execution of deploys (Rholang code) is managed via RuntimeManager which spawns Rholang Runtime instances. Rholang Runtime is connection to Rholang and RSpace projects and can create a checkpoint which is then used as a new state in block creation and validation.
The Casper project is little overloaded with implementations which could be in Node project e.g. reference to LMDB via shared project. Refactoring and cleanup is in progress and the idea is to create SDK project with definition of the protocol, serialization, hashing and etc. to be as a reference for easier implementation in other languages.
The Node project contains main function and it's the point where instances (mostly) are created.
TODO: more details about shared and helper projects.
Here are examples of a few tests that runs overall functionality of RNode and Rholang.
Casper project tests which executes Rholang but also involves block creation (replay) and depends on creation of genesis block as part of test execution. Rholang project tests which are smaller in scope with Rholang execution only.
- succeed if given enough phlos for deploy - execute deploy and create a block
- perform the most basic deploy successfully - execute deploy, create a block and replay the block
For more detailed logging in Casper tests, logback config level can be set to info
.
- basic Rholang - execution of Rholang term
- stack safety tests - Rholang execution with more heavy operations like deeply nested terms or string concatenation (recursion limit can be increased to increase the load)