forked from pipermerriam/eth-testrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testrpc.py
executable file
·328 lines (250 loc) · 8.68 KB
/
testrpc.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler
from ethereum import tester as t
from rlp.utils import encode_hex
from ethereum import blocks
from ethereum import utils
from ethereum import transactions
from ethereum import processblock
from ethereum.utils import sha3
from ethereum.tester import keys
from ethereum.tester import accounts
from ethereum.tester import languages
from collections import namedtuple
from ethereum import slogging
# Override the SimpleJSONRPCRequestHandler to support access control (*)
class SimpleJSONRPCRequestHandlerWithCORS(SimpleJSONRPCRequestHandler):
def do_OPTIONS(self):
self.send_response(200)
self.end_headers()
# Add these headers to all responses
def end_headers(self):
self.send_header("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept")
self.send_header("Access-Control-Allow-Origin", "*")
SimpleJSONRPCRequestHandler.end_headers(self)
# Ensure tester.py uses the "official" gas limit.
t.gas_limit = 3141592
Snapshot = namedtuple("Snapshot", ["block_number", "data"])
evm = None
snapshots = None
def strip_0x(s):
if s[0] == "0" and s[1] == "x":
s = s[2:]
return s
# Special non-standard function to reset the evm
# state. Useful for automated testing.
def evm_reset():
global evm
global snapshots
print "Resetting EVM state..."
evm = t.state()
snapshots = []
return True
def evm_snapshot():
global evm
global snapshots
print "Creating snapshot #" + str(len(snapshots))
snapshots.append(Snapshot(block_number=evm.block.number, data=evm.snapshot()))
return "0x" + int_to_hex(len(snapshots) - 1)
def evm_revert(index=None):
global evm
global snapshots
if len(snapshots) == 0:
return False
if index != None:
index = int(strip_0x(index), 16)
else:
index = len(snapshots) - 1
print "Reverting snapshot #" + str(index)
if index >= len(snapshots):
return False
snapshot = snapshots[index]
# print str(snapshot.block_number + 1)
# print str(len(evm.blocks))
# Remove all blocks after our saved block number.
del evm.blocks[snapshot.block_number + 1:len(evm.blocks)]
# Revert the evm
evm.revert(snapshot.data)
# Remove all snapshots after and including this one.
del snapshots[index:len(snapshots)]
return True
# init state
evm_reset()
t.set_logging_level(2)
#slogging.configure(':info,eth.pb:debug,eth.vm.exit:trace')
#slogging.configure(':info,eth.vm.exit:debug,eth.pb.tx:info')
print "Ready!"
def isContract(transaction):
if "to" not in transaction and "data" in transaction:
return True
else:
return False
def int_to_hex(int_value):
encoded = format(int_value, 'x')
return encoded.zfill(len(encoded)*2 % 2)
def eth_coinbase():
print 'eth_coinbase'
return '0x' + encode_hex(evm.block.coinbase)
def eth_gasPrice():
print 'eth_gasPrice'
return '0x' + int_to_hex(1)
def eth_blockNumber():
print 'eth_blockNumber'
return '0x' + int_to_hex(evm.block.number)
def send(transaction):
if "from" in transaction:
addr = strip_0x(transaction['from']).decode("hex")
sender = keys[accounts.index(addr)]
else:
sender = keys[0]
if "value" in transaction:
value = transaction['value']
else:
value = 0
if "to" in transaction:
to = strip_0x(transaction['to'])
else:
to = None
if "data" in transaction:
data = strip_0x(transaction['data']).decode("hex")
else:
data = None
if "gas" in transaction:
gas = int(strip_0x(transaction['gas']), 16)
else:
gas = None
# print "value: " + value.encode("hex")
# print "to: " + to
# print "from: " + accounts[keys.index(sender)].encode("hex")
# print "data: " + data.encode("hex")
# print "gas: " + str(gas)
if isContract(transaction):
estimated_cost = len(data.encode("hex")) / 2 * 200
print "Adding contract..."
print "Estimated gas cost: " + str(estimated_cost)
if gas != None and estimated_cost > gas:
print "* "
print "* WARNING: Estimated cost higher than sent gas: " + str(estimated_cost) + " > " + str(gas)
print "* "
r = evm.evm(data, sender, value, gas).encode("hex")
else:
r = evm.send(sender, to, value, data, gas).encode("hex")
r = "0x" + r
return r
# To mimic a real transaction, we return the transaction hash
# instead of the return value of the transaction.
def eth_sendTransaction(transaction):
global evm
print 'eth_sendTransaction'
r = send(transaction)
if not isContract(transaction):
tx = evm.block.transaction_list[-1]
r = "0x" + tx.hash.encode("hex")
evm.mine()
return r
def eth_call(transaction, block_number):
print "eth_call"
snapshot = evm.snapshot()
r = send(transaction)
evm.revert(snapshot)
return r
def eth_accounts():
r = []
for index in range(len(accounts)):
r.append("0x" + accounts[index].encode("hex"))
return r
def eth_getCompilers():
return languages.keys()
def eth_compileSolidity(code):
combined = languages["solidity"].combined(code)
name = combined[len(combined) - 1][0]
contract = combined[len(combined) - 1][1]
val = {}
val[name] = {
"code": '0x' + contract['binary'],
"info": {
"source": code,
"language": "Solidity",
"languageVersion": "0",
"compilerVersion": "0",
"abiDefinition": contract['json-abi'],
"userDoc": {
"methods": {}
},
"developerDoc": {
"methods": {}
}
}
}
return val
# Warning: block.get_code() seems to ignore the block number.
def eth_getCode(address, block_number="latest"):
address = strip_0x(address)
if block_number == "latest" or block_number == "pending":
block_number = len(evm.blocks) - 1
elif block_number == "earliest":
block_number = 0
else:
block_number = int(strip_0x(block_number), 16)
if block_number >= len(evm.blocks):
return None
block = evm.blocks[block_number]
return "0x" + block.get_code(address).encode("hex")
def eth_getTransactionByHash(h):
h = strip_0x(h).decode("hex")
total = len(evm.blocks)
current = total - 1
tx = None
tx_index = 0
block = None
while current >= 0 and tx == None:
block = evm.blocks[current]
tx_index = 0
for tx in block.get_transactions():
tx_index += 1
if tx.hash == h:
break
current -= 1
# Comply with the RPC spec as much as possible.
# We need to return an object with some things as null.
if tx == None:
return {
"hash": "0x" + h.encode("hex")
}
return {
"hash": "0x" + tx.hash.encode("hex"),
"nonce": "0x" + int_to_hex(tx.nonce),
"blockHash": "0x" + block.hash.encode("hex"),
"blockNumber": "0x" + int_to_hex(block.number),
"transactionIndex": "0x" + int_to_hex(tx_index),
"from": "0x" + tx.sender.encode("hex"),
"to": "0x" + tx.to.encode("hex"),
"value": "0x" + int_to_hex(tx.value),
"gas": "0x" + int_to_hex(tx.startgas),
"gasPrice": "0x" + int_to_hex(tx.gasprice),
"input": "0x" + tx.data.encode("hex")
}
def web3_sha3(argument):
print 'web3_sha3'
return '0x' + sha3(argument[2:].decode('hex')).encode('hex')
def web3_clientVersion():
return "Consensys TestRPC/v0.0.1/python"
server = SimpleJSONRPCServer(('localhost', 8545), SimpleJSONRPCRequestHandlerWithCORS)
server.register_function(eth_coinbase, 'eth_coinbase')
server.register_function(eth_accounts, 'eth_accounts')
server.register_function(eth_gasPrice, 'eth_gasPrice')
server.register_function(eth_blockNumber, 'eth_blockNumber')
server.register_function(eth_call, 'eth_call')
server.register_function(eth_sendTransaction, 'eth_sendTransaction')
server.register_function(eth_getCompilers, 'eth_getCompilers')
server.register_function(eth_compileSolidity, 'eth_compileSolidity')
server.register_function(eth_getCode, 'eth_getCode')
server.register_function(eth_getTransactionByHash, 'eth_getTransactionByHash')
server.register_function(web3_sha3, 'web3_sha3')
server.register_function(web3_clientVersion, 'web3_clientVersion')
server.register_function(evm_reset, 'evm_reset')
server.register_function(evm_snapshot, 'evm_snapshot')
server.register_function(evm_revert, 'evm_revert')
server.serve_forever()