-
Notifications
You must be signed in to change notification settings - Fork 2
/
sktest_helpers.py
248 lines (191 loc) · 6.99 KB
/
sktest_helpers.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
import json
import os
import pickle
import time
from hexbytes import HexBytes
from web3.auto import w3
from sktest import list_differences, \
LocalStarter, ManualStarter, Node, \
SChain, safe_input_with_timeout
# w3.eth.enable_unaudited_features()
BASE_PORT = 10000
PORT_RANGE = 11
global sktest_exe
sktest_exe = os.getenv("SKTEST_EXE",
"/home/dimalit/skaled/build-no-mp/skaled/skaled")
class HexJsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, HexBytes):
return obj.hex()
return super().default(obj)
def dump_node_state(obj):
return json.dumps(obj, indent=1, cls=HexJsonEncoder)
def create_custom_chain(num_nodes=2, num_accounts=2, empty_blocks=True,
rotate_after_block=-1,
config_file=None, chainID=None, same_ip=False,
run_container=False, bls=False, mtm=False, sync=False, historic=False, **kwargs):
if config_file == None:
config_file = "config_base.json"
dbStorageLimit = -1
# VERY empiric formula %)
# (specially for test_rotation.py test)
if rotate_after_block > 0:
dbStorageLimit = (612+1322)*rotate_after_block*5+2000 # 2k is empirical %)
dbStorageLimit = int(dbStorageLimit)
nodes = []
balances = []
base_ports = [BASE_PORT + PORT_RANGE * i for i in range(num_nodes+historic+sync)]
emptyBlockIntervalMs = kwargs.get('emptyBlockIntervalMs', -1)
for i, port in enumerate(base_ports):
this_is_sync = sync and i==num_nodes
this_is_historic = historic and i==num_nodes+sync
if empty_blocks and emptyBlockIntervalMs < 0:
emptyBlockIntervalMs = 1000
if run_container or same_ip:
node = Node(
bindIP='0.0.0.0',
basePort=port, bls=bls,
sync = this_is_sync,
historic = this_is_historic
)
else:
base_port = 1231
node = Node(bindIP=f'127.0.0.{i+1}', basePort=base_port,
bls=bls,
sync = this_is_sync,
historic = this_is_historic
)
nodes.append(node)
for node in nodes:
assert node.sChain is None
for i in range(num_accounts):
balances.append(str((i + 1) * 1000000000000000000000))
config = None
with open(config_file, "r") as f:
config = json.load(f)
print(f"Loaded {config_file}")
if chainID is None:
chainID = "0x1"
global sktest_exe
#starter = ManualStarter(config)
starter = LocalStarter(sktest_exe, config)
emptyBlockIntervalMs = kwargs.get('emptyBlockIntervalMs', -1)
if empty_blocks and emptyBlockIntervalMs < 0:
emptyBlockIntervalMs = 1000
snapshotIntervalSec = kwargs.get('snapshotIntervalSec', -1)
chain = SChain(nodes, starter, balances,
emptyBlockIntervalMs=emptyBlockIntervalMs, chainID=chainID,
dbStorageLimit=dbStorageLimit, bls=bls, mtm=mtm,
snapshotIntervalSec=snapshotIntervalSec)
return chain
def create_default_chain(num_nodes=2, num_accounts=2, empty_blocks=True,
config_file=None, **kwargs):
run_container = os.getenv('RUN_CONTAINER')
return create_custom_chain(num_nodes, num_accounts, empty_blocks, -1,
config_file,
run_container=run_container, **kwargs)
def create_chain_with_id(num_nodes=2, num_accounts=2, empty_blocks=True,
chain_id=None):
run_container = os.getenv('RUN_CONTAINER')
return create_custom_chain(num_nodes, num_accounts, empty_blocks, -1,
None, chain_id, run_container=run_container)
def load_private_keys(path, password, count=0):
# TODO Exceptions?!
files = os.listdir(path)
res = []
i = 0
for f in files:
fd = open(path+"/"+f)
key_crypted = fd.read()
fd.close()
key_open = w3.eth.account.decrypt(key_crypted, password)
res.append(key_open)
i += 1
if count != 0 and i == count:
break
if i % 10 == 0:
print(f"Loaded {i} of {count} keys")
return res
def count_txns(eth):
sum = 0
for i in range(eth.blockNumber+1):
b = eth.getBlock(i)
n = len(b.transactions)
sum += n
return sum
def generate_or_load_txns(ch, nAcc, nTxns):
transactions = []
file = "transactions_"+str(nAcc)+"_"+str(nTxns)
try:
with open(file, "rb") as fd:
transactions = pickle.load(fd)
print("Loaded transactions from file")
except Exception:
ch.wait_start()
print("Generating txns")
for i in range(nTxns):
acc1 = i % nAcc
acc2 = (i+1) % nAcc
nonce = i // nAcc
print("from=%d nonce=%d %s" % (acc1, nonce, ch.accounts[acc1]))
txn_str = ch.transaction_obj(value=1,
_from=acc1, to=acc2, nonce=nonce)
transactions.append(txn_str)
safe_input_with_timeout("Sending txns - press", 10)
with open(file, "wb") as fd:
pickle.dump(transactions, fd)
return transactions
def wait_for_txns(ch, nTxns, t1=0):
count = 0
from_block = 0
while count < nTxns:
do_not_print = False
while True:
try:
to_block = ch.eth.blockNumber
n = 0
for i in range(from_block, to_block+1):
b = ch.eth.getBlock(i)
n += len(b.transactions)
count += n
from_block = to_block + 1
break
except Exception as e:
if not do_not_print:
print(e)
do_not_print = True
t2 = time.time()
if t2 != t1:
print("%d txns %d blocks" % (count, ch.eth.blockNumber), end=' ')
if t1 > 0:
print("perf = %f tx/sec" % (count / (t2 - t1)), end='')
print()
time.sleep(1)
t2 = time.time()
return t2 - t1
def wait_for_blocks(ch, nBlocks):
t1 = time.time()
count = 0
while count < nBlocks:
while True:
try:
count = ch.eth.blockNumber
break
except Exception as e:
print(e)
t2 = time.time()
if t2 != t1:
print("%d blocks rate = %f blocks/sec" % (count, count/(t2-t1)))
time.sleep(1)
t2 = time.time()
return t2 - t1
def print_states_difference(ch):
nNodes = len(ch.nodes)
states = [ch.state(index) for index in range(nNodes)]
for a_index in range(nNodes):
for b_index in range(a_index + 1, nNodes):
diff = list_differences(states[a_index], states[b_index])
if diff:
print(f'\nDifference between node '
f'#{a_index + 1} and #{b_index + 1}')
print('\n'.join(diff))