-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin.py
62 lines (46 loc) · 1.59 KB
/
bitcoin.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
#!/usr/bin/env python
import os
import sys
import multiprocessing
import Node
import util
import time
if __name__ == "__main__":
node_cnt = int(sys.argv[1])
if util.get_debug():
print("# number of nodes :",node_cnt)
'''
Creating a shared memory for each process to communicate between each other.
Source https://www.youtube.com/watch?v=sp7EhjLkFY4
'''
q_list = []
for i in range(node_cnt):
q_list.append(multiprocessing.Queue())
if util.get_debug():
print("# queue_list :",q_list)
# number of zeros in proof of work and leaf size for merkel tree
pow_zeros = int(sys.argv[2])
leaf_sz = int(sys.argv[3])
if util.get_debug():
print("# Proof of work Zeros :",pow_zeros)
print("# leaf size :",leaf_sz)
# creating common list for all the nodes
common_list = multiprocessing.Manager().list()
token = multiprocessing.Manager().list()
# Creating nodes for the bitcoin
nodes = []
message_limit = 5
for i in range(node_cnt):
nodes.append(Node.Node(i,node_cnt,pow_zeros,leaf_sz,common_list,message_limit,token))
# creating multi-threads using the following funtion
node_process = []
for i in range(node_cnt):
if util.print_logs():
print("Node",i,"is being created.")
process = multiprocessing.Process(target=nodes[i].run_node,args = (q_list,))
node_process.append(process)
# Running the process
for i in node_process:
i.start()
for i in node_process:
i.join()