Deploy Your First Ethereum Smart Contract on a Blockchain!
Louis Pinsard5 min read
Understanding the concepts behind a blockchain is not as hard as one could imagine.
Of course some specific concepts of a blockchain environment are harder to understand (e.g. mining) but I will try first to give you a simple introduction to the most important concept of blockchain.
Then we will write a first smart contract inside a blockchain.
That will be the first step before trying to build a decentralized web application implementing smart contracts!
Basically a blockchain is a chain of block, more precisely a linked chain of blocks.
A block is one element of a blockchain and each block has a common structure.
The security of a blockchain is assured by its decentralized nature.
Unlike a classic database, a copy of the blockchain is stored in each node of its peer-to-peer network.
In the case where the local version of a blockchain in a networks node were corrupted, all the other nodes of the P2P network would still be able to agree on the actual value of the blockchain: thats the consensus.
In order to add a fraudulous transaction to a blockchain, one should be able to hack half of the user of the network.
Furthermore, it is also very hard to alter even only a local version of a blockchain because of a block structure.
Block structure
What is interesting in the concept of blockchain is that we can store any kind of data with very high security. Data is stored inside a block that contains several elements:
- A hash
- The hash of the previous block
- An index (number of the block in the chain)
- A nonce (an integer that will be used during the mining process)
- A timestamp
- The data
- A hash representing the data
If you are not familiar with the concept of hash, the important things to know are:
- From a very large departure set (typically infinite and very diverse), a hash function gives back a string of fixed length
- The hash of a block is easy to calculate
- Given a hash, it is impossible to build a message with this hash value
- If you modify the message (even very slightly), you totally change the hash
- It is almost impossible to find two messages with equal hash
The hash of a block is calculated by taking as argument the hash of the previous block, the index, the nonce, the timestamp, and a hash representing the data of the current block.
Among these inputs, the nonce will vary during the process of mining but of course the others stay fixed.
Because of the properties of a block, if you try to change the transactions/data inside a block, its hash will not be consistent with the data anymore (neither will be the previous hash of the next block).
Mining a block
When a block is created, nodes of the network will try to mine it, which means to insert it into the blockchain.
The miner who succeeds will get a reward.
Proof of work
In order to mine a block, you have to find a valid hash for this block, but as you may remember a hash is quite easy to calculate.
Thats why you need to add a condition on the desired hash, typically a fixed number of zeros the hash must start with.
The proof-of-work enhances blockchain’s security because of the considerable amount of calculation needed to modify a single block.
To conclude, a miner has to solve a time consuming mathematical problem to mine a block.
The nonce
A nonce is an integer which will be incremented during the mining process.
Without a nonce, the data of a block is constant, and thus the hash function always returns the same result.
If your hash consists of hexadecimal characters, and you want to have a hash starting with 5 zeros you will have a probability of 1/1048576 to produce a hash verifying this condition with a random nonce.
Each time you fail to get a hash verifying the condition, you can update the nonce and try again.
This assures that the miners of the network will have to work to add a block to the blockchain and thats why this algorithm is called proof of work.
Smart Contracts
Smart contracts have been introduced in the ethereum blockchain.
This is some code written inside the block of a blockchain that is able to execute a transaction if some conditions are fulfilled.
It is useful when the execution of a contract depends on some difficult conditions, or when you usually use a third-party to ensure the execution of the contract.
Initializing the project
In this part we will write a smart contract using Solidity, the programming language used in the ethereum blockchain and Truffle; a development environment for Ethereum.
We will use Ganache (you can download it here which will give us access to a personal ethereum blockchain in order to test our smart-contracts and deploy them in a blockchain.
First, lets install Truffle and initialize our project by running the following commands:
npm install -g truffle
mkdir my-first-smart-contracts
cd my-first-smart-contracts
truffle init
The truffle init command will provide the basic folders and files of your project:
contracts/
migrations/
test/
build/
truffle.js
truffle-config.js
You can now open Ganache.
You will see 10 fake accounts that we can use and a local blockchain with only one initial block.
You can also see the port where your local blockchain runs. It should be by default HTTP://127.0.0.1:7545.
Let’s open your truffle.js
file and modify it to connect it with the blockchain.
module.exports = {
networks: {
development: {
host: 'localhost',
port: 7545,
network_id: '*'
}
}
};
Our first smart contract
In our contracts folder, we will create a basic smart contract Message.sol
.
It will only enable us to write a message in the blockchain but it will be a good start to understand the way solidity works.
pragma solidity ^0.4.17;
contract Message {
bytes32 public message;
function setMessage(bytes32 newMessage) public {
message = newMessage;
}