forked from drandreaskrueger/chainhammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
executable file
·376 lines (283 loc) · 11 KB
/
send.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python3
"""
@summary: submit many contract.set(arg) transactions to the example contract
@version: v43 (16/December/2018)
@since: 17/April/2018
@organization:
@author: https://github.com/drandreaskrueger
@see: https://github.com/drandreaskrueger/chainhammer for updates
"""
# extend sys.path for imports:
if __name__ == '__main__' and __package__ is None:
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from hammer.config import RPCaddress, ROUTE, PRIVATE_FOR, ABI
################
## Dependencies:
# standard library:
import sys, time, random
from threading import Thread
from queue import Queue
from pprint import pprint
# pypi:
import requests # pip3 install requests
from web3 import Web3, HTTPProvider # pip3 install web3
from web3.utils.abi import filter_by_name, abi_to_signature
from web3.utils.encoding import pad_hex
# chainhammer:
from hammer.config import NUMBER_OF_TRANSACTIONS, PARITY_UNLOCK_EACH_TRANSACTION
from hammer.deploy import loadFromDisk
from hammer.clienttools import web3connection, unlockAccount
##########################
## smart contract related:
def initialize_fromAddress():
"""
initialise contract object from address, stored in disk file by deploy.py
"""
contractAddress, abi = loadFromDisk()
myContract = w3.eth.contract(address=contractAddress,
abi=abi)
return myContract
def contract_set_via_web3(contract, arg, privateFor=PRIVATE_FOR, gas=90000):
"""
call the .set(arg) method, possibly with 'privateFor' tx-property
using the web3 method
"""
txParameters = {'from': w3.eth.defaultAccount,
'gas' : gas}
if privateFor:
txParameters['privateFor'] = privateFor # untested
# pprint (txParameters)
if PARITY_UNLOCK_EACH_TRANSACTION:
unlockAccount()
tx = contract.functions.set( x=arg ).transact(txParameters)
print ("[sent via web3]", end=" ") # TODO: not print this here but at start
tx = w3.toHex(tx)
return tx
def try_contract_set_via_web3(contract, arg=42):
"""
test the above
"""
tx = contract_set_via_web3(contract, arg=arg)
print (tx)
tx_receipt = w3.eth.waitForTransactionReceipt(tx)
storedData = contract.functions.get().call()
print (storedData)
return storedData
## Manually build & submit transaction, i.e. not going though web3
## (the hope of @jpmsam was that this would speed it up)
##
## Note that the data compilation steps are already implemented as
## myContract.functions.myMethod(*args, **kwargs).buildTransaction(transaction)
## but the following bypasses web3.py completely!
def contract_method_ID(methodname, abi):
"""
build the 4 byte ID, from abi & methodname
"""
method_abi = filter_by_name(methodname, abi)
assert(len(method_abi)==1)
method_abi = method_abi[0]
method_signature = abi_to_signature(method_abi)
method_signature_hash_bytes = w3.sha3(text=method_signature)
method_signature_hash_hex = w3.toHex(method_signature_hash_bytes)
method_signature_hash_4bytes = method_signature_hash_hex[0:10]
return method_signature_hash_4bytes
def argument_encoding(contract_method_ID, arg):
"""
concatenate method ID + padded parameter
"""
arg_hex = w3.toHex(arg)
arg_hex_padded = pad_hex ( arg_hex, bit_size=256)
data = contract_method_ID + arg_hex_padded [2:]
return data
def timeit_argument_encoding():
"""
test the above:
'Doing that 10000 times ... took 0.45 seconds'
"""
timer = time.clock()
reps = 10000
for i in range(reps):
method_ID = contract_method_ID("set", ABI)
data = argument_encoding(method_ID, 7)
timer = time.clock() - timer
print (data)
# no need to precalculate, it takes near to no time:
print ("Doing that %d times ... took %.2f seconds" % (reps, timer) )
def contract_set_via_RPC(contract, arg, privateFor=PRIVATE_FOR, gas=90000):
"""
call the .set(arg) method
not going through web3
but directly via RPC
suggestion by @jpmsam
https://github.com/jpmorganchase/quorum/issues/346#issuecomment-382216968
"""
method_ID = contract_method_ID("set", contract.abi) # TODO: make this "set" flexible for any method name
data = argument_encoding(method_ID, arg)
txParameters = {'from': w3.eth.defaultAccount,
'to' : contract.address,
'gas' : w3.toHex(gas),
'data' : data}
if privateFor:
txParameters['privateFor'] = privateFor # untested
method = 'eth_sendTransaction'
payload= {"jsonrpc" : "2.0",
"method" : method,
"params" : [txParameters],
"id" : 1}
headers = {'Content-type' : 'application/json'}
response = requests.post(RPCaddress, json=payload, headers=headers)
# print('raw json response: {}'.format(response.json()))
tx = response.json()['result']
print ("[sent directly via RPC]", end=" ") # TODO: not print this here but at start
return tx
def try_contract_set_via_RPC(contract, steps=3):
"""
test the above, write 3 transactions, and check the storedData
"""
rand = random.randint(1, 100)
for number in range(rand, rand+steps):
tx = contract_set_via_RPC(contract, number)
print ("after set(%d) tx" % number, tx, " the storedData now is", end=" ")
# TODO: wait for receipt!
storedData = contract.functions.get().call()
print (storedData)
# CHOOSE which route to choose (web3 / RPC) depending on constant ROUTE
contract_set = contract_set_via_web3 if ROUTE=="web3" else contract_set_via_RPC
################################################################
###
### benchmarking routines
###
### 0 blocking
### 1 async
### 2 async, queue, can give number of workers
### 3 async, batched (obsolete)
###
################################################################
def many_transactions_consecutive(contract, numTx):
"""
naive approach, blocking --> 15 TPS
"""
print ("send %d transactions, non-async, one after the other:\n" % (numTx))
txs = []
for i in range(numTx):
tx = contract_set(contract, i)
print ("set() transaction submitted: ", tx) # Web3.toHex(tx)) # new web3
txs.append(tx)
return txs
def many_transactions_threaded(contract, numTx):
"""
submit many transactions multi-threaded.
N.B.: 1 thread / transaction
--> machine can run out of threads, then crash
"""
print ("send %d transactions, multi-threaded, one thread per tx:\n" % (numTx))
threads = []
for i in range(numTx):
t = Thread(target = contract_set,
args = (contract, i))
threads.append(t)
print (".", end="")
print ("%d transaction threads created." % len(threads))
for t in threads:
t.start()
print (".", end="")
sys.stdout.flush()
print ("all threads started.")
for t in threads:
t.join()
print ("all threads ended.")
def many_transactions_threaded_Queue(contract, numTx, num_worker_threads=25):
"""
submit many transactions multi-threaded,
with size limited threading Queue
"""
line = "send %d transactions, via multi-threading queue with %d workers:\n"
print (line % (numTx, num_worker_threads))
q = Queue()
def worker():
while True:
item = q.get()
contract_set(contract, item)
print ("T", end=""); sys.stdout.flush()
q.task_done()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
print ("W", end=""); sys.stdout.flush()
print ("\n%d worker threads created." % num_worker_threads)
for i in range(numTx):
q.put (i)
print ("I", end=""); sys.stdout.flush()
print ("\n%d items queued." % numTx)
q.join()
print ("\nall items - done.")
def many_transactions_threaded_in_batches(contract, numTx, batchSize=25):
"""
submit many transactions multi-threaded;
but in batches of rather small numbers.
OBSOLETE <-- not faster than threaded2.
"""
line = "send %d transactions, multi-threaded, one thread per tx, " \
"in batches of %d parallel threads:\n"
print (line % (numTx, batchSize))
howManyLeft=numTx
while howManyLeft>0:
line = "Next batch of %d transactions ... %d left to do"
print (line % (batchSize, howManyLeft))
threads = []
for i in range(batchSize):
t = Thread(target = contract_set,
args = (contract, i))
threads.append(t)
print (".", end="")
print ("\n%d transaction threads created." % len(threads))
for t in threads:
t.start()
print (".", end="")
sys.stdout.flush()
print ("\nall threads started.")
for t in threads:
t.join()
print ("\nall threads ended.")
howManyLeft -= batchSize
###########################################################
###
### choose, depending on CLI parameter
###
###########################################################
def sendmany(numTransactions = NUMBER_OF_TRANSACTIONS):
print("\nBlockNumber = ", w3.eth.blockNumber)
if len(sys.argv)==1 or sys.argv[1]=="sequential":
# blocking, non-async
many_transactions_consecutive(contract, numTransactions)
elif sys.argv[1]=="threaded1":
many_transactions_threaded(contract, numTransactions)
elif sys.argv[1]=="threaded2":
num_workers = 100
if len(sys.argv)>2:
try:
num_workers = int(sys.argv[2])
except:
pass
many_transactions_threaded_Queue(contract,
numTx=numTransactions,
num_worker_threads=num_workers)
elif sys.argv[1]=="threaded3":
batchSize=25
many_transactions_threaded_in_batches(contract,
numTx=numTransactions,
batchSize=batchSize)
else:
print ("Nope. Choice '%s'" % sys.argv[1], "not recognized.")
if __name__ == '__main__':
global w3, NODENAME, NODETYPE, CONSENSUS, NETWORKID, CHAINNAME, CHAINID
w3, chainInfos = web3connection(RPCaddress=RPCaddress, account=None)
NODENAME, NODETYPE, CONSENSUS, NETWORKID, CHAINNAME, CHAINID = chainInfos
w3.eth.defaultAccount = w3.eth.accounts[0] # set first account as sender
# timeit_argument_encoding(); exit()
contract = initialize_fromAddress()
# try_contract_set_via_web3(contract); exit()
# try_contract_set_via_RPC(contract); exit()
sendmany()