Scout App for monitoring

Blockchain Basics

A blockchain in its simplest form is a public ledger which holds a list of records where anything that has ever been recorded once cannot be altered or removed. It is called blockchain — as in chain of blocks — because its data structure is an ever-growing, ordered list of blocks.

Every block contains a cryptographic hash of the previous block. So if an older block is altered, its cryptographic hash will automatically change, and this change will immediately be detected.

But detected by whom? The blockchain runs on a borderless, decentralized, peer-to-peer network of computers (called "miners") which all run the same protocol for validating new blocks.

This protocol can be Bitcoin, Ethereum, or any of the other numerous cryptocurrencies we’ve been introduced to as they’ve begun to dominate financial news cycles.

The most powerful property of a blockchain is its ability to store trust. For the first time in human history, the Bitcoin blockchain gave us the ability to store trust in a peer-to-peer network, without any governmental oversight or ruling authority.

The first application, of course, has been in the form of a currency called Bitcoin.

The Bitcoin paper, which appeared in October 2008, was authored by an anonymous figure using the alias Satoshi Nakamoto.

There have been previous attempts to create electronic money, but until Satoshi presented his paper, all failed to effectively solve the double spending problem. This is the problem of preventing someone from sending one bitcoin twice without central authority.

The algorithm that solves this problem is an integral part of the blockchain called the Proof of Work algorithm.

Proof of Work is the method by which all blocks in the chain are verified. It requires all miners in the blockchain network to solve difficult mathematical puzzles to verify each block. Solving these puzzles requires a great expense of electricity, which costs a lot of money and makes it prohibitively expensive for dishonest participants to alter the block in any way. What miners are trying to do is alter the input to a hash function just enough to get an output that matches a condition. For example alter the string "Hello world!" by adding a number at the end so that the resulting hash starts with 000. We try adding a number at the end of the string and increment it until we get a satisfying result. In this case that takes 4250 tries.

image alt text

Blockchain 2.0

In 2014 Vitalik Buterin, then 19 years old, published a paper about a cryptocurrency called Ethereum that runs on a new type of blockchain, which has a compiler built in, enabling it to process computer code as well as transactions. Pieces of code that run on this blockchain are called smart contracts, and they open up many more use cases for blockchain technology. For example, smart contracts can eliminate the need for a middleman in areas that were previously impossible to navigate without one. Currently, companies like AirBnb and Uber exist primarily to serve as a centralized point of trust for things like payments, discovery, and communication between clients and providers. In the case of a lodging company like AirBnB, each of these services can be replaced or greatly improved by smart contracts:

One smart contract can be used to manage the guest / host relationship, processing payments, transferring money, and providing refunds;

a second can be used to pay for the hosting and maintenance of the booking website itself; and a third

can be used to schedule and pay the company that cleans and maintains apartments after guests leave.

This becomes even more interesting with the use of IoT-enabled devices like smart locks.

A guest can send payment (in Ethereum) to a smart contract which is connected to the smart lock on the front door of an AirBnb apartment, along with the start and end dates of his stay.

Upon arrival, the guest can identify himself using his mobile phone. The smart lock reads his public Ethereum address, recognizes that he’s made a payment, and unlocks the door for the duration of his stay. At the end of his stay, the smart lock locks the door.

To make all of this more clear, let’s write a simple smart contract that implements ownership of stocks in some company or asset.

We will be coding in a programming language called Solidity which is the most mature of all blockchain programming languages. A blockchain programming language means that this code, when compiled, can be executed directly on the Ethereum blockchain by any computer participating in that network.

If you compile and deploy this contract, then call one of its methods, there’s nothing that can stop it from executing. That’s why the mantra of Ethereum has been Unstoppable Code.

Much like the transactions in the Bitcoin blockchain are irreversible, the code in smart contracts deployed on the Ethereum blockchain is also irreversible.

Let’s begin.

contract Greeting {



    address creator;

    string greeting;

    function Greeting(string _greeting) {

        greeting = _greeting;

        creator = msg.sender;

    }

We define smart contracts with the contract keyword, much like a class.

Our contract is called Greeting, and we have a constructor with the same name. We have two state variables called creator and greeting. The creator is of a type called address. Address is a special type of variable that represents another account on the blockchain. This account can be a person or another smart contract. The other variable is a string value of our greeting.

The constructor takes a string parameter and stores it as the contract’s greeting.

On the second line where we assign the creator, we are using a special global variable called msg.

Msg is a global variable that holds special information about the originator of the call to the method. For now, we are concerned only with its property called sender. This represents the account that is deploying the smart contract. Note that the constructor of the smart contract is executed only once, during deployment.

Of course, if we need more instances of the same smart contract then we can deploy it multiple times. Each of those deployments will have its own separate address.

Here’s a Getter method.

function greet() constant returns (string) {

        return greeting;

    }

This method is the simplest method in the world. It is basically a getter for the greeting state variable. The constant keyword indicates that this method doesn’t alter the state of the smart contract and doesn’t write anything to the blockchain.

At the end of the method definition we define the return type of string. If a method doesn’t return anything this part is omitted.

Access controlled setter method

    function setGreeting(string new_greeting) {

        assert(creator != msg.sender);

        greeting = new_greeting;

        }

setGreeting is a setter method which sets new value for our greeting. But it has access control built in.

In the first line we check if the caller of the method is the same as the creator of the smart contract. If there is a mismatch an exception is thrown. Otherwise, the new greeting is saved.

Now, let’s use Remix.

Remix is an online debugger, code editor and compiler for Solidity.

Let’s copy and paste our full Smart Contract into the editor.

image alt text

There are a few warnings on the right side which we don’t have to worry about, but our code is correct.

Let’s break our code and see if Remix will notice. I will delete the semicolon and the letter "r" from msg.sender on line 10. As you can see, we immediately get a red cross.

In the right sidebar we see a red balloon.

image alt text

The full text says:

browser/Greeting.sol:11:5: ParserError: Expected token Semicolon got 'RBrace'

    }

    ^

If we click the balloon it takes us to the offending line. It says it’s expecting a semicolon, let’s fix that and see what happens.

Straight away it detects that sende is not a member of the msg object.

browser/Greeting.sol:10:19: TypeError: Member "sende" not found or not visible after argument-dependent lookup in msg
creator = msg.sende;
^-------^

The error disappears after we add the missing r.

Speaking of deployment: tomorrow we’ll set up a local environment and deploy our smart contract.

Summary

Today we learned what blockchain is and its central role in the cryptocurrency ecosystem. Besides running cryptocurrency systems, the blockchain can also be used to run general-purpose programming code. This code is organized into smart contracts, which are the equivalents of classes in most programming languages. In the end, we wrote the "Hello World" of smart contracts and debugged it online.

Resources

Remix Online Editor

Solidity Documentation

Mastering Bitcoin Book

Bitcoin Whitepaper

Ethereum Whitepaper

Scout App for monitoring

Darko Kolev

Student of life, people, software development and business. Currently Lead Software Engineer at BitcoinAverage

  1. Comments for Introduction to Blockchain

You must login to comment

You May Also Like