|
| 1 | +.. _rholang: |
| 2 | + |
| 3 | +################################################################################ |
| 4 | +Rholang - A Concurrent Smart Contracting Language |
| 5 | +################################################################################ |
| 6 | + |
| 7 | +Rholang is a fully featured, general purpose, Turing complete programming language |
| 8 | +built from the rho-calculus. It is a behaviorally typed, reflective, higher-order |
| 9 | +process language, and the official smart contracting language of RChain - its purpose |
| 10 | +is to concretize language-level concurrency. |
| 11 | + |
| 12 | +Necessarily, the language is concurrency-oriented, with a focus on message-passing |
| 13 | +through input-guarded channels. Channels are statically typed and can be used as |
| 14 | +single message-pipes, streams, or data stores. Similar to typed functional languages, |
| 15 | +Rholang will support immutable data structures. |
| 16 | + |
| 17 | +To get a taste of Rholang, here’s a contract named :code:`{Cell}` that holds a value |
| 18 | +and allows clients to get and set it: |
| 19 | + |
| 20 | +.. code-block:: scala |
| 21 | +
|
| 22 | + contract Cell( get, set, state ) = { |
| 23 | + select { |
| 24 | + case rtn <- get; v <- state => { |
| 25 | + rtn!( *v ) | state!( *v ) | Cell( get, set, state ) |
| 26 | + } |
| 27 | + case newValue <- set; v <- state => { |
| 28 | + state!( newValue ) | Cell( get, set, state ) |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +
|
| 33 | +This contract takes a channel for {get} requests, a channel for {set} requests, and a |
| 34 | +{state} channel where we will hold a the data resource. It waits on the {get} and {set} |
| 35 | +channels for client requests. Client requests are pattern matched via {case} class. |
| 36 | + |
| 37 | +Upon receiving a request, the contract joins {;} an incoming client with a request against |
| 38 | +the {state} channel. This join does two things. Firstly, it removes the internal {state} |
| 39 | +from access while this, in turn, serializes {get} and {set} actions, so that they are |
| 40 | +always operating against a single consistent copy of the resource - simultaneously providing |
| 41 | +a data resource synchronization mechanism and a memory of accesses and updates against the {state}. |
| 42 | + |
| 43 | +In the {case} of {get}, a request comes in with a {rtn} channel where the value, {v}, in i |
| 44 | +{state} will be sent. Since {v} has been taken from the {state} channel, it is put back, and |
| 45 | +the {Cell} behavior is recursively invoked. |
| 46 | + |
| 47 | +In the {case} of {set}, a request comes in with a {newValue}, which is published to the |
| 48 | +{state} channel (the old value having been stolen by the join). Meanwhile, the {Cell} behavior |
| 49 | +is recursively invoked. |
| 50 | + |
| 51 | +Confirmed by {select}, only one of the threads in {Cell} can respond to the client request. |
| 52 | +It’s a race, and the losing thread, be it getter or setter, is killed. This way, when the |
| 53 | +recursive invocation of {Cell} is called, the losing thread is not hanging around, yet the new |
| 54 | +{Cell} process is still able to respond to either type of client request. |
| 55 | + |
| 56 | +For an historical narrative leading up to Rholang, see `Mobile Process Calculi for Programming the Blockchain`_. |
| 57 | + |
| 58 | + |
| 59 | +.. _Mobile Process Calculi for Programming the Blockchain: https://docs.google.com/document/d/1lAbB_ssUvUkJ1D6_16WEp4FzsH0poEqZYCi-FBKanuY/edit |
| 60 | + |
| 61 | +================= |
| 62 | +Behavioural Types |
| 63 | +================= |
| 64 | + |
| 65 | +The task of creating a tractable and fault-tolerant execution environment for a |
| 66 | +system of communicating processes is non-trivial without a built-in mechanism to |
| 67 | +standardize the notion of 'safe' interaction among those processes. |
| 68 | + |
| 69 | +Our intention from the beginning was to create a rho-bust language capable of |
| 70 | +expressing smart contracts with confidence. Our approach is correct-by-construction, |
| 71 | +'programs from theorems' language development. But to have true utility, Rholang |
| 72 | +had to be expressive and easy to reason about, which means that it had to boast |
| 73 | +a full-bodied typing discipline and include an automated formal spec. verification |
| 74 | +feature for mission-critical contracts. When we commit to the mobile process calculi, |
| 75 | +we are afforded those mechanisms in the form of 'Behavioral Types'. |
| 76 | + |
| 77 | +Behavioral types represent a novel typing discipline that constrains not only the |
| 78 | +shape of input and output, but the permitted order of inputs and outputs among |
| 79 | +communicating and (possibly) concurrent processes. They exist to specify 'safe' |
| 80 | +interaction between objects in a composite system; we can think of an object |
| 81 | +(i.e. a process, channel, or variable), with an assigned behavioral type, as |
| 82 | +an object that is necessarily bound to operate in an authorized set of predictable |
| 83 | +and discrete interaction patterns. |
| 84 | + |
| 85 | +.. note:: |
| 86 | + |
| 87 | + TODO: Example |
| 88 | + |
| 89 | +Thereby, we can compose sub-processes while preserving their individual typings, |
| 90 | +which means that, whether the number of processes that execute (concurrently or not), |
| 91 | +between the time a transaction is initiated and the time it is verified, is one process, |
| 92 | +or 1,000,000 processes, correctness of data is always preserved. And that whether |
| 93 | +data is sent across the network to 1 location, or to 1,000,000 locations, correctness |
| 94 | +is preserved. |
| 95 | + |
| 96 | +There are a number of additional benefits that we get for free with behavioral types: |
| 97 | + |
| 98 | +System informatics analysis and optimization criterion for: |
| 99 | + |
| 100 | +* Data/Information Flow |
| 101 | +* Control Flow |
| 102 | +* Resource Access Patterns |
| 103 | +* Capability based addressing |
| 104 | + |
| 105 | +In their seminal paper, `Logic as a Distributive Law`_, Mike Stay & Gregory Meredith, |
| 106 | +develop an algorithm to iteratively generate a spatial-behavioral logic from any monadic |
| 107 | +data structure. The result is equivalent to a full-bodied type system decorated with |
| 108 | +modal logical operators, which can, not exclusively, be leveraged to generate a |
| 109 | +mathematically precise specification for polymorphic structure and behavior of processes. |
| 110 | + |
| 111 | +The behavioral type systems Rholang will support make it possible to evaluate collections |
| 112 | +of contracts against how their code is shaped and how it behaves. As such, Rholang contracts |
| 113 | +elevate semantics to a type-level vantage point, where we are able to scope how entire |
| 114 | +protocols can safely interface. |
| 115 | + |
| 116 | +For more information, see `Rholang Specification - Contracts, Composition, and Scaling`_. |
| 117 | + |
| 118 | +.. _Logic as a Distributive Law: https://arxiv.org/pdf/1610.02247v3.pdf |
| 119 | +.. _Rholang Specification - Contracts, Composition, and Scaling: https://docs.google.com/document/d/1gnBCGe6KLjYnahktmPSm_-8V4jX53Zk10J-KFQl7mf8/edit#heading=h.k4bk2akncduu |
0 commit comments