Blockchain for Developers
Blockchain
• Have you heard of Bitcoin?
• Who hasn’t.
• Well, blockchain is the technology powering the Bitcoin.
The Problem Domain
• We want a safe place to store our (virtual) money.
• We want anonymity.
• We want the platform to be smart (i.e., no intermediaries).
• Blockchain is the enabler.
Blockchain & Bitcoin
• The machines that constitute the blockchain network are called:
nodes.
• The nodes are anonymous.
• The “ledger’ of the transactions is replicated.
• Each node has a copy.
• Wait, transactions?
Have an Address
• So, we can generate a private key to some virtual wallet
address.
• No chance of collision.
• A transaction is a money transfer.
Adding a Block to the Chain
• In order to add transaction to the chain, a node needs to create
the next block containing that transaction (and probably some
more).
• Block size is cupped at 1mb.
Hashes
• In order to add a new block, the node (miner) has to solve a
difficult problem.
• Find a hash that includes the transactions, fees and the
previous block reference.
• Difficulty is dynamic and tries to make a new block a 10 minutes
task.
Miners
• Miners are fully autonomous and can even create empty blocks
(only the ”new block” fee).
• The ”source of truth” is the longest blockchain visible by the
majority of the nodes.
Not Only Currency
• The blockchain technology can be used for more things than
virtual currency:
• Smart Contracts.
• Source of Truth.
• Anonymity.
Ethereum
• The main purpose of Bitcoin is to leverage the blockchain
technology for safe and visible ownership of cryptocurrency
along with an online transaction platform.
• The main purpose behind ethereum is to run a decentralized
application.
• Application? Over blockchain?
Ethereum
• The cryptocurrency is called: Ether.
• Every node in the network runs the EVM (Ethereum Virtual
Machine).
• The consensus mechanism in Ethereum.
• Implementations are available in many programming
languages.
EVM
• Turing complete.
• The EVM specification is low-level.
• There are many high-level languages that compile into EVM
bytecode.
• One of the popular ones is: solidity.
Gwei and Gas
• Gwei is a 1 billionth (10-9) of Ether (ETH).
• A transaction fee is: (Gas consumed * Gas price).
• Gas prices can be checked at: https://ethgasstation.info/
• Note that the Gas consumed “per-line” is constant.
• “Gas consumed” for a simple transaction is: 21,000 (Gwei).
Smart Contracts - Solidity
contract Example1 {
uint age;
function setAge(uint x) public { age = x; }
function getAge() public view returns (uint) {
return age;
}
}
Discussion
• A very simple contract.
• Actually no contract at all.
• Everyone can change age.
• Though, history will be public and visible.
• Let’s see Gas…
Auction (Solidity by Example)
contract SimpleAuction {
address public beneficiary;
uint public auctionEnd;
address public highestBidder;
uint public highestBid;
// Allowed withdrawals of previous bids
mapping(address => uint) pendingReturns;
// Set to true at the end, disallows any change
bool ended;
event HighestBidIncreased(address bidder, uint amount);
event AuctionEnded(address winner, uint amount);
Discussion
• A getter function is generated for public fields.
• The address type is a built-in type allows for holding Ethereum
addresses.
• Supports the following members:
• balance, transfer().
• Some low-level methods (call, delegatecall, callcode).
Auctionconstructor( uint _biddingTime, address _beneficiary ) public {
beneficiary = _beneficiary;
auctionEnd = now + _biddingTime;
}
function bid() public payable {
require( now <= auctionEnd, "Auction already ended." );
require( msg.value > highestBid, "There already is a higher bid." );
if (highestBid != 0) {
pendingReturns[highestBidder] += highestBid;
}
highestBidder = msg.sender;
highestBid = msg.value;
emit HighestBidIncreased(msg.sender, msg.value);
}
Discussion
• The bid function is: payable.
• A keyword in Solidity.
• You can send ether to a payable function.
• Sending ether to non-payable functions, will invoke the (you
should provide) anonymous payable fallback function.
Discussion
• Require accepts a condition and an error message.
• If the condition is not met, undo all state changes and return the
second argument.
• Remaining gas will be refunded to the caller.
Auction
function withdraw() public returns (bool) {
uint amount = pendingReturns[msg.sender];
if (amount > 0) {
pendingReturns[msg.sender] = 0;
if (!msg.sender.send(amount)) {
pendingReturns[msg.sender] = amount;
return false;
}
}
return true;
}
function auctionEnd() public {
require(now >= auctionEnd, "Auction not yet ended.");
require(!ended, "auctionEnd has already been called.");
ended = true;
emit AuctionEnded(highestBidder, highestBid);
beneficiary.transfer(highestBid);
}
Ethereum Fork
• DAO (Decentrelized Autonomous Organization) was
crowdfunded as a form of venture capital fund.
• Not tied to any government.
• Launched at the end of April 2016.
• By may 2016 had an ether value of over 150 million $.
• On June 17 2016, a hacker managed to take advantage of a
vulnerability and took 11.5 million ether.
The Fork
• Caused a big debate whether to do something about it.
• Caused a hard fork of the Ethereum blockchain.
• Classic Ethereum incorporated the hack with Ethereum
returned the amount to DAO.
The Fork
• A hard fork is not always possible.
• In the case of the DAO attack, due to the nature of the smart
contract, the money was put in a DAO child account for 28
days.
• Allowing for an hard fork.
• This was not the case for several other attacks.
Security
• What does it mean for us?
• Bugs are very costly in smart contracts.
• Not your average “where is my button bug”.
Best Practices
• Mark calls to untrusted contracts.
• Check for failures in send() invocations.
• Assume that participants may not cooperate.
• E.g., consider a paper, scissors, rock game.
Ecosystem
• Remix – online IDE & compiler.
• Embark – JS based for doing TDD with smart contracts.
• Solidity Standard Library – a standard library?
• Collection Framework.
• Truffle – Environment for developing and testing smart
contracts.

Blockchain for Developers

  • 1.
  • 2.
    Blockchain • Have youheard of Bitcoin? • Who hasn’t. • Well, blockchain is the technology powering the Bitcoin.
  • 3.
    The Problem Domain •We want a safe place to store our (virtual) money. • We want anonymity. • We want the platform to be smart (i.e., no intermediaries). • Blockchain is the enabler.
  • 4.
    Blockchain & Bitcoin •The machines that constitute the blockchain network are called: nodes. • The nodes are anonymous. • The “ledger’ of the transactions is replicated. • Each node has a copy. • Wait, transactions?
  • 5.
    Have an Address •So, we can generate a private key to some virtual wallet address. • No chance of collision. • A transaction is a money transfer.
  • 6.
    Adding a Blockto the Chain • In order to add transaction to the chain, a node needs to create the next block containing that transaction (and probably some more). • Block size is cupped at 1mb.
  • 7.
    Hashes • In orderto add a new block, the node (miner) has to solve a difficult problem. • Find a hash that includes the transactions, fees and the previous block reference. • Difficulty is dynamic and tries to make a new block a 10 minutes task.
  • 8.
    Miners • Miners arefully autonomous and can even create empty blocks (only the ”new block” fee). • The ”source of truth” is the longest blockchain visible by the majority of the nodes.
  • 9.
    Not Only Currency •The blockchain technology can be used for more things than virtual currency: • Smart Contracts. • Source of Truth. • Anonymity.
  • 10.
    Ethereum • The mainpurpose of Bitcoin is to leverage the blockchain technology for safe and visible ownership of cryptocurrency along with an online transaction platform. • The main purpose behind ethereum is to run a decentralized application. • Application? Over blockchain?
  • 11.
    Ethereum • The cryptocurrencyis called: Ether. • Every node in the network runs the EVM (Ethereum Virtual Machine). • The consensus mechanism in Ethereum. • Implementations are available in many programming languages.
  • 12.
    EVM • Turing complete. •The EVM specification is low-level. • There are many high-level languages that compile into EVM bytecode. • One of the popular ones is: solidity.
  • 13.
    Gwei and Gas •Gwei is a 1 billionth (10-9) of Ether (ETH). • A transaction fee is: (Gas consumed * Gas price). • Gas prices can be checked at: https://ethgasstation.info/ • Note that the Gas consumed “per-line” is constant. • “Gas consumed” for a simple transaction is: 21,000 (Gwei).
  • 14.
    Smart Contracts -Solidity contract Example1 { uint age; function setAge(uint x) public { age = x; } function getAge() public view returns (uint) { return age; } }
  • 15.
    Discussion • A verysimple contract. • Actually no contract at all. • Everyone can change age. • Though, history will be public and visible. • Let’s see Gas…
  • 16.
    Auction (Solidity byExample) contract SimpleAuction { address public beneficiary; uint public auctionEnd; address public highestBidder; uint public highestBid; // Allowed withdrawals of previous bids mapping(address => uint) pendingReturns; // Set to true at the end, disallows any change bool ended; event HighestBidIncreased(address bidder, uint amount); event AuctionEnded(address winner, uint amount);
  • 17.
    Discussion • A getterfunction is generated for public fields. • The address type is a built-in type allows for holding Ethereum addresses. • Supports the following members: • balance, transfer(). • Some low-level methods (call, delegatecall, callcode).
  • 18.
    Auctionconstructor( uint _biddingTime,address _beneficiary ) public { beneficiary = _beneficiary; auctionEnd = now + _biddingTime; } function bid() public payable { require( now <= auctionEnd, "Auction already ended." ); require( msg.value > highestBid, "There already is a higher bid." ); if (highestBid != 0) { pendingReturns[highestBidder] += highestBid; } highestBidder = msg.sender; highestBid = msg.value; emit HighestBidIncreased(msg.sender, msg.value); }
  • 19.
    Discussion • The bidfunction is: payable. • A keyword in Solidity. • You can send ether to a payable function. • Sending ether to non-payable functions, will invoke the (you should provide) anonymous payable fallback function.
  • 20.
    Discussion • Require acceptsa condition and an error message. • If the condition is not met, undo all state changes and return the second argument. • Remaining gas will be refunded to the caller.
  • 21.
    Auction function withdraw() publicreturns (bool) { uint amount = pendingReturns[msg.sender]; if (amount > 0) { pendingReturns[msg.sender] = 0; if (!msg.sender.send(amount)) { pendingReturns[msg.sender] = amount; return false; } } return true; } function auctionEnd() public { require(now >= auctionEnd, "Auction not yet ended."); require(!ended, "auctionEnd has already been called."); ended = true; emit AuctionEnded(highestBidder, highestBid); beneficiary.transfer(highestBid); }
  • 22.
    Ethereum Fork • DAO(Decentrelized Autonomous Organization) was crowdfunded as a form of venture capital fund. • Not tied to any government. • Launched at the end of April 2016. • By may 2016 had an ether value of over 150 million $. • On June 17 2016, a hacker managed to take advantage of a vulnerability and took 11.5 million ether.
  • 23.
    The Fork • Causeda big debate whether to do something about it. • Caused a hard fork of the Ethereum blockchain. • Classic Ethereum incorporated the hack with Ethereum returned the amount to DAO.
  • 24.
    The Fork • Ahard fork is not always possible. • In the case of the DAO attack, due to the nature of the smart contract, the money was put in a DAO child account for 28 days. • Allowing for an hard fork. • This was not the case for several other attacks.
  • 25.
    Security • What doesit mean for us? • Bugs are very costly in smart contracts. • Not your average “where is my button bug”.
  • 26.
    Best Practices • Markcalls to untrusted contracts. • Check for failures in send() invocations. • Assume that participants may not cooperate. • E.g., consider a paper, scissors, rock game.
  • 27.
    Ecosystem • Remix –online IDE & compiler. • Embark – JS based for doing TDD with smart contracts. • Solidity Standard Library – a standard library? • Collection Framework. • Truffle – Environment for developing and testing smart contracts.