/ blockchain

EOSIO Blockchain dApp Step by Step: Part 1 - Setup

Preface

After I listen (Bettina Warburg's speech)[https://www.youtube.com/watch?v=RplnSVTzvnU], I am fascinated by the concept of decentralized economy (dApps). The traditional web application is:
Front End → Backend → Database
In contrast, dApp website is:
Front End → Smart Contract → Blockchain

For instance, when you go onto e-banking, the web page will call the backend code to grab your personal data and display them on the page. The backend code is running on centralized servers. As opposed to traditional, where dApps have their backend code (smart contract) running on a decentralized P2P network (Blockchain).

Why Is Blockchain So Hot?

Blockchain is the technology that underpins the digital currency Bitcoin – but it has far wider applications and is being commercialised in a growing number of areas. It has
generated much interest in technology circles and beyond,
because of the new possibilities it opens up in financial services, the public sector and other areas.
THOUGHT LEADERSHIP Nov 2017

What Is EOSIO blockchain?

EOSIO advertised as an “operating system” for dApps. It is being built from the ground up to perform millions of transactions per second (compares with current largest Blockchain network: Ethereum is only 15 transactions per second), making it more suitable for a complex dapp ecosystem and decentralized, tokenized economy.

What does this blog cover?

In this Blog, I'm going to show you how I setup the EOSIO blockchain and develop the Smart Contract. This is the part 1 of this series. Below demonstrates the EOSIO installation step-by-step with screenshots description. And how I setup the wallet, accounts and tokens. Let's get started.

A Clean Virtual Machine

To avoid conflict with existing software, I've prepared a clean virtual machine for this experiment. I'm using Linux KVM virtualization infrastructure (KVM is much faster than Virtualbox, it delivers only 2 percent worse than bare metal). I've allocated below config for the VM:

  • 8 GB RAM, 4 vCPU
  • 30 GB disk space
  • Ubuntu 17.10 desktop

1. Download the EOSIO

When the OS is installed, then I performed below actions in Terminal:

If you'd like to do the same thing, you can copy & paste the below commands to your Terminal:

$ sudo apt install git-core
$ git clone https://github.com/EOSIO/eos --recursive
$ cd eos
$ git submodule update --init --recursive
$ ./eosio_build.sh
$ export PATH=${HOME}/opt/mongodb/bin:$PATH
$ ~/opt/mongodb/bin/mongod -f ~/opt/mongodb/mongod.conf &
$ cd ~/eos/build: make test
$ sudo make install

2. Start the Servers

The EOSIO is installed at this point, I typed below commands to start the servers:

If you'd like to do the same thing, you can copy & paste the below commands to your Terminal:

$ cd ~/eos/build/programs/keosd
$ keosd --http-server-address=localhost:8899
# Open a new tab in Terminal
$ cd ~/eos/build/programs/nodeos
$ nodeos -e -p eosio --contracts-console --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --plugin eosio::wallet_api_plugin
# Open a new tab in Terminal
$ alias cleos='~/eos/build/programs/cleos/cleos --wallet-url=http://localhost:8899'

3. Create Wallet, Keypairs, Accounts and Tokens

To store information in Blockchain, we need an account for identifier the data and the wallet to protect the keys for signing a transaction. Refer here for EOSIO Accounts and Wallets Conceptual Overview

I performed below actions:

If you'd like to do the same thing, you can copy & paste the below commands to your Terminal (make sure the keosd & nodeos are running):

$ cd ~/eos
$ cleos wallet create
# Record the password
$ cleos wallet key
# Record the private & public key values
$ cleos wallet key
# Record the private & public key values
$ cleos wallet import ${private_key_1}
$ cleos wallet import ${private_key_2}
$ cleos wallet keys
# You should see two keys shown
$ cleos create account eosio myaccount ${public_key_1} ${public_key_2}
# You should see an error message as shown in slide 5
$ find ~ -name config.ini # The config.ini may locates at another directory in other platforms
$ nano ~/.local/share/eosio/nodes/config/config.ini
# Copy & paste the signature-provider's private key, as shown in slide 8
$ cleos wallet import ${private_key_signature-provider}
$ cleos wallet keys
# You should see three keys shown
$ cleos create account eosio myaccount ${public_key_1} ${public_key_2}
# The account should be created successfully as shown in slide 11
$ cleos create account eosio user ${public_key_1} ${public_key_2}
$ cleos create account eosio tester ${public_key_1} ${public_key_2}
$ cleos create account eosio eosio.token ${public_key_1} ${public_key_2}
$ cleos set contract eosio.token ~/eos/build/contracts/eosio.token -p eosio.token
$ cleos push action eosio.token create '{"issuer":"eosio", "maximum_supply":"1000000000.0000 SYS"}' -p eosio.token
$ cleos create account eosio user ${public_key_1} ${public_key_2}
$ cleos push action eosio.token issue '[ "user", "100.0000 SYS", "memo" ]' -p eosio
$ cleos push action eosio.token transfer '[ "user", "tester", "1.0000 SYS", "m" ]' -p user
$ cleos create account eosio exchange ${public_key_1} ${public_key_2}
$ cleos set contract exchange ~/eos/build/contracts/exchange -p exchange
$ cleos create account eosio eosio.msig ${public_key_1} ${public_key_2}
$ cleos set contract eosio.msig ~/eos/build/contracts/eosio.msig -p eosio.msig
$ mkdir backup-my-wallet
$ cp -R ~/eosio-wallet ./backup-my-wallet/

4. Try compiling a smart contract: hello

EOSIO Smart Contract is a C++ program executes in blockchain. Refer here for the docs.

EOSIO provides several sample contracts in contracts/ directory. I took the hello contract for testing as below:

If you'd like to do the same thing, you can copy & paste the below commands to your Terminal (make sure the keosd & nodeos are running):

$ cd ~/eos/contracts/hello
$ eosiocpp -o hello.wast hello.cpp
$ eosiocpp -g hello.abi hello.cpp
$ cleos create account eosio hello.code ${public_key_1} ${public_key_2}
$ cleos set contract hello.code ../hello -p hello.code
$ cleos push action hello.code hi '["user"]' -p user
# Modify the hello.cpp as shown in slide 8
$ eosiocpp -o hello.wast hello.cpp
$ cleos set contract hello.code ../hello -p hello.code
$ cleos push action hello.code hi '["tester"]' -p user
# You should see an Error message as shown in slide 9
$ cleos push action hello.code hi '["tester"]' -p tester
# It should be no error
$ pkill keosd && pkill nodeos

What's Next

In part 2, I will step-by-step show you how to develop a smart contract which illustrates the power of EOSIO blockchain.