python-local-blockchain/mini_blockchain.py

51 lines
1.2 KiB
Python
Raw Normal View History

2019-11-01 23:51:39 +01:00
#!/usr/bin/python3
"""Mini blockchain using Python."""
import time
2019-11-01 23:51:39 +01:00
from blockchain import Blockchain
import mempool
2019-11-01 23:51:39 +01:00
def main():
"""main function"""
# generate mempool with random values for 10 000 transacations
local_mempool = mempool.create_mempool(10 * 1000)
print('Mempool created.')
2019-11-01 23:51:39 +01:00
local_blockchain = Blockchain()
print('Blockchain initialized.')
local_blockchain.print_blocks()
2019-11-01 23:51:39 +01:00
time.sleep(3)
2019-11-01 23:51:39 +01:00
print('Adding blocks from the mempool...')
time.sleep(2)
2019-11-01 23:51:39 +01:00
while len(local_mempool) > 0:
# prepare 100 transactions from the mempool for one block
# the 100 transactions are removed from the mempool at the same time
block_transactions = [local_mempool.pop(0) for i in range(100)]
local_blockchain.add_block(block_transactions)
local_blockchain.print_blocks()
local_blockchain.validate_chain()
time.sleep(3)
fake_transactions = {
"amount": "25",
"sender": "Bob",
"receiver": "Alice",
}
print('Modifying a transaction...')
2019-11-01 23:51:39 +01:00
local_blockchain.chain[2].transactions = fake_transactions
time.sleep(2)
2019-11-01 23:51:39 +01:00
local_blockchain.validate_chain()
if __name__ == "__main__":
main()