This is a simple implementation of a blockchain using Python. The code defines a Block
class that creates a basic blockchain structure and generates a hash for each block using the SHA-256 algorithm.
The Python script consists of the following components:
The Block
class represents a block in the blockchain. Each block contains:
prev_hash
: The hash of the previous blocktransaction
: Details about the transactionamount
: The amount related to the transactionhash
: The hash of the current blocktime
: Timestamp of the block creation
- Initializes the Block class with provided parameters and generates the block hash.
- Returns the data associated with the block.
- Creates a hash for the block using SHA-256.
- Adds new data (transaction, amount) to the blockchain.
- Prints the information of all the blocks in the blockchain.
To use this code:
- Initialize a block using
Block(prev_hash, transaction, amount)
. - Add more data to the blockchain using
add_data(transaction, amount)
. - View the details of the blockchain using
print_blocks(block)
.
test_block = Block('000746bd11a3a38ad2ac56a51cec7be5ce5930da347d8b31b5ef505568182a49', 'Ivan', 100)
test_block.add_data("Boris", 1042)
print_blocks(test_block)