concept of blockchain & decentralized application

63
Concept of BlockChain & Decentralized Application timakin / @__timakin__ YAP(achimon)C::Asia Hachioji 2016 mid in Shinagawa

Upload: seiji-takahashi

Post on 21-Apr-2017

2.558 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: Concept of BlockChain & Decentralized Application

Concept of BlockChain

& Decentralized

Application

timakin / @__timakin__

YAP(achimon)C::Asia Hachioji 2016 mid in Shinagawa

Page 2: Concept of BlockChain & Decentralized Application
Page 3: Concept of BlockChain & Decentralized Application

• github: timakin

• twitter: @__timakin__

• DeNA -> Translimit

• Go / Ruby / Node

• Blogmedium: https://medium.com/@timakin timekin.log: http://tech-savvy.hatenablog.com/

Page 4: Concept of BlockChain & Decentralized Application
Page 5: Concept of BlockChain & Decentralized Application

Page 6: Concept of BlockChain & Decentralized Application
Page 7: Concept of BlockChain & Decentralized Application

Page 8: Concept of BlockChain & Decentralized Application
Page 9: Concept of BlockChain & Decentralized Application

P2P

Page 10: Concept of BlockChain & Decentralized Application
Page 11: Concept of BlockChain & Decentralized Application

P2P

Page 12: Concept of BlockChain & Decentralized Application

Chain Flow

hash

target nonce

transactions transactions transactions

blockblock block

Page 13: Concept of BlockChain & Decentralized Application

Inside of blockchain

• JSON

JSON

nonce

• parent hash

hash

Page 14: Concept of BlockChain & Decentralized Application

Example of block

{ difficulty: '137447', extraData: '0x476574682f76312e302e312f6c696e75782f676f312e342e32', gasLimit: 3141592, gasUsed: 0, hash: '0x4d3063b91cbaa12bf2de81014c1319febc9f197c93f81b0746afaffaa9496620', nonce: '0x28fda83cb19ed497', number: 100, parentHash: '0x5885cdec1d1410580eaaf1fb7ef9db245a735822d48e816c73d926b7c9872f15', size: 536, timestamp: 1439451765, totalDifficulty: '13551548', transactions: [ ], transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',}

Page 15: Concept of BlockChain & Decentralized Application

Chain management• P2P

Node json

• Node

Page 16: Concept of BlockChain & Decentralized Application

A

B

C

fork

Page 17: Concept of BlockChain & Decentralized Application

Consensus Algorithm• Proof of Work

• Node 0

Node

• target nonce(hash value)

• Node

• Proof of Stake

• ex) SHA256(prevhash + address + timestamp) <= 2^256 * balance (volume of stake) / diff

Proof of Stake Velocity

Page 18: Concept of BlockChain & Decentralized Application
Page 19: Concept of BlockChain & Decentralized Application

• PoW

10

• PoS

• Node

Page 20: Concept of BlockChain & Decentralized Application
Page 21: Concept of BlockChain & Decentralized Application
Page 22: Concept of BlockChain & Decentralized Application
Page 23: Concept of BlockChain & Decentralized Application

DApp Stack

Page 24: Concept of BlockChain & Decentralized Application

Decentralized ApplicationP2P Node

Permanent web

Page 25: Concept of BlockChain & Decentralized Application

Decentralized Application

Processing

File Storage Database

Page 26: Concept of BlockChain & Decentralized Application

Decentralized Application

Processing

File Storage Database

Processing

File Storage Database

Page 27: Concept of BlockChain & Decentralized Application

Euthareum

ether

Page 28: Concept of BlockChain & Decentralized Application

Euthareum Client

• Geth (go-euthareum)

Node

Page 29: Concept of BlockChain & Decentralized Application

Install

https://github.com/ethereum/go-ethereum

OS

# install euthareum$ brew tap ethereum/ethereum$ brew install ethereum

# install go, gmp$ brew install gmp go

# install geth$ git clone https://github.com/ethereum/go-ethereum$ cd go-ethereum$ make geth

Page 30: Concept of BlockChain & Decentralized Application

Build private network

$ mkdir /home/test_u/eth_private_net{ "nonce": "0x0000000000000042", "timestamp": "0x0", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x0", "gasLimit": "0x8000000", "difficulty": "0x4000", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x3333333333333333333333333333333333333333", "alloc": {}}

Genesis

Page 31: Concept of BlockChain & Decentralized Application

Connect to private network

$ geth --networkid "10" --nodiscover --datadir "/home/test_u/eth_private_net" --genesis "/home/test_u/eth_private_net/myGenesis.json" console 2>> /home/test_u/eth_private_net/geth_err.log

# private 10 id# peer# console

Page 32: Concept of BlockChain & Decentralized Application

Contract Code

# Solidity$ sudo add-apt-repository ppa:ethereum/ethereum$ sudo apt-get update$ sudo apt-get install solc$ brew install cpp-ethereum$ brew linkapps cpp-ethereum$ solc —-version$ which solc

# solc geth$ admin.setSolc(“which solc path")$ eth.getCompilers()

• • Ethereum Virtual Machine

Euthareum

• Solidity

• IDE: https://github.com/ethereum/browser-solidity

Page 33: Concept of BlockChain & Decentralized Application

Contract Code

# Solidity

contract SingleNumRegister { uint storedData; function set(uint x) { storedData = x; } function get() constant returns (uint retVal) {

return storedData; }}

Solidity JavaScript

Page 34: Concept of BlockChain & Decentralized Application

Contract Code

# $ var source = "contract SingleNumRegister { uint storedData; function set(uint x) { storedData = x; } function get() constant returns (uint retVal) { return storedData; }}"$ var sourceCompiled = eth.compile.solidity(source)

# $ var contractAbiDefinition = sourceCompiled.SingleNumRegister.info.abiDefinition$ var sourceCompiledContract = eth.contract(contractAbiDefinition)$ var contract = sourceCompiledContract.new({from:eth.accounts[0], data: sourceCompiled.SingleNumRegister.code})

Node

Page 35: Concept of BlockChain & Decentralized Application

Contract Code$ contract{ address: '0x8ea277dfe4195daf7b8c101d79da35d1eb4c4aeb', transactionHash: '0xeb76caefdfe5a9aa10b11743d317cf15f881d3b2e52ba3251dcf8e0718ed5b33', allEvents: function (), get: function (), set: function ()}

# $ contractAbiDefinition

Page 36: Concept of BlockChain & Decentralized Application

Contract Code# $ var cnt = eth.contract([{ constant: false, inputs: [{ name: 'x', type: 'uint256' } ], name: 'set', outputs: [ ], type: 'function' }, { constant: true, inputs: [ ], name: 'get', outputs: [{ name: 'retVal', type: 'uint256' } ], type: 'function' } ]).at(‘0x8ea277dfe4195daf7b8c101d79da35d1eb4c4aeb');

# $ cnt.set.sendTransaction(3,{from:eth.accounts[0]})‘0x979c4e413a647673632d74a6c8b7f5b25a3260f3fefa4abea2dc265d61215939'

# $ cnt.get()

Page 37: Concept of BlockChain & Decentralized Application

Run the app on EVM

# RPC geth$ geth --networkid "10" --nodiscover --datadir "/home/test_u/eth_private_net" --genesis "/home/test_u/eth_private_net/myGenesis.json" --mine --unlock 0xa7653f153f9ead98dc3be08abfc5314f596f97c6 --rpc --rpcaddr "192.168.5.6" --rpcport "8545" --rpccorsdomain "*" console 2>> /home/test_u/eth_private_net/geth_err.log

# meteor project$ cd ~/eth-test # $ meteor create simple-app # Meteor$ meteor add twbs:bootstrap $ meteor add ethereum:web3$ meteor add ethereum:accounts$ meteor add ethereum:blocks

EVM

Page 38: Concept of BlockChain & Decentralized Application

Run the app on EVM# geth$ vim client/lib/init.js

```//Web3web3 = new Web3();

//RPCif(!web3.currentProvider) web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));

// EthAccountsEthAccounts.init();

//EthBlocksEthBlocks.init();```

Page 39: Concept of BlockChain & Decentralized Application

Run the app on EVM$ vim client/main.html

```<head> <title>Simple Ether Wallet</title></head>

<body><template name="nodeStatusComponent"> <div class="panel panel-default"> <div class="panel-heading"> <h4>Node Status</h4> </div> <table class="table"> <tbody> <tr> <th scope="row">Node</th> <td>{{currentProvider}}</td> </tr> <tr> <th scope="row">Is Mining?</th> <td>{{isMining}}</td> </tr> <tr> <th scope="row">Hashrate</th> <td>{{currentHashrate}}</td> </tr> <tr> <th scope="row">Peer Count</th> <td>{{currentPeerCount}}</td> </tr> </tbody> </table> </div></template>```

Page 40: Concept of BlockChain & Decentralized Application

Run the app on EVM# $ vim client/main.html

```<head> <title>Simple Ether Wallet</title></head>

<body><template name="nodeStatusComponent"> <div class="panel panel-default"> <div class="panel-heading"> <h4>Node Status</h4> </div> <table class="table"> <tbody> <tr> <th scope="row">Node</th> <td>{{currentProvider}}</td> </tr> <tr> <th scope="row">Is Mining?</th> <td>{{isMining}}</td> </tr> <tr> <th scope="row">Hashrate</th> <td>{{currentHashrate}}</td> </tr> <tr> <th scope="row">Peer Count</th> <td>{{currentPeerCount}}</td> </tr> </tbody> </table> </div></template>```

Page 41: Concept of BlockChain & Decentralized Application

Run the app on EVM# $ vim client/main.js

```// nodeStatusComponentTemplate.nodeStatusComponent.helpers({

// currentProvider: function(){ return web3.currentProvider.host; },

// // true false isMining: function(){ return web3.eth.mining; },

// currentHashrate: function(){ return web3.eth.hashrate; },

// currentPeerCount: function(){ return web3.net.peerCount; }});```

meteor

Page 42: Concept of BlockChain & Decentralized Application

Euthareum•

• Euthareum

Page 43: Concept of BlockChain & Decentralized Application

BigChainDB

• key-value

JSON

• python

Page 44: Concept of BlockChain & Decentralized Application

BigChainDB

RethinkDB

BigChainDB

https://speakerdeck.com/vrde/bigchaindb-how-we-built-a-blockchain-database-

on-top-of-rethinkdb

Page 45: Concept of BlockChain & Decentralized Application

Install, Configuration# rethinkdb http://rethinkdb.com/docs/install/

# bigchainDB$ sudo pip install bigchaindb$ vim instance1.conf``` server-tag=originaldirectory=/databind=alldirect-io# Replace node?_hostname with actual node hostnames below, e.g. rdb.examples.comjoin=node0_hostname:29015join=node1_hostname:29015join=node2_hostname:29015# continue until there's a join= line for each node in the federation ```

Page 46: Concept of BlockChain & Decentralized Application

Run the BigChainDB server# rethinkdb bigchaindb server$ rethinkdb --config-file path/to/instance1.conf$ bigchaindb init$ bigchaindb set-shards 1$ bigchaindb set-replicas 1$ bigchaindb start

Page 47: Concept of BlockChain & Decentralized Application

Create a Digital Assetfrom bigchaindb import crypto

# testuser1_priv, testuser1_pub = crypto.generate_key_pair()

# digital_asset_payload = {'msg': 'Hello BigchainDB!'}

# tx = b.create_transaction(b.me, testuser1_pub, None, 'CREATE', payload=digital_asset_payload)

# tx_signed = b.sign_transaction(tx, b.me_private)

# b.write_transaction(tx_signed)

# tx_retrieved = b.get_transaction(tx_signed['id'])tx_retrieved

Page 48: Concept of BlockChain & Decentralized Application

BigChainDB

• key-value RethinkDB

Page 49: Concept of BlockChain & Decentralized Application

IPFS

• gateway REST API

curl

• P2P

Page 50: Concept of BlockChain & Decentralized Application

Installhttps://ipfs.io/docs/install/

go

# $ ipfs init$ ipfs daemon# peer$ ipfs swarm peers

# ipfs image$ ipfs cat /ipfs/QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/cat.jpg >cat.jpg$ open cat.jpg

# localhost webui$ open http://localhost:5001/webui

Page 51: Concept of BlockChain & Decentralized Application

WEBUI

Page 52: Concept of BlockChain & Decentralized Application

Upload assets# $ ipfs add test.jpgadded QmaC9pUA3grWJ948u1VWRLG1wPLP8YZe7b3HopBGk4zZyA test.jpg

$ ipfs cat /ipfs/QmaC9pUA3grWJ948u1VWRLG1wPLP8YZe7b3HopBGk4zZyA > butaman.jpg$ open https://ipfs.io/ipfs/QmaC9pUA3grWJ948u1VWRLG1wPLP8YZe7b3HopBGk4zZyA

# $ ipfs add -r ~/myproject

# Fuse$ ipfs mount$ ls /ipfs/$hash/

# $ ipfs add -q test.mp4

Page 53: Concept of BlockChain & Decentralized Application

API Client

var ipfs = require('ipfs-client'); var stream = ipfs.cat('QmTE9Xp76E67vkYeygbKJrsVj8W2LLcyUifuMHMEkyRfUL');stream.pipe(process.stdout);

ipfs.add(process.stdin, function(err, hash) { console.log(hash);});

https://www.npmjs.com/package/ipfs-client

API

Scala Go JavaScript

Page 54: Concept of BlockChain & Decentralized Application

ex) gx-gohttps://github.com/whyrusleeping/gx-go

ipfs version

go package manager

Page 55: Concept of BlockChain & Decentralized Application

ex) ipfs-picshttps://ipfs.pics/

ipfs

script

Page 56: Concept of BlockChain & Decentralized Application

IPFS•

• gateway REST API curl

• gateway

(https://ipfs.io/ipfs/$hash)

Page 57: Concept of BlockChain & Decentralized Application

DApp•

• DB Storage

CDN

BigChainDB

Peer Node IPFS

Peer Node

Processing

File Storage Databasetr tr tr

bb b

bb b bb b

Page 58: Concept of BlockChain & Decentralized Application
Page 59: Concept of BlockChain & Decentralized Application

• P2P

CDN

• Node

Client-Server

• or <

Page 60: Concept of BlockChain & Decentralized Application

http://www.meti.go.jp/press/2016/04/20160428003/20160428003-2.pdf

http://www.slideshare.net/ks91020/ss-58535780

• 5

http://www.slideshare.net/cookle/5-58379474

Page 61: Concept of BlockChain & Decentralized Application

• Ethereum-WhitePaper-JP

https://github.com/kurihei/Ethereum-WhitePaper-JP/blob/master/%5BJapanese%5D-White-Paper.md

• Ethereum Specification

https://github.com/ethereum/go-ethereum/wiki/Ethereum-Specification

• Gitbook Ethereum

https://www.gitbook.com/book/a-mitani/mastering-ethereum/details

• BigchainDB: how we built a blockchain database on top of RethinkDB

https://speakerdeck.com/vrde/bigchaindb-how-we-built-a-blockchain-database-on-top-of-rethinkdb

• White Paper: BigchainDB: A Scalable Blockchain Database(DRAFT)

https://www.bigchaindb.com/whitepaper/bigchaindb-whitepaper.pdf

• White Paper: IPFS - Content Addressed, Versioned, P2P File System (DRAFT 3)

https://ipfs.io/ipfs/QmR7GSQM93Cx5eAg6a6yRzNde1FQv7uL6X1o4k7zrJa3LX/ipfs.draft3.pdf

Page 62: Concept of BlockChain & Decentralized Application

• OSS

Go Go

• Docs White Paper

pdf

• Euthareum

Github

Github

Page 63: Concept of BlockChain & Decentralized Application