2019-11-01 23:51:39 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2019-11-04 10:20:18 +01:00
|
|
|
"""Example implementation of a simple blockchain using Python."""
|
2019-11-01 23:51:39 +01:00
|
|
|
|
|
|
|
from blockchain import Blockchain
|
2019-11-03 10:57:19 +01:00
|
|
|
import mempool
|
2019-11-01 23:51:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-11-04 10:20:18 +01:00
|
|
|
"""Command line utility to see a simple blockchain in action."""
|
2019-11-01 23:51:39 +01:00
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
# generate mempool with random values for 10 000 transactions
|
2019-11-03 10:57:19 +01:00
|
|
|
local_mempool = mempool.create_mempool(10 * 1000)
|
|
|
|
print('Mempool created.')
|
2019-11-01 23:51:39 +01:00
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
input("...Show mempool >>>")
|
|
|
|
|
|
|
|
for i, transaction in enumerate(local_mempool):
|
|
|
|
print(f'Transaction {i + 1}:')
|
|
|
|
print(f' sender: {transaction["sender"]}')
|
|
|
|
print(f' receiver: {transaction["receiver"]}')
|
|
|
|
print(f' amount: {transaction["amount"]}')
|
|
|
|
print()
|
|
|
|
|
|
|
|
input("...Initalize Blockchain >>>")
|
|
|
|
|
2019-11-03 10:57:19 +01:00
|
|
|
local_blockchain = Blockchain()
|
|
|
|
print('Blockchain initialized.')
|
|
|
|
local_blockchain.print_blocks()
|
2019-11-01 23:51:39 +01:00
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
input("...Add blocks from mempool >>>")
|
2019-11-01 23:51:39 +01:00
|
|
|
|
2019-11-03 10:57:19 +01:00
|
|
|
print('Adding blocks from the mempool...')
|
2019-11-01 23:51:39 +01:00
|
|
|
|
2019-11-03 10:57:19 +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)]
|
2019-11-04 11:01:53 +01:00
|
|
|
local_blockchain.add_block(block_transactions)
|
|
|
|
print(f'Proof for block {len(local_blockchain.chain) - 1}: '
|
|
|
|
f'{local_blockchain.chain[-1].proof}')
|
2019-11-03 12:07:42 +01:00
|
|
|
|
|
|
|
input("...Show blocks >>>")
|
|
|
|
local_blockchain.print_blocks()
|
2019-11-03 10:57:19 +01:00
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
input("...Validate blockchain >>>")
|
2019-11-03 10:57:19 +01:00
|
|
|
local_blockchain.validate_chain()
|
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
input("...Modify a transaction >>>")
|
2019-11-03 10:57:19 +01:00
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
fake_transactions = [{
|
2019-11-03 10:57:19 +01:00
|
|
|
"sender": "Bob",
|
|
|
|
"receiver": "Alice",
|
2019-11-03 12:07:42 +01:00
|
|
|
"amount": "25",
|
|
|
|
}]
|
2019-11-03 10:57:19 +01:00
|
|
|
|
2019-11-03 12:07:42 +01:00
|
|
|
print("These transactions:\n")
|
|
|
|
print(local_blockchain.chain[2].transactions)
|
|
|
|
print('... were modified to:\n')
|
|
|
|
print(fake_transactions)
|
2019-11-01 23:51:39 +01:00
|
|
|
local_blockchain.chain[2].transactions = fake_transactions
|
2019-11-03 12:07:42 +01:00
|
|
|
|
|
|
|
input("...Validate blockchain >>>")
|
2019-11-01 23:51:39 +01:00
|
|
|
local_blockchain.validate_chain()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|