-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleBlockchain2.py
More file actions
73 lines (53 loc) · 1.97 KB
/
Copy pathSampleBlockchain2.py
File metadata and controls
73 lines (53 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import threading
import time
from time import sleep
from blockchain.blockchain_manager import BlockchainManager
from blockchain.block_builder import BlockBuilder
from transaction.transaction_pool import TransactionPool
# TransactionPoolの確認頻度
CHECK_INTERVAL = 10
block_timer = None
def generate_block_with_tp(tp, bb, bm, prev_block_hash):
result = tp.get_stored_transactions()
if len(result) == 0:
print('Transaction Pool is empty ...')
new_block = bb.generate_new_block(result, prev_block_hash)
bm.set_new_block(new_block.to_dict())
prev_block_hash = bm.get_hash(new_block.to_dict())
# ブロック生成に成功したらTransaction Poolはクリアする
index = len(result)
tp.clear_my_transactions(index)
print('Current Blockchain is ... ', bm.chain)
print('Current prev_block_hash is ... ', prev_block_hash)
block_timer = threading.Timer(CHECK_INTERVAL, generate_block_with_tp, args=(tp, bb, bm, prev_block_hash))
block_timer.start()
def main():
bb = BlockBuilder()
my_genesis_block = bb.generate_genesis_block()
bm = BlockchainManager(my_genesis_block.to_dict())
tp = TransactionPool()
prev_block_hash = bm.get_hash(my_genesis_block.to_dict())
print('genesis_block_hash :', prev_block_hash)
transaction = {
'sender': 'test1',
'recipient': 'test2',
'value': 3
}
tp.set_new_transaction(transaction)
transaction2 = {
'sender': 'test1',
'recipient': 'test3',
'value': 2
}
tp.set_new_transaction(transaction2)
block_timer = threading.Timer(CHECK_INTERVAL, generate_block_with_tp, args=(tp, bb, bm, prev_block_hash))
block_timer.start()
sleep(10) # TransactionPoolの周期的なチェックからタイミングをずらして追加する
transaction3 = {
'sender': 'test5',
'recipient': 'test6',
'value': 10
}
tp.set_new_transaction(transaction3)
if __name__ == '__main__':
main()