Published Date : 2021年8月1日5:11

011 Pythonでビットコインを学ぶ (ブロックチェーン 2)
011 Use python to learn bitcoin (Blockchain 2)

This blog has an English translation


This is a summary blog post about a video I uploaded to NicoNico.


Please refer to the video for details.


Table of Contents

① 動画の説明
① Video Description


Continued from last time.


Also, in the Python script in the previous video, the value of the merkle root appeared.

これは取引を一組ずつハッシュ化して最終的に一つのハッシュ値にまとめたroot hashと呼ばれるものです。

This is called root hash, which hashes a set of transactions into a single hash value.

取引は一つ一つハッシュ化され、それらの複数の取引記録のハッシュ値を統合させて一つのroot hashを作ります。

Each transaction is hashed one by one, and the hash values of the transaction records are combined to create one root hash.


If all the transaction records are stored, the storage capacity of the storage medium becomes large, so this method is used to save the storage space of the storage medium.

しかし、全ての取引記録を保存しているノードも必要なので、Full Nodeと呼ばれるノードは全ての取引記録を保存するようにします。

However, you also need a node that stores all transaction records, so the nodes called Full Nodes store all transaction records.

そして、先ほどの取引記録のroot hashやその他の情報を含めたブロックヘッダーだけを保存しているLightweight Nodeと呼ばれるノードに分けることにしました。

We then split it into nodes called lightweight nodes that store only the block headers, including the root hash and other information from the previous transaction records.


Let's create a simple merkle root in Python.


It's not a strict creation method, but it's easier to understand if it's simple.


I think you learned how to use sha256 in hashlib, in my previous videos, so let's turn the rough transactions into strings and hash them one by one.


All that remains is to combine them into one hash value (that is, the root hash value).

from hashlib import sha256

tx1 = "transaction1"

tx2 = "transaction2"

tx3 = "transaction3"

tx4 = "transaction4"

tx1_hash = sha256(tx1.encode('utf-8')).hexdigest()


tx2_hash = sha256(tx2.encode('utf-8')).hexdigest()


tx12_hash = sha256((tx1 + tx2).encode('utf-8')).hexdigest()


tx3_hash = sha256(tx3.encode('utf-8')).hexdigest()

tx4_hash = sha256(tx4.encode('utf-8')).hexdigest()



tx34_hash = sha256((tx3 + tx4).encode('utf-8')).hexdigest()


root_hash = sha256((tx12_hash + tx34_hash).encode('utf-8')).hexdigest()


[explorer bitcoin]と検索すると、今までに繋がれた全てのブロック(ブロック0番(ジェネシスブロックと呼ばれる)から最新のものまで)と取引記録が閲覧できます。

Search for [explorer bitcoin], then you'll see all transaction records and blocks from the first creating block (Block 0, also called Genesis Block) to the latest.

より詳しい内容や正確な情報は[satoshi nakamonoto]の論文[A Peer-to-Peer Electronic Cash System]を参照してください。

Please refer to the paper [A Peer-to-Peer Electronic Cash System] by [satoshi nakamoto] for more details and accurate information.

1990年代前半に設立されたサイファーパンクと呼ばれる集団のメーリングリストに[satoshi nakamonoto]の論文は投稿されました。

[satoshi nakamoto]'s paper was submitted to a group mailing list called Cipherpunk, which was established in the early 1990s.


Thankfully, the original bitcoin software is still stored on the net, and you can also see its source code.


This software was used to trade and mine bitcoins.

Githubには初期の頃の[satoshi nakamonoto]によるソフトウェアのソースコードが残されています。

Github stores the source code of the software written by [satoshi nakamoto] which was made in the early days.


You should be able to understand more detailed and accurate contents by referring to these things.


That's all. Thank you for your hard work.