forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitcoin_console.py
93 lines (79 loc) · 2.47 KB
/
bitcoin_console.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
import os
import json
import time
import re
import readline
datadir = ''
# Main logger node path
if os.path.exists(f'/home/{os.getlogin()}/BitcoinFullLedger/blocks'):
datadir = f' -datadir=/home/{os.getlogin()}/BitcoinFullLedger'
elif os.path.exists('/media/sf_Bitcoin/blocks'):
datadir = ' -datadir=/media/sf_Bitcoin'
elif os.path.exists(f'/media/{os.getlogin()}/BITCOIN/blocks'):
datadir = f' -datadir=/media/{os.getlogin()}/BITCOIN'
elif os.path.exists(f'/media/{os.getlogin()}/Blockchains/Bitcoin/blocks'):
datadir = f' -datadir=/media/{os.getlogin()}/Blockchains/Bitcoin'
elif os.path.exists(f'/media/{os.getlogin()}/Long Term Storage/Bitcoin/blocks'):
datadir = f' -datadir=/media/{os.getlogin()}/Long Term Storage/Bitcoin'
def bitcoin(cmd):
return os.popen(f'src/bitcoin-cli{datadir} {cmd}').read()
def console(width):
os.system('clear')
print('-' * width)
print('Bitcoin Console'.center(width))
print('-' * width)
count = 1
lastCmd = ''
while True:
numTimes = 1
cmd = input(str(count).ljust(4) + '> ')
cmds = re.split(r'\s*\*\s*(?=[0-9]+$)', cmd)
if len(cmds) == 2: # Add command multiplier
cmd = cmds[0]
numTimes = int(cmds[1])
if len(cmd) == 0:
cmd = lastCmd
elif cmd == 'clear':
os.system('clear')
continue
elif str(cmd).endswith('*'): # Infinite loop
cmd = str(cmd[:-1])
print(cmd)
while True:
bitcoin(cmd)
return
else:
cmd = str(cmd)
lastCmd = cmd
count += 1
print()
internalTime = 0
externalTime = 0
for i in range(numTimes):
# Internal
t1 = time.perf_counter()
output = bitcoin(cmd)
t2 = time.perf_counter()
externalTime += t2 - t1
# External
t = re.search(r'That took ([0-9\.]+) clocks', output)
if t == None: # Support different timer formats (send, mine)
t = re.search(r'"Elapsed time \(seconds\)": ([0-9\.]+)', output)
if t != None:
internalTime += float(t.group(1))
internalTime /= numTimes
externalTime /= numTimes
if numTimes == 1:
print(output)
print(f'That took {externalTime} seconds (external).')
else:
if t != None:
# Remove the built in "That took ... clocks"
print('\n'.join(map(str, output.split('\n')[:-2])))
else:
print(output)
print(f'Number of samples: {numTimes}.')
print(f'Average time (Internal Bitcoin): {internalTime} clocks.')
print(f'Average time (External Console): {externalTime} seconds.')
print('-' * width)
console(80)