Slide 1

Slide 1 text

BlockChain on Go @__timakin__ / DevFest Tokyo 2017

Slide 2

Slide 2 text

ࣗݾ঺հ • ߴڮ ੣ೋ • Github: timakin / Twitter: @__timakin__ • Gunosy Inc. ৽نࣄۀ։ൃࣨ ΤϯδχΞ • Go / Swift • community • golang.tokyo • roppongi-golunch • blockchain.tokyo

Slide 3

Slide 3 text

golangtokyo.connpass.com ࠓ͙͢ϝϯόʔʹʂ

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

ࠓ೔࿩͢͜ͱ • Blockchainͷ֓ཁ • GoͱBlockchainʹ͍ͭͯ

Slide 6

Slide 6 text

Blockchainͷ֓ཁ • ෳ਺୆ͷίϯϐϡʔλʔʹσʔλΛ؅ཧ͢Δ୆ாΛ ࣋ͨͤɺωοτϫʔΫӽ͠ʹ֤ίϯϐϡʔλʔ্ͷ ୆ாΛಉظͤ͞Δ • ୆ாʁ -> લͷཁૉ΁ͷϙΠϯλΛ࣋ͬͨJSONͷ ू·Γ • P2PωοτϫʔΫ্ͷϊʔυ͕ݸʑʹͨͩ̍ͭͷ ϋογϡνΣʔϯΛ࣋ͭ

Slide 7

Slide 7 text

Chain Flow લͷऔҾ৘ใͷhashΛ࣋ͪͭͭɺཤྺ֬ఆʹ࢖ΘΕΔ targetʢܭࢉ໨ඪ஋ʣ΍ɺͦͷ݁Ռͷnonceɺެ։伴౳ ͕ೖͬͨϒϩοΫ͕࠯ঢ়ʹ࿈ଓͯ͠ܗ੒͞Ε͍ͯ·͢ɻ IBTI IBTI QVCLFZ TJHO IBTI OPODF transactions transactions transactions block block block UBSHFU QVCLFZ TJHO OPODF UBSHFU QVCLFZ TJHO OPODF UBSHFU

Slide 8

Slide 8 text

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', }

Slide 9

Slide 9 text

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ʣ

Slide 10

Slide 10 text

Decentralized Application Processing File Storage Database Processing File Storage Database

Slide 11

Slide 11 text

ϝϦοτσϝϦοτ • ϝϦοτ • վ᜵ࠔ೉ੑ • ଱ো֐ੑ • σϝϦοτ • ཤྺ֬ఆ·Ͱͷ࣌ؒ • ϒϩοΫνΣʔϯͷछผʹΑͬͯ͸਺ඦmsʹ཈੍ • εέʔϧΞ΢τ

Slide 12

Slide 12 text

Α͋͘Δޡղ • ෼ࢄ؅ཧ͞Εͨສೳͷσʔλ؅ཧํ๏Ͱ͸ͳ͍ • 2017/10/1࣌఺Ͱ͓Αͦ135GBͷϋογϡνΣʔϯ Λݸผͷϊʔυ͕ίϐʔ͋͠͏۪௚ͳෳ੡ • εέʔϧΞ΢τͷ໰୊͕ੜ͡Δ

Slide 13

Slide 13 text

ϒϩοΫνΣʔϯͷछผ • ύϒϦοΫ • Bitcoin΍EthereumͳͲͷɺϊʔυ਺ʹݶఆ͕ͳ͘ ෆಛఆଟ਺͕߹ҙܗ੒ʹࢀՃͰ͖Δ • ϓϥΠϕʔτɺίϯιʔγΞϜ • ͋Β͔͡Ί߹ҙܗ੒ʹࢀՃͰ͖Δϊʔυ͕ݶఆ͞Ε ͨΓɺνΣʔϯ΁ͷRead/WriteΛڐՄ੍ʹ͢ΔͳͲ ͷ౷੍ԼͰνΣʔϯ͕ܗ੒͞ΕΔ

Slide 14

Slide 14 text

ϓϥΠϕʔτνΣʔϯ • ύϒϦοΫͳϒϩοΫνΣʔϯΑΓɺཤྺ֬ఆͷ߹ҙ ܗ੒ʹ͕͔͔࣌ؒΒͳ͍ʢ਺ඦmsఔ౓ʣ • σʔλෆ੔߹΍εέʔϧΞοϓͷίϯτϩʔϧ͕͖ͭ ΍͍͢ • όοΫΤϯυΛϒϩοΫνΣʔϯʹ͚ͨͩ͠Ͱࣄ্࣮ ͷதԝूݖͱ΋ݴ͑ΔͷͰɺࢥ૝తʹ͸ٙ೦͕࢒Δ

Slide 15

Slide 15 text

ؾܰʹࢼ͠ɺࢹ֮తʹཧղ͢Δʹ͸ http://blockchaindemo.io/ ͕ศར

Slide 16

Slide 16 text

GoͱBlockchain • ϒϩοΫνΣʔϯؔ࿈ͷϓϩδΣΫτͰ͸ɺGoɾ C++ɾPython͋ͨΓ͕Α͘࢖ΘΕΔɻ • ࠓճ͸ɺେن໛ͳϒϩοΫνΣʔϯؔ࿈ͷϦϙδτϦ ͷதͰ΋ɺGo͕࢖ΘΕͯΔ΋ͷͷ֓ཁΛ঺հ͠·͢ɻ

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

ͦ΋ͦ΋ͳΜͰGo? • ΠϯλʔϑΣʔεʹΑΓɺந৅తͳఆٛͱ࣮૷Λ੾Γ෼͚ Δͷ͕༻ҙ • ఆٛͷํΛ௥͍͚ͬͯ͹ॳݟͰ΋શମ૾͕௫Έ΍͍͢ • ίϯτϥΫτίʔυͷ࣮૷Λنఆ͠΍͍͢ • ଎౓໘ɺΫϩείϯύΠϧͳͲͷ໘΋͋Δͱࢥ͏͕ɺͲͪ Β͔ͱ͍͏ͱந৅ԽʹΑΔཧղଅਐ΍γϯλοΫεͷγϯ ϓϧ͞ͷํ͕େ͖͍ͱࢥ͏

Slide 19

Slide 19 text

btcd • Bitcoinຊମͷ୅ସ࣮૷ɻҰछͷfork • bitcoin/bitcoin͸C++࣮૷ • ଞʹ΋libbitcoin(C++), BitcoinJ(Java)ͳͲ • ΫϩείϯύΠϧՄೳͰҠ২ੑ͕ߴ͘ɺϝϞϦ؅ཧ΍ ฒߦॲཧͳͲͷγϯϓϧ͞ɺඋ͑෇͚ͷϓϩϑΝΠϦ ϯάɾϑΥʔϚοτ͕͋Δͱ͍͏఺Ͱརศੑ͕ߴ͍ɻ • JSON-RPCͷAPI͕ىಈͯ͠ɺΦϓγϣϯΛ͚ͭΕ͹Ϛ ΠχϯάΛ։࢝͢Δ


Slide 20

Slide 20 text

geth(go-ethereum) • ଟ෼Ұ൪༗໊ͳGo׆༻ࣄྫ • Ethereum protocolͷGo࣮૷ • Ethereumͷ࣮ߦྫ͕ࡌ͍ͬͯΔ৔߹͸͍͍ͩͨ͜Ε • Windows͚ͩएׯΠϯετʔϧखॱ͕ෳࡶ͕ͩɺ֤OS Ͱಈ͘؀ڥ͕༻ҙ͞Ε͍ͯΔ • geth(˒7832)ଞʹ΋cpp-ethereum(˒1710), pythereum(˒1487), ethereumj(760)ͳͲ͕͋Δ

Slide 21

Slide 21 text

EthereumΛ੒ཱͤ͞Δ΋ͷ ΞϓϦέʔγϣϯ ϥΠϒϥϦ܈ +40/31$ *1$ 8FC4PDLFU͕༗ޮͳ ϒϩοΫνΣʔϯ؅ཧͷͨΊͷ"1*ɻ
 ج൫෦(geth) .JTU FUI OFUTUBUT %BQQT +4 /FUXPSL
 *OUFMMJHFOU"1* 8FCKT P2P Network $POUSBDU$PEF +

Slide 22

Slide 22 text

Mist Wallet

Slide 23

Slide 23 text

https://ethstats.net/

Slide 24

Slide 24 text

HyperLedger • ۚ༥Λத৺ͱͨ͠൚ۀछͰͷϒϩοΫνΣʔϯ ٕज़ͷߴ౓ԽΛਐΊΔڞಉϓϩδΣΫτ • Supported by The Linux Foundation • ίϯιʔγΞϜܕͷϒϩοΫνΣʔϯ • IBMɺIntelɺJ.P.MorganɺMUFGɺNECɺΞΫ ηϯνϡΞɺ෋࢜௨ɺ೔ཱetc…େاۀ͕ϝϯ όʔͱͳ͍ͬͯΔ

Slide 25

Slide 25 text

HyperLedgerͱଞͷϒϩοΫνΣʔϯ ref: http://gaiax-blockchain.com/hyperledger-project

Slide 26

Slide 26 text

HyperLedger Projects Fabric PBFTʹΑΔߴ଎ͳ߹ҙܗ੒
 খ਺ϊʔυͰՄೳͳӡ༻
 ϝϯόʔγοϓ੍ʹΑΔԾ૝௨՟ͷෆࡏ
 ։ൃස౓͸ߴ͘ɺ࣮ূ࣮ݧࡁΈ*ɻ
 * http://www.jpx.co.jp/corporate/research-study/working-paper/tvdivq0000008q5y-att/ JPX_working_paper_No15.pdf


Slide 27

Slide 27 text

HyperLedger Fabric Network ref: https://www.ibm.com/developerworks/jp/cloud/library/j_cl-blockchain-basics-bluemix/

Slide 28

Slide 28 text

PBFT (Practical Byzantine Fault Tolelance) ref: https://www.ibm.com/developerworks/jp/cloud/library/j_cl-blockchain-basics-bluemix/

Slide 29

Slide 29 text

ChainCode • εϚʔτίϯτϥΫτɻGo͔JavaͰΠϯλʔ ϑΣʔεΛ࣮૷͢Ε͹ϒϩοΫνΣʔϯ্Ͱ࣮ߦ ՄೳͳϓϩάϥϜɻ

Slide 30

Slide 30 text

ChainCode

Slide 31

Slide 31 text

World State • ChainCode͔ΒR/WͰ͖ΔKVSɻJSONจࣈྻΛ ֨ೲͰ͖Δɻ • ϑΝΠϧΛอଘ͍ͨ͠৔߹͸όΠφϦจࣈྻʹ ม׵ͯ͠ɺChainCode্͔Βࢀর͢Δ͜ͱʹͳ Δɻ

Slide 32

Slide 32 text

shim.ChaincodeStubInterface • chaincodeͰWorldStateɺLedgerɺೝূ৘ใͳ ͲʹΞΫηε͢ΔAPI • Init, Invokeͷ྆ํͰୈҰҾ਺ʹ౉ͬͯ͘Δɻ • Get/Put/Delete StateͰWorld StateʹΞΫηε • shim.Success, shim.ErrorͰres payload writer͕ ϥοϓ͞Ε͍ͯΔɻʢΤϥʔίʔυ͸200, 400, 500ͷΈʣ

Slide 33

Slide 33 text

HyperLedger Composer Fabric্ʹϒϩοΫνΣʔϯͷ ΞϓϦΛߏங͢ΔͨΊͷWeb IDE

Slide 34

Slide 34 text

HyperLedger Composer Web UIͰϒϩοΫνΣʔϯ(Fabric)্ʹ৐ͤΔΞϓϦ͕࡞ΕΔɻ ҎԼ4ͭͷߏ੒ཁૉͰ੒Γཱͭɻ Model: τϥϯβΫγϣϯͰѻ͏ΤϯςΟςΟ Access Control: Read/Writeݖݶͷઃఆ Script: νΣʔϯ্Ͱ࣮ߦ͞ΕΔεΫϦϓτɻJSɻ Query: εΫϦϓτ಺෦Ͱݺ΂Δ Modelఆٛʹج͍ͮͯREST APIΛߏஙͨ͠ΓϢχοτςετ΋ Մೳ

Slide 35

Slide 35 text

IPFS • P2PͳϑΝΠϧγεςϜ • ࣮࣭BlockchainͰ௥Ճɾมߋཤྺ͕؅ཧ͞ΕͨϑΝΠ ϧετϨʔδͱͯ͠࢖ΘΕΔ • ը૾ɺಈըɺσΟϨΫτϦͳͲ༷ʑͳ΋ͷΛΞοϓϩʔ υ͢ΔνϡʔτϦΞϧ͕͋Δ • ipfs.picsͳͲɺGyazoతͳαʔϏε΍Winnyతͳαʔ ϏεΛ࡞ΕΔ

Slide 36

Slide 36 text

Upload assets # ը૾ͷΞοϓϩʔυ $ ipfs add test.jpg added 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

Slide 37

Slide 37 text

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౳ͷ ݴޠͰΫϥΠΞϯτ࣮૷͕͞Εͯ·͢ɻ

Slide 38

Slide 38 text

WebUI

Slide 39

Slide 39 text

·ͱΊ • Go͸ϒϩοΫνΣʔϯͷ։ൃͰ΋C++ͱฒΜͰ Α͘࢖ΘΕΔݴޠͰ͢ɻ • ϒϩοΫνΣʔϯͦͷ΋ͷͷAPIͷଞʹɺίϯτ ϥΫτίʔυɺCLIπʔϧɺͷ࣮૷ͳͲʹར༻͞ Ε·͢ɻ • Ұ෦WebUIͰ։ൃɾར༻Ͱ͖ΔαʔϏε͕͋Δ ͷͰɺͥͻ৮ͬͯΈ͍ͯͩ͘͞ɻ