forked from infonetGIST/Blockchain_lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trader.py
104 lines (89 loc) · 3.02 KB
/
trader.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'''
%
%
% Blockchain and Future Society
% GIST
% Rep. of Korea
%
% Prof. Heung-No Lee
% HW for Python Programming of Blockchain
% Oct. 2018
% Programmed and Submitted by Jae Hyuck Jang
%
'''
from threading import Thread, Event
import time
from flask import Flask, jsonify, request
import requests
import hashlib
import json
from urllib.parse import urlparse
from uuid import uuid4
import random
from blockchain_core import blockchain
from blockchain_core import app
from blockchain_core import INTERRUPT_EVENT1
from blockchain_core import INTERRUPT_EVENT2
from blockchain_core import STOP_EVENT
trading_addresses=[
'Node1',
'Node2',
'Node3'
]
class LoopThread(Thread):
def __init__(self, stop_event, interrupt_event1, interrupt_event2):
self.stop_event = stop_event
self.interrupt_event1 = interrupt_event1
self.interrupt_event2 = interrupt_event2
Thread.__init__(self)
def run(self):
while len(blockchain.chain) < blockchain.max_blocks and not self.stop_event.is_set():
self.loop_process()
if self.interrupt_event1.is_set():
self.interrupted_process1()
self.interrupt_event1.clear()
if self.interrupt_event2.is_set():
self.interrupted_process2()
self.interrupt_event2.clear()
def loop_process(self):
TX_period = 20
senderInd = random.randrange(0,3)
sender=trading_addresses[senderInd]
recipientInd=random.randrange(0,3)
recipient=trading_addresses[recipientInd]
amount = random.randrange(1, 100)
blockchain.new_transaction(sender, recipient, amount)
blockchain.awaiting_transactions.append(blockchain.current_transactions[-1])
blockchain.announcement()
print("Transaction generated")
time.sleep(TX_period)
def interrupted_process1(self):
blockchain.resolve_conflicts()
blockchain.update_awaiting_TX()
print("Transactions in awaiting are updated")
def interrupted_process2(self):
#get_transactions
print("Transaction transfer")
#time.sleep(1)
thread = LoopThread(STOP_EVENT, INTERRUPT_EVENT1, INTERRUPT_EVENT2)
@app.route('/transactions/auto', methods=['get'])
def auto_transaction():
start_time = time.time()
thread.start()
end_time = time.time()
time_consumption = end_time - start_time
response = {
'message': 'Auto-TX completed',
'TX length': len(blockchain.current_transactions),
'starting time': start_time,
'ending time': end_time,
'time consumption': time_consumption,
}
return jsonify(response), 200
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(host='127.0.0.24', port=2000)