Articles Developer How to Research

Controlling our decentralized future with MacRhoLang

We hope to maintain a semblance of control of the chaotic, asynchronous processes our world is composed of. Rholang, a language of reflective process calculus, offers that promise. While utterly simple—not unlike quantum logic, which is also simple and general—application of the process calculus can prove to be difficult to understand.

“Rholang is simple but not easy.” MacRhoLang @RHO-bot on Discord aims to make it more accessible. @RHO-bot enables simple client-side programming of functions to be performed on RChain test nets in a collaborative development environment that includes users.

Chatbots are a leading means of managing distributed systems. @RHO-bot in the CoLab Discord chat allows us to interface with an RChain node (rhobot.net, soon to be on testnet) in a language we cooperatively define.

We define macros of parameterized Rholang snippets, allowing them to be used like user-defined commands, functions, or variables generating code to be run on RChain serving the chat community on a Discord server. It employs Discord identities to create toy private and public keys, enabling demonstration of contracts running on RChain with a simple command interface.

e.g. testing Dan Connolly’s Rholang voting contracts and getting community sentiment on Discord without people needing to learn Rholang:

newBallot: “colors”, Set(“red”, “green”, “blue”)

allowtovote: “jimscarver”,”colors”

vote: “colors”, “green”

voteresults: “colors”

An environment is created where we can learn to share our capabilities and orchestrate our processes safely on RChain. It is an arena where the mystery of process calculus might be gently revealed.

@RHO-bot is a work in progress being cooperatively constructed in the CoLab , currently employing the cpp macro preprocessor and clang-format formatter for Rholang on RChain with a discord.js chatbot. @RHO-bot is running on rhobot.net. We evolve the language of rho-bot by defining macros; there are inevitable bugs we can help each other fix as we learn together. The source code is on github.

Built in commands

echo: <macrholang code>

Expands global macros (excluding personal macros such as private key) and posts the result to the channel.

To echo macros without posting in a public channel, you can direct message @RHO-bot but most other operations should be done transparently in a shared environment. e.g. in the #testing-bots channel.

eval: <macrholang code>

Expands macros, if any, and then executes the resulting Rholang code and posts result from the log in the channel. Macros can also be executed as global user defined commands.

define: $<name> <macrholang code>

define: $<name>($arg, …) <macrholang code including $args substitution>

e.g.

define: $Ballot `rho:id:3qfh1fy7jwfcai7ceyorux4a18hzcn83n9xb6dramjf5gs7cw8fynf`

define: $print($expression) new stdout(`rho:io:stdout`) in { stdout!($expression) }

All macros in macrholang must begin with a dollar sign to avoid most of the well-known pitfalls of the cpp processor; the dollar sign will be flagged as an illegal character in Rholang, catching macros that failed to expand.

Macros are defined globally for all channels using the same @RHO-bot. Global macros are also defined if the output of an eval includes [“#define $name”,aValue]

In a shared environment like the CoLab, rhobot.net defines should be done transparently in a public channel so conflicts may be avoided or discovered, diagnosed and remediated. Carefully choose macro and contract names already in use for a new purpose. Use the find command to see what is already defined.

find: <optional search regular expression>

deploy:

No arguments. Deploys the tuple space of the last thing you evaluated on RChain.

propose:

No arguments. Proposes a block consisting of prior deploys from any user.

Built in personal macros

Discord identities are mapped to @dcks’s toy identities used in his private data Locker contract. These values are defined dynamically by @RHO-bot to reflect the user that executed the macrolang code.

$myusername

$mypubkey

$myprivkey

The values are automatically defined by @RHO-bot for the user executing macrolang code.

Private data data locker macro commands

makemylocker:

mylockerGet: *stdout

mylockerGet: “key”, *stdout

mylockerStore: “key”, “value”

mylockerUpdate: “key”, “value”

Mailbox macro commands

We communicate with other members through their write only public mailbox (inbox.rho). We can send messages that may include capabilities granting access rights such as the right to vote on a ballot.

send: “username”, “type”, “subtype”, data….

receive: “type”, subtype”, *stdout

peek: *stdout

peek: “type”, *stdout

peek: “type”, “subtype”, *stdout

Directories

$newDirectory($name)

$addUser($username,$directory)

(other functions are coming soon)

Voting

newBallot: “colors”, Set(“red”, “green”, “blue”)

allowtovote: “jimscarver”,”colors”

allowgrouptovote: “colab”, “colors”

vote: “colors”, “red”

voteresults: “colors”

Other preprocessor directives

Advanced users can take advantage use other preprocessor functions besides simple macro substitution. #if, #include or any other cpp directive including any header and conditionals may be used anywhere in macrolang code on a line by themselves.

Client side logic may be implemented using #if conditional expressions.

Use #include to include rholang and macholang files anywhere in your code on rhobot,net or your machine.

#include “/home/username/file.rho”

To simply run some file you can #include it by typing eval: #include “/home/dckc/awesome.rho”

Discord messages are limited to 2,000 characters. While it is a good practice to limit programs to that size, there may be many programs in an app file. #include must be used for code longer than 2,000 characters.

Example using macros in Rholang code to share data and capabilities

Discord markup renders Rholang wrong, such that characters are missing and copied programs are broken unless in a code block started and ended with “`

We get the advantage of somewhat useful syntax highlighting if we start with “`scala

“`scala
define: $share($username, $type, $subtype)
new return in (
 $peek($type, $subtype, *return) |
 for ( list <- return) {
 $send($username, [$type, $subtype] ++ *list)
 }
}
“`

Then I can invoke the macro as a command to share my CoLab directory admin capabilities:

share: “Emmanuel_mebyz”, “directory”, “colab”

Your RChain application

Define your macros in a colab channel in Discord like #testing-bots and in direct messages to @RHO-bot or fork the code on github and create your own bot. You can get help in the CoLab. You can join the CoLab here. Add your app to this document.

Programming for the masses has largely become customizing copy/pasted code. To reuse code from existing defined macros search for “define $macroname” and copy the code from the most recent. Macro definition allow us to parameterize copied code, adding to our language a tool that reuses code for any parameters. To see all the Rholang generated, use the echo command; if that includes values that will change, it may break if copied and reused in a different or changed environment.

The command line on the system running your rhobot can be useful for testing and debugging. CoLab participants can request an account on rhobot.nrt in #sysadmin. You can test your macrholang on rhobot.net or your local Linux without using a Discord bot using the bash script:

./rhoexec file.rho [locker nonce if needed]

The file dummyuser.h is automatically included to define the private variables for dummyuser. If running on a different Linux system uri’s for stock contracts can be redefined or edited in global.h after using ./rhoexec or #include to load the stock contracts needed. If using the stock Locker.rho on your system initialize the locker for dummyuser with ./rhoexec makemylocker.rho

Future

Macrobot’s future includes all that anyone engages in organizing

  • Output processor to filter and render RChain output for a command
  • Add user-defined personal and channel macros
  • Rchain-api integration (fix Rholang:)
  • Linking Discord and RChain channels
  • Universal chat system/bridge support, e.g. slack, mattermost, matrix, movim,
  • Replace the cpp processor and clang-formatter with more modern alternatives
  • Integrate RSign security and self sovereign chat client for using real keys
  • rhobot.net website.

To learn more about RChain visit RChain.coop. I suggest there is good reason to get some skin in the game. Your own @RHO-bot can put you in control.