Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
スマートコントラクトを理解する 〜解説と実演〜 / Understanding Smart C...
Search
Kenji Saito
PRO
June 07, 2017
Technology
0
830
スマートコントラクトを理解する 〜解説と実演〜 / Understanding Smart Contracts
2017年6月7日(水)、Interop Tokyo 2017 の同名のセッションにて私用したスライドです。
Kenji Saito
PRO
June 07, 2017
Tweet
Share
More Decks by Kenji Saito
See All by Kenji Saito
P 値と有意差/分散分析 / P-value, Significant Difference and Analysis of Variance
ks91
PRO
0
17
関連2群のt検定/独立2群のt検定 / Related 2-group t-test and independent 2-group t-test
ks91
PRO
0
47
A Guide to Paper Writing Support with Generative AI - A Joint Zemi
ks91
PRO
0
9
正規分布と簡単な統計理論/t分布と信頼区間 / Normal distribution, simple statistical theory, t-distribution and confidence intervals
ks91
PRO
0
42
じわじわ迫ってきている自動化社会 (その先にメタ・ネイチャー) / The Slowly Approaching Automated Society (and its beyond: Meta-Nature)
ks91
PRO
0
6
起こりうる誤った推論/平均・分散・標準偏差・自由度 / Possible false inferences, means, variances, standard deviations and degrees of freedom
ks91
PRO
0
57
LaTeX と Overleaf によるショートペーパー作成 / Short paper writing with LaTeX and Overleaf
ks91
PRO
0
23
R を用いた検定(補講) (1) — Welch 検定 / Tests using R (supplementary) (1) - Welch test
ks91
PRO
0
12
R を用いた検定(補講) (2) — カイ二乗検定 / Tests using R (supplementary) (2) - Chi-squared test
ks91
PRO
0
13
Other Decks in Technology
See All in Technology
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
190
20241125 - AI 繪圖實戰魔法工作坊 @ 實踐大學
dpys
1
330
10年もののバグを退治した話
n_seki
0
110
AIエージェントに脈アリかどうかを分析させてみた
sonoda_mj
2
110
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
3
670
2024年にチャレンジしたことを振り返るぞ
mitchan
0
160
Fearsome File Formats
ange
0
430
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
28
24k
最近のSfM手法まとめ - COLMAP / GLOMAPを中心に -
kwchrk
8
1.5k
20240522 - 躍遷創作理念 @ PicCollage Workshop
dpys
0
210
いまからでも遅くないコンテナ座学
nomu
0
160
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
1
360
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Six Lessons from altMBA
skipperchong
27
3.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Thoughts on Productivity
jonyablonski
68
4.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Transcript
∼ ( 2 ) ∼ SFC
[email protected]
— — 2017-06-07
– p.1/70
( ) SFC ( ) CSO (Chief Science Officer) 1993
M.Eng ( ) 2006 ( ) SFC 16 P2P 2011 → ( ) — — 2017-06-07 – p.2/70
1. 2. 3. 4. 5. : 6. — — 2017-06-07
– p.3/70
1. — — 2017-06-07 – p.4/70
Vitalik Buterin, “Ethereum White Paper: A NEXT GENERATION SMART CONTRACT
& DECENTRALIZED APPLICATION PLATFORM” 15 — — 2017-06-07 – p.5/70
— — 2017-06-07 – p.6/70
. . . — — 2017-06-07 – p.7/70
vs. — — 2017-06-07 – p.8/70
= run — — 2017-06-07 – p.9/70
Ether Ethereum EOA : Externally-Owned Account Ether EVM EVM —
— 2017-06-07 – p.10/70
EVM : Ethereum Virtual Machine Gas — — 2017-06-07 –
p.11/70
EVM : : Solidity — JavaScript LLL — Lisp —
— 2017-06-07 – p.12/70
Solidity contract metaCoin { mapping (address => uint) balances; function
metaCoin() { balances[msg.sender] = 10000; } function sendCoin(address receiver, uint amount) returns(bool sufficient) { if (balances[msg.sender] < amount) return false; balances[msg.sender] -= amount; balances[receiver] += amount; return true; } } by hshimo — — 2017-06-07 – p.13/70
— — 2017-06-07 – p.14/70
2. — — 2017-06-07 – p.15/70
ADEPT ADEPT : IBM IoT — — 2017-06-07 – p.16/70
— — 2017-06-07 – p.17/70
⇒ — — 2017-06-07 – p.18/70
The DAO ( ) The DAO Split ( ) 360
ETH (50∼60 ) (2016/6/17) ( ) ( ; ) (2016/7/20 ) — — 2017-06-07 – p.19/70
3. — — 2017-06-07 – p.20/70
3-1. Hyperledger Fabric (IBM) IBM Digital Asset Holdings http://hyperledger-fabric.readthedocs.io/en/latest/ —
— 2017-06-07 – p.21/70
(Docker) (chaincode) PBFT (Practical BFT) RocksDB CA PKI v1.0 DB
CA — — 2017-06-07 – p.22/70
3-2. Hyperledger Iroha ( ) https://github.com/hyperledger/iroha — — 2017-06-07 –
p.23/70
JVM (chaincode) Sumeragi (BFT) — — 2017-06-07 – p.24/70
3-3. Corda (R3) R3 https://github.com/corda/corda — — 2017-06-07 – p.25/70
JVM ( ) (Uniqueness Services) UTXO X.509 PKI ( )
Corda — — 2017-06-07 – p.26/70
4. Mac OS X (Yosemite 10.10.5) + Homebrew Linux (Ubuntu
16.04) Windows 10 + Bash on Ubuntu on Windows geth, solidity populus — — 2017-06-07 – p.27/70
geth GO https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum #installation-instructions geth 1.6.1-stable (5/16 ) $ geth
-help — — 2017-06-07 – p.28/70
Solidity http://solidity.readthedocs.io/en/latest/installing-solidity.html solc 0.4.11 (5/16 ) $ solc --version —
— 2017-06-07 – p.29/70
Python pip macOS http://qiita.com/oct_itmt/items/2d066801a7464a676994 (pyenv ) Ubuntu 16.04 — —
2017-06-07 – p.30/70
Populus Python http://populus.readthedocs.io/en/latest/quickstart.html Open SSL Populus $ pip install populus
version 1.6.9 (5/16 ) — — 2017-06-07 – p.31/70
$ populus init contracts/Greeter.sol tests/test_greeter.py $ populus compile $ py.test
tests/ — — 2017-06-07 – p.32/70
5. ( ) — — 2017-06-07 – p.33/70
— — 2017-06-07 – p.34/70
( ) / / — — 2017-06-07 – p.35/70
BcH-smart-contract-programming.zip contracts tests — — 2017-06-07 – p.36/70
Solidity JavaScript ( , ) (constructor) ( ) ( )
Ether — — 2017-06-07 – p.37/70
pragma solidity ˆ0.4.8; contract MyToken { ( ) : (EVM
) : function MyToken(...) { /* */ : } : } function C (/* */ // ) — — 2017-06-07 – p.38/70
MyToken string public name; string public symbol; uint8 public decimals;
mapping (address => uint256) public balanceOf; name, symbol decimals : 2 100 1.00 mapping balanceOf — — 2017-06-07 – p.39/70
MyToken event Transfer(address indexed from, address indexed to, uint256 value);
indexed (3 ) MyToken function Transfer() Ethereum-Wallet — — 2017-06-07 – p.40/70
MyToken function MyToken(uint256 _supply, string _name, string _symbol, uint8 _decimals)
{ if (_supply == 0) { _supply = 1000000; /* _supply 1,000,000 */ } balanceOf[msg.sender] = _supply; name = _name; symbol = _symbol; decimals = _decimals; } msg.sender _supply C++ _ — — 2017-06-07 – p.41/70
MyToken getBalanceOf() function getBalanceOf(address _addr) returns (uint256 balance) { return
(balanceOf[_addr]); } — — 2017-06-07 – p.42/70
MyToken transfer() function transfer(address _to, uint256 _value) { if (balanceOf[msg.sender]
< _value) { /* */ throw; } if (balanceOf[_to] + _value < balanceOf[_to]) { /* */ throw; } balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; Transfer(msg.sender, _to, _value); } throw (function ) catch — — 2017-06-07 – p.43/70
(1) import pytest @pytest.fixture() def token_contract(chain): TokenFactory = chain.get_contract_factory(’MyToken’) deploy_txid
= TokenFactory.deploy(args=[ 0, "BcH Coin", "BcH", 0, ]) contract_address = chain.wait.for_contract_address(deploy_txid) return TokenFactory(address=contract_address) populus — — 2017-06-07 – p.44/70
(2) def test_my_token(token_contract, chain): account0 = chain.web3.eth.accounts[0] account1 = chain.web3.eth.accounts[1]
assert token_contract.call().getBalanceOf(account0) == 1000000 assert token_contract.call().getBalanceOf(account1) == 0 txid = token_contract.transact().transfer(account1, 10) chain.wait.for_receipt(txid) assert token_contract.call().getBalanceOf(account0) == 999990 assert token_contract.call().getBalanceOf(account1) == 10 account0 coinbase account1 account0 account1 10BcH — — 2017-06-07 – p.45/70
$ py.test tests/test_my_token.py — — 2017-06-07 – p.46/70
— — 2017-06-07 – p.47/70
(m2 ) 1 1 — — 2017-06-07 – p.48/70
IndivisibleAsset string public _name; string public _symbol; uint256 public _quantity;
address public _owner; _name ( ) _symbol _quantity (m2 ) _owner _ Solidity . . . — — 2017-06-07 – p.49/70
IndivisibleAsset event Transfer(address indexed from, address indexed to); from to
— — 2017-06-07 – p.50/70
IndivisibleAsset function IndivisibleAsset(string name, string symbol, uint256 quantity) { _name
= name; _symbol = symbol; _quantity = quantity; _owner = msg.sender; } — — 2017-06-07 – p.51/70
IndivisibleAsset getOwner() function getOwner() returns (address owner) { return (_owner);
} — — 2017-06-07 – p.52/70
IndivisibleAsset transfer() function transfer(address to) { if (_owner != msg.sender)
{ throw; } _owner = to; Transfer(msg.sender, to); } — — 2017-06-07 – p.53/70
(1) import pytest @pytest.fixture() def asset_contract(chain): AssetFactory = chain.get_contract_factory(’IndivisibleAsset’) deploy_txid
= AssetFactory.deploy(args=[ "5322 Endo, Fujisawa", "mˆ2", 300, ]) contract_address = chain.wait.for_contract_address(deploy_txid) return AssetFactory(address=contract_address) SFC (300m2 ) — — 2017-06-07 – p.54/70
(2) def test_indivisible_assset(asset_contract, chain): account0 = chain.web3.eth.accounts[0] account1 = chain.web3.eth.accounts[1]
assert asset_contract.call().getOwner() == account0 txid = asset_contract.transact().transfer(account1) chain.wait.for_receipt(txid) assert asset_contract.call().getOwner() == account1 account0 account1 — — 2017-06-07 – p.55/70
$ py.test tests/test_indivisible_asset.py — — 2017-06-07 – p.56/70
— — 2017-06-07 – p.57/70
transfer settle, retrieve asset, retrieve token 3 — — 2017-06-07
– p.58/70
1. ( ) 2. ( ) 3. ( ) —
— 2017-06-07 – p.59/70
OneTimeEscrow settle() function settle() { address addr = this; /*
this */ if (_token.getBalanceOf(this) < _price || _asset.getOwner() != addr) { throw; } _token.transfer(_seller , _price); _asset.transfer(_buyer); Settled(); /* */ } settle() transfer — — 2017-06-07 – p.60/70
(1) import pytest @pytest.fixture() def token_contract(chain): . . . @pytest.fixture()
def asset_contract(chain): . . . — — 2017-06-07 – p.61/70
(2) def test_one_time_escrow(token_contract, asset_contract, chain): account0 = chain.web3.eth.accounts[0] account1 =
chain.web3.eth.accounts[1] txid = chain.web3.eth.sendTransaction({ ’from’: account0, ’to’: account1, ’value’: chain.web3.toWei(1, "ether") }) chain.wait.for_receipt(txid) txid = token_contract.transact().transfer(account1, 300) chain.wait.for_receipt(txid) account0 account1 1Ether 300BcH 300BcH TX — — 2017-06-07 – p.62/70
(3) EscrowFactory = chain.get_contract_factory(’OneTimeEscrow’) txid = EscrowFactory.deploy(args=[ token_contract.address, account1, asset_contract.address,
account0, 300, ]) contract_address = chain.wait.for_contract_address(txid) EscrowFactory.address = contract_address; account1 account0 300BcH — — 2017-06-07 – p.63/70
(4) txid = token_contract.transact({ ’from’: account1 }).transfer(contract_address, 300) chain.wait.for_receipt(txid) txid
= asset_contract.transact().transfer(contract_address) chain.wait.for_receipt(txid) assert token_contract.call().getBalanceOf(account0) == 999700 assert token_contract.call().getBalanceOf(account1) == 0 assert token_contract.call().getBalanceOf(contract_address) == 300 assert asset_contract.call().getOwner() == contract_address account1 300BcH account0 — — 2017-06-07 – p.64/70
(5) txid = EscrowFactory.transact().settle(); chain.wait.for_receipt(txid) assert token_contract.call().getBalanceOf(account0) == 1000000 assert
token_contract.call().getBalanceOf(account1) == 0 assert token_contract.call().getBalanceOf(contract_address) == 0 assert asset_contract.call().getOwner() == account1 settle() — — 2017-06-07 – p.65/70
$ py.test tests/test_one_time_escrow.py : settle() — — 2017-06-07 – p.66/70
6. — — 2017-06-07 – p.67/70
( ) (by ) (since 1984) ( ) ( )
. . . ( ) — — 2017-06-07 – p.68/70
( ) ( ) ( ) ( ) (by )
— — 2017-06-07 – p.69/70
— — 2017-06-07 – p.70/70