forked from infonetGIST/Blockchain_lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Miner2.py
94 lines (77 loc) · 2.55 KB
/
Miner2.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
'''
%
%
% 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
blockchain.mining_reward_address='Miner2'
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()
print("Mining completed")
def loop_process(self):
blockchain.mine()
#blockchain.announcement()
#print("Mining success!")
def interrupted_process1(self):
blockchain.resolve_conflicts()
blockchain.update_transactions()
print("Updated")
def interrupted_process2(self):
print("asd")
thread = LoopThread(STOP_EVENT, INTERRUPT_EVENT1, INTERRUPT_EVENT2)
@app.route("/mine/auto")
def auto_mine():
#start_time = time.time()
blockchain.resolve_conflicts()
thread.start()
#end_time = time.time()
#time_consumption = end_time - start_time
#response = {
# 'message': 'Auto-mining completed',
# 'chain length': len(blockchain.chain),
# 'starting time': start_time,
# 'ending time': end_time,
# 'time consumption': time_consumption,
#}
return "Mining starts", 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.22', port=2000)