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.

Seiji Takahashi

October 09, 2017
Tweet

More Decks by Seiji Takahashi

Other Decks in Programming

Transcript

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

    • Gunosy Inc. ৽نࣄۀ։ൃࣨ ΤϯδχΞ • Go / Swift • community • golang.tokyo • roppongi-golunch • blockchain.tokyo
  2. 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', }
  3. 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ʣ
  4. ϝϦοτσϝϦοτ • ϝϦοτ • վ᜵ࠔ೉ੑ • ଱ো֐ੑ • σϝϦοτ •

    ཤྺ֬ఆ·Ͱͷ࣌ؒ • ϒϩοΫνΣʔϯͷछผʹΑͬͯ͸਺ඦmsʹ཈੍ • εέʔϧΞ΢τ
  5. btcd • Bitcoinຊମͷ୅ସ࣮૷ɻҰछͷfork • bitcoin/bitcoin͸C++࣮૷ • ଞʹ΋libbitcoin(C++), BitcoinJ(Java)ͳͲ • ΫϩείϯύΠϧՄೳͰҠ২ੑ͕ߴ͘ɺϝϞϦ؅ཧ΍

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

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

    • ίϯιʔγΞϜܕͷϒϩοΫνΣʔϯ • IBMɺIntelɺJ.P.MorganɺMUFGɺNECɺΞΫ ηϯνϡΞɺ෋࢜௨ɺ೔ཱetc…େاۀ͕ϝϯ όʔͱͳ͍ͬͯΔ
  7. shim.ChaincodeStubInterface • chaincodeͰWorldStateɺLedgerɺೝূ৘ใͳ ͲʹΞΫηε͢ΔAPI • Init, Invokeͷ྆ํͰୈҰҾ਺ʹ౉ͬͯ͘Δɻ • Get/Put/Delete StateͰWorld

    StateʹΞΫηε • shim.Success, shim.ErrorͰres payload writer͕ ϥοϓ͞Ε͍ͯΔɻʢΤϥʔίʔυ͸200, 400, 500ͷΈʣ
  8. HyperLedger Composer Web UIͰϒϩοΫνΣʔϯ(Fabric)্ʹ৐ͤΔΞϓϦ͕࡞ΕΔɻ ҎԼ4ͭͷߏ੒ཁૉͰ੒Γཱͭɻ Model: τϥϯβΫγϣϯͰѻ͏ΤϯςΟςΟ Access Control: Read/Writeݖݶͷઃఆ

    Script: νΣʔϯ্Ͱ࣮ߦ͞ΕΔεΫϦϓτɻJSɻ Query: εΫϦϓτ಺෦Ͱݺ΂Δ Modelఆٛʹج͍ͮͯREST APIΛߏஙͨ͠ΓϢχοτςετ΋ Մೳ
  9. 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
  10. 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౳ͷ ݴޠͰΫϥΠΞϯτ࣮૷͕͞Εͯ·͢ɻ