forked from abitmore/bexi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
executable file
·237 lines (180 loc) · 6.52 KB
/
cli.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
#!/usr/bin/env python3
import click
import time
from bexi import Config, factory
from bexi.connection import requires_blockchain
import logging
import threading
import json
from pprint import pprint
from bexi.operation_storage.exceptions import OperationNotFoundException
config = Config.get("wsgi")
@click.group()
def main():
pass
@main.command()
@click.option("--host")
@click.option("--port")
def wsgi(host, port):
host = host or config["host"]
port = port or config["port"]
from bexi.wsgi.app import get_manage_service_app, get_sign_service_app
app = get_manage_service_app()
app = get_sign_service_app(app)
app.logger.info("Starting " + config["name"] + " with manage and sign service ...")
app.run(host=host, port=port, debug=True)
@main.command()
@click.option("--host")
@click.option("--port")
def sign_service(host, port):
host = host or config["host"]
port = port or config["port"]
from bexi.wsgi.app import get_sign_service_app
app = get_sign_service_app()
app.logger.info("Starting " + config["name"] + " sign service ...")
app.run(host=host, port=port)
@main.command()
@click.option("--host")
@click.option("--port")
def manage_service(host, port):
host = host or config["host"]
port = port or config["port"]
from bexi.wsgi.app import get_manage_service_app
app = get_manage_service_app()
app.logger.info("Starting " + config["name"] + " manage service ...")
app.run(host=host, port=port)
@main.command()
@click.option("--host")
@click.option("--port")
def blockchain_monitor_service(host, port):
host = host or config["host"]
port = port or config["port"]
from bexi.wsgi.app import get_blockchain_monitor_service_app
app = get_blockchain_monitor_service_app()
logging.getLogger(__name__).info("Starting BitShares blockchain monitor as coroutines ...")
thr = threading.Thread(target=start_block_monitor, daemon=True)
thr.start() # run in background
app.logger.info("Starting " + config["name"] + " blockchain monitor service ...")
app.run(host=host, port=port)
@main.command()
@click.option("--start")
@click.option("--stop")
def only_blockchain_monitor(start, stop):
Config.load(["config_bitshares_connection.yaml",
"config_bitshares_memo_keys.yaml",
"config_bitshares.yaml",
"config_operation_storage.yaml"])
logging.getLogger(__name__).info("Starting BitShares blockchain monitor ...")
start_block_monitor(start, stop)
@main.command()
@click.option("--host")
@click.option("--port")
def only_blockchain_monitor_service(host, port):
host = host or config["host"]
port = port or config["port"]
from bexi.wsgi.app import get_blockchain_monitor_service_app
app = get_blockchain_monitor_service_app()
app.logger.info("Starting " + config["name"] + " blockchain monitor service ...")
app.run(host=host, port=port)
@requires_blockchain
def start_block_monitor(start=None, stop=None):
from bexi.blockchain_monitor import BlockchainMonitor
retry = True
while (retry):
retry = stop is None
try:
monitor = BlockchainMonitor()
if start is not None:
monitor.start_block = start
if stop is not None:
monitor.stop_block = stop
monitor.listen()
except Exception as e:
logging.getLogger(__name__).info("Blockchain monitor failed, exception below. Retrying after sleep")
logging.getLogger(__name__).exception(e)
time.sleep(1.5)
logging.getLogger(__name__).error("Monitoring done")
@main.command()
@click.option("--txid")
@click.option("--customerid")
@click.option("--contains")
@click.option("--status")
@click.option("--incidentid")
def find(txid, customerid, contains, status, incidentid):
Config.load(["config_bitshares_connection.yaml",
"config_bitshares.yaml",
"config_operation_storage.yaml"])
storage = factory.get_operation_storage()
def get_all():
return (storage.get_operations_completed() +
storage.get_operations_in_progress() +
storage.get_operations_failed())
operations = []
if contains:
for op in get_all():
print(op)
if status is not None and not status == op["status"]:
continue
if contains in str(op):
operations.append(op)
if incidentid:
for op in list(storage._service.query_entities(
storage._operation_tables["incident"])):
if incidentid in str(op):
operations.append(op)
print("---------- finding transfers ---------------")
print("found: " + str(len(operations)))
for op in operations:
pprint(op)
@main.command()
@click.option("--take")
def balance(take=100):
Config.load(["config_bitshares_connection.yaml",
"config_bitshares.yaml",
"config_operation_storage.yaml"])
pprint(factory.get_operation_storage().get_balances(take))
@main.command()
@click.option("--take")
def balance_calc(take=100):
Config.load(["config_bitshares_connection.yaml",
"config_bitshares.yaml",
"config_operation_storage.yaml"])
pprint(factory.get_operation_storage()._get_balances_recalculate(take))
@main.command()
def tracked():
Config.load(["config_bitshares_connection.yaml",
"config_bitshares.yaml",
"config_operation_storage.yaml"])
storage = factory.get_operation_storage()
pprint(list(storage._service.query_entities(storage._azure_config["address_table"] + "balance")))
# @main.command()
# def yaml_to_json():
# os.environ["SettingsUrl"] = None
# os.environ["PrivateKey"] = None
#
# Config.reset()
#
# Config.load("config_common.yaml")
# get_sign_service_app()
# Config.dump_current("config_sign_service.json")
#
# Config.reset()
#
# Config.load("config_common.yaml")
# get_manage_service_app()
# Config.dump_current("config_manage_service.json")
#
# Config.reset()
#
# Config.load("config_common.yaml")
# load_blockchain_monitor_config()
# Config.dump_current("config_blockchain_monitor.json")
#
# Config.reset()
#
# Config.load("config_common.yaml")
# get_blockchain_monitor_service_app()
# Config.dump_current("config_blockchain_monitor_service.json")
#
# print("JSon configuration files for sign_service, manage_service and blockchain_monitor written to dump folder")
main()