forked from siddharthkul/coffee-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_of_work.py
111 lines (90 loc) · 3.15 KB
/
proof_of_work.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
105
106
107
108
109
110
# This is the main file for Coffee Coin Proof of Work and Proof of Stake that will create
# a target hash to get to and modify the difficulty level (d(t)) according to the
# Originally adapted from [https://gist.github.com/aunyks/47d157f8bc7d1829a729c2a6a919c173]
# Authored by Siddharth Kulkarni and Sharan Duggirala November 18th, 2017
import hashlib
import random
import string
import json
difficulty_level = 1
known_cpu = 0
known_gpu = 0
# Function to Initialize the Challenge String
def init_challenge(peer_nodes, h):
# There is not point if the Server has not detected any nodes as
# of now
if (len(peer_nodes) == 0):
answer = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(4))
if(h == str(0)):
answer = hashlib.md5(answer).hexdigest()[:4]
elif(h == str(1)):
answer = hashlib.sha1(answer).hexdigest()[:4]
elif(h == str(2)):
answer = hashlib.sha256(answer).hexdigest()[:4]
return answer
tot_gpu = 0
tot_cpu = 0
# Need to initialize the Known CPU and GPU values to start with
for key in peer_nodes:
tot_gpu += peer_nodes[key].miner_gpu
tot_cpu += peer_nodes[key].miner_cpu
if (tot_gpu + tot_cpu > known_gpu + known_cpu):
known_gpu = tot_gpu
known_cpu = tot_cpu
answer = ''.join(random.choice(string.ascii_lowercase +
string.ascii_uppercase +
string.digits)
for _ in range(4))
return answer
# Function to refresh the Challenge String
def refresh_challenge(peer_nodes, h):
# Need to Discover the Known CPU and GPU values to start with
diff_level(peer_nodes)
challenge = ''
# Add the Required Number of 0s from the Difficulty Level
for i in range(difficulty_level):
challenge+='0'
# Generate the 16 byte string that will function as the target
global difficulty_level
answer = ''.join(random.choice(string.ascii_lowercase +
string.ascii_uppercase +
string.digits)
for _ in range(4-difficulty_level))
if(h == str(0)):
answer = hashlib.md5(answer).hexdigest()[:4]
elif(h == str(1)):
answer = hashlib.sha1(answer).hexdigest()[:4]
elif(h == str(2)):
answer = hashlib.sha256(answer).hexdigest()[:4]
challenge+=answer
global difficulty_level
return challenge, difficulty_level
# Calculate the Total GPU and CPU utilazation and change the value of
# of the difficulty level
def diff_level(peer_nodes):
# There is not point if the Server has not detected any nodes as
# of now
if (len(peer_nodes) == 0) :
return
else:
tot_gpu = 0
tot_cpu = 0
# Need to Discover the new CPU and GPU values to start with
for key in peer_nodes:
tot_gpu += peer_nodes[key]['miner_gpu']
tot_cpu += peer_nodes[key]['miner_cpu']
#In the case where the CPU and GPU has doubled
if (tot_gpu + tot_cpu > 2*(known_gpu + known_cpu)):
global known_gpu
global known_cpu
global difficulty_level
difficulty_level+=1
known_gpu = tot_gpu
known_cpu = tot_cpu
# In the case where CPU and GPU have halved
if (tot_gpu + tot_cpu < 2*(known_gpu + known_cpu)):
global difficulty_level
difficulty_level-=1
known_gpu = tot_gpu
known_cpu = tot_cpu
# If neither of these cases are true, just stay as is