-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit-test.py
More file actions
221 lines (184 loc) · 7.69 KB
/
unit-test.py
File metadata and controls
221 lines (184 loc) · 7.69 KB
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
#!/usr/bin/python3
"""
This program initializes two liquid clients
Instructions to setup:
1. run otctradetool/tools/set_env.sh - (alias: set_env)
2. run star_liquid_instances.sh - (alias: l1d)
3. python3 unit-test.py
"""
import os
import json
import subprocess
import platform
import warnings
import unittest
from bitcoinrpc.authproxy import AuthServiceProxy
import http.client
import logging
l1 = None # bitcoin rpc auth pointer, client 1
l2 = None
# User define following params
node1_datadir = '/home/casa/liquiddir1/'
node2_datadir = '/home/casa/liquiddir2/'
class Node():
def __init__(self, datadir, name):
self.datadir = datadir
self.name = name
self.conf = {}
self.cli = None
self.start_daemon()
def start_daemon(self):
"""
Starts liquidd as subprocess, on data dir given by global liq_datadir
"""
self.load_conf(self.datadir + 'liquid.conf')
command = 'liquidd -datadir='+self.datadir
logging.info('Running: '+command)
subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
self.cli = self.get_rpc_connection(self.conf['rpcuser'],
self.conf['rpcpassword'],
self.conf['rpcport'])
def load_conf(self, filename):
"""Loads liquid.conf file into a dictionary"""
with open(filename) as f:
for line in f:
if len(line) == 0 or line[0] == '#' or len(line.split('=')) != 2:
continue
self.conf[line.split('=')[0]] = line.split('=')[1].strip()
self.conf['filename'] = filename
@staticmethod
def wait4sync(c1, c2):
while (c1.getblockchaininfo()['bestblockhash'] !=
c2.getblockchaininfo()['bestblockhash']):
continue
return
@staticmethod
def get_new_rpc_connection(user, password, port):
"""
Establish a new RPC connection to a Liquid node.
"""
connection = AuthServiceProxy('http://{}:{}@localhost:{}'.format(user,
password,
port))
return connection
@staticmethod
def get_rpc_connection(user=None, password=None, port=None):
"""
Wrap the connection object with a re-connect retryable. Also, don't
re-create a connection object if one already exists.
"""
global _connection
global CONNECTION_PARAMS
if password is None and CONNECTION_PARAMS is None:
Node.configure_with_liquid_magic()
# Setup an initial connection using (user, pass, port) as parameters.
# Only do so if one has not already been created.
if user is None and password is None and port is None:
# Only create a connection object if one does not already exist.
if _connection is not None:
return _connection
else:
connection = Node.get_new_rpc_connection(*CONNECTION_PARAMS)
else:
connection = Node.get_new_rpc_connection(user, password, port)
CONNECTION_PARAMS = (user, password, port)
original_call = connection.__class__.__call__
def custom_retryable_call(*args, retries_remaining=3, **kwargs):
"""
Override some RPC connection code in the library and retry an RPC
command if the connection was stale.
"""
# don't loop forever
if retries_remaining == 1:
return original_call(*args, **kwargs)
try:
# execute over RPC or fail here
return original_call(*args, **kwargs)
except (BrokenPipeError, http.client.CannotSendRequest) as exception:
# create a new connection
args[0]._AuthServiceProxy__conn = http.client.HTTPConnection(connection._AuthServiceProxy__url.hostname,
connection._AuthServiceProxy__url.port,
connection._AuthServiceProxy__timeout)
# try again
return custom_retryable_call(*args, retries_remaining=retries_remaining-1, **kwargs)
# monkeypatch the class
connection.__class__.__call__ = custom_retryable_call
# save as global
_connection = connection
return connection
@staticmethod
def configure_with_liquid_magic():
"""
Attempt to guess the location of the Liquid config file and read the liquid
config file for RPC port and other information.
"""
if platform.system() == "Darwin":
conf_file = os.path.expanduser("~/Library/Application Support/Liquid/")
elif platform.system() == "Windows":
conf_file = os.path.join(os.environ["APPDATA"], "Liquid")
else:
conf_file = os.path.expanduser("~/.liquid")
conf_file = os.path.join(conf_file, "liquid.conf")
conf = {}
try:
with open(conf_file, "r") as fd:
for line in fd.readlines():
if "#" in line:
# trim line
line = line[:line.index('#')]
if "=" not in line:
continue
k, v = line.split("=", 1)
conf[k.strip()] = v.strip()
except FileNotFoundError:
raise Exception("Liquid configuration file not found.")
rpcport = int(conf.get("rpcport", 7040))
rpcuser = conf.get("rpcuser", "") # Bitcoin Core accepts empty rpcuser
rpcpassword = conf.get("rpcpassword", None)
rpchost = conf.get("rpcconnect", "localhost")
if rpcpassword is None:
raise Exception("Liquid config file does not specify rpcpassword. "+
"Can't connect to Liquid node.")
global CONNECTION_PARAMS
CONNECTION_PARAMS = (rpcuser, rpcpassword, rpcport)
return CONNECTION_PARAMS
class TestTrade(unittest.TestCase):
global l1, l2
def setUp(self):
global node1_datadir, node2_datadir
warnings.simplefilter('ignore', ResourceWarning)
logging_setup("experiments", "DEBUG")
l1 = Node(node1_datadir, "proposer")
l2 = Node(node2_datadir, "respondent")
def test(self):
self.assertEqual(1, 1)
def tearDown(self):
logging.info('Tearing down test environment...')
def logging_setup(filename, level):
"""Sets logging to file and std. errror"""
global l1, l2
logFormatter = logging.Formatter("%(asctime)s [%(funcName)-6.6s]"
"[%(levelname)-8.8s]: %(message)s")
rootLogger = logging.getLogger()
if level is "DEBUG":
rootLogger.setLevel(logging.DEBUG)
else:
rootLogger.setLevel(logging.INFO)
# Debugging - list of all logging modules
# for key in logging.Logger.manager.loggerDict:
# print("Logging module: ",key)
# Setting logging levels per module
# urlib3 = logging.getLogger('urllib3')
# urlib3.setLevel(logging.WARNING)
bitcoin_rpc = logging.getLogger('BitcoinRPC')
bitcoin_rpc.setLevel(logging.WARNING)
# Setting up stream handlers
logPath = "."
fileHandler = logging.FileHandler("{0}/{1}.log".format(logPath, filename))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
if __name__ == '__main__':
unittest.main()