-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtxCast_stagger.py
379 lines (328 loc) · 11.6 KB
/
txCast_stagger.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
376
377
378
379
# txCast Staggered broadcast
import time
import requests
import secrets
import json
import getpass
from datetime import datetime
from datetime import timedelta
from stem import Signal
from stem.control import Controller
tx_list = []
failed_tx_list = []
not_in_mempool_tx_list = []
time_list = []
class RPCHost(object):
def __init__(self, url):
self._session = requests.Session()
self._url = url
self._headers = {'content-type': 'application/json'}
def call(self, rpcMethod, *params):
payload = json.dumps({"method": rpcMethod, "params": list(params), "jsonrpc": "2.0"})
tries = 5
hadConnectionFailures = False
while True:
try:
response = self._session.post(self._url, headers=self._headers, data=payload)
except requests.exceptions.ConnectionError:
tries -= 1
if tries == 0:
raise Exception('Failed to connect for remote procedure call.')
hadFailedConnections = True
print("Couldn't connect for remote procedure call, will sleep for five seconds and then try again ({} more tries)".format(tries))
time.sleep(10)
else:
if hadConnectionFailures:
print('Connected for remote procedure call after retry.')
break
if not response.status_code in (200, 500):
raise Exception('RPC connection failure: ' + str(response.status_code) + ' ' + response.reason)
responseJSON = response.json()
if 'error' in responseJSON and responseJSON['error'] != None:
raise Exception('Error in RPC call: ' + str(responseJSON['error']))
return responseJSON['result']
def main():
setup_tor()
setup_endpoint()
setup_node()
build_lists()
process_all()
conclude()
def setup_tor():
tor_ready = False
while tor_ready == False:
configure_tor()
tor_ready = check_tor()
def setup_endpoint():
endpoint_ready = False
while endpoint_ready == False:
configure_endpoint()
endpoint_ready = check_endpoint()
def setup_node():
node_ready = False
while node_ready == False:
configure_node()
node_ready = check_node()
def configure_tor():
while True:
global tor_password
tor_password = getpass.getpass(prompt="Enter tor password (set in .torrc): ")
# Check tor is accessible
try:
renew_tor_ip()
break
except:
print("Could not connect to tor node. Please try again.")
return
def check_tor():
print("\n--- Performing tor check ---")
renew_tor_ip()
ip_tor = get_ip_tor()
ip = get_ip()
print("IP without tor : " + str(ip))
print("IP with tor : " + str(ip_tor))
if ip != ip_tor:
tor_ready = True
else:
tor_ready = False
return tor_ready
def renew_tor_ip():
with Controller.from_port(port=9051) as controller:
controller.authenticate(password=tor_password)
controller.signal(Signal.NEWNYM)
time.sleep(1) # Wait to ensure a new IP is in use
# What if next time is within 1 second?
def get_ip_tor():
session = requests.session()
# TO Request URL with SOCKS over TOR
session.proxies = {}
session.proxies['http']='socks5h://localhost:9050'
session.proxies['https']='socks5h://localhost:9050'
try:
ip_tor = session.get('http://httpbin.org/ip').text
ip_tor = ip_tor.partition('\"origin\": \"')[2]
ip_tor = ip_tor.rpartition('\"')[0] # Get only IP
except Exception as e:
print(str(e))
else:
return ip_tor
def get_ip():
ip = requests.get('http://httpbin.org/ip').text
ip = ip.partition('\"origin\": \"')[2]
ip = ip.rpartition('\"')[0]
return ip
def configure_endpoint():
while True:
global endpoint
print("\n--- Service Selection ---")
service = input("Enter m for mempool.space / b for blockstream.info then press ENTER: ")
if service in ("m","M"):
endpoint = "http://mempoolhqx4isw62xs7abwphsq7ldayuidyx2v2oethdhhj6mlo2r6ad.onion/testnet/api/tx"
print("Endpoint Set: Mempool.Space | " + endpoint)
break
elif service in ("b","B"):
endpoint = "http://explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion/testnet/api/tx"
print("Endpoint Set: Blockstream.Info | " + endpoint)
break
else:
print("Input not recognized. Please try again.")
return
def check_endpoint():
#
# to-do
#
# Check endpoint connects rather than it exists?
try:
endpoint
endpoint_ready = True
except:
endpoint_ready = False
return endpoint_ready
def configure_node():
while True:
global rpcPort
global rpcUser
global rpcPassword
global host
global ownNode
print("\n--- Own Node Config ---")
ownNode = input("Are you running bitcoind on this machine? Enter Y if yes, n press ENTER: ")
if ownNode in ("Y","y"):
ownNode = True
# Get node info
print("\nrpcPort defaults:")
print(" mainnet: 8332")
print(" testnet: 18332")
print(" signet: 38332")
print(" regtest: 18443\n")
rpcPort = input("Enter bitcoin rpcPort (e.g. 8332): ")
rpcUser = input("Enter bitcoin rpcUser : ")
rpcPassword = getpass.getpass(prompt="Enter bitcoin rpcPassword : ")
# Access RPC local server
serverURL = 'http://' + rpcUser + ':' + rpcPassword + '@localhost:' + str(rpcPort)
# Using the class defined in the bitcoin_rpc_class.py
host = RPCHost(serverURL)
break
else:
ownNode = False
print("Not checking local mempool.")
break
return
def check_node():
# If using own node, check node is accessible
if ownNode == True:
try:
host.call('getblockcount')
node_ready = True
except:
print("\nCould not connect to node. Please try again.\n")
node_ready = False
# If not using own node, proceed
else:
node_ready = True
return node_ready
def build_lists():
# Create randomly sorted list of transactions to broadcast:
get_tx_list()
print("\nNumber of Signed Transactions Entered: " + str(len(tx_list)))
secrets.SystemRandom().shuffle(tx_list)
# Create ordered random times at which to broadcast:
print("\n--- Set Delay ---")
while True:
try:
user_input_minutes = int(input('Minutes: '))
#
# to-do
#
# How do cleanly deal with negative values here?
break
except ValueError:
print("Not an integer! Try again.")
while True:
try:
user_input_hours = int(input('Hours : '))
break
except ValueError:
print("Not an integer! Try again.")
while True:
try:
user_input_days = int(input('Days : '))
break
except ValueError:
print("Not an integer! Try again.")
start = datetime.now()
min_delay = timedelta(minutes=0)
min_time = start + min_delay
max_delay = timedelta(minutes=user_input_minutes, hours=user_input_hours, days=user_input_days)
max_time = min_time + max_delay
number_of_times = len(tx_list)
max_duration = max_time - min_time
for i in range(0, number_of_times):
random_time = secrets.SystemRandom().uniform(0, 1) * max_duration
time_list.append(min_time + random_time)
time_list.sort()
# Print list of transactions & target broadcast times
print("\n--- txCast Schedule ---")
for i in range(0, len(tx_list)):
print("Time: " + str(time_list[i]) + " | tx: " + str(tx_list[i])[:20] + "...")
return
def get_tx_list():
global tx_list
print("\n--- Enter Signed Transactions ---")
print("Paste signed transaction (CTRL-SHIFT-V) then press ENTER\n")
finished = False
while not finished:
tx_next = input('tx: ')
if tx_next in ("X","x"):
finished = True
else:
tx_list.append(tx_next)
print("\nPaste next transaction and press ENTER or Type X then press ENTER to END")
return
def process_all():
for i in range(0, len(tx_list)):
print("")
result = process_tx(i)
push_time = result[0]
inMempool = result[1]
valid = result[2]
if valid:
if ownNode:
if inMempool == False:
print("Transaction " + str(i+1) + "not detected in local mempool")
not_in_mempool_tx_list.append(tx_list[i])
else:
print("Transaction seen in local mempool")
else:
print("Transaction " + str(i+1) + "pushed to endpoint at " + str(push_time))
else:
print("Invalid Transaction: " + tx_list[i])
failed_tx_list.append(tx_list[i])
print(str(len(tx_list)-i-1) + " Transactions Remaining")
def process_tx(i):
renew_tor_ip() # Renew tor IP address
# Set broadcast values
next_broadcast_tx = tx_list[i]
next_broadcast_time = time_list[i]
current_time = datetime.now()
if current_time > next_broadcast_time:
time_remaining = next_broadcast_time - next_broadcast_time
else:
time_remaining = next_broadcast_time - current_time
time.sleep(time_remaining.total_seconds())
if ownNode:
# Decode txid from raw transaction (using local node)
try:
valid = host.call('testmempoolaccept', [next_broadcast_tx])[0]['allowed']
txid = host.call('testmempoolaccept', [next_broadcast_tx])[0]['txid']
except:
valid = False
if valid:
push_tx(next_broadcast_tx)
push_time = datetime.now()
# Check if transaction has hit mempool
inMempool = False
attempts = 0
while not inMempool and attempts < 10:
inMempool = check_local_mempool(txid)
time.sleep(6)
attempts += 1
#
# to-do
#
# What if next time is within 6 seconds?
else:
inMempool = False
push_time = "not pushed"
else:
push_tx(next_broadcast_tx)
push_time = datetime.now()
inMempool = False
return push_time, inMempool, valid
def push_tx(payload):
session = requests.session()
# TO Request URL with SOCKS over TOR
session.proxies = {}
session.proxies['http']='socks5h://localhost:9050'
session.proxies['https']='socks5h://localhost:9050'
requests.post(endpoint, data=payload, proxies=session.proxies)
print("Transaction Uploaded")
def check_local_mempool(txid):
try:
# host.call('getrawtransaction', txid)
host.call('getmempoolentry', txid)
inMempool = True
except:
inMempool = False
return inMempool
def conclude():
print("\n############################# TXCAST COMPLETE #############################")
if failed_tx_list:
print("\nThe Following transactions were invalid:")
for tx in failed_tx_list:
print(tx)
if not_in_mempool_tx_list:
print("\nThe Following transactions were broadcast but were not seen in local mempool...")
for tx in not_in_mempool_tx_list:
print(tx)
main()