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

EU Wants to Monitor Bitcoin, Not Regulate It

The European Parliament’s Economic and Monetary A...

Filecoin’s crypto-economic design

Original title: "Filecoin's Cryptoeconom...

Whose business is the booming Bitcoin?

Source: Yuyuan Tantian (WeChat public account) Th...

What clues does Litecoin's halving on Thursday provide for Bitcoin's halving?

Key points: Litecoin will have its third halving ...

Is the lower sclera of the eyes a sign of lack of confidence?

Generally speaking, there are two types of three ...

Does a man with one eye bigger than the other unlucky his wife?

Does a man with one eye bigger than the other unl...

Is the marriage fortune good for men with broken palms?

From the perspective of palmistry, some palm line...

Palmistry - Lifeline

Men use left hand, women use right hand. The life...

The hidden "husband and wife look" and "lucky husband look" in face shape

We often say that a couple should look like a cou...