Create a Blockchain with Substrate and run on a free cloud-native platform

Jeevanjot Singh
7 min readJan 27, 2021

Greetings everyone,

In this post, I will create a blockchain using an example template with the Substrate framework. It will be a substrate create your first chain guide ported to a free cloud development service called Okteto for cloud-native development and testing of our blockchain, which means no local environment setup needed. No need to set up different rust environments locally(releases, versions) or need to have another nodejs installation (versions) to create on this amazing technology.

About Substrate

Substrate is a very flexible framework to create blockchains with defining custom logic for their needs Like storage, consensus, Transaction handling, etc. More features can be added by pallets (They are like packages that help to add an additional set of operations for your blockchain).

About Okteto (Okteto cloud)

Okteto is a cloud-native development platform that helps you host your development environments on the cloud infrastructure from which you can perform local development on the cloud-native environment to deliver your applications much faster.

Requirements:

Let’s Begin

First, we should set up the backend of the substrate (substrate node template) in the rust language environment.

the node here does not refer to nodejs it’s more related to a single miner (in our related terms)

We need two rust releases installed. The first is stable and the second a nightly version. I choose both versions that work without any problem now. If you watching this later or if trying other tutorials and if these versions aren’t working, try to change them.

Rust 1.48.0 (taken from the docker hub) and Rust nightly released on 2020–10–03 (we will install later)

Setting up substrate node

Create a new directory for the project to separate it from other future projects etc.

Let’s construct our Okteto manifest to start our development environment. It can be done either using the okteto CLI command okteto init or to create two files (.stignore and okteto.yml) manually

I will go manual for okteto.yml. So you can do okteto init and then edit yml file. Choose rust as the starting template when you run okteto init

okteto CLI generated files

Now to manually edit the main parts of the okteto manifest so you can cover the requirements to run a substrate node. You can replace your manifest with the below file.

Explanations

image: Public image to pull and run the pod with
command
: The command to perform on your pod when you connect with okteto up
workdir: The starting workdirectory when you connect to your pod
sync: The directory which you want to sync between local and remote environments respectively.
remote: To activate ssh service from that port locally.
resources: I added sufficient resources with current experiment that can perform make build while keeping in the free tier of okteto cloud.
persistentVolume: Requesting Enough volume from free tier to get you started
forward: To forward some ports from local to cloud environment.

and .stignore with this (or manually create a new line and type target)to avoid syncing the target folder…

After that, you can run okteto CLI command to connect with your cloud environment. All the changes you made locally will be synced with the pod hosted on the okteto cloud.

> okteto up

Now we need to install Rust nightly version and other dependencies to properly set up requirements by substrate node template. When connected with this pod through okteto up command, run:

> rustup install nightly-2020-10-03> apt update> apt install -y cmake pkg-config libssl-dev git build-essential clang libclang-dev curl

All the rust tools are already installed with the image rust:1.48.0 so running this will install another release of rust that we required (we require both with those versions currently)

installation succeeded for rust nightly build

You can now clone the substrate repository and run make commands to build it.

git clone -b v2.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-node-template

change directory (cd substrate-node-template) to your cloned project so you have the root project folder opened in your terminal, Now run these to initialize (make init) & build (make build) the project

make initmake build
This process takes time, come back when it’s done
make build finished

Now don’t disconnect your pod yet. Don’t close the pod process (the terminal) and start a new one then follow this…

Setting up substrate front-end

This Substrate example template also comes with a front-end to analyze blockchain and perform transactions.

Now since our node pod is rust based and installing nodejs or yarn on the same pod sounds complicated then let’s create a new pod with node environment (setting up nodejs image will install necessary nodejs based tools like yarn and okteto image also have nodemon)

Create a new directory for this separate from other projects and inside that just use the way defined above to create two okteto files for a new pod.

okteto init

This time choose a nodejs template (javascript)

Customize/Replace your okteto.yml with this…

Explanations

reverse: Reverse port connections to help our front-end to listen the node conncections that the pod, hosting our node is forwarding from.

So in short we are doing this where your current environment (local env) is acting as a middleman between 2 pods (node and front-end)

Consider Node1 as front-end & Node2 as backend, Your desktop is Local. Now with Reverse port forwarding (That we defined recently for front-end) is asking our computer for the ports that are required by front-end and those ports we are forwarding to backend to handle them from ours. So like middlemen. We carry connection or other requests from front-end (as per it's asking) and give it to backend (Well talk about front-end demands) and if backend processes it then we pick it from backend and front-end pick it from us.

Now, Activate your front-end pod with:

okteto up

Then Run these commands when you are connected, To clone the front-end template repository (git clone…) and then install all dependencies (yarn) inside the project directory (cd).

> git clone -b v2.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-front-end-template> cd substrate-front-end-template

Inside the directory, there is a file yarn.lock, You should delete it to resolve into latest addition of dependencies

Now perform dependencies install with yarn

yarn install

Running yarn only works fine too.

Running the front-end without running backend (node) won’t be of any use because the front-end needs node connections to fetch blockchain info.

Dependencies installation completed

Starting Blockchain

Now to start the blockchain, Head back to the previous terminal (node template pod) and run: (in the project directory)

./target/release/node-template --dev --tmp

like this…

through our latest build, we are running the substrate node

And once started running it will produce logs that also include these

ports opened to listen to these…
block finalization is started, look at increasing block numbers (like #2 in this image)

So far it started mining new blocks and pushing them to the chain.

Keep the above running and Now to visualize this we will use the front-end, Go back to front-end project and start the server with (make sure you are connected with your front-end pod with okteto up then run the serving command on top of it)

yarn start

It will start the server by calling react specific serving method, Defined in package.json

Package.json file

Here it’s compiled successfully and started serving from your pod.

The compilation is successful and now it’s serving

Since we also do port forwarding here on port 8000 from local to port 8000 where it’s serving. We can visit localhost:8000 from our local desktop browser to check it.

Substrate Front-end interface

It is showing the status of my blockchain running in a different pod.

To learn interaction with blockchain or different wallet, Visit https://substrate.dev/docs/en/tutorials/create-your-first-substrate-chain/interact#interact

You can also make it public by hosting things on 8080 port on okteto dev environment because okteto creates a new ssl endpoint for you for your related deployments.

Example of public SSL endpoint created by okteto.

Here are some more tutorials by substrate https://substrate.dev/en/tutorials

If you have any more questions please feel free to comment down.

Substrate Homepage: https://substrate.dev/en/

Okteto Homepage: https://okteto.com/

My Linkedin Profile: https://www.linkedin.com/in/jeevanjot-singh-a96baa78/

My Twitter Profile: https://twitter.com/genievot

--

--