$30 off During Our Annual Pro Sale. View Details »

2018 - How the Rubyist use Blockchain

2018 - How the Rubyist use Blockchain

The talks about ruby and blockchain in rubyelixirconftw.

蒼時弦や

May 07, 2018
Tweet

More Decks by 蒼時弦や

Other Decks in Technology

Transcript

  1. How the Rubyist use
    Blockchain

    View Slide

  2. WEB DEVELOPER
    GAME DEVELOPER


    䓛⛳
    @elct9620

    View Slide

  3. View Slide

  4. I will talk about
    What is the Blockchain
    The examples use Ruby and Ethereum

    View Slide

  5. What is the Blockchain

    View Slide

  6. Linked List
    Cryptography
    Key-Value Data Store
    Distributed System

    View Slide

  7. There is nothing new for us

    View Slide

  8. View Slide

  9. Cryptocurrency

    View Slide

  10. View Slide

  11. So, how the Blockchain works?

    View Slide

  12. Chain =~ Linked List

    View Slide

  13. The Chain
    Block #1
    Block #2
    Block #3
    Pointer
    Pointer

    View Slide

  14. Ok, I think you already learned it!

    View Slide

  15. But, we have to limit anyone to write it!!!

    View Slide

  16. 1. Proof of Work

    View Slide

  17. 1. Prevent mass request from somebody
    2. Other people can verify in short time
    Cryptography

    View Slide

  18. Use cryptography to resolve question!

    View Slide

  19. Cryptography
    Network
    Hash start with “000”
    Workers
    Define a Question
    Broadcast

    View Slide

  20. Cryptography
    Workers
    000abcdefaaabc…
    Network
    Try hash block
    Submit

    View Slide

  21. Cryptography
    Network
    000abcdefaaabc
    Block Created
    Verify result
    Accept

    View Slide

  22. Wait!
    The same content will get the same hash result!

    View Slide

  23. View Slide

  24. We add a random “Proof” string with block data

    View Slide

  25. So, the worker should spend the time

    View Slide

  26. And others can check it in short time

    View Slide

  27. If we receive many correct answers…

    View Slide

  28. Which one should accept?

    View Slide

  29. 2. Byzantine Generals Problem

    View Slide

  30. Distributed
    1. Longest is right
    2. 51% node think it is right

    View Slide

  31. But, we didn’t discuss the algorithm in this talk

    View Slide

  32. And last

    View Slide

  33. How the Blockchain save data?

    View Slide

  34. Merkle Tree

    View Slide

  35. View Slide

  36. Very useful to update and verify data

    View Slide

  37. But read will spend more time

    View Slide

  38. Should I use pure ruby gem to operate blockchain?

    View Slide

  39. It is hard to implement the same API in pure Ruby

    View Slide

  40. Use the existed API to operate Blockchain.

    View Slide

  41. But, you can implement blockchain in Ruby

    View Slide

  42. Example
    Ethereum’s IPC
    Use Ruby to operate Ethereum

    View Slide

  43. IPC =~ UnixSocket

    View Slide

  44. View Slide

  45. How to get IPC and comminuted with it?

    View Slide

  46. IPC
    1. Ethereum Wallet
    2. brew install geth
    3. Compile from source code

    View Slide

  47. “geth —light —testnet”

    View Slide

  48. But I suggest using Ethereum Wallet

    View Slide

  49. Because you can find IPC file very easy

    View Slide

  50. But how to use UnixSocket to operate Ethereum?

    View Slide

  51. JSON-RPC 2.0

    View Slide

  52. View Slide

  53. View Slide

  54. And it always ends with a linefeed (\n)

    View Slide

  55. View Slide

  56. View Slide

  57. Now, we can interact with Ethereum in Ruby

    View Slide

  58. But the result has many hash value

    View Slide

  59. What are the meaning and real data?

    View Slide

  60. Ethereum Data
    1. Call another API to get transaction value
    2. Read from “data” depend on ABI

    View Slide

  61. If we just want to know transaction value

    View Slide

  62. It always contains in API response

    View Slide

  63. So, we talk about smart contract event

    View Slide

  64. View Slide

  65. But we got the data in hex

    View Slide

  66. View Slide

  67. It always returns the 32byte hex string for us

    View Slide

  68. So, we need ABI to help us know the data type

    View Slide

  69. View Slide

  70. Demo - Subscribe event and get data

    View Slide

  71. Now, we know what can we do on the Ethereum

    View Slide

  72. And last, is there has any useful resource?

    View Slide

  73. Ruby Reference
    1. digest-sha3
    2. rlp
    3. ruby-ethereum
    4. Naivechain.rb

    View Slide

  74. Conclusion

    View Slide

  75. Build a blockchain in Ruby can help us learn it

    View Slide

  76. And didn’t try to write a client in pure ruby

    View Slide

  77. THANKS

    View Slide