forked from siddharthkul/coffee-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffeecoin-server.py
247 lines (203 loc) · 7.64 KB
/
coffeecoin-server.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# This is the main file for Coffee Coin Experimentation
# Authored by Siddharth Kulkarni and Sharan Duggirala November 18th, 2017
# Imports
from flask import Flask
from flask import request
import json
node = Flask(__name__)
from Block import *
from coffee_functions import *
from txion import *
from proof_of_work import *
import random
import hashlib
from datetime import datetime
from math import pow
import sys
import atexit
args_passed = False
if(len(sys.argv)-1 == 0):
print("No args passed")
args_passed = True
if(args_passed):
sys.exit([arg])
h = sys.argv[1]
# Ignore all Warnings for demo (Please comment out otherwise)
import warnings
warnings.filterwarnings("ignore")
# This node's blockchain copy
blockchain = []
blockchain.append(create_genesis_block())
# Store the mined transactions in a list on the server
this_nodes_transactions = []
# Dictionary of Miner Information
miner_information_dict = {}
# Global variable challenge initialization
challenge = init_challenge(miner_information_dict, h)
# To Calculate the Energy Efficiency Timing is essential
startTime= datetime.now()
timeElapsed = 0
# Global Initializations to calculate the energy efficiency
# Please change according to the country and situation
rewardPerMine = 1
# This is the electricity cost in San Jose State currently
electricityCost = 0.189
# For the purpose of our experiment and demo efficiency, we
# have set the d_f to 1
difficultyLevel = 1
# The average power consumption of a laptop is around 12W
powerConsumption = 0.012
# The exchange price of a coffeecoin is 1 Dollar
exchangeRate = 1
#############################################################################################################
def end_sequence():
for key in miner_information_dict:
# Need to calculate the Hash Rate First
global timeElapsed
coins_earned = miner_information_dict[key]['coins_earned']
hashrate = (pow(2,32)*coins_earned)/(timeElapsed.total_seconds())
miner_information_dict[key]['hashrate'] = hashrate
#Calculate the Left side of the Power Efficiency
global powerConsumption
leftEquation = hashrate/powerConsumption
miner_information_dict[key]['efficiency'] =leftEquation
# Calculate the Right side of the Power Efficiency
global rewardPerMine
global electricityCost
global difficultyLevel
global exchangeRate
rightEquation = (electricityCost*
difficultyLevel)/(rewardPerMine*exchangeRate)
# Determine if the System is Profitable
if leftEquation > rightEquation:
miner_information_dict[key]['profitability'] = 1
print miner_information_dict
#############################################################################################################
@node.route('/txion', methods=['POST'])
def transaction():
# On each new POST request,
# we extract the transaction data
new_txion = request.get_json()
# Then we add the transaction to our list
this_nodes_transactions.append(new_txion)
# Because the transaction was successfully
# submitted, we log it to our console
print "New transaction"
print "FROM: {}".format(new_txion['from'].encode('ascii','replace'))
print "TO: {}".format(new_txion['to'].encode('ascii','replace'))
print "AMOUNT: {}\n".format(new_txion['amount'])
# Then we let the client know it worked out
return "Transaction submission successful\n"
#############################################################################################################
# New Miner Method
@node.route('/mine', methods=['POST'])
def mine():
# Get the miner information including the answer
informationMiner = request.get_json()
miner_information_dict[informationMiner['miner_address']] = informationMiner
answer = informationMiner['answer']
# Get a random number, upto 10, transactions filled out from the transaction server
for _ in range(random.randint(2,11)):
[new_sender, new_reciever, amount] = get_transactions()
this_nodes_transactions.append(
{ "from": new_sender, "to": new_reciever, "amount": amount}
)
print "NEW TRANSACTIONS :"
print this_nodes_transactions
# Check if the Challenge and Answer are truly equal
if(len(this_nodes_transactions) != 0 and answer == challenge):
# Create a new global challenge, since challenge has already been solved
global challenge
[challenge, d_level] = refresh_challenge(miner_information_dict, h)
print str(challenge) + " new challenge"
# Add the transaction where this server has given a coin to the miner
this_nodes_transactions.append(
{ "from": "network", "to": informationMiner['miner_address'], "amount": 1 }
)
# Now we can gather the data needed to create the new block
last_block = blockchain[len(blockchain) - 1]
new_block_data = {
"transactions": list(this_nodes_transactions)
}
new_block_index = last_block.index + 1
new_block_timestamp = this_timestamp = date.datetime.now()
last_block_hash = last_block.hash
# Empty transaction list and create a new block
this_nodes_transactions[:] = []
mined_block = Block( new_block_index, new_block_timestamp, new_block_data,
last_block_hash)
blockchain.append(mined_block)
# Let the client know we mined a block
return "\n" + json.dumps({
"index": new_block_index,
"timestamp": str(new_block_timestamp),
"data": new_block_data,
"hash": last_block_hash
}) + "\n"
# In the case where there is nothing to mine (no transactions)
# Note: As of now, this part of the condition should never be invoked
else:
#print "Spam Miner"
return "Try Again"
#############################################################################################################
# Get Challenge
@node.route('/info', methods=['POST'])
def get_info():
informationMiner = request.get_json()
miner_information_dict[informationMiner['miner_address']] = informationMiner
return challenge
@node.route('/board', methods=['POST'])
def board():
return json.dumps(miner_information_dict)
#############################################################################################################
# Internal Blocks Method
@node.route('/blocks', methods=['GET'])
def get_blocks():
chain_to_send = blockchain
for i in range(len(chain_to_send)):
block = chain_to_send[i]
block_index = str(block.index)
block_timestamp = str(block.timestamp)
block_data = str(block.data)
block_hash = block.hash
assembled = json.dumps({
"index": block_index,
"timestamp": block_timestamp,
"data": block_data,
"hash": block_hash
})
if blocklist == "":
blocklist = assembled
else:
blocklist = blocklist + assembled
return blocklist
#############################################################################################################
#External Blocks Method
@node.route('/print_block', methods=['GET'])
def print_blocks():
#print '\nLength ',len(this_nodes_transactions)
chain_to_send = blockchain
blocklist = ""
for i in range(len(chain_to_send)):
block = chain_to_send[i]
block_index = str(block.index)
block_timestamp = str(block.timestamp)
block_data = str(block.data)
block_hash = block.hash
assembled = json.dumps({
"index": block_index,
"timestamp": block_timestamp,
"data": block_data,
"hash": block_hash}, sort_keys=False, indent=4)
if blocklist == "":
blocklist = assembled
else:
blocklist = blocklist + assembled
return blocklist
def exit_handler():
print 'My application is ending!'
with open('file.txt','w') as file:
file.write(json.dumps(miner_information_dict))
atexit.register(exit_handler)
# Start server
node.run(threaded=True, host='0.0.0.0')