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

Whether you can be rich or not is written on your face

Everyone has a dream of becoming a rich man in th...

Talk about what kind of facial features can bring good luck to a husband

As for whether a woman can bring good luck to her...

What does a mole next to the nose mean? OK?

Modern people don’t like to have moles. After all,...

Physiognomy: Dimples on a man's face

Physiognomy: Dimples on a man's face In physi...

The facial features that make Xu Ziqi a lucky woman

The facial features that make Xu Ziqi a lucky wom...

What does having many forehead wrinkles mean?

A person's destiny can actually be predicted ...

People with this feature are destined to be sad in life.

In real life, people are always chasing after wea...

Former US agent admits to stealing Bitcoin from online black market

A former U.S. Secret Service agent pleaded guilty...

New coin BCD lands in F2Pool

Bitcoin Diamond (BCD) is a forked coin based on B...

What does the fate palace represent?

In ancient times, the Palace of Life was given gr...

Is it good for girls to look like boys?

The masculine appearance of a girl is mainly expr...