-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
56 lines (51 loc) · 1.91 KB
/
node.py
File metadata and controls
56 lines (51 loc) · 1.91 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
from queue import Queue
from gan import MockGAN as GAN
from block import Block, LogBlock
from p2p import p2pInterface
from blockchain import Blockchain
from GPoHC import GPoHC
import threading
import sys
class Node():
def __init__(self, private_key, host, name="chain"):
self.host = host
self.private_key = private_key
self.address = private_key
self.p2pInterface = p2pInterface(self)
self.consensus = GPoHC(self, name)
self.chain = Blockchain(self, name,self.p2pInterface)
self.seed_store = {}
@staticmethod
def runtime(self, first_run=True):
pass
def run(self):
try:
data_queue = Queue()
self.main_thread = threading.Thread(target=self.p2pInterface.listen, args=(data_queue,), daemon=True)
self.main_thread.start()
first_run = True
while self.p2pInterface.listening:
self.runtime(self, first_run)
first_run = False
if data_queue.qsize() > 0:
data_type, data = data_queue.get(timeout=1)
if data_type == "blck":
print("Received Block Data")
block = Block(self,"").deserialised(data.decode())
if not block.valid():
try:
self.chain.append(block)
print(f"Block {block.hash[:6]} added to chain")
except ValueError as e:
print(e)
else:
print(f"Block {block.hash[:6]} invalid")
except KeyboardInterrupt as e:
print("Shutting down")
sys.exit(0)
def shutdown(self):
self.p2pInterface.listening = False
self.main_thread.terminate()
def restart(self):
self.shutdown()
self.run()