Blockchain Technology: Smart Contracts

CertiK | Oct 18, 2019

Article's Poster

Bitcoin Beginnings

The history of blockchain began with the first cryptocurrency: Bitcoin. Its creator, a mystery person going by “Satoshi Nakamoto”, had a vision of a fully decentralized digital currency – meaning that no single entity would have control over its transfer, creation, or storage.

Instead, it would belong to those who used it.

To do this, Satoshi laid out a protocol for a peer-to-peer network of computers. When someone on the Bitcoin network wants to transfer Bitcoin to another user, that initial user must simultaneously notify other users on the network to communicate their intent to make this new transaction.

Then–according to the rules of the network–every computer participating in this network must store the complete history of Bitcoin transactions. This works as if each computer maintains their own accounting book which keeps track of how much Bitcoin everyone else on the network has spent. This decentralized process allows Bitcoin to maintain a transaction history without a centralized database that a single party has control over.

In order to track the flow of the cryptocurrency accurately, these Bitcoin accounting-books must store identical information. However, due to the fact that networks are unreliable and users enter and leave the network at whim, it is not immediately obvious how to keep everyone's accounting-books perfectly in sync.

Enter the blockchain.

Blockchain as a Solution

Instead of users writing transactions to their accounting-books one at a time, they write them in large batches called "blocks." A set of users called "miners" are directly responsible for creating these blocks. Miners listen to the network for users indicating that they would like to perform a Bitcoin transaction. Once a transaction is picked up and recorded, a miner is then allowed to publish a new block full of all the transactions that occurred––not just that transaction, but that transaction and all transactions before it..

When the miner publishes a block to the rest of the network, each user first ensures that all the transactions in it are valid according to a set of predefined rules (i.e. no one is spending money that they don't have, money is not being created out of thin air, etc.). If the block is valid, they will then add it to their ever-growing list of transactions. Hence the name blockchain - the users store transaction history in a database that is a chain of blocks.

There is much more to discuss when it comes to blockchain and all the various subtleties that allow a cryptocurrency to be securely sent across this network. If you would like to learn more about how mining works, how a decentralized system can be resistant to attacks, and how Bitcoin uses cryptography to grant access to funds, or if you would simply like to understand the design decisions involved in-depth, read CertiK's Intro to Bitcoin series.

Let’s break this down one more time:

  • A blockchain is a database that exists in identical copies in computers all across the world.
  • When a user wants to make a transaction on a blockchain, he or she will have to send it out and wait for a miner to publish it in a block. For a block to be valid, it must adhere to a set of rules that all parties on the network agree to.
  • Once that block is published, the user's transaction will be added to the blockchain and thus included in the transaction history of the network.
  • Transactions cannot be deleted or reversed; they are in the blockchain history forever.
  • The blockchain is the way we record transactions, and it operates as the backbone of decentralized cryptocurrencies like Bitcoin. By having everyone record all transactions, there has to be a consensus––one that can’t be easily hacked or changed.

Ta-dah! You now understand the basics.

Ethereum and Smart Contracts

Let's take a step back and ask ourselves: what if we used this system for more than just sending payments ? What if we described a mechanism for globally agreeing on a set of information and rules to change that information?

In Bitcoin, the set of information being tracked is the wealth in Bitcoin of every user on the network. The rules for changing that information are pretty simple: no one can spend someone else's money, and no one can spend money they don't have, among other rules and restrictions encoded into the very framework of the blockchain itself.

However, there is no reason that a blockchain would be limited to simple money transfers. In theory, the information we're storing can be anything we want -- and the rules for modifying it can be anything we want as well.

That would change the digital world.

Consider a standard computer program: one that begins with a certain storage state that accepts user input, then modifies the state based on running the user input through a predetermined set of instructions.

Any program like this could be "blockchain-ified". The state in question can be represented on the blockchain, and any transaction can serve as the input to the program. The instructions that represent these programs can be communicated to each miner and user on the blockchain network, thus allowing them to update to the new state based on the latest set of transactions.

In Bitcoin, the state we are trying to update is the set of account balances. While a Bitcoin transaction is the user input, the account balance is modified by subtracting the spender’s balance and adding the transaction amount to the receiver.

(Note that in actuality the blockchain itself is simply a list of transactions - the state itself, e.g. the balances of all accounts, is not written to this list––but any user can derive the state using the known rules of the network.)

Let’s use a famous example.

Ethereum was invented by Vitalik Buterin, a Bitcoin enthusiast who advocated for the use of "smart contracts" in Bitcoin. When the Bitcoin community rejected his proposal, he decided to create his own alternative blockchain system in which smart contracts are possible.

To understand why Vitalik made Ethereum, we must first examine what a smart contract is and why it matters. A smart contract is a program that allows us to deterministically program the flow of currency in predefined ways, but it has broad implications for the blockchain world.

Imagine an actual legal contract between a group of three people: Amy, Bill, and Chris. This contract reads, "If Bill has paid Amy $1000 by August 13th, then Amy will pay $2000 to Chris. Otherwise Amy will not pay Chris."

In a sense, this is a sort of "program" for deterministically transferring money based on a certain set of conditions and enforced by the law. A smart contract is the same thing, but enforced by a machine–done to be unbreakable and applied without a central authority

Given that blockchain technology allows us to specify any program we want to determine the state transfer of cryptocurrency, blockchain makes smart contracts possible.

Ethereum enables users to publish whatever smart contracts they want onto the blockchain and use these smart contracts to dictate the flow of future monetary transfers.

When you publish a smart contract, you must write an Ethereum transaction with the contract's code in it. Once your transaction is approved, the Ethereum contract becomes part of the state of the blockchain - forever!

On Ethereum, both users and uploaded smart contracts have "addresses" that they are referenced by. Anyone can send cryptocurrency to whatever address they want, allowing for free decentralized exchange.

If a user has sent cryptocurrency, he is then in control of where he wants to send it next. If a smart contract has sent cryptocurrency, the flow of the cryptocurrency will be determined by the smart contract's code. This code will decide if the cryptocurrency ends up being paid to another user, refunded, or remains in the smart contract forever.

You can think of a smart contract as a sort of "robot account" that pays people in programmed ways and can’t be turned off– creating another level of use within the blockchain by creating automated assistants

In Ethereum, transactions do not just contain simple money transfers. A "transaction" in Ethereum can include the publishing of a new smart contract or an interaction with an existing smart contract.

Smart contracts expose certain functions that allow end users to interact with them. When a user wants to call a function, they publish a transaction on the blockchain communicating the name of the function they want to call and its parameters. When a transaction with a function call is mined and published, each computer on the Ethereum network will run the function in the smart contract to the application's state in a predictable way, to execute it. This means performing whatever monetary transfers and other changes to state variables the code describes.

By the way, just because these applications are called "contracts" doesn't mean they can only do things that resemble contracts in the real world. Anything is possible here - you can code whatever programs you want! Apps published on Ethereum include games, auctions, decentralized exchanges, token sales, and much more.

Smart contracts are just blockchain butlers, moving code and upgrading what’s possible for the automated, decentralized world.

If this does not quite make sense yet, the best way to learn is to start writing smart contracts yourself! You can easily start writing smart contracts using Ethereum's Solidity language with just basic programming knowledge.

We recommend CertiK's Solidity Tour, which we believe to be the most comprehensive Solidity tutorial series available. You will be guided through a number of real-world use cases for Ethereum contracts. By the end you’ll be ready to publish your own smart contract!