Using geth to attach to a RSK local node

RSK’s virtual machine implementation is compatible with the Ethereum virtual machine (EVM), which enables us to make use of many Ethereum developer tools.

In this tutorial I will show you step-by-step how to use the Ethereum client Geth to attach to an RSK local node (which is called regtest) and run a few JSON-RPC commands. We do this to establish that the local node is running.

Overview

We will do these steps:

  1. Install the requirements: Java SDK, RSK local node and Geth;
  2. Do a Geth attach;
  3. Check if the node is running;
  4. Know some useful functions;
  5. Learn how to interact with accounts: list, create;
  6. Transfer funds between accounts.

Requirements

  • Java JDK
  • RSK local node
  • Geth

Installing Java JDK

First check if you already have Java installed:

java -version

Go to Java Download if you need to install it:

Java Download

Installing RSK local Node

There are several different ways to set up a RSK node. Here we will download and run a JAR file, and run it using the Java SDK that has been installed.

Download

Go to the releases page and click on the most recent to download it.

You need to click on the JAR file, in the end of the post about the lastest release. It’s name should be rskj-core-*.jar:

Download last RSK release

Run

To run the node:

java -cp <PATH-TO-THE-RSKJ-JAR> co.rsk.Start --regtest

(Replace with your path to the JAR file).

I am using a Windows OS and I saved the file at C:\RSK\node, so for me the full path is C:\RSK\node\rskj-core-1.3.0-WASABI-all.jar.

The commands required to run the RSK node are:

On Windows

java -cp C:\RSK\node\rskj-core-1.3.0-WASABI-all.jar co.rsk.Start --regtest

On Linux and Mac

java -cp C:/RSK/node/rskj-core-1.3.0-WASABI-all.jar co.rsk.Start --regtest

If you do not have any output after running the command, this usually means that the node is running successfully. We will confirm this in the next step.

Run local node

Important:

Do not close this terminal / console window. The node is running here, and if you close, you will stop it.

Check if the node is running using cURL

Open a new terminal window.

Issue a request to the node’s RPC HTTP server. This is an example using cURL:

curl localhost:4444/1.1.0/ -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'

The response should look similar to:

{"jsonrpc":"2.0","id":1,"result":"0xfc0"}

The result property is the number of the latest block that has been synced. Note that this value is in hexadecimal, so the output above indicates that the current block number is 4032.

To get more information about this process: Setup RSKj with Java

If you encounter any problems, check if your system meets the minimum requirements.

There are other ways to install a RSK node, in other supported platforms: Installing RSKj

Installing Geth

Download and install Geth.

Geth download page

Do the installation with all default options.

You do not need to install Developer tools.

Geth install

This tutorial was made using version 1.9.12, I recommend using this version.

At terminal, run this command to check the version, if it runs and returns a version, it is ok:

geth version

geth version

In this link, you have more information about how to install Geth: https://geth.ethereum.org/docs/install-and-build/installing-geth

Geth attach

This procedure is only for a node running in your machine or any in a network that you are allowed. Geth attach gives you full control of the remote instance, so do not expect someone else to give you such access to their machine.

geth attach http://127.0.0.1:4444

image alt text

We are running geth to attach (connect) to RSK local node. The address http://127.0.0.1 connects to your own computer (localhost). The local node has the HTTP-RPC server enabled and listening at port 4444, and this is what we connect to.

Supported RPC methods

There is a list with all RPC methods enabled on RSK nodes here: RSK JSON-RPC

For more information about RPC methods, check the Ethereum website, but remember that not all of them are implemented on RSK nodes:

Check if the node is running

Check block number

eth.blockNumber

eth.blockNumber

Every time you run it, you will get a higher number, because the node is mining blocks.

Tips and tricks

Copy and Paste in Windows terminals

In the Geth console, to paste something which you copied from another place, you need to use:

  • Right Button
  • Right Arrow

Do not press the keys at the same time, but rather in sequence: First Right Button, then Right Arrow.

List commands

A small trick to see the list of initial commands. Type 2 spaces then hit TAB twice. You will get:

Geth list commands

Useful functions

I will show some RPC methods which are useful for understanding how things work.

Useful eth functions

eth.blockNumber

Shows the last block number

eth.getBlockNumber

eth.getBlockNumber

gasPrice

On a local node, we expect the gas price to be 0.

eth.gasPrice

eth.gasPrice

Useful net functions

net.version

net.version

This is the network ID.

net.version

net.peerCount

net.peerCount

net.peerCount

You are alone on this network.

Accounts

Personal

List all things related to accounts in your local node.

personal

RSK is pre-configured with some accounts:

Personal

List Accounts

You can only list the accounts:

personal.listAccounts

personal.listAccounts

There are another command to do the same thing:

eth.accounts

eth.accounts

Create account

Also it is possible to create new accounts:

personal.newAccount("mypasswd")

You need to save or remember the password as it is used to encrypt your private key on disk.

In the example, the password is "mypasswd".

personal.newAccount

My new account is 0xf6e443fd1c869c6a25d18a9866f3a6c7f8dfb703

Balances

To retrieve the balance of an account, per example, account[1]:

eth.getBalance(eth.accounts[1])

eth.getBalance

We get a big number because the result is denominated in wei. We can convert to Ether:

web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")

balance in ethers

On a local node, the pre-configured accounts are funded with a lot of “money”! On the RSK network, this is called R-BTC. So I have 1.000.000.000.000 = One trillion of R-BTC!

Balance of a specific account

I would like to check the account (0xf6e443fd1c869c6a25d18a9866f3a6c7f8dfb703) that I created before:

web3.fromWei(eth.getBalance("0xf6e443fd1c869c6a25d18a9866f3a6c7f8dfb703"),"ether")

Balance of a specific account

I do not have any funds in my account, so the next step is to transfer some R-BTC to it.

Transfer R-BTC

I have one trillion R-BTC in account 1 and nothing on the new account. I’d like to transfer 300 billion R-BTC from account 1 to the new account:

eth.sendTransaction({from:eth.accounts[1], to:"0xf6e443fd1c869c6a25d18a9866f3a6c7f8dfb703", value: web3.toWei(300000000000, "ether")})

Transfer

Perfect! I got a transaction hash. This means that my transaction was sent to Blockchain and it will be included in a block in a few seconds.

Now I will check the balance of account 1 and the new account:

web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")

web3.fromWei(eth.getBalance("0xf6e443fd1c869c6a25d18a9866f3a6c7f8dfb703"),"ether")

And the result is:

Balances after transfer

Great! The new account has 300 billion R-BTC and account 1 has 700 billion R-BTC.

Geth exit

To exit the geth console:

exit

exit

Final considerations

Did you think that it would be so easy to use Geth, an Ethereum client, to interact with an RSK local node?

We can do more things using Geth, such as deploy a smart contract at RSK local node using Geth and Remix.

Our goal is to join forces and give options to people who believe in smart contracts based on Ethereum, and also believe in the power of Bitcoin, through RSK.

I hope this tutorial has been helpful and I’d appreciate your feedback. Share it if you like it :)