The book “Creating Solidity Smart Contracts for the Ethereum Blockchain. Practical Guide»

The book “Creating Solidity Smart Contracts for the Ethereum Blockchain. Practical Guide»
For over a year I have been working on the book “Building Solidity Smart Contracts for the Ethereum Blockchain. Practical Guide”, and now this work is completed, and the book published and available in Litres.

Hopefully my book will help you get started quickly building Solidity smart contacts and distributed DApps for the Ethereum blockchain. It consists of 12 lessons with practical tasks. After completing them, the reader will be able to create their own local Ethereum nodes, publish smart contracts and call their methods, exchange data between the real world and smart contracts using oracles, and work with the Rinkeby test debug network.

The book is addressed to everyone who is interested in advanced technologies in the field of blockchains and wants to quickly gain knowledge that allows them to do interesting and promising work.

Below you will find the table of contents and the first chapter of the book (also on Litrese fragments of the book are available). I hope to receive feedback, comments and suggestions. I will try to take all this into account when preparing the next edition of the book.

Table of contentsIntroductionOur book is intended for those who want to not only understand the principles of the Ethereum blockchain, but also gain practical skills in creating distributed DApps in the Solidity programming language for this network.

It is better not just to read this book, but to work with it, completing the practical tasks described in the lessons. To work, you will need a local computer, virtual or cloud server with Debian or Ubuntu installed. You can also use the Raspberry Pi to perform many tasks.

At the first lesson we will cover the principles of the Ethereum blockchain and the basic terminology, as well as talk about where this blockchain can be used.

Goal second lesson - create a private Ethereum blockchain node for further work within the framework of this course on an Ubuntu and Debian server. We will look at the features of installing basic utilities, such as geth, which ensures the operation of our blockchain node, as well as the swarm decentralized data storage daemon.

Third lesson teaches you how to experiment with Ethereum on an inexpensive Raspberry Pi microcomputer. You will install the Rasberian operating system (OS) on the Raspberry Pi, the Geth utility that powers the blockchain node, and the Swarm decentralized data warehouse daemon.

fourth lesson is dedicated to accounts and cryptocurrency units in the Ethereum network, as well as ways to transfer funds from one account to another from the Geth console. You will learn how to create accounts, initiate fund transfer transactions, get transaction status and receipt.

At the fifth lesson you will get acquainted with smart contracts on the Ethereum network, learn about their execution by the Ethereum virtual machine.

You will create and publish your first smart contract on the Ethereum private network and learn how to call its functions. To do this, you will use the Remix Solidity IDE. In addition, you will learn how to install and use the solc package compiler.
We will also talk about the so-called Application Binary Interface (ABI) and teach you how to use it.

Sixth lesson is dedicated to creating JavaScript scripts running Node.js and performing operations with Solidity smart contracts.

You will install Node.js on Ubuntu, Debian and Rasberian OS, write scripts to publish a smart contract on the Ethereum local network and call its functions.

In addition, you will learn how to transfer funds between regular accounts using scripts, as well as credit them to smart contract accounts.

In the seventh lesson you will learn how to install and use the Truffle integrated environment, popular among Solidity smart contract developers. You will learn how to create JavaScript scripts that call contract functions using the truffle-contract module, and test your smart contract with Truffle.

Eighth lesson dedicated to Solidity data types. You will write smart contracts that work with data types such as signed and unsigned integers, signed numbers, strings, addresses, complex type variables, arrays, enumerations, structures, and dictionaries.

In the ninth lesson you will be one step closer to creating smart contracts for the main Ethereum network. You will learn how to publish contracts using Truffle on the Geth private network, as well as on the Rinkeby testnet. Debugging a smart contract on the Rinkeby network is very useful before publishing it on the main network - almost everything is real there, but for free.

As part of the lesson, you will create a Rinkeby testnet node, replenish it with funds, and publish a smart contract.

Lesson 10 dedicated to Ethereum Swarm distributed data stores. Using distributed storage, you save on storing large amounts of data on the Ethereum blockchain.

In this tutorial, you will create a local Swarm storage, write and read files, and file directories. Next, you will learn how to work with the public Swarm gateway, write scripts to access Swarm from Node.js, as well as using the Net::Ethereum::Swarm Perl module.

Lesson 11 Goal — master working with Solidity smart contracts using the popular Python programming language and the Web3.py framework. You will install this framework, write scripts to compile and publish a smart contract, as well as to call its functions. In this case, Web3.py will be used both by itself and in conjunction with the Truffle integrated development environment.

At lesson 12 you will learn how to transfer data between smart contracts and the real world using oracles. This is useful for receiving data from websites, IoT devices, various devices and sensors, and sending data from smart contracts to these devices. In the practical part of the lesson, you will create an oracle and a smart contract that receives the current USD-to-ruble exchange rate from the website of the Central Bank of the Russian Federation.

Lesson 1. Briefly about the blockchain and the Ethereum networkThe purpose of the lesson: get acquainted with the principles of the Ethereum blockchain, its areas of application and basic terminology.
Practical tasks: not included in this lesson.

There is hardly a software developer today who has not heard anything about blockchain technology (Blockchain), cryptocurrencies (Cryptocurrency or Crypto Currency), bitcoins (Bitcoin), initial coin offering (ICO, Initial coin offering), smart contracts (Smart Contract), as well as other concepts and terms related to the blockchain.

Blockchain technology opens up new markets and creates jobs for programmers. If you comprehend all the intricacies of cryptocurrency technologies and smart contract technologies, then you should have no problems applying this knowledge in practice.

I must say that there is a lot of speculation around cryptocurrencies and blockchains. We will leave aside discussions about changes in cryptocurrency rates, about the creation of pyramids, about the intricacies of cryptocurrency legislation, etc. In our tutorial, we will mainly focus on the technical aspects of using smart contracts on the Ethereum blockchain (ethereum, ether) and developing so-called decentralized applications (Distributed Application, DApp).

What is blockchain?

Blockchain (Blockchain, Block Chain) is a chain of blocks of data related to each other in a certain way. At the beginning of the chain is the first block, which is called the primary block (genesis block) or genesis block. It is followed by a second, then a third, and so on.

All these blocks of data are automatically duplicated on numerous nodes of the blockchain network. This ensures decentralized storage of blockchain data.
You can think of a blockchain system as a large number of nodes (physical or virtual servers) networked together and replicating all changes in the data block chain. It is like a giant multi-server computer, and the nodes of such a computer (servers) can be scattered around the world. And you too can add your computer to the blockchain network.

Distributed database

Blockchain can be thought of as a distributed database that is replicated to all nodes of the blockchain network. In theory, the blockchain will be operational as long as at least one node is working, storing all the blocks of the blockchain.

Distributed Data Registry

Blockchain can be thought of as a distributed ledger of data and operations (transactions). Another name for such a register is a ledger.

Data can be added to the distributed ledger, but it cannot be changed or deleted. This impossibility is achieved, in particular, by the use of cryptographic algorithms, special algorithms for adding blocks to the chain, and decentralized data storage.

When adding blocks and performing operations (transactions), private and public keys are used. They restrict blockchain users by giving them access to only their own blocks of data.

  Transactions

Blockchain stores information about operations (transactions) in blocks. At the same time, old, already completed transactions cannot be rolled back or changed. New transactions are stored in new, added blocks.

Thus, the entire history of transactions can be recorded in the blockchain unchanged. Therefore, the blockchain can be used, for example, to securely store bank transactions, copyright information, the history of changes in property owners, etc.

The Ethereum blockchain contains the so-called system states. As transactions progress, the state changes from initial to current. Transactions are recorded in blocks.

Public and private blockchains

It should be noted here that all of the above is true only for the so-called public blockchain networks, which cannot be controlled by any individual or legal entity, state bodies or governments.
The so-called private blockchain networks are under the complete control of their creators, and everything is possible there, for example, the complete replacement of all blocks of the chain.

Practical Applications of Blockchain

What can blockchain be used for?

In short, the blockchain allows you to securely conduct transactions (transactions) between persons or companies that do not trust each other. The data recorded in the blockchain (transactions, personal data, documents, certificates, contracts, invoices, etc.) cannot be faked or replaced after recording. Therefore, based on the blockchain, it is possible to create, for example, trusted distributed registries of various kinds of documents.

Of course, you know that blockchain-based crypto-currency systems are being created to replace conventional paper money. Paper money is also called fiat money (from Fiat Money).
Blockchain provides storage and immutability of transactions recorded in blocks, so it can be used to create cryptocurrency systems. It contains the entire history of the transfer of crypto funds between different users (accounts), and any operation can be tracked.

Although transactions within crypto-currency systems may be anonymous, withdrawing crypto-currency and exchanging it for fiat money usually reveals the identity of the owner of the crypto-currency asset.

The so-called smart contracts, which are software running on the Ethereum network, allow you to automate the process of concluding transactions and monitoring their implementation. This is especially effective if the payment for the transaction is carried out with the Ether cryptocurrency (ether).

The Ethereum blockchain and Ethereum smart contracts written in the Solidity programming language can be used, for example, in the following areas:

  • an alternative to notarization of documents;
  • keeping a register of real estate objects and information about transactions with real estate objects;
  • storage of copyright information for intellectual property (books, images, musical works, etc.);
  • establishing independent voting systems;
  • finance and banking;
  • logistics on an international scale, tracking the movement of goods;
  • storage of personal data as an analogue of the identity card system;
  • safe transactions in the commercial area;
  • storing the results of medical examinations, as well as the history of prescribed procedures

Problems with the blockchain

But, of course, not everything is as simple as it might seem!

There are problems with verifying data before adding it to the blockchain (for example, are they fake?), problems with the security of system and application software used to work with the blockchain, problems with the possibility of using social engineering methods to steal access to cryptocurrency wallets, etc. .P.

Again, if we are not talking about a public blockchain whose nodes are scattered all over the world, but about a private blockchain owned by a person or organization, then the level of trust here will be no higher than the level of trust in this person or organization.

It should also be taken into account that the data recorded in the blockchain becomes available to everyone. In this sense, the blockchain (especially public) is not suitable for storing confidential information. However, the fact that the information on the blockchain cannot be changed can help prevent or investigate various kinds of fraudulent activities.

Ethereum decentralized applications will be convenient if you pay for their use with cryptocurrency. The more people who own or are willing to buy cryptocurrencies, the more popular DApps and smart contracts will become.

Among the general problems of the blockchain that hinder its practical application, we can mention the limited speed of adding new blocks and the relatively high cost of transactions. But technology in this area is actively developing, and it is hoped that technical problems will be resolved over time.

Another problem is that Ethereum blockchain smart contracts run in an isolated environment of virtual machines, and do not have access to real world data. In particular, the smart contract program cannot itself read data from websites or any physical devices (sensors, contacts, etc.), and cannot output data to any external devices. We will discuss this problem and ways to solve it in the lesson devoted to the so-called Oracles - information intermediaries of smart contracts.

There are also legal restrictions. In some countries, for example, it is forbidden to use cryptocurrency as a means of payment, but you can own it as a kind of digital asset, like securities. Such assets can be bought and sold on the stock exchange. In any case, when creating a project working with cryptocurrencies, you need to familiarize yourself with the legislation of the country under whose jurisdiction your project falls.

How the blockchain chain is formed

As we have already said, a blockchain is a simple chain of blocks of data. First, the first block of this chain is formed, then the second is added to it, and so on. Transaction data is supposed to be stored in blocks and added to the very last block.

On fig. 1.1 we have shown the simplest version of the block sequence, where the first block refers to the next one.

The book “Creating Solidity Smart Contracts for the Ethereum Blockchain. Practical Guide»
Rice. 1.1. Simple Block Sequence

In this case, however, it is very easy to fake the contents of any block in the chain, since the blocks do not contain any information to protect against changes. Considering that the blockchain is intended for the use of people and companies between which there is no trust, it can be concluded that this method of storing data is not suitable for the blockchain.

Let's deal with protecting blocks from forgery. At the first stage, we will try to protect each block with a checksum (Fig. 1.2).

The book “Creating Solidity Smart Contracts for the Ethereum Blockchain. Practical Guide»
Rice. 1.2. Adding checksum protection for block data

Now an attacker cannot change the block just like that, since it contains the checksum of the block data. A checksum check will show that the data has been changed.

You can use one of the hash functions such as MD-5, SHA-1, SHA-256, etc. to calculate the checksum. Hash functions calculate some value (for example, in the form of a text string of constant length) as a result of performing irreversible operations on a block of data. Operations depend on the type of hash function.

Even if the content of the data block changes slightly, the hash value will also change. By analyzing the value of a hash function, it is impossible to recover the block of data for which it was calculated.

Will such protection be sufficient? Unfortunately no.

In this scheme, the checksum (hash function) only protects individual blocks, but not the entire block chain. Knowing the algorithm for calculating the hash function, an attacker can easily change the contents of the block. Also, nothing will prevent him from removing blocks from the chain or adding new ones.

To protect the entire chain as a whole, you can store in each block along with the data also a hash of the data of the previous block (Fig. 1.3).

The book “Creating Solidity Smart Contracts for the Ethereum Blockchain. Practical Guide»
Rice. 1.3. Add the hash of the previous block to the data block

In this scheme, to change any block, you need to recalculate the hash functions of all subsequent blocks. It would seem, what is the problem?

In real blockchains, artificial difficulties are additionally created for adding new blocks - algorithms are used that require a lot of computing resources. Taking into account the fact that in order to make changes to a block, it is necessary to recalculate not one of this block, but all subsequent ones, it will be extremely difficult to do this.

Recall also that blockchain data is stored (replicated) on numerous network nodes, i.e. decentralized storage is used. And this greatly complicates the forgery of the block, because. changes need to be made to all network nodes.

Since blocks store information about the previous block, it is possible to check the contents of all blocks in the chain.

Blockchain Ethereum

The Ethereum blockchain is a platform on which you can build distributed DApps. Unlike other platforms, Ethereum allows the use of so-called smart contracts (smart contracts, smart contracts) written in the Solidity programming language.

This platform was created in 2013 by Vitalik Buterin, founder of Bitcoin Magazine, and launched in 2015. Everything that we will learn or do in our training course is related specifically to the Ethereum blockchain and Solidity smart contracts.

Mining or how blocks are created

Mining is a rather complex and resource-intensive process of adding new blocks to the blockchain, and not “cryptocurrency mining” at all. Mining ensures the efficiency of the blockchain, because. it is this process that is responsible for adding transactions to the Ethereum blockchain.

The people and organizations involved in adding blocks are called miners.
The software (software) running on the nodes of the miners tries to pick up a hashing parameter called Nonce for the last block in order to get a certain hash value given by the network. The Ethash hashing algorithm used in Ethereum allows you to get the Nonce value only by sequential enumeration.

If the miner node found the correct Nonce value, then this is the so-called proof of work (PoW, Proof-of-work). In this case, if the block is added to the Ethereum network, the miner receives a certain reward in the network's currency - Ether. At the time of writing this book, the reward is 5 Ether, but this will decrease over time.

Thus, Ethereum miners ensure the operation of the network by adding blocks, and receive cryptocurrency money for this. You will find a lot of information about miners and mining on the Internet, and we will focus on creating Solidity contracts and DApps on the Ethereum network.

Lesson summary

In the first lesson, you got acquainted with the blockchain and learned that it is a sequence of blocks composed in a special way. The contents of previously recorded blocks cannot be changed, since this will require recalculating all subsequent blocks on many network nodes, which requires a lot of resources and time.

Blockchain can be used to store the results of transactions. Its main purpose is to organize the secure execution of transactions between parties (persons and organizations), between which there is no trust. You have learned in which specific areas of business and in which areas you can use the Ethereum blockchain and Solidity smart contracts. This is the banking sector, registration of property rights, documents, etc.

You also learned that various problems can arise when using blockchain. These are the problems of verifying information added to the blockchain, the speed of the blockchain, the cost of transactions, the problem of data exchange between smart contracts and the real world, as well as potential attacks by intruders aimed at stealing cryptocurrency funds from user accounts.

We also briefly talked about mining as a process of adding new blocks to the blockchain. Mining is required to complete transactions. Those who are engaged in mining ensure the efficiency of the blockchain and receive a reward in cryptocurrency for this.

Lesson 2: Preparing a Desktop Environment on Ubuntu and DebianOperating system selection
Installing the necessary utilities
Installing Geth and Swarm on Ubuntu
Installing Geth and Swarm on Debian
Savant Providence
Downloading the Go distribution
Setting environment variables
Checking the Go version
Installing Geth and Swarm
We create a private blockchain
Preparing the genesis.json file
Create a work directory
Create an account
Run node initialization
Node startup options
Connecting to our site
Mining management and balance check
Shutting down the Geth console
Lesson summary

Lesson 3Getting Your Raspberry Pi 3 Ready
Installing Rasberian
Installing updates
Enabling SSH Access
Setting a static IP address
Installing the necessary utilities
Installing Go
Downloading the Go distribution
Setting environment variables
Checking the Go version
Installing Geth and Swarm
We create a private blockchain
Account and balance check
Lesson summary

Lesson 4Viewing and adding accounts
Viewing a list of accounts
Adding an account
geth account command options
Account passwords
Cryptocurrency in Ethereum
Ethereum currency
Determine the current balance of our accounts
Transferring funds from one account to another
eth.sendTransaction Method
Viewing the Status of a Transaction
Transaction receipt
Lesson summary

Lesson 5Smart contracts in Ethereum
Smart contract execution
Ethereum Virtual Machine
Integrated Development Environment Remix Solidity IDE
Compilation start
Calling Contract Functions
Publication of a contract in a private network
Get the ABI definition and contract binary
Publication of the contract
Checking the status of a contract publish transaction
Calling Contract Functions
solc batch compiler
Installing solc in Ubuntu
Installing solc on Debian
Compiling the HelloSol contract
Publication of the contract
Installing solc on Rasberian
Lesson summary

Lesson 6. Smart contracts and Node.jsInstalling Node.js
Installation in Ubuntu
Installation on Debian
Installing and running Ganache-cli
Installing Web3
Solc installation
Installing Node.js on Rasberian
Script to get a list of accounts in the console
Script for publishing a smart contract
Running and getting parameters
Getting launch options
Contract Compilation
Unlocking your account
Download ABI and contract binary code
Estimating the required amount of gas
Create an object and start publishing a contract
Running the contract publishing script
Calling smart contract functions
Is it possible to update the published smart contract
Working with Web3 version 1.0.x
Get a list of accounts
Publication of the contract
Calling Contract Functions
Transferring funds from one account to another
Transfer of funds to the contract account
Updating the HelloSol smart contract
Create a script to view the account balance
Add a getBalance function call to the call_contract_get_promise.js script
Replenishment of the smart contract account
Lesson summary

Lesson 7. Introduction to TruffleInstalling the Truffle
Create a HelloSol project
Creating the Project Directory and Files
contracts directory
Directory migrations
test directory
truffle-config.js file
Compiling the HelloSol contract
Start publishing a contract
Calling HelloSol contract functions in a Truffle prompt
Calling HelloSol contract functions from a JavaScript script running Node.js
Installing the truffle-contract module
Calling the contract functions getValue and getString
Calling contract functions setValue and setString
Change of contract and republishing
Working with Web3 version 1.0.x
Making changes to the HelloSol smart contract
Scripts for calling contract methods
Testing in Truffle
Solidity test
JavaScript test
Lesson summary

Lesson 8Contract for learning data types
Boolean data types
Unsigned and signed integers
Fixed Point Numbers
Address
Variables of Complex Types
Fixed Arrays
Dynamic arrays
Listing
Structure
dictionaries
Lesson summary

Lesson 9Publishing a contract from Truffle to the Geth private network
Preparing a Private Network Host
Contract preparation for work
Compiling and migrating the contract to the Truffle network
Running geth LAN migration
Getting artifacts Truffle
Publishing a contract from Truffle to the Rinkeby testnet
Preparing the Geth Node for Rinkeby
Node Synchronization
Adding accounts
Topping up your Rinkeby account with Ether
Starting contract migration to the Rinkeby network
View contract information on the Rinkeby network
Truffle Console for Rinkeby Network
Easier way to call contract functions
Calling contract methods with Node.js
Transfer funds between accounts in the Truffle console for Rinkby
Lesson summary

Lesson 10How Ethereum Swarm Works
Installing and running Swarm
Operations on files and directories
Uploading a file to Ethereum Swarm
Reading a file from Ethereum Swarm
Viewing the Manifest of an Uploaded File
Loading directories with subdirectories
Reading a file from an uploaded directory
Using the Swarm Public Gateway
Calling Swarm from Node.js scripts
Perl Net::Ethereum::Swarm module
Installing the Net::Ethereum::Swarm module
Writing and reading data
Lesson summary

Lesson 11Installing Web3.py
Update and install required packages
Installing the easysolc module
Publishing a contract with Web3.py
Contract Compilation
Connecting to a provider
Execute contract publishing
Saving the contract address and abi in a file
Running the contract publishing script
Calling Contract Methods
Reading address and contract abi from JSON file
Connecting to a provider
Create a contract object
Calling Contract Methods
Truffle and Web3.py
Lesson summary

Lesson 12Can a smart contract trust data from the outside world
Oracles as Blockchain Information Intermediaries
Data source
Code to represent data from a source
Oracle to record the exchange rate in the blockchain
Contract USDRateOracle
Updating the exchange rate in a smart contract
Using the Web Socket Provider
Waiting for the RateUpdate event
Handling the RateUpdate event
Initiating a data update in a smart contract
Lesson summary

Source: habr.com

Add a comment