How to set up a Filecoin Lotus node and connect remotely to it

2020-11-09

How to set up a Filecoin Lotus node and connect remotely to it

At Wrapped Filecoin (WFIL) we are developing the first Filecoin Wrapped Token on Ethereum. To achieve our goal, we need to be able to explore and analyze Filecoin’s blockchain in detail.

Introduction

Used to working with Ethereum, we can quickly think about several tools to do that (p.e. Infura), but currently Fielecoin is in a very early phase and those tools aren’t available yet.

So, we decided to set up our own Lotus node and configure it to allow us to call its JSON RPC methods from our backend API. The main instructions we followed are located here: Filecoin Lotus Installation docs, but since we needed to modify some of the steps, we thought it was worth it to share it and help anyone with the same problem.

Steps

1. Launch a Virtual Server and connect through SSH

We use AWS as a cloud provider, so we set up an EC2 of the type m5ad.2xlarge. Selecting Ubuntu Server 20.04 LTS is important since all the tools and libraries needed to install Lotus and Go are unavailable on the Amazon Linux System.

To connect to the AWS instance through SSH, make sure to have configured the security group to allow remote access through port 22. After that, you should use the PEM certificate generated at the moment of launching the instance, but first, let’s set the proper permission to be able to use it:

chmod 400 <path-to-pem-file>

Now we can connect to the EC2 instance:

ssh -i <path-to-pem-file> ubuntu@<ip-of-your-instance>

2. Install Dependencies, Go, and Rust

There is not much to do here, we can follow the guide on Filecoin’s website:

Dependencies:

sudo apt install mesa-opencl-icd ocl-icd-opencl-dev gcc git bzr jq pkg-config curl clang build-essential libhwloc-dev -y && sudo apt upgrade -y

Rustp:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Go:

wget -c https://dl.google.com/go/go1.14.7.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local

3. Configure environment variables

This step is not well explained in the docs; if you are unfamiliar with dealing with this kind of software, it can be tricky.

First, we should add the variables to the environment configuration. Let’s open the bash profile file:

nano ~/.bashrc

Add the variables for go and rust by adding the following lines at the end of the file:

export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:/home/ubuntu/.cargo/bin

Source the file or restart the terminal:

source ~/.bashrc

4. Install and launch Lotus daemon

Same as before, we execute the given commands.

Clone the repository:

git clone https://github.com/filecoin-project/lotus.git

Checkout the branch of your choice (mainnet or any testnet). We chose calibration since it is the testnet that best simulates mainnet:

git checkout ntwk-calibration

Build and install

make clean all
sudo make install

Check the version:

lotus --version #should output something like lotus version 1.1.0+git.b039f44a

5. Allow remote connections

Open the lotus config ~/.lotus/config.toml. You will see different sections to configure with examples of the values you can set. We are interested in the API Section for ListendAddress and RemoteListenAddress values. Remove their comments and set the following value to each of them:

ListenAddress = "/ip4/0.0.0.0/tcp/4001/http"
RemoteListenAddress = "/ip4/xxx.xxx.xxx.xxx/tcp/4001/http"

Be sure to replace xxx.xxx.xxx.xxx with your instance public IP and to set the port that fits you best (also note that this port also should be opened on the security group of your EC2 instance).

Launch the daemon:

lotus daemon

Create a token to access it remotely (p.e. as admin)

lotus auth create-token --perm admin

Use the token to call your instance from a different computer and test everything we have done:

curl -X POST      -H "Content-Type: application/json"      -H "Authorization: Bearer <the-token-you-just-generated>"      --data '{ "jsonrpc": "2.0", "method": "Filecoin.Version", "params": [], "id": 1 }'      'http://xxx.xxx.xxx.xxx:4001/rpc/v0'

6. [BONUS] setup pm2 to always start the daemon

If the instance is restarted or the daemon stops, we should be aware of it, connect to it, and restart it. Having a library like pm2 can save you a lot of trouble in those cases and restart the daemon when needed.

To configure it you should install NodeJS, NPM, and pm2:

sudo apt install nodejs
sudo apt install npm
npm install pm2@latest -g

Then we should create a one-line shell script to launch the daemon, configure pm2, and make it able to start:

echo lotus daemon >> start-lotus-daemon.sh
sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu/start-lotus-daemon.sh

Start the daemon from pm2

pm2 start start-lotus-daemon.sh

And monitor it whenever we want:

pm2 monit

Conclusion

Setting up your own Lotus node can seem like hard work, but having complete control of the data you can get from Filecoin’s blockchain can really be worth it.

Feel free to check out WFIL dapp and let us know your thoughts. We are really looking forward to building the first Filecoin Wrapped Token on Ethereum, hand in hand with the community.

Special thanks to Swaroop and Mostafa for all the help in this process.