Developing Ethereum applications based on Ruby-on-Rails

Developing Ethereum applications based on Ruby-on-Rails

1. Understanding Ethereum’s network architecture

The nodes run geth or eth, and they connect to each other through port 30303 for p2p. The protocol running on them is commonly known as the mining protocol, also known as the consensus protocol, which includes several parts, such as broadcasting transactions or messages, synchronizing blocks, etc.

Node is a node, often also called geth client or geth server.

Each node provides JSONRPC (port 8545) or IPC port to the outside world, and the entire blockchain can be operated through Node, such as querying account information, deploying contracts, etc.

Ethereum's wallet is a typical application that operates the blockchain through the IPC interface, but it is not a dApp (distributed app).

2. Ethereum’s dApp Architecture

After understanding the network architecture above, it is easier to understand the architecture of an Ethereum dApp.

The bottom layer is the blockchain layer, and you can choose ethereum, testnet or testrpc.

The green part will be quite different depending on the framework you choose, but the basic principle is the same, that is, the framework should provide the function of operating the blockchain interface, preferably a complete protocol stack. Since the official web3.js is a complete RPC protocol interface based on node.js, it is more natural to implement a Web Framework based on JS. The more well-known ones are truffle, and the official recommendation is meteor. But in theory, any framework that provides a protocol for interacting with the Blockchain (i.e. connecting to the 8545 RPC port) can be used to implement dApp, and more than 80% of it is traditional WEB or mobile application development content.

3. Environmental Preparation

Operating system Ubuntu 16.04 AMD64

Step 1: Install rvm/ruby/rails

#gpg –keyserver hkp://keys.gnupg.net –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

#\curl -sSL https://get.rvm.io | bash

#rvm install 2.3

#gem install bundler

#gem install rails

Step 2: After the above steps, Rails 4.2.x should have been installed in the system. Since there are not many developers using Ruby in China, the limited group should have the basic skills of installing the environment. In addition, this article does not focus on the installation and use of Rails. If you have any questions, you can search Baidu or Google. Check the environment to ensure that Rails runs correctly.

$ rails –v

Step 3: Create a rails project

$ rails new helloworld

step4: install nodejs

$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

$ sudo apt-get install -y nodejs

$ export PATH=/usr/bin:$PATH

step5: Install testrpc

$ npm install –g ethereumjs-testrpc

step5b: Install geth/eth. This step is optional. If you just want to experience operating the blockchain through rails, installing testrpc is enough.

$ bash <(curl https://install-geth.ethereum.org -L)

$ bash <(curl https://install-eth.ethereum.org -L)

step6: Install solc /This step can also be installed through npm, so I won’t go into details.

$ sudo add-apt-repository ppa:ethereum/ethereum

$ sudo apt-get update

$ sudo apt-get install solc

At this point, a blockchain test development environment is basically OK.

4. Integrate the ethereum-ruby Gem into the project

Step 1: Create a new project

$ rails new helloworld

Step 2: Modify Gemfile and add ethereum

gem 'ethereum'

Step 3: Run bundle install

$ bundle install

Step 4: Run testrpc

Step 5: Start the rails console

$ rails c

And establish a link with testrpc on the console as follows:

Step 6: Create a simple smart contract

$ mkdir contracts

$ vi contracts/Helloworld.sol

contract Helloworld {

mapping (address => bool) public myMapping;

function register(address _a, bytes32 _b) {

}

}

Step 7: Deploy and compile on the console

> con=Ethereum::Initializer.new 'contracts/Helloworld.sol', c

The results are as follows:

> con=Ethereum::Initializer.new 'contracts/Helloworld.sol', c

If you switch to the testrpc window at this time, you can find that you have received a compileSolidity command:


<<:  Can Bitcoin overcome its biggest obstacle — decentralization — and achieve the best governance?

>>:  NSA may have been hacked, hackers are asking for $568 million in Bitcoin to auction important documents

Recommend

How to tell the face of a woman with big ears

The one who can make accurate predictions and pre...

The character of a woman who walks in small steps

The character of a woman who walks in small steps...

What does a mole on the chin mean?

The face is one of the most obvious parts of the ...

How to tell at what age to get married from facial features

Many people are curious about their marriage, but...

Is it good for a woman to have wide cheekbones?

In our lives, everyone's cheekbones are diffe...

How to read the marriage line and love line in palmistry

Many people mistakenly believe that the marriage ...

What kind of woman is born to make money?

People say that it is easy to make a fortune but ...

Bitcoin Scaling Question: What Exactly Is Segregated Witness?

There has been a lot of talk about the Segregated...

Palmistry diagram: The health line shows your health status

Palmistry diagram: The health line shows your hea...

Do you have the four types of fortune and wealth?

Our bodies have many characteristics, and some of...

What are the facial features of a mean person?

1. Hooked Nose The bridge of the nose is thin and...

Russia's Crypto Taxes Reach $13 Billion, Which Countries Are Taxing Crypto?

Recently, a report from the local Russian media T...