Beginner's Guide to Getting Started with RSK Blockchain Node
Published: 2021-03-30 09:39:38

RSK is a secured smart contract platform, thanks to merged mining with the Bitcoin network, **merged mining** allows Bitcoin miners to mine RSK’s sidechain using the same work for both. This is the basis for RSK’s extremely high hash rate, which is approximately 63% of the Bitcoin network’s hash rate currently; and therefore the reason that RSK is the most secure smart contract platform.

**RSKj** is the software used to run nodes on the RSK network.
You can find it on Github:
[rsksmart/rskj](https://github.com/rsksmart/rskj).

## Interact with the RSK network

You can interact with the RSK network
in various ways without running your own node.

### Connect to RSKj

You can run
[RSKj](https://developers.rsk.co/rsk/node/) locally to connect to the public RSK networks -

1. [Connect to RSK Mainnet](https://developers.rsk.co/rsk/public-nodes/#mainnet)
2. [Connect to RSK Testnet](https://developers.rsk.co/rsk/public-nodes/#testnet).

> Also, check out an alternative to connecting to the
[public nodes](https://developers.rsk.co/rsk/public-nodes/).

Furthermore, you can run RSKj in a **localhost-only** environment, without connecting to any public network using the [RSK Regtest](https://developers.rsk.co/rsk/node/configure/switch-network/#regtest)

### Interacting with a dApp

To interact with a dApp, you can use a [Web3 enabled wallet software](https://developers.rsk.co/wallet/use/), for wallets that support RBTC and RIF tokens.
  
### Configure MetaMask to connect to RSK

To configure Metamask to connect to RSK,
see the step by step guide demonstrating
[how to interact with a DApp on RSK using Remix and MetaMask](https://developers.rsk.co/tutorials/ethereum-devs/remix-and-metamask-with-rsk-testnet/).
Both Remix and MetaMask are tools that were originally built for Ethereum,
and thanks to RSK's comaptibility
(both in its Virtual Machine executing smart contracts,
and in its communications protocol using JSON-RPC),
these tools work on RSK too!

We'll use them to create and deploy a simple smart contract on RSK’s Testnet.

### Check the status of the RSK network

To check the status of the RSK Network in real-time, use:

- [RSK Mainnet Stats](https://stats.rsk.co/)
- [RSK Testnet Stats](https://stats.testnet.rsk.co/)

![RSK Mainnet stats](https://i.imgur.com/oJQH7ZL.jpg)

You can also check the status of a particular transaction and deployed smart contracts, using:

- [RSK Explorer](https://explorer.rsk.co/)
- [RSK Testnet Explorer](https://explorer.testnet.rsk.co/)

See the [RIF Smart Contract](https://explorer.rsk.co/address/0x2acc95758f8b5f583470ba265eb685a8f45fc9d5) address and this address: [`0xc218fc2b765ab321a907d6125fe7763e2eaec8e16dd4a72e1a4829a9baa2451a`](https://explorer.rsk.co/tx/0xc218fc2b765ab321a907d6125fe7763e2eaec8e16dd4a72e1a4829a9baa2451a?__ctab=Transaction) is a transaction hash. You will notice that this type of transaction is perfmormed by neither an externally owned account or a smart contract. In fact it is a REMASC transaction.
> Reward Manager Smart Contract (REMASC) is a pre-compiled smart-contract that is executed on every block and has the responsibility to fairly distribute rewards collected from transaction fees into several participants of the network.


From: [REMASC architecture]
(https://developers.rsk.co/rsk/architecture/mining/remasc/)

This type of transaction is found in neither Bitcoin nor Ethereum, but is a key component of how the RSK network is able to achieve block times that are frequent enough to enable smart contract based decentralised applications, and while being merge-mined with Bitcoin.
See [fast payments](https://developers.rsk.co/rsk/architecture/fast-payments/) for further details.

### Send JSON-RPC requests

To send JSON-RPC requests, see list of 
[JSON-RPC Methods](https://developers.rsk.co/rsk/node/architecture/json-rpc/)
that RSK currently supports.

### Obtain test tokens

You can use the faucet to get test tokens (tRBTC) for use in RSK test network.

- Obtain tRBTC token in the [tRBTC Faucet](https://faucet.testnet.rsk.co/)
- Obtain tRIF token in the [tRIF Faucet](https://faucet.rifos.org/).

## Getting Started

If none of the above fits the bill for you,
you will need to run your own RSK node.

Especially if you are developing smart contracts,
developing DApps, or participating in a hackathon,
you are likely to want to iterate fast, and therefore need quicker feedback loops.

Running RSKj **in Regtest mode** is the best fit for these needs.

### System Requirements

The following are the minimum requirements needed to install an RSKj node.

- 2 cores
- 8 GB RAM
- 50 GB storage
- OS x64

### Install Java

The RSKj executable is a Java Archive file (JAR file),
and requires Java to run.
See guide on [how to install the Java 8 JDK](https://www.java.com/download/).

![Java downloads page](https://i.imgur.com/YYSQ3Qu.png)
    
### Download RSKj JAR

Navigate to [RSKj's releases page](https://github.com/rsksmart/rskj/releases).

From the **latest** release, download a file whose name looks like `rskj-core-*.jar`, where `*` is replaced by the release tag name, for example `2.2.0-PAPYRUS`.

![Download RSKj core](https://i.imgur.com/PbsKmLT.png)
  
### Run RSKj

To run RSKj, copy the following commands below, and paste in terminal where `rskj-core-*.jar` is located.

```shell
java -cp <PATH-TO-THE-RSKJ-JAR> \
  -Drpc.providers.web.cors=* \
  co.rsk.Start \
  --regtest
```
The above command runs RSKj connected to Regtest, which is a `localhost`-only network, clears the database each time the node is started, and enables both CORS. These are the most useful and commonly used flags and options for when you are developing or testing smart contracts and DApps.

If you see no output - that is a good thing: Its output is directed to a log file.

> Note: Remember to replace <PATH-TO-THE-RSKJ-JAR> with the path which contains the downloaded jar file. See example command below.

```shell
java -cp /Users/owanate/Downloads/rskj-core-2.2.0-PAPYRUS-all.jar \                   
  -Drpc.providers.web.cors=* \
  co.rsk.Start \
  --regtest
```
  
## Developer Tools

### Truffle

Here is how you would configure Truffle to connect to the RSK Testnet.

See below code for how to implement this in a Truffle project.

In your `truffle-config.js` file:

(1) Set a variable `testnetSeedPhrase` to contain a valid BIP-39 mnemonic phrase

(2) Set a variable `gasPriceTestnet` to contain the gas price you wish to use denominated in Wei.

(3) In the exported `config` object, set the value of `config.networks.testnet` to be the following.

```truffle
testnet: {
      provider: () => new HDWalletProvider({
        mnemonic: {
          phrase: testnetSeedPhrase,
        },
        providerOrUrl: 'https://public-node.testnet.rsk.co/',
        derivationPath: "m/44'/37310'/0'/0/",
        // Higher polling interval to check for blocks less frequently
        pollingInterval: 15e3,
      }),
      // Ref: http://developers.rsk.co/rsk/architecture/account-based/#chainid
      network_id: 31,
      gasPrice: gasPriceTestnet,
      networkCheckTimeout: 1e6,
      timeoutBlocks: 100,
      // Higher polling interval to check for blocks less frequently
      // during deployment
      deploymentPollingInterval: 15e3,
    },
```

This enables you to understand Truffle’s default configuration values (based on Ethereum),
in particular surrounding polling intervals. Using these two relatively new configuration options allows you to configure Truffle to better connect to an RSK node.

For more details, see
[Configuring Truffle to RSK](https://developers.rsk.co/kb/configure-truffle-to-rsk/).

### Debugging

Need to debug transactions in RSK?

See an example of [how to debug transactions in an RSK Network on Stackoverflow](https://stackoverflow.com/q/66144175/194982)

![debug transactions in an RSK Network on stackoverflow](https://i.imgur.com/U1oY4LE.png)

If your contract emits messages in the reversions, then you can find them out by using `debug_traceTransaction`.

Note that the debug RPC module is enabled by default in RSK config, but this is disabled on public nodes.

Furthermore, the RSK public nodes do not expose this feature, and you must run your own node in order to do so.

The following assumes that you have a local node running with RPC exposed on port `4444`.

First, you need to enable `debug` module in your config file:

```json
modules = [
    ...
    {
        "name": "debug",
        "version": "1.0",
        "enabled": "true",
    },
    ...
]
```

Then, you can execute the RPC method passing the
transaction ID as a parameter, like in this example:

```shell
curl \
    -X POST \
    -H "Content-Type:application/json" \
    --data '{"jsonrpc":"2.0","method":"debug_traceTransaction","params":["0xa9ae08f01437e32973649cc13f6db44e3ef370cbcd38a6ed69806bd6ea385e49"],"id":1}' \
    http://localhost:4444
```

You will get the following response
(truncated for brevity):

```json
{
    ... 
    "result": "08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001e536166654d6174683a207375627472616374696f6e206f766572666c6f770000",
    "error": "",
    "reverted": true,
    ...
}
```

Finally, convert `result` from hexadecimal to ASCII,
to obtain a readable message:

```
Ãy  SafeMath: subtraction overflow
```

### RPC

[Remote Procedure Calls (JSON-RPC)](https://developers.rsk.co/rsk/node/architecture/json-rpc/)
are the primary interface through which RSK nodes
communicate over the network.
RSK's JSON-RPC has a high level of compatibility with Ethereum's JSON-RPC.

JSON-RPC is available over two network transport protocols: **HTTP** and **WebSockets**

- [Configuring and using RPC over HTTP](https://developers.rsk.co/rsk/node/architecture/json-rpc/)
- [Configuring and using RPC over WebSockets](https://developers.rsk.co/rsk/node/architecture/json-rpc/)

> Note that [RSK public nodes](https://developers.rsk.co/rsk/node/architecture/json-rpc/)
> do not expose WebSockets, they are HTTP only.
> To work around this, you may either
> run your own RSK node,
> or use a third-party node provider,
> such as [Getblock](https://developers.rsk.co/solutions/getblock/).

## Wrap up

In this article, we interacted and connected with the different public RSK Networks, Configured Metamask to connect to RSK, Downloaded and ran an RSK Node in `regtest` network.

Thanks for reading!

> For more tutorials and guides, check out the [RSK/RIF knowledgebase in DevPortal](https://developers.rsk.co/kb/).

This article was originally published on [Hackernoon](https://hackernoon.com/interacting-with-public-rsk-networks-configuring-metamask-and-running-a-node-a-beginners-guide-no7j331a)

## Kudos

Thanks to [Brendan Graetz](http://blog.bguiz.com/) and [Diego Masini](https://github.com/diegomasini) for their help in reviewing this guide!