Create your Blockchain DApp with Ethereum and VueJS – Part 1

In this first part of the tutorial we are going to create the smart contract that handles the registration of users; then we are going to deploy the smart contract to the blockchain using Truffle.

If you missed the introduction and you want to know what this tutorial is about, please read the intro.

This part of tutorial is divided into three sections:

  1. Set up of the truffle environment.
  2. Smart contract development.
  3. Deploy the smart contract to the blockchain.

Note before you start: if you are using Windows you must use Power Shell as terminal.

Set up of the truffle environment

First let’s create the project folder called ethereum-vuejs-dapp, then open the terminal in the folder just created and run the following command:

$ truffle init

This command initializes the truffle project and creates the following items:

  • contracts: folder containing the smart contracts written in Solidity.
  • migrations: folder containing the files that take care of the deployment.
  • test: folder containing the files for testing the smart contracts.
  • truffle.js: file that contains the list of blockchain networks where you can deploy your contracts.

Let’s add the details of our blockchain network to the file truffle.js. So first start Ganache and, once it runs, check the host and the port as shown below.

Ethereum Vue - Ganache host and port settings

Now paste the following code in truffle.js:

module.exports = {
  networks: {
    ganache: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*"
    }
  }
};

IMPORTANT: make sure that the host and the port of the code pasted in truffle.js match with the settings displayed in Ganache.

Smart contract development

As described in the introduction, the main features of our dApp are:

  • Let users resister their profiles.
  • The owner of a profile is the only person who can edit his own information.

The following information of a user’s profile is going to be stored in the blockchain:

  • Name of the owner.
  • Status.
  • When the profile has been created and updated.

What we need is to store the list of the users and associate each user with an account address (or wallet address).

The smart contract must provide a set of functions that:

  • Let the user register his profile.
  • Let the owner of a profile update his details.
  • Get the list of users and their details.
  • Check if the user is already registered.

Below is the code of the smart contract (you can find it on GitHub as well).

Deploy the smart contract to the blockchain

It’s time to deploy the smart contract to the blockchain! Make sure that your blockchain is running and the file truffle.js is properly set as explained before.

Go to the folder migration and create a file called 2_migrate_users.js.
The migration filename has a number as prefix and a description as suffix. The numbered prefix is required in order to record whether the migration ran successfully and the suffix just describes what the file is about.

Copy and paste the following code inside the file 2_migrate_users.js:

var Users = artifacts.require("./Users.sol");

  deployer.deploy(Users);
};

Now let’s open the terminal in the project folder ethereum-vuejs-dapp and run the command:

$ truffle console ––network ganache

Once the truffle console is running, type the following command:

>> migrate ––reset ––compile-all

This command compiles all smart contracts and deploys the smart contracts to the blockchain.

If everything went fine, on the console you should see a message as follows:

Compiling .\contracts\Migrations.sol...
Compiling .\contracts\Users.sol...
Writing artifacts to .\build\contracts

Using network 'ganache'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x18edd24941e9b7edfae4966af544d5413e31622da90cecae2d3153635a7ffba2
  Migrations: 0xcac26201ab3b38d2f0f9b56f81a8897dfe036da6
Saving successful migration to network...
  ... 0x125c70f37e143b13acb4827dda16e8571758402db81c08a3a2ae27573910105f
Saving artifacts...
Running migration: 2_migrate_users.js
  Replacing Users...
  ... 0x043c2cf363da321b8a1ec06672f12fd794bf894342fa51100250bc6975806385
  Users: 0x782b9ce19fe792ed3c4ccbfb439ab59ddc5369f2
Saving successful migration to network...
  ... 0x9e56386f94093c4f56f6b84190503376363f15a6d16661e7bfaa8fefe1b4be1f
Saving artifacts...
truffle(ganache)>

After the deployment you will see on Ganache some new transactions in the transaction list.

Ethereum Vue - Ganache transactions

In the second part of the tutorial we will build the dApp interface using Vue JS and interact with the smart contracts through the interface.

The second part of the tutorial is on the way… see you soon!

___________

Read the original article at www.danielefavi.com

___________

Create your Blockchain DApp with Ethereum and VueJS – Intro

This is a simple decentralized application built using Ethereum blockchain and VueJS for the front-end. This DApp example lets the users store their name and status on the blockchain.

Ethereum Vue JS DApp Screenshot - User list
Ethereum Vue JS DApp Screenshot - User Profile

Brief description

The DApp allows users to register their name and status on the blockchain. The user’s profile is associated with an account address (or wallet address).

The owner of the profile is the only person who can modify his own data. This will give you a technical explanation of what it means when the user is in control of his own data (just like Facebook… sarcasm!).
The smart contract is designed to give the user control of his own information and not even the creator of the smart contract can control any data.

This tutorial is split in two parts:

  • PART 1 (coming soon): set up of the truffle environment, brief explanation of the smart contract and deploy the smart contract to the blockchain.
  • PART 2 (coming soon): set up Vue JS and explanation of the project structure.

Prerequisite

Quick Start

  1. Download the project and decompress it into the folder ~/ethereum-vuejs-dapp (or wherever you want).
  2. Start Ganache (or your private blockchain).
  3. Open the terminal (if you are using Windows you must use the Power Shell) in the folder ~/ethereum-vuejs-dapp and run the command:
    $ truffle console ––network ganache
  4. If ganache is running you should be inside the truffle console; now run the following command in the truffle console:
    > migrate ––reset ––compile-all
  5. If the migration was successful, copy the content of the file ~/ethereum-vuejs-dapp/build/contracts/Users.json into ~/ethereum-vuejs-dapp/app-users/src/assets/UsersContract.json
  6. Open another terminal in the folder ~/ethereum-vuejs-dapp/app-users and run the command:
    $ npm install
  7. Once all the dependencies are installed run the command:
    $ npm run dev

    If everything went fine you should see this message:

    Your application is running here: http://localhost:8080
  8. Open the browser and go to URL above to try the DApp!

NOTE: if you want to try to add more profiles you have to install metamask in your browser, then import the accounts into metamask and finally change account on metamask in order to register a new profile.

___________

Read the original article at www.danielefavi.com

___________

What is a fork? Why is it dangerous?

Often a blockchain can have some flaws, bugs or it just needs improvements. To fix those vulnerabilities the blockchain has to be forked.
A fork rewrites the rules which a full node must follow in order to consider a block valid. A full node can decide either to accept the new rule (updating its software) or reject it.

If some nodes choose to support the fork while others choose not to do so, then a blockchain fork might happen: in this case the blockchain splits up in two different branches.
The two blockchains share the same chunk of chain from the genesis block to a certain block before the divergence.

There are two kinds of fork: hard fork and soft fork. Both hard and soft forks can cause a blockchain fork.

When a fork happens, the choice made by the community is crucial because it can weaken the blockchain. When the community of the full nodes split then each branch of the blockchain created by the split will have less hashpower: so both blockchains become weak to a 51% attack.
If the community cannot make a strong decision about the fork, then also the community itself can become weak; this could lead to bad consequences regarding the trust on the platform: the reliability of the platform will decrease and the interest of investors and developers will move away.

Let’s take some real examples.
Before August 2017, the bitcoin community was discussing how to improve the bitcoin transaction performance (aka scalability). The community had two different opinions:

  • to increase the block size to 8MB
  • to store the data within a block with a method called SegWit [1]

In August 2017 the bitcoin blockchain split up in two branches: Bitcoin Cash [2] with an increased block size to 8M and Bitcoin that uses SegWit.

Another example.
In 2016 an attacker managed to use the vulnerability of ethereum security for his own good and many people lost their investments [3]. Within the ethereum community there were two ways to solve this problem:

  • to revert the transactions due to the flaw
  • not to make any changes, keeping the original protocol

In July 2016 the ethereum blockchain forked into Ethereum (with the flaw fixed) and Ethereum Classic (original protocol) [3].

What is the difference between hard fork and soft fork?

Soft fork: a block created with a new rule is considered valid also by the old rule; this means a soft fork is forward compatible (in other words the blocks created with the new rule are compatible with the blocks created with the old rule but not the other way around).
Example: you have a blockchain where each block has a maximum size of 2MB; then you apply a soft fork to reduce the maximum size of the block to 1MB. The old rule will consider the blocks created with the new rule valid since they do not exceed the 2MB maximum size.

Hard fork: blocks created with the new rule are not considered valid by the old rule; this means that a hard fork is not forward compatible (the blocks created with the new rule are not compatible with the blocks created with the old rule).
Example: a blockchain has the block size limit of 1MB; then a hard fork is applied in order to increase the block size limit to 3MB. The blocks created with the hard fork rule are not compatible with the old rule since the 3MB limit exceeded the 1MB limit but the old blocks are still compatible with the new rule.

SOFT FORK: how does the blockchain fork happen?

If the hashpower of the full nodes that accepted the soft fork is 51% or more, then there will be no blockchain fork.

If less than 51% of the hashpower comes from the full nodes which do not support the soft fork then the blockchain will split: a branch of blocks will be created by the nodes following the old rule (since the old rule is not compatible with the new rule) and another branch of blocks by the nodes following the new rule.

If the full nodes start upgrading their software to the soft fork and the hashing power supporting the soft fork is more than 51%, then all the network (both supporting and not supporting the fork) will consider the blockchain branch created with the soft fork rule as true.
This is possible because the branch created with the soft fork became longer than the other branch; since the soft fork branch is compatible with the old rule, all the network will consider the longer branch valid and there will be one single branch.

HARD FORK: how does the blockchain fork happen?

Here we have to consider two cases.

Case 1: 51% of the hashpower of the network do not update their software to the hard fork.
Since the blocks created with the old rule are compatible with the hard fork and the blockchain branch supported by the old rule grow faster (because it has more hashpower), then the blockchain branch supported by the old rule is considered valid and there is no blockchain split.

Case 2: 51% of the full nodes update their software to the hard fork but a smaller percentage is still using the old rule.
In this case the blockchain will split: since the blocks created with the hard fork rule are not compatible with the old rule then there will be a branch supported by the nodes following the old rule and a branch supported by the nodes following the hard fork.

Obviously in case when 100% of the community supports or does not support the hard fork there will be no blockchain split.
 

References

[1] Bitcoin Cash – Wikipedia

[2] SegWit Protocol – CoinDesk

[3] The stolen ether and the birth of Ethereum Classic:

[4] 51% Attack

 

Sources

Blockchain Fork – Wikipedia

Guide to Forks: Everything You Need to Know About Forks, Hard Fork and Soft Fork

What is a soft fork?

The differences between a hard fork, a soft fork, and a chain split, and what they mean for the future of bitcoin

___________

Read the original article at www.danielefavi.com