blockchain programming

23
Blockchain Programming Software Development With Ethereum Rob Myers - https://robmyers.org/

Upload: rob-myers

Post on 15-Jul-2015

166 views

Category:

Internet


7 download

TRANSCRIPT

Page 1: Blockchain Programming

Blockchain ProgrammingSoftware Development With Ethereum

Rob Myers - https://robmyers.org/

Page 2: Blockchain Programming

Implementations Of Ethereum

● go-ethereum● cpp-ethereum● pyethereum● ethereumj (java)● node-ethereum (client)All implement the same VM / network.Docker images are available.

Page 3: Blockchain Programming

Ðapps

Distributed Applications● Ethereum - Smart contracts● Whisper - Ephemeral communication● Swarm - DHT storage

Page 4: Blockchain Programming

How Will We Use Ðapps?

Page 5: Blockchain Programming

How Will We Develop Ðapps?

Page 6: Blockchain Programming

How Do We Write Ðapps Now?

Page 7: Blockchain Programming

How Do We Deploy And Test Ðapps?

Page 8: Blockchain Programming

What Is A Contract?

● A small piece of code.● A self-contained piece of code.● A small amount of data.● A public body of data.● A small amount of Ether.

Page 9: Blockchain Programming

If You Know MVC...

Page 10: Blockchain Programming

So How Do We Get External State?

● Direct user input● Oracles● Transactions / feeds from trusted parties● SchellingCoins● By hash...

Page 11: Blockchain Programming

What Is A Transaction?

● How we communicate with contracts.● Data and Ether can be sent via transactions.● Sent from the JS API or other contracts.● Transactions cost "gas", the sender pays.● JavaScript API can use accessors instead.

Page 12: Blockchain Programming

Transaction Costs

“usually, a computational step costs 1 gas, but some operations cost higher amounts of gas because they are more computationally expensive, or increase the amount of data that must be stored as part of the state. There is also a fee of 5 gas for every byte in the transaction data.” - The Whitepaper.

Page 13: Blockchain Programming

Bad Ideas For Contracts

● Doom on the EVM (too much processing).● Video on the blockchain (too much storage).● Ethereum web server (no network access).● Blockchain DRM (blockchain is public).● Anything that needs too much processor

time, storage, or secrecy.

Page 14: Blockchain Programming

Good Ideas For Contracts

● Altcoins, tokens, assets.● Crowdfunding, fan incentive schemes.● Voting systems, prediction markets, lotteries.● Access control - sites, games, doors, cars.● DAOs - Organizations on the blockchain.● Use your imagination! :-)

Page 15: Blockchain Programming

Writing Contracts

● Write contracts in the Solidity language.● Create the UI for contracts in HTML/JS.● AlethZero is a good development system.● solc / eth / Chrome is the cutting edge.● Test on a private chain.● Mix IDE & Mist browser are the future.

Page 16: Blockchain Programming

Solidity

● C-ish syntax() {;}● Statically typed● Types for hashes, addresses, memory cells● Structs, mappings, events● Support for invariants & other conditions● Provably correct subset

Page 17: Blockchain Programming

A Solidity Contractcontract Coin {

address minter;

mapping (address => uint) balances;

function Coin() {

minter = msg.sender;

}

function mint(address owner, uint amount) {

if (msg.sender != minter) return;

balances[owner] += amount;

}

function send(address receiver, uint amount) {

if (balances[msg.sender] < amount) return;

balances[msg.sender] -= amount;

balances[receiver] += amount;

Page 18: Blockchain Programming

Compiling The Contract

$ solc --binary file --json-abi file Coin.sol

(Or paste it into AlethZero...)

Page 19: Blockchain Programming

The Compiled Contract60056013565b6101e0806100216000396000f35b336000819060000155505b56006000357c010000

00000000000000000000000000000000000000000000000000009004806337f42841146100455780

6340c10f191461005a578063d0679d341461006e57005b6100506004356101a8565b806000526020

6000f35b610068600435602435610082565b60006000f35b61007c6004356024356100fd565b6000

6000f35b60005473ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffff

ffffffffffffffffffffff1614156100bd576100c2565b6100f9565b80600160008473ffffffffff

ffffffffffffffffffffffffffffff16815260200190815260200160002090815401908190600001

55505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff168152602001

908152602001600020541061013257610137565b6101a4565b80600160003373ffffffffffffffff

ffffffffffffffffffffffff16815260200190815260200160002090815403908190600001555080

600160008473ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000

209081540190819060000155505b5050565b6000600160008373ffffffffffffffffffffffffffff

ffffffffffff1681526020019081526020016000205490506101db565b91905056

Page 20: Blockchain Programming

The Contract's ABI[

{

"constant" : true,

"inputs" : [

{

"name" : "addr",

"type" : "address"

}

],

"name" : "queryBalance",

"outputs" : [

{

"name" : "balance",

"type" : "uint256”,

Page 21: Blockchain Programming

Call The Contract From JavaScript<script type="text/javascript" src="../ext/bignumber.min.js"></script><script type="text/javascript" src="../ext/ethereum.js/dist/ethereum.js"></script>...var web3 = require('web3');web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8545'));var contract_address = "0xaf1206fcb32fbdb42878c429e61a49d5143e6f32";var contract_abi = ... // see previous slidevar contract = web3.eth.contract(contract_address, contract_abi);...var accountBalance = parseInt(contract.call().queryBalance(queryAddress));contract.call().send("0xaf1206fcb32fbdb42878c429e61a49d5143e6f32", accountBalance / 100);

Page 22: Blockchain Programming

Try It Yourself

Online compiler:https://chriseth.github.io/cpp-ethereum/

The official tutorial:https://dappsforbeginners.wordpress.com/

Page 23: Blockchain Programming

Any Questions?

http://robmyers.org/notes/ethereum/