Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Blockchain on Go

Blockchain on Go

The presentation at DevFest Tokyo 2017 / @__timakin__
An introduction of blockchain and why go is nice to implement blockchain.
Additionally described about the blockchain projects that are based on Go.

5bfed9aa3a9ebccb0c0f0cb65ee9e012?s=128

Seiji Takahashi

October 09, 2017
Tweet

Transcript

  1. BlockChain on Go @__timakin__ / DevFest Tokyo 2017

  2. ࣗݾ঺հ • ߴڮ ੣ೋ • Github: timakin / Twitter: @__timakin__

    • Gunosy Inc. ৽نࣄۀ։ൃࣨ ΤϯδχΞ • Go / Swift • community • golang.tokyo • roppongi-golunch • blockchain.tokyo
  3. golangtokyo.connpass.com ࠓ͙͢ϝϯόʔʹʂ

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

  6. Blockchainͷ֓ཁ • ෳ਺୆ͷίϯϐϡʔλʔʹσʔλΛ؅ཧ͢Δ୆ாΛ ࣋ͨͤɺωοτϫʔΫӽ͠ʹ֤ίϯϐϡʔλʔ্ͷ ୆ாΛಉظͤ͞Δ • ୆ாʁ -> લͷཁૉ΁ͷϙΠϯλΛ࣋ͬͨJSONͷ ू·Γ

    • P2PωοτϫʔΫ্ͷϊʔυ͕ݸʑʹͨͩ̍ͭͷ ϋογϡνΣʔϯΛ࣋ͭ
  7. 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
  8. 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', }
  9. 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ʣ
  10. Decentralized Application Processing File Storage Database Processing File Storage Database

  11. ϝϦοτσϝϦοτ • ϝϦοτ • վ᜵ࠔ೉ੑ • ଱ো֐ੑ • σϝϦοτ •

    ཤྺ֬ఆ·Ͱͷ࣌ؒ • ϒϩοΫνΣʔϯͷछผʹΑͬͯ͸਺ඦmsʹ཈੍ • εέʔϧΞ΢τ
  12. Α͋͘Δޡղ • ෼ࢄ؅ཧ͞Εͨສೳͷσʔλ؅ཧํ๏Ͱ͸ͳ͍ • 2017/10/1࣌఺Ͱ͓Αͦ135GBͷϋογϡνΣʔϯ Λݸผͷϊʔυ͕ίϐʔ͋͠͏۪௚ͳෳ੡ • εέʔϧΞ΢τͷ໰୊͕ੜ͡Δ

  13. ϒϩοΫνΣʔϯͷछผ • ύϒϦοΫ • Bitcoin΍EthereumͳͲͷɺϊʔυ਺ʹݶఆ͕ͳ͘ ෆಛఆଟ਺͕߹ҙܗ੒ʹࢀՃͰ͖Δ • ϓϥΠϕʔτɺίϯιʔγΞϜ • ͋Β͔͡Ί߹ҙܗ੒ʹࢀՃͰ͖Δϊʔυ͕ݶఆ͞Ε

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

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

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

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

    Β͔ͱ͍͏ͱந৅ԽʹΑΔཧղଅਐ΍γϯλοΫεͷγϯ ϓϧ͞ͷํ͕େ͖͍ͱࢥ͏
  19. btcd • Bitcoinຊମͷ୅ସ࣮૷ɻҰछͷfork • bitcoin/bitcoin͸C++࣮૷ • ଞʹ΋libbitcoin(C++), BitcoinJ(Java)ͳͲ • ΫϩείϯύΠϧՄೳͰҠ২ੑ͕ߴ͘ɺϝϞϦ؅ཧ΍

    ฒߦॲཧͳͲͷγϯϓϧ͞ɺඋ͑෇͚ͷϓϩϑΝΠϦ ϯάɾϑΥʔϚοτ͕͋Δͱ͍͏఺Ͱརศੑ͕ߴ͍ɻ • JSON-RPCͷAPI͕ىಈͯ͠ɺΦϓγϣϯΛ͚ͭΕ͹Ϛ ΠχϯάΛ։࢝͢Δ

  20. geth(go-ethereum) • ଟ෼Ұ൪༗໊ͳGo׆༻ࣄྫ • Ethereum protocolͷGo࣮૷ • Ethereumͷ࣮ߦྫ͕ࡌ͍ͬͯΔ৔߹͸͍͍ͩͨ͜Ε • Windows͚ͩएׯΠϯετʔϧखॱ͕ෳࡶ͕ͩɺ֤OS

    Ͱಈ͘؀ڥ͕༻ҙ͞Ε͍ͯΔ • geth(˒7832)ଞʹ΋cpp-ethereum(˒1710), pythereum(˒1487), ethereumj(760)ͳͲ͕͋Δ
  21. EthereumΛ੒ཱͤ͞Δ΋ͷ ΞϓϦέʔγϣϯ ϥΠϒϥϦ܈ +40/31$ *1$ 8FC4PDLFU͕༗ޮͳ ϒϩοΫνΣʔϯ؅ཧͷͨΊͷ"1*ɻ
 ج൫෦(geth) .JTU FUI

    OFUTUBUT %BQQT +4 /FUXPSL
 *OUFMMJHFOU"1* 8FCKT P2P Network $POUSBDU$PEF +
  22. Mist Wallet

  23. https://ethstats.net/

  24. HyperLedger • ۚ༥Λத৺ͱͨ͠൚ۀछͰͷϒϩοΫνΣʔϯ ٕज़ͷߴ౓ԽΛਐΊΔڞಉϓϩδΣΫτ • Supported by The Linux Foundation

    • ίϯιʔγΞϜܕͷϒϩοΫνΣʔϯ • IBMɺIntelɺJ.P.MorganɺMUFGɺNECɺΞΫ ηϯνϡΞɺ෋࢜௨ɺ೔ཱetc…େاۀ͕ϝϯ όʔͱͳ͍ͬͯΔ
  25. HyperLedgerͱଞͷϒϩοΫνΣʔϯ ref: http://gaiax-blockchain.com/hyperledger-project

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


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

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

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

  30. ChainCode

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

  32. shim.ChaincodeStubInterface • chaincodeͰWorldStateɺLedgerɺೝূ৘ใͳ ͲʹΞΫηε͢ΔAPI • Init, Invokeͷ྆ํͰୈҰҾ਺ʹ౉ͬͯ͘Δɻ • Get/Put/Delete StateͰWorld

    StateʹΞΫηε • shim.Success, shim.ErrorͰres payload writer͕ ϥοϓ͞Ε͍ͯΔɻʢΤϥʔίʔυ͸200, 400, 500ͷΈʣ
  33. HyperLedger Composer Fabric্ʹϒϩοΫνΣʔϯͷ ΞϓϦΛߏங͢ΔͨΊͷWeb IDE

  34. HyperLedger Composer Web UIͰϒϩοΫνΣʔϯ(Fabric)্ʹ৐ͤΔΞϓϦ͕࡞ΕΔɻ ҎԼ4ͭͷߏ੒ཁૉͰ੒Γཱͭɻ Model: τϥϯβΫγϣϯͰѻ͏ΤϯςΟςΟ Access Control: Read/Writeݖݶͷઃఆ

    Script: νΣʔϯ্Ͱ࣮ߦ͞ΕΔεΫϦϓτɻJSɻ Query: εΫϦϓτ಺෦Ͱݺ΂Δ Modelఆٛʹج͍ͮͯREST APIΛߏஙͨ͠ΓϢχοτςετ΋ Մೳ
  35. IPFS • P2PͳϑΝΠϧγεςϜ • ࣮࣭BlockchainͰ௥Ճɾมߋཤྺ͕؅ཧ͞ΕͨϑΝΠ ϧετϨʔδͱͯ͠࢖ΘΕΔ • ը૾ɺಈըɺσΟϨΫτϦͳͲ༷ʑͳ΋ͷΛΞοϓϩʔ υ͢ΔνϡʔτϦΞϧ͕͋Δ •

    ipfs.picsͳͲɺGyazoతͳαʔϏε΍Winnyతͳαʔ ϏεΛ࡞ΕΔ
  36. 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
  37. 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౳ͷ ݴޠͰΫϥΠΞϯτ࣮૷͕͞Εͯ·͢ɻ
  38. WebUI

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

    ͷͰɺͥͻ৮ͬͯΈ͍ͯͩ͘͞ɻ