September 4, 2018

Blockchain, Smart Contracts and Ethereum

By

Theta

Blockchain, Smart Contracts and Ethereum

Blockchain is another of those “suitcase words” – like artificial intelligence or UX, a term stuffed with multiple meanings. It’s often held up as a signifier of innovation, and is seen by many as the door to a distributed, decentralised internet, and a solution to almost any technological problem. What ‘blockchain’ actually is can be harder to decipher.

In this post, I’ll start with some blockchain basics before taking a closer look at smart contracts, one blockchain application we’ve been exploring at Theta.

When people hear the word blockchain their first thought is usually cryptocurrency or Bitcoin. They’ve heard about the wildly volatile market and the people making or losing millions – Bitcoin rose from a value of around $1000 USD at the start of 2017 to almost $20,000 in December 2017 and is now floating around $7,000, with approximately $4b traded daily. There is a lot of investment in this space, but what is everyone really investing in?

In reality, a blockchain is simply a continuously growing list of blocks or records that are securely linked to create a distributed ledger that can record interactions or transactions between two parties in a verifiable and permanent way.  The consensus of the majority of a blockchain network is required to perform a transaction or to make a change.

This makes a blockchain particularly good for recording events, storing medical or financial records, or for transferring funds. Once transactions are recorded it requires consensus of the majority of the network to change them. There is no centralised location that can be hacked and altered.

How is blockchain used now?

Some current applications of blockchain technology:

Guardtime is a blockchain security company whose clients include Maersk, the largest container ship and supply vessel operator in the world. Maersk has also released plans for a blockchain solution for streamlining marine insurance.

STORJ.io is a cloud storage company that allows users to share unused hard drive space.

Followmyvote.com is using blockchain for secure, transparent, tamper-proof voting in an attempt to encourage true democracy.

Estonia, after being hit by one of the largest ever instances of potentially state-sponsored cyberwarfare, in 2007 is now working on an initiative to move public records onto a blockchain as part of a massive cybersecurity effort.

Kodak has developed a blockchain system for managing intellectual property rights and payments for photographers. Their shares more than tripled in the days following their announcement but have since returned almost to their old value.

Blockchain @ Theta: investigating smart contracts

We probably couldn’t call ourselves an Innovation Lab without looking into blockchain, so I did some research on Ethereum.

The first blockchain that comes to mind for most people is Bitcoin – but Ethereum has the 2nd highest market cap of any cryptocurrency at approximately $27B (at time of writing) and has something that Bitcoin doesn’t – the Ethereum Virtual Machine (EVM).  The EVM allows you to execute smart contracts - immutable, permanent pieces of code.  This provides an interesting platform for use-cases where security is paramount.

So, how do you interact with the EVM?  Can you store data?  What’s so smart about smart contracts?

Smart contracts & Ethereum

For the rest of this article I’ll be talking about smart contracts – so what exactly are they?  A smart contract is a piece of code, written in the Solidity language if we’re using Ethereum, that ‘miners’ execute for you on their machines. In return for this execution, they receive a small fee for the processing power they used, measured in ETH, the cryptocurrency associated with the Ethereum blockchain. The key thing about smart contracts is that they are visible to everyone on the blockchain, and are immutable. The contract will exist as it was published, indefinitely. This means that if, for example, you had a smart contract where you pay everyone a certain amount of currency on a certain date, everyone could see who you were going to pay, and see that you’ve paid them.  This can cut out a middle man in this type of transaction – there is no need for a third party to guarantee a payment will happen when it’s already locked in.

Getting to grips with Ethereum

Mist

To get started, you can download Mist, an application that syncs you with the Ethereum blockchain and allows you to interact with smart contracts. It also allows you to create an account – make sure you remember your password, as there is no forgot password option. Here’s a great tutorial on Mist in general, including info on how to create a test account so you can test your contracts without having to pay real ETH.

Gas

Gas is the name for a special unit used in Ethereum.  It measures how much “work” an action or set of actions takes to perform. It’s used to make a call to a smart contract (aka making a transaction) or to actually execute the smart contract on the blockchain. This means that more complex smart contracts use more gas, so you need to be careful doing loops and other things that use a lot of processing power/time. Gas has a price in ETH, the cryptocurrency of the Ethereum blockchain, so you need to load your account up with ETH if you want your smart contracts to be executed and usable. Smart contracts can also accept ETH from users interacting with it and require certain amounts to do things, they can store it, and they can distribute it to users whose addresses are loaded into the contract.  

Solidity

Solidity is the language of the Ethereum virtual machine. This is what you write and deploy your contract in. The language is influenced by C++, Javascript and Python, is statically typed, and is well featured with things like inheritance, libraries etc. There is extensive documentation on the language, and plenty of advice on how to optimise your code performance-wise. You can play around with Solidity on the Remix online IDE.

How do you interact with a smart contract?

The first step to creating smart contracts and interacting with the Ethereum blockchain, other than using an Ethereum client such as Mist, is to get the appropriate code library – in this case I was writing a Universal Windows Platform (UWP) app in .NET, so I used the Nethereum NuGet package.

Here is some code we can use to deploy a new smart contract:

private async Task DeployContract( string senderAddress, string password, string abi, string bytecode, HexBigInteger transactionGas, params object[] contractConstructorParameters) { var unlockAccountResult = await Web3.Personal.UnlockAccount.SendRequestAsync( senderAddress, password, 120); var transactionHash = await Web3.Eth.DeployContract.SendRequestAsync( abi, bytecode, senderAddress, transactionGas, contractConstructorParameters); var receipt = await GetTrasactionReceiptAsync( Web3, Web3Geth, transactionHash); return receipt.ContractAddress; }

This gets us the address of this smart contract – its immutable location on the Ethereum blockchain.

To use this, we need the ABI and bytecode of our smart contract. The ABI is the definition of a smart contract – it lets everyone know what they can do with it, what functions they can perform etc.  The bytecode is the compiled smart contract itself. You can get these either by using some implementation of the Solidity compiler, e.g. solcjs, or by writing your contracts in Remix, Ethereum’s online dev-kit.

You also need your sender address and password, and the amount of gas you want to spend on this transaction. This is the maximum amount to allow, so set it to something like four million. If your contract fails to execute, the first thing to try is a higher gas price.    

Once we’ve got our smart contract deployed, we’ll probably want to interact with it; we can call functions with the following:

var smartContract = Web3.Eth.GetContract(smartContract.Abi, smartContract.ContractAddress);
await smartContract.GetFunction("FunctionName") .SendTransactionAsync(senderAddress, new HexBigInteger(100000), new HexBigInteger(0), "A Parameter");

You can also set up events in your smart contracts that you can then listen to externally, allowing you to wait for state changes and transactions to take place. To define and call events in Solidity, you can use the following code:

// Definition (in Solidity contract) event OurEvent(address indexed sender, string param1, string param2, string param3); // Event trigger (in Solidity contract) OurEvent(msg.sender, “one”, “two”, “three”);

And then to listen for this event in C#:

// Listen to event in C# var ourEvent = smartContract.GetEvent("OurEvent"); ourEventFilter = await ourEvent.CreateFilterAsync(); var ourEventResult = await ourEvent.GetFilterChanges(filter);

This toolkit allows you to create flexible yet immutable programs that live on the Ethereum blockchain.

Things to keep in mind

Solidity is Turing complete, meaning you can do compute anything that is computable, given enough processing power and time. This means you can translate programs written in other Turing complete languages like Python or C#.

Smart contracts are publicly viewable – don’t store sensitive information in them!

Computation time is incredibly important in a smart contract as it directly correlates to the cost of a transaction. If your contract is too computationally expensive, it might never be run (or will cost you a fortune).

Smart contracts can never be updated. If you load up a smart contract with Ethereum and decide you want to change it or find a bug, you’re out of luck.

Blockchain potential

So, after this experimentation, do we see potential for blockchain technology in the Innovation Lab?

There are some scenarios where blockchain can be useful. This might include a voting system where each address is guaranteed to only have voted once, or a cryptocurrency payment system where individuals get paid out a certain amount at a given time.

Smart contracts are also great for storing information that is intended to be publicly available but that should never change – this could be something like official information or public records.

In a nutshell: anything where security or consistency is critical could be a candidate for a blockchain application. It’s a useful technology to have in the mix, and, like many innovative technologies blockchain also provides a creative spark. It allows us to consider and develop new and alternative solutions to age-old problems, and because of widespread media coverage stimulates public discussion on the future of technology.