今更ながらBlockChainを、その2

カテゴリ: Technology

Author: f_prg

X(Twitter): f_prg

今更ながらBlockChainを、その2

なんか、それっぽいコード

https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b

Pythonをカスタマイズ

バージョンが合わなかったりするので、調整程度で。

Pythonのバージョンを確認

[f_prg@~] $ python --version
Python 3.10.0

ざっと改変したPythonのコード

import hashlib
import json
import datetime
import datetime as date


import hashlib as hasher

class Block:
    def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.hash_block()

    def hash_block(self):
        sha = hasher.sha256()
        block_text = str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)
        print(hashlib.sha256(block_text.encode("utf-8")).hexdigest())
        print("└prev: " + self.previous_hash)
        sha.update(block_text.encode("utf-8"))

        return sha.hexdigest()

def create_genesis_block():
    # Manually construct a block with
    # index zero and arbitrary previous hash
    return Block(0, date.datetime.now(), "Genesis Block", "0")

def next_block(last_block):
    this_index = last_block.index + 1
    this_timestamp = date.datetime.now()
    this_data = "Hey! I'm block " + str(this_index)
    this_hash = last_block.hash
    return Block(this_index, this_timestamp, this_data, this_hash)


# ---------------
# entrypoint
# ---------------

# Create the blockchain and add the genesis block
blockchain = [create_genesis_block()]
previous_block = blockchain[0]

# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 20

# Add blocks to the chain
for i in range(0, num_of_blocks_to_add):
    block_to_add = next_block(previous_block)
    blockchain.append(block_to_add)
    previous_block = block_to_add
    # Tell everyone about it!
    print("Block #{} has been added to the blockchain!".format(block_to_add.index))
    print("Hash: {}\n".format(block_to_add.hash))

実行

[f_prg@blockchain_docker] $ python ./program/bexe.py
522f260dda0f30ac2b867a41e72223f35a615036164a1c21861a9dfcfc2bfa83
└prev: 0
f8457d79199c182f46497fff7e3fee4c75767bec208283a7a303f03bf483acf9
└prev: 522f260dda0f30ac2b867a41e72223f35a615036164a1c21861a9dfcfc2bfa83
Block #1 has been added to the blockchain!
Hash: f8457d79199c182f46497fff7e3fee4c75767bec208283a7a303f03bf483acf9

2b11da7639cdedd6e648be52b6ae944321e4d69d53bd1a967a79531ab4abe10a
└prev: f8457d79199c182f46497fff7e3fee4c75767bec208283a7a303f03bf483acf9
Block #2 has been added to the blockchain!
Hash: 2b11da7639cdedd6e648be52b6ae944321e4d69d53bd1a967a79531ab4abe10a

b909e56d02a0f34699541c324d4e291fddcc3b5bd3eac995da03489c5b8b8b2b
└prev: 2b11da7639cdedd6e648be52b6ae944321e4d69d53bd1a967a79531ab4abe10a
Block #3 has been added to the blockchain!
Hash: b909e56d02a0f34699541c324d4e291fddcc3b5bd3eac995da03489c5b8b8b2b

623857468631fa39a1987b739a638422b923cf369ef248095c8e534d99cafa17
└prev: b909e56d02a0f34699541c324d4e291fddcc3b5bd3eac995da03489c5b8b8b2b
Block #4 has been added to the blockchain!
Hash: 623857468631fa39a1987b739a638422b923cf369ef248095c8e534d99cafa17

e7b719e63c0f3de8e5ffc69e6c280dc1d9c1a92aa9bdf16192a205963edf4cd3
└prev: 623857468631fa39a1987b739a638422b923cf369ef248095c8e534d99cafa17
Block #5 has been added to the blockchain!
Hash: e7b719e63c0f3de8e5ffc69e6c280dc1d9c1a92aa9bdf16192a205963edf4cd3

632acd5eec2270980b0a80295635b75b9638394af47102961ab19b6174a77df8
└prev: e7b719e63c0f3de8e5ffc69e6c280dc1d9c1a92aa9bdf16192a205963edf4cd3
Block #6 has been added to the blockchain!
Hash: 632acd5eec2270980b0a80295635b75b9638394af47102961ab19b6174a77df8

6919426714af8441cdd3ce1bfa15914da80a0e8244ce30c73626a78a5412124d
└prev: 632acd5eec2270980b0a80295635b75b9638394af47102961ab19b6174a77df8
Block #7 has been added to the blockchain!
Hash: 6919426714af8441cdd3ce1bfa15914da80a0e8244ce30c73626a78a5412124d

8c36feaac100451a58926a30c143a651537e4bcb09d9e6a26cb8cec1f2af5235
└prev: 6919426714af8441cdd3ce1bfa15914da80a0e8244ce30c73626a78a5412124d
Block #8 has been added to the blockchain!
Hash: 8c36feaac100451a58926a30c143a651537e4bcb09d9e6a26cb8cec1f2af5235

d514d7cd888e346098ceadc90b7066665d86bb75ed9c0210630ae64eecb1b553
└prev: 8c36feaac100451a58926a30c143a651537e4bcb09d9e6a26cb8cec1f2af5235
Block #9 has been added to the blockchain!
Hash: d514d7cd888e346098ceadc90b7066665d86bb75ed9c0210630ae64eecb1b553

d688cc3873055a8e05396b4b729add4e2e3e8d0ccd9be33beca21a672653a299
└prev: d514d7cd888e346098ceadc90b7066665d86bb75ed9c0210630ae64eecb1b553
Block #10 has been added to the blockchain!
Hash: d688cc3873055a8e05396b4b729add4e2e3e8d0ccd9be33beca21a672653a299

0cada378d1f23223c7a0f9518f215a6aa52ae42c565d85e788f2004f5c45d845
└prev: d688cc3873055a8e05396b4b729add4e2e3e8d0ccd9be33beca21a672653a299
Block #11 has been added to the blockchain!
Hash: 0cada378d1f23223c7a0f9518f215a6aa52ae42c565d85e788f2004f5c45d845

059f7305c71a8ddde74371910d7895c01780080c3fa7181899bddf992ef59617
└prev: 0cada378d1f23223c7a0f9518f215a6aa52ae42c565d85e788f2004f5c45d845
Block #12 has been added to the blockchain!
Hash: 059f7305c71a8ddde74371910d7895c01780080c3fa7181899bddf992ef59617

68c8cc09a85eeb980ae93e66aa5f3672041462a26e031721833e58e5d28d4080
└prev: 059f7305c71a8ddde74371910d7895c01780080c3fa7181899bddf992ef59617
Block #13 has been added to the blockchain!
Hash: 68c8cc09a85eeb980ae93e66aa5f3672041462a26e031721833e58e5d28d4080

50f633688080dc8eda6327fd84694a04ec055587ca9cf36e2507b9b19f67f80f
└prev: 68c8cc09a85eeb980ae93e66aa5f3672041462a26e031721833e58e5d28d4080
Block #14 has been added to the blockchain!
Hash: 50f633688080dc8eda6327fd84694a04ec055587ca9cf36e2507b9b19f67f80f

a860997d3d4dac06f7ec2810cd83e719b2b9cc6db9c47375f1a1b2538dd9ba75
└prev: 50f633688080dc8eda6327fd84694a04ec055587ca9cf36e2507b9b19f67f80f
Block #15 has been added to the blockchain!
Hash: a860997d3d4dac06f7ec2810cd83e719b2b9cc6db9c47375f1a1b2538dd9ba75

08aa5d72631449a6bb72787e66566d278552bd8066c33d553d9bded75d4a5567
└prev: a860997d3d4dac06f7ec2810cd83e719b2b9cc6db9c47375f1a1b2538dd9ba75
Block #16 has been added to the blockchain!
Hash: 08aa5d72631449a6bb72787e66566d278552bd8066c33d553d9bded75d4a5567

358929c3c7bac5f5cb6d03369c6cf0ad3fb9d697433c46523d71700d5cff0734
└prev: 08aa5d72631449a6bb72787e66566d278552bd8066c33d553d9bded75d4a5567
Block #17 has been added to the blockchain!
Hash: 358929c3c7bac5f5cb6d03369c6cf0ad3fb9d697433c46523d71700d5cff0734

594fedb91740295b715686bfb0defc4ff4b63fd201f10435eb1b29b7b8921b30
└prev: 358929c3c7bac5f5cb6d03369c6cf0ad3fb9d697433c46523d71700d5cff0734
Block #18 has been added to the blockchain!
Hash: 594fedb91740295b715686bfb0defc4ff4b63fd201f10435eb1b29b7b8921b30

cf3a9873219d9aa392cf7732a607e1215aa7fa25eb4a5e78167ce96ce0ca8d7d
└prev: 594fedb91740295b715686bfb0defc4ff4b63fd201f10435eb1b29b7b8921b30
Block #19 has been added to the blockchain!
Hash: cf3a9873219d9aa392cf7732a607e1215aa7fa25eb4a5e78167ce96ce0ca8d7d

bfef2386f4ef5499f32ec4587e4f75876224f8dae775044797f447f16d8cb211
└prev: cf3a9873219d9aa392cf7732a607e1215aa7fa25eb4a5e78167ce96ce0ca8d7d
Block #20 has been added to the blockchain!
Hash: bfef2386f4ef5499f32ec4587e4f75876224f8dae775044797f447f16d8cb211

まとめ

一度、実行してみて データの流れを見たら イメージが固まる。