introduction to ethereum

63
Ethereum Developers Community Introduction to Ethereum Arnold Pham Lunyr Inc. https://www.linkedin.com/in/ arnoldpham/ Unless otherwise stated, these slides are licensed under the Creative Commons Attribution-NonCommercial 3.0 License (https://creativecommons.org/licenses/by-nc/3.0/us/)

Upload: arnold-pham

Post on 06-Apr-2017

126 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Introduction to Ethereum

Ethereum Developers CommunityIntroduction to Ethereum

Arnold PhamLunyr Inc.https://www.linkedin.com/in/arnoldpham/

Unless otherwise stated, these slides are licensed under the Creative Commons Attribution-NonCommercial 3.0 License (https://creativecommons.org/licenses/by-nc/3.0/us/)

Page 2: Introduction to Ethereum

Computer

Page 3: Introduction to Ethereum

State

Page 4: Introduction to Ethereum

11/15/201607:30:00 PM

11/15/201607:30:14 PM

11/15/201607:30:28 PM

How it works

14s 14s

Page 5: Introduction to Ethereum

• σ , the world state of Ethereum• t , time • σt+1

, the next world state of Ethereum• T , a transaction• σt+1 ≡ Υ(σt, T)

11/15/201607:30:14 PM

11/15/201607:30:28 PM

14s

Ethereum State Transition Function (Υ)

Page 6: Introduction to Ethereum

State History is represented by the blockchain

Page 7: Introduction to Ethereum
Page 8: Introduction to Ethereum

The Ethereum Virtual Machine

Page 9: Introduction to Ethereum

Ether -> gas

Page 10: Introduction to Ethereum

Ethereum Clients

• Geth (Go-ethereum, implemented in Go)• Parity (implemented in Rust)• Pyethapp (implemented in Python)

Page 11: Introduction to Ethereum

Ethereum Clients

• By installing and running a client you can• Mine real ether• Transfer funds between addresses• Create contracts and send transactions• Explore block history• And more…

Page 12: Introduction to Ethereum

Account types that exist in the World State

• Externally Owned Account• Address (160-bit identifier)• Nonce• Balance

• Contract Account• Address (160-bit identifier)• Nonce• Balance• Storage• Code

Page 13: Introduction to Ethereum

Account addresses

• The last 20 bytes of a public key• Steps resulting in an address

• During Account Creation• A 256-bit Private Key is initialized• Lots of cryptography-related steps in between• A Public Key is hashed with SHA-3 to produce a 256-bit output• The lower 160 bits (20 bytes) become the address

Page 14: Introduction to Ethereum

Transactions and Messages

• Transactions contain• Signature identifying sender• Recipient address• Ether to send• Data field (Optional)• STARTGAS value• GASPRICE value

Page 15: Introduction to Ethereum
Page 16: Introduction to Ethereum

Contract Accounts

Page 17: Introduction to Ethereum

Contract Accounts – Potential uses

• Maintain a data store• Forwarding contract which has access policy and some conditions to send

messages• Ongoing contract such as escrow, crowdfunding• Library type contract which provides functions to other contracts

Page 18: Introduction to Ethereum

Contract Demo

Page 19: Introduction to Ethereum

Benefits of Ethereum

• Ubiquitous• Anyone can upload and request programs to be executed• 100% uptime• DDOS Resistant• Persistent permanent data storage

Page 20: Introduction to Ethereum

Benefits of Ethereum

• Auditable• Interoperability• Built-in user authentication• Built-in direct payment system (no 3rd party required)• Develop and run decentralized applications without relying on

centralized servers

Page 21: Introduction to Ethereum

Solidity

• High-level language with JavaScript-like Syntax

Page 22: Introduction to Ethereum

A Block

• B = (BH + BT + BU)

Page 23: Introduction to Ethereum

JSON

• JSON – a simple (data interchange or serialization) format that uses human readable text for transmitting data

{ "id": 1, "name": "A green door", "price": 12.50, "tags": ["home", "green"]}

Page 24: Introduction to Ethereum

A little more detail

• If you have some object and you want to store it in a file or send it over a network or something of that nature, you have to convert it to a string first, because you can't send objects over a network or write objects to a file. You can only write strings. And the other end has to receive that string and reconstitute it into an object before it can work with it.

Page 25: Introduction to Ethereum

Remote Procedure Call (RPC)

• Fancy word which generally means calling procedures with arguments on remote systems

• Remote Procedure Call (RPC) is a protocol that one program can use to request a service from a program located in another computer on a network without having to understand the network's details.

Page 26: Introduction to Ethereum

JSON RPC

• A stateless, light-weight remote procedure call (RPC) protocol• http://www.jsonrpc.org/specification#conventions

• Defines data structures and rules around their processing

• Example rpc call with positional parameters:• Client → Request object: {"jsonrpc": "2.0", "method": "subtract", "params":

[42, 23], "id": 1} → Server• Server→ Response object: {"jsonrpc": "2.0", "result": 19, "id": 1} → Client

Page 27: Introduction to Ethereum

• Library written to allow developers to focus on developing apps instead of plumbing to interact with Ethereum clients and ecosystem

• Web3.js (JavaScript)• Library implementing the JavaScript API for Dapps to conveniently interact with an

Ethereum node

Page 28: Introduction to Ethereum

Default JSON-RPC endpoints

• Geth: http://localhost:8545

Page 29: Introduction to Ethereum

Compiler

• Solc• A standalone solidity compiler• You only need this if you want to use your Dapp or console to compile solidity

code

Page 30: Introduction to Ethereum

Test Networks

• Ropsten testnet (--testnet)• Local private testnet

Page 31: Introduction to Ethereum

Setting Up a Test Network$ geth --testnet --nodiscover --maxpeers --

Flags• --nodiscover (make sure your node is not discoverable by people who do not

manually add you. )• --maxpeers (the number of peers you want connecting to your private chain. Set

as 0)• --gasprice (0 Makes contracts affordable)• --port (The network listening port. 0 for random)• --datadir (the data directory that your private chain data will be stored in. Should

be different from the public Ethereum chain folder)• Don’t use the deprecated –genesis flag

Page 32: Introduction to Ethereum

Setting Up a Test Network

• $get --dev attach• >personal.newAccount(‘password’)• miner.start()• miner.stop()

Page 33: Introduction to Ethereum

Augur Dapp Demo• https://app.augur.net/• git clone https://github.com/AugurProject/augur.git• cd augur• npm install• npm run build• // build and watch source for changes• npm run watch

• // run local web server• npm start

• http://localhost:8080

Page 34: Introduction to Ethereum

Geth Console

• JavaScript console

• geth attach

• attempts to open the console on a running geth instance

• accepts an endpoint in case the geth node is runnign with a non default interprocess communication (ipc) endpoint or you would like to connect over the remote procedure call (rpc) interface$ geth attach ipc:/some/custom/path

$ geth attach http://191.168.1.1:8545

$ geth attach ws://191.168.1.1:8546

Page 35: Introduction to Ethereum

Starting geth

• By default, doesn’t start the http and websocket service and not all functionality is provided over these interfaces due to security

• defaults can be overridden with geth --rpcapi and --wsapi arguments

Page 36: Introduction to Ethereum

Managing Accounts with Geth

• It is not possible to access your account without a password• No forgot my password option

• If you lose your keyfile, and thus your private key, then you lose access to your account

Page 37: Introduction to Ethereum

What you can do

• Create new accounts• List all existing accounts• Import an private key into a new account• Migrate to the newest key format• Change your password

Page 38: Introduction to Ethereum

Create new accounts

• geth account new• For non-interactive mode supply the --password flag

• geth account --password <passwordfile> new•Create an account that uses an existing private key

•geth --datadir /someOtherDataDrive account import ./key.prv

• Create an account from geth console• personal.newAccount(“password”)

Page 39: Introduction to Ethereum

List all existing accounts

• geth account list

• For geth console

• eth.accounts

Page 40: Introduction to Ethereum

Show primary account address

• From geth console

• eth.coinbase

Page 41: Introduction to Ethereum

Convert a number of Wei into a different unit

Use web3.fromWei(number, unit) converts a number of wei into a different unit

• unit must be a string

• unit can be a kwei/ada, mwei/babbage, gwei/channon, szabo, finney, ether, kether/grand/einstein, mether, gether, tether

Page 42: Introduction to Ethereum

Checking account balance

• Check the balance (in Wei) of an address

• eth.getBalance(“address”)

• For primary account

• web3.fromWei(eth.getBalance(eth.coinbase), “ether”)

• For a specific address

• web3.fromWei(eth.getBalance(“address”), “ether”)

Page 43: Introduction to Ethereum

Print all balances with a JavaScript function

Inside of geth console:

function checkAllBalances() {

var i =0;

eth.accounts.forEach( function(e){

console.log(" eth.accounts["+i+"]: " + e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether") + " ether");

i++;

})

};

Then call checkAllBalances() inside geth console

Page 44: Introduction to Ethereum

Sending Ether

• eth.sendTransaction({from:sender, to:receiver, value:amount})

• you can use built-in JavaScript to set variables to values

• var sender = eth.accounts[0];

• var receiver = eth.accounts[1];

• var amount = web3.toWei(0.01, “ether”)

• the value is in Wei

• you must have your account password to complete the sendTransaction

Page 45: Introduction to Ethereum

Mining

• analogous to mining gold or precious metals

• secures the network and verifies computation

Page 46: Introduction to Ethereum

Proof of Work

• A block is only valid if it contains proof of work of a given difficulty

• the PoW algorithm is called Ethash

Page 47: Introduction to Ethereum

Ethash

• a modified version of Dagger-Hashimoto which involves finding a nonce input ot the algorithm so that the result is below a certain threshold depending on the difficulty

• PoW algorithms rely on the assumption that there’s no better strategy to find such a nonce than enumerating the possibilties

• Verification of a solution is trivial and cheap

Page 48: Introduction to Ethereum

Difficulty

• the average time needed to find a nonce depends on the difficulty threshold

• the difficulty dynamically adjusts so that the network produces a block every 12 seconds

• the synchronization of system state makes it impossible to maintain a fork or rewrite history without controlling more than half of the network mining power

Page 49: Introduction to Ethereum

Miners

• The expected revenue of a miner is directly proportional to the miner’s hashrate (the nonces tried per second normalized by the total hashrate of the network)

Page 50: Introduction to Ethereum

Ethash DAG (Directed Acyclic Graph)

• The algorithm is memory hard, which makes it ASIC resistant

• Calculating the PoW requires choosing subsets of a fixed resource (the DAG) dependent on the block header and nonce

• several gigabytes of data

• Totally different every 30,000 blocks

• 100 hour window called an epoch

• takes a while to generate

• Since the DAG only depends on the block number, it can be pregenerated to avoid long wait times at each epoch transition

Page 51: Introduction to Ethereum

Ethash DAG

• Geth implements automatic DAG generation by default including when you use “geth --mine”

• maintains two DAGs at a time for smooth epoch transitions

• Clients share a DAG resource, so if you are running multiple instances of any client, make sure automatic DAG generation is only enabled on one client

• to pregenerate a DAG for an arbitrary epoch use

• geth makedag <blocknumber> <outputdir>

Page 52: Introduction to Ethereum

Mining with geth

• Set your etherbase (or coinbase)

• Before earning ether, you must have your etherbase (or coinbase) address set

• setting etherbase on the command line

• use the --etherbase option

• geth --etherbase '0xa4d8e9cae4d04b093aac82e6cd355b6b963fb7ff' --mine 2>> geth.log

• setting etherbase in the console

• use miner.setEtherbase

• miner.setEtherbase('0xa073edbcac4a489c3c0f71ec50dd6ffcefa49a00')

• or miner.setEtherbase(eth.accounts[2])

• the account address doesn’t need to be from a local account. It can be any existing address

Page 53: Introduction to Ethereum

Extra Data in Block

• As the one who mined the block , you can add a short vanity tag

• Can only be 32 bytes long

• miner.setExtra(“Arnold was here”)

• Interpreted as unicode

Page 54: Introduction to Ethereum

Start Mining

• from the command line

• use --mine option

• geth --mine

• from the console

• miner.start()

• miner.stop() to stop

• Check your hashrate

• miner.hashrate

Page 55: Introduction to Ethereum

Mining information anomaly

• Often you’ll find a block that never makes it to the canonical chain

• Locally it may show that your mined block, and the mining reward was credited to your account, however, after a while the better chain is discovered and the network switches to a chain in which your block is not included and therefore no mining reward is credited

• A miner monitoring their coinbase balance will find that it fluctuates quite a bit for this reason

Page 56: Introduction to Ethereum

Spending your gas

• In order to spend your gas to transact you need to unlock the account

• personal.unlockAccount(eth.coinbase)

Page 57: Introduction to Ethereum

Checking a Block Header

• eth.getBlock(blocknumber)

Page 58: Introduction to Ethereum

Software Development Principles

Page 59: Introduction to Ethereum

The hardest single part of building a software system is deciding exactly what to build

Page 60: Introduction to Ethereum

Behavioral requirements to consider

• Security

• Safety

• Performance

• Timing

• Fault-tolerance

(Bass et al. 2003)

Page 61: Introduction to Ethereum

Developmental Quality Attributes

• Testability

• Changeability (Major problem in Dapp development)

• Maintainability (Major problem in Dapp development)

• Reusability

(Bass et al. 2003)

Page 62: Introduction to Ethereum

Develop with multi-versions in mind

• even in cases in which only one system intended, systems inevitably become multi-version as developers better understand

• what they can do for users

• what they can and cannot do within the constraints of cost and schedule

Page 63: Introduction to Ethereum

Repair and maintainence problems are costly

• immortal contracts

• cannot change code

• storage expensive