-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
282 lines (212 loc) · 7.71 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import json
from flask import Flask, jsonify, request
from web3 import Web3, HTTPProvider
from web3.eth import Account
import os
# not sure if dotenv will work in google cloud (can't pip install it)
try :
from dotenv import load_dotenv
load_dotenv()
except:
pass
app = Flask(__name__)
print('starting..')
web3 = Web3(HTTPProvider(os.getenv('HTTPProvider')))
# do this a a test of connectivity
print('block', web3.eth.blockNumber)
GAP = int(60/4) # Kovan updates every 4 seconds, let's wait for a minute (60)
compiler_contract_path = '../build/contracts/GameOfLife.json'
compiler_contract_path = 'GameOfLife.json'
deployed_contract_address = os.getenv('contract_address')
contract_json = json.load(open(compiler_contract_path))
contract_abi = contract_json['abi']
deployed_contract_address=Web3.toChecksumAddress(deployed_contract_address)
print('deployed contract address',deployed_contract_address)
contract = web3.eth.contract(
address=deployed_contract_address,
abi=contract_abi,
)
rows = 32
cols = 32
cells = [0 for x in range(rows*cols)]
blocks = 0
# use to this to print the network name to the web app
network_name = os.getenv('network_name')
print('network name',network_name)
@app.route('/<cmd>', methods=['GET','POST','OPTIONS'])
def gameoflife(cmd):
# just extract the full path as the decorator's dont work in a google cloud environment
cmd = request.path
print("cmd",cmd)
if cmd =='/data':
return get_data()
elif cmd in ['/step','/setcells']:
return do_command(cmd)
else:
return jsonify({'unknown cmd':cmd})
def do_command(cmd):
# not sure if this particular bit is needed, but it referenced in the google cloud function docs
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return jsonify({'status':'ok'}), 204, headers
# Set CORS headers for the main request
headers = {
'Access-Control-Allow-Origin': '*'
}
if cmd in ['/step','/setcells']:
pass
else :
return jsonify({"unknown cmd":cmd})
private_key = os.getenv('private_key')
print('private_key',private_key)
acct = Account.from_key(private_key)
caller_address = acct.address
print('caller_address',caller_address)
nonce = web3.eth.getTransactionCount(caller_address)
chainId = int(os.getenv('chain_id'))
gwei = 1_000_000_000
gasPrice = 1 * gwei
caller_balance0 = web3.eth.get_balance(caller_address)
gasPrice = Web3.toWei(1, 'gwei')
myblock = contract.functions.getMyBlock().call()
print('myblock',myblock)
if 'setcells' in cmd:
print("setcells")
data = request.data
print('type',type(data))
print('data',data)
# convert from text to dict
try :
cells = json.loads(data)
print("cells",cells)
except Exception as e:
return jsonify({'status':str(e)})
# convert array into 4 x 256 bit elements
try:
cell4 = generate_cell4(cells['cells'])
print("cell4",cell4)
except Exception as e:
return jsonify({'code':str(e)})
txn = contract.functions.setCells(cell4[0],cell4[1],cell4[2],cell4[3]).buildTransaction(
{
'chainId' : chainId,
'nonce': nonce,
'gasPrice': gasPrice,
}
)
else :
print("starting step..")
# check when the last step was done
# compare to current block
# only execute the step if the time is greater than one minute
myblock = contract.functions.getMyBlock().call()
current = web3.eth.blockNumber
target = myblock + GAP
print(f"current {current} target {target}")
if current < target :
return jsonify({'code':f'current block is {current}, skipping until block {target}'})
txn = contract.functions.step().buildTransaction(
{
'chainId' : chainId,
'nonce': nonce,
'gasPrice': gasPrice,
}
)
print('txn',txn)
signed_txn = web3.eth.account.sign_transaction(
txn,
private_key=private_key
)
try:
result = web3.eth.sendRawTransaction(signed_txn.rawTransaction)
print('result',result)
txn['status'] = 'ok'
except Exception as e:
print(type(e))
print(e)
txn = {'status':str(e)}
response = txn
caller_balance1 = web3.eth.get_balance(caller_address)
response['block'] = web3.eth.blockNumber
response['balance_before'] = caller_balance0
response['balance_after'] = caller_balance1
response['cost'] = caller_balance0 - caller_balance1
response['myblock'] = contract.functions.getMyBlock().call()
return jsonify(response), 200 , headers
# take an array of 4 x uint256 and convert into a linear array of 1s and 0s.
def generate_cells(cell4):
cells = [0 for x in range(rows*cols)]
index = 0
for row in range(rows):
for col in range(cols):
pos = (col + row*cols) % (rows*cols)
i = int(pos / 256)
j = pos % 256
#print(row,col,pos,i,j,cells[i])
if (cell4[i] >> j) & 0x01:
cells[index] = 1
else:
cells[index] = 0
index += 1
return cells
# take an array of 1s and 0s and create an 4 x uint256 array
def generate_cell4(cells):
cell4 = [0,0,0,0]
for row in range(rows):
for col in range(cols):
pos = (col + row*cols) % (rows*cols)
i = int(pos / 256)
j = pos % 256
if cells[pos] == 1:
cell4[i] |= ( 1 << j)
return cell4
def get_data():
# not sure if this particular bit is needed, but it referenced in the google cloud function docs
if request.method == 'OPTIONS':
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '3600'
}
return ('', 204, headers)
# Set CORS headers for the main request
headers = {
'Access-Control-Allow-Origin': '*'
}
cell4 = contract.functions.getCells().call()
print("cells ", cell4)
cells = generate_cells(cell4)
private_key = os.getenv('private_key')
print('private_key',private_key)
acct = Account.from_key(private_key)
caller_address = acct.address
caller_balance = web3.eth.get_balance(caller_address)
# convert balance into ether..
caller_balance = str(web3.fromWei(caller_balance,'ether'))
block = web3.eth.blockNumber
myblock = contract.functions.getMyBlock().call()
response = {
'status' : 'ok',
'network' : network_name,
'block' : block,
'myblock' : myblock,
'caller_address' : caller_address,
'caller_balance' : caller_balance,
'contract_address': deployed_contract_address,
'contract_balance' : web3.eth.get_balance(deployed_contract_address),
'target' : myblock + GAP,
'rows' : rows,
'cols' : cols,
'cells' : cells
}
return jsonify(response), 200 , headers