-
Notifications
You must be signed in to change notification settings - Fork 1
/
locustfile.py
116 lines (99 loc) · 3.63 KB
/
locustfile.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
import random
from locust import task, between
from locust.contrib.fasthttp import FastHttpUser
from lib import TestEnv
IGNORE_ADDRESSES = {'fee', '__fee__', 'coinbase', 'unknown'}
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
class QuickstartUser(FastHttpUser, TestEnv):
wait_time = between(0.5, 2)
def on_start(self):
"""create user"""
self.create_user()
def on_stop(self):
"""delete user"""
self.delete_user()
@task
def get_blockchains_client(self):
self.client.get(
'/blockchains',
headers=self.client_headers,
name='get_blockchains_client'
)
@task
def get_blockchains_user(self):
self.client.get(
'/blockchains',
headers=self.user_headers,
name='get_blockchains_user'
)
@task
def get_blockchains_testnet_client(self):
self.client.get(
'/blockchains?testnet=true',
headers=self.client_headers,
name='get_blockchains_testnet_client'
)
@task
def get_blockchains_testnet_client(self):
self.client.get(
'/blockchains?testnet=true',
headers=self.user_headers,
name='get_blockchains_testnet_user'
)
@task
def get_verified_currencies_client(self):
self.client.get(
'/currencies?verified=true',
headers=self.client_headers,
name='get_verified_currencies_client'
)
@task
def get_verified_currencies_user(self):
self.client.get(
'/currencies?verified=true',
headers=self.user_headers,
name='get_verified_currencies_user'
)
@task
def sync_bitcoin_mainnet_random_wallet_user(self):
# get the most recent block hash
chain = self.client.get(
'/blockchains/bitcoin-mainnet',
name='/blockchains/[id]',
headers=self.client_headers
).json()
# fetch the block
block = self.client.get(
f'/blocks/bitcoin-mainnet:{chain["verified_block_hash"]}',
name='/blocks/[id]',
headers=self.client_headers
).json()
# fetch 100 random transactions and capture at least 500 addresses
addresses = []
for i in range(100):
txid = random.choice(block['transaction_ids'])
transaction = self.client.get(
f'/transactions/bitcoin-mainnet:{txid}',
name='/transactions/[id]',
headers=self.client_headers,
).json()
for transfer in transaction['_embedded']['transfers']:
if transfer['from_address'] not in IGNORE_ADDRESSES:
addresses.append(transfer['from_address'])
if transfer['to_address'] not in IGNORE_ADDRESSES:
addresses.append(transfer['to_address'])
if len(addresses) > 500:
break
# grab up to 500 addresses and "sync" them from block height - 10,000 blocks
for address_sublist in chunks(addresses[:500], 25):
query = '&address='.join(f'{a}' for a in address_sublist)
start_height = chain['verified_height'] - 10_000
end_height = chain['verified_height']
# TODO: paginate the responses
self.client.get(
f'/transactions?blockchain_id=bitcoin-mainnet&max_page_size=100&start_height={start_height}&end_height={end_height}&address={query}',
name='/transactions&address=[addr]',
headers=self.client_headers
).json()