[PYTHON] Hello world! (Minimum Viable Block Chain)

0. Introduction

Regarding blockchain, I just read the articles on the net, and although I had somehow heard that "blocks are connected in a chain" or "distributed rather than centralized", I do not understand it properly. I didn't have it, so I looked it up.

Rather than reading a book and understanding the concept superficially, I thought that it would be fun to actually make a "minimal blockchain" that can not be said to be a blockchain if it is cut further, so I caught the net When I tried it, there was an article that was just right for me.

Sample code of the smallest feasible block chain (unfinished) (imsut/minimum-viable-block-chain)

By translating this into Python and looking at the execution results, I was able to understand the following with a concrete image (I felt like).

--What is a block? ――What does it mean that blocks are held in a chain? -Will a decentralized trust network be built instead of centralized? ――What is Bitcoin mining? ――How do you prevent falsification of past data?

1. Overview of the classes that appear

In a nutshell, I understand what a blockchain is, "a write-once ledger that is distributed but difficult to tamper with and ensures data reliability." This is realized by the interaction of five classes, User, Transaction, Verifier, Network, and Block.

User A class that represents a participant in a blockchain network. Exchange messages with other users.

(Source code)

Transaction A class that represents the interaction between Users.

There are the following three types of Transaction.

--MessageTransaction: Represents the exchange of messages between Users --FeeTransaction: Transaction that authenticates past message exchanges between Users --SignedTransaction: Signed MessageTransaction. It seems that it is usually encrypted with PKI etc., but it is omitted here.

In FeeTransaction, Fee can be obtained by authenticating MessageTransaction between other Users. (Minning in Bitcoin?)

(Source code)

Verifier --A class that authenticates interactions between Users. --Users inherit Verifier, so all Users are also Verifiers. --Holds an unauthenticated Transaction and creates a Block when any of the Users triggers a FeeTransaction. --Keep the blockchain as attribute __last_block.

(Source code)

Network --The blockchain network itself. --Announce the occurrence of a transaction to users participating in the network.

(Source code)

Block --Data that summarizes several Transactions. --Generated by User (Verifier) authenticating Transaction of another User. --To create a block, you need to complete a task that requires computational power (create_id in this case). This is the so-called Proof of Work. --Block has an attribute called prev_block that points to the previous block, and all Transaction data is held as a collection of blocks connected in a chain.

(Source code)

2. Experiment

For example, execute Transaction as shown below and observe the overall movement.

from simplest.network import Network
from simplest.user import User

network = Network()

alice = User('Alice', network)
bob = User('Bob', network)
chris = User('Chris', network)
dan = User('Dan', network)

alice.send('Yo!', to=bob, fee=1.0)
bob.send('Ho!', to=chris, fee=2.0)
chris.send('Yo!Ho!', to=alice, fee=3.0)

alice.verify_message_trxs()
dan.verify_message_trxs()
chris.verify_message_trxs()

(Source code)

Execution result

~/Python35/bin/python ~/blockchain/simplest/main.py
[User.init] Alice: joining the block chain network
[User.init] Bob: joining the block chain network
[User.init] Chris: joining the block chain network
[User.init] Dan: joining the block chain network
↑
User joins Network as soon as it is created

[User.send] Alice: sending a message to Bob. (Yo!:1.0).
[Network.announce_signed_trx] announcing "Transaction signed by Alice (Message Transaction: "Yo!" to Bob with fee 1.0)"
↑
When Alice sends a message to Bob, it's announced throughout the Network.

[Verifier.receive_signed_trx] Dan: unconfirmed transactions [<simplest.transaction.SignedTransaction object at 0x108f09828>]
[Verifier.receive_signed_trx] Chris: unconfirmed transactions [<simplest.transaction.SignedTransaction object at 0x108f09828>]
↑
User who is not involved in Transaction receives the signed Transaction and keeps it as an unconfirmed Transaction.

[Verifier.receive_signed_trx] Alice: not going to verify this transaction as I'm involved
[Verifier.receive_signed_trx] Bob: not going to verify this transaction as I'm involved
↑
Transaction parties(Alice and Bob)Does nothing.

[User.send] Bob: sending a message to Chris. (Ho!:2.0).
[Network.announce_signed_trx] announcing "Transaction signed by Bob (Message Transaction: "Ho!" to Chris with fee 2.0)"
[Verifier.receive_signed_trx] Alice: unconfirmed transactions [<simplest.transaction.SignedTransaction object at 0x108f09c50>]
[Verifier.receive_signed_trx] Dan: unconfirmed transactions [<simplest.transaction.SignedTransaction object at 0x108f09828>, <simplest.transaction.SignedTransaction object at 0x108f09c50>]
[Verifier.receive_signed_trx] Chris: not going to verify this transaction as I'm involved
[Verifier.receive_signed_trx] Bob: not going to verify this transaction as I'm involved
↑
Similarly, when Bob sends a message to Chris, the Transaction is announced throughout the Network, and the User who is not involved in the Transaction keeps it as an unconfirmed Transaction.

[User.send] Chris: sending a message to Alice. (Yo!Ho!:3.0).
[Network.announce_signed_trx] announcing "Transaction signed by Chris (Message Transaction: "Yo!Ho!" to Alice with fee 3.0)"
[Verifier.receive_signed_trx] Alice: not going to verify this transaction as I'm involved
[Verifier.receive_signed_trx] Dan: unconfirmed transactions [<simplest.transaction.SignedTransaction object at 0x108f09828>, <simplest.transaction.SignedTransaction object at 0x108f09c50>, <simplest.transaction.SignedTransaction object at 0x108f09cc0>]
[Verifier.receive_signed_trx] Chris: not going to verify this transaction as I'm involved
[Verifier.receive_signed_trx] Bob: unconfirmed transactions [<simplest.transaction.SignedTransaction object at 0x108f09cc0>]
↑
Similarly

[Verifier.verify_message_trxs] Alice: created Transaction signed by Alice (Fee Transaction: Alice gets confirmation fee 2.0)
↑
Alice authenticates previous Transaction(Mining in Bitcoin?)To do.

[Block.init] trxs: [<simplest.transaction.SignedTransaction object at 0x108f09c50>, <simplest.transaction.SignedTransaction object at 0x108f09d30>], prev_block: None
↑
A Block that collects multiple Transactions is generated. Transactions not involving Alice(Bob-Transactions between Chris)You can only authenticate, so you get 2 Fees.0。

[Network.announce_block] announcing "Block of 2 transactions (Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, Previous Block Id: None, secret: 27)"
↑
Two transactions(1 MessageTransaction and 1 FeeTransaction)It is announced that a block containing is generated.

[Verifier.receive_block] Alice: validated Block of 2 transactions (Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, Previous Block Id: None, secret: 27) is valid
[Verifier.receive_block] Alice: my trx "Fee Transaction: Alice gets confirmation fee 2.0" is validated by network!
[Verifier.receive_block] Dan: validated Block of 2 transactions (Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, Previous Block Id: None, secret: 27) is valid
[Verifier.receive_block] Chris: validated Block of 2 transactions (Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, Previous Block Id: None, secret: 27) is valid
[Verifier.receive_block] Bob: validated Block of 2 transactions (Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, Previous Block Id: None, secret: 27) is valid
[Verifier.receive_block] Bob: my trx "Message Transaction: "Ho!" to Chris with fee 2.0" is validated by network!

[Verifier.verify_message_trxs] Dan: created Transaction signed by Dan (Fee Transaction: Dan gets confirmation fee 4.0)
↑
Dan then authenticates the rest of the Transaction. Dan is not involved in any Message Transaction, so he should be able to authenticate all the remaining Message Transactions.

[Block.init] trxs: [<simplest.transaction.SignedTransaction object at 0x108f09828>, <simplest.transaction.SignedTransaction object at 0x108f09cc0>, <simplest.transaction.SignedTransaction object at 0x108f09da0>], prev_block: Block of 2 transactions (Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, Previous Block Id: None, secret: 27)
↑
A Block containing Transactions not approved by Alice was generated. prev_It has a block generated by Alice as a block. (The blocks are in a chain!)

[Network.announce_block] announcing "Block of 3 transactions (Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, Previous Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, secret: 1545)"
[Verifier.receive_block] Alice: validated Block of 3 transactions (Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, Previous Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, secret: 1545) is valid
[Verifier.receive_block] Alice: my trx "Message Transaction: "Yo!" to Bob with fee 1.0" is validated by network!
[Verifier.receive_block] Dan: validated Block of 3 transactions (Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, Previous Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, secret: 1545) is valid
[Verifier.receive_block] Dan: my trx "Fee Transaction: Dan gets confirmation fee 4.0" is validated by network!
[Verifier.receive_block] Chris: validated Block of 3 transactions (Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, Previous Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, secret: 1545) is valid
[Verifier.receive_block] Chris: my trx "Message Transaction: "Yo!Ho!" to Alice with fee 3.0" is validated by network!
[Verifier.receive_block] Bob: validated Block of 3 transactions (Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, Previous Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, secret: 1545) is valid

[Verifier.verify_message_trxs] Chris: created Transaction signed by Chris (Fee Transaction: Chris gets confirmation fee 0)
↑
Then Chris tries to authenticate, but he can't get Fee because Dan has authenticated everything.

[Block.init] trxs: [<simplest.transaction.SignedTransaction object at 0x108f2e0f0>], prev_block: Block of 3 transactions (Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, Previous Block Id: 0003430f7a84c5973edf31e42e9c25c0eed725af7e818adba62b0efe4d966188, secret: 1545)
[Network.announce_block] announcing "Block of 1 transactions (Block Id: 0006a4dc569ff1a61c6c371980635341d39be3b0c23363030511501e76ac6536, Previous Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, secret: 1062)"
[Verifier.receive_block] Alice: validated Block of 1 transactions (Block Id: 0006a4dc569ff1a61c6c371980635341d39be3b0c23363030511501e76ac6536, Previous Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, secret: 1062) is valid
[Verifier.receive_block] Dan: validated Block of 1 transactions (Block Id: 0006a4dc569ff1a61c6c371980635341d39be3b0c23363030511501e76ac6536, Previous Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, secret: 1062) is valid
[Verifier.receive_block] Chris: validated Block of 1 transactions (Block Id: 0006a4dc569ff1a61c6c371980635341d39be3b0c23363030511501e76ac6536, Previous Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, secret: 1062) is valid
[Verifier.receive_block] Chris: my trx "Fee Transaction: Chris gets confirmation fee 0" is validated by network!
[Verifier.receive_block] Bob: validated Block of 1 transactions (Block Id: 0006a4dc569ff1a61c6c371980635341d39be3b0c23363030511501e76ac6536, Previous Block Id: 000302fdb2b0a45e99e40675cce0299de96d227289d200210a0d774653a80b9a, secret: 1062) is valid

Process finished with exit code 0

3. Summary

I was able to understand (I feel like) the simple question about blockchain, as mentioned at the beginning, with not only a conceptual explanation but also an image of concrete source code. On the other hand, regarding Proof of work, I got an image of the movement, but I was not sure about the necessity and how difficult the task should be, so I would like to investigate it again when I have time.

4. Referenced site

  1. Minimum Viable Block Chain An article explaining why blockchain works like it does now.

  2. Understanding blockchain more deeply Explanation in Japanese of 1.

  3. Sample code of the smallest feasible block chain (unfinished) (imsut/minimum-viable-block-chain) Implementation by Scala in 1. The original story of this article.

  4. izqui/blockchain 1 Golang implementation

  5. Explanation of what is an easy-to-understand blockchain? What is Blockchain (conceptual explanation)

  6. Is it time to start studying Blockchain? What is Blockchain (conceptual explanation)

  7. TED: How the blockchain will radically transform the economy What is Blockchain (conceptual explanation)

Recommended Posts

Hello world! (Minimum Viable Block Chain)
Hello world
Pymacs hello world
cython hello world
web2py memo: Hello World
hello world with ctypes
RabbitMQ Tutorial 1 ("Hello World!")
Hello, World with Docker
Hello World on Django
Django's first Hello World
Hello world with flask
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Until hello world with zappa
Programming language in "Hello World"
Hello World in GO language
Hello World (beginners) on Django
Python starting with Hello world!