Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 87da5cd

Browse files
committed
Allow multiple libbitcoin servers in the config file
1 parent eb51164 commit 87da5cd

File tree

4 files changed

+79
-51
lines changed

4 files changed

+79
-51
lines changed

api/restapi.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from twisted.internet import defer, reactor, task
1818
from twisted.protocols.basic import FileSender
1919

20-
from config import DATA_FOLDER, RESOLVER, LIBBITCOIN_SERVER_TESTNET, LIBBITCOIN_SERVER, \
21-
set_value, get_value, str_to_bool, TRANSACTION_FEE
20+
from config import DATA_FOLDER, RESOLVER, delete_value, set_value, get_value, str_to_bool, TRANSACTION_FEE
2221
from protos.countries import CountryCode
2322
from protos import objects
2423
from keys import blockchainid
@@ -820,17 +819,16 @@ def set_settings(self, request):
820819
settings = self.db.settings
821820
resolver = RESOLVER if "resolver" not in request.args or request.args["resolver"][0] == "" \
822821
else request.args["resolver"][0]
823-
if self.protocol.testnet:
824-
libbitcoin_server = LIBBITCOIN_SERVER_TESTNET if "libbitcoin_server" not in request.args \
825-
or request.args["libbitcoin_server"][0] == "" else request.args["libbitcoin_server"][0]
822+
if "libbitcoin_server" in request.args and request.args["libbitcoin_server"][0] != "":
823+
if self.protocol.testnet:
824+
set_value("LIBBITCOIN_SERVERS_TESTNET", "testnet_server_custom", request.args["libbitcoin_server"][0])
825+
else:
826+
set_value("LIBBITCOIN_SERVERS", "server_custom", request.args["libbitcoin_server"][0])
826827
else:
827-
libbitcoin_server = LIBBITCOIN_SERVER if "libbitcoin_server" not in request.args \
828-
or request.args["libbitcoin_server"][0] == "" else request.args["libbitcoin_server"][0]
829-
830-
if self.protocol.testnet and libbitcoin_server != get_value("CONSTANTS", "LIBBITCOIN_SERVER_TESTNET"):
831-
set_value("CONSTANTS", "LIBBITCOIN_SERVER_TESTNET", libbitcoin_server)
832-
elif not self.protocol.testnet and libbitcoin_server != get_value("CONSTANTS", "LIBBITCOIN_SERVER"):
833-
set_value("CONSTANTS", "LIBBITCOIN_SERVER_TESTNET", libbitcoin_server)
828+
if self.protocol.testnet:
829+
delete_value("LIBBITCOIN_SERVERS_TESTNET", "testnet_server_custom")
830+
else:
831+
delete_value("LIBBITCOIN_SERVERS", "server_custom")
834832
if resolver != get_value("CONSTANTS", "RESOLVER"):
835833
set_value("CONSTANTS", "RESOLVER", resolver)
836834

@@ -888,8 +886,8 @@ def get_settings(self, request):
888886
"shipping_addresses": json.loads(settings[7]),
889887
"blocked_guids": json.loads(settings[8]),
890888
"libbitcoin_server": get_value(
891-
"CONSTANTS", "LIBBITCOIN_SERVER_TESTNET")if self.protocol.testnet else get_value(
892-
"CONSTANTS", "LIBBITCOIN_SERVER"),
889+
"LIBBITCOIN_SERVERS_TESTNET", "testnet_server_custom")if self.protocol.testnet else get_value(
890+
"LIBBITCOIN_SERVERS", "server_custom"),
893891
"seed": KeyChain(self.db).signing_key.encode(encoder=nacl.encoding.HexEncoder),
894892
"terms_conditions": "" if settings[9] is None else settings[9],
895893
"refund_policy": "" if settings[10] is None else settings[10],

config.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
'ksize': '20',
2828
'alpha': '3',
2929
'transaction_fee': '10000',
30-
'libbitcoin_server': 'tcp://libbitcoin1.openbazaar.org:9091',
31-
'libbitcoin_server_testnet': 'tcp://libbitcoin2.openbazaar.org:9091',
30+
'libbitcoin_servers': 'tcp://libbitcoin1.openbazaar.org:9091,None',
31+
'libbitcoin_servers_testnet': 'tcp://libbitcoin2.openbazaar.org:9091, <Z&{.=LJSPySefIKgCu99w.L%b^6VvuVp0+pbnOM',
3232
'resolver': 'http://resolver.onename.com/',
3333
'ssl_cert': None,
3434
'ssl_key': None,
@@ -137,18 +137,21 @@ def _validate_key(key):
137137
return True
138138

139139

140-
def _is_seed_tuple(tup):
140+
def _is_tuple(tup, key):
141141
if isinstance(tup, tuple):
142-
return 'seed' in tup[0]
142+
return key in tup[0]
143143

144144
return False
145145

146146

147-
def _tuple_from_seed_string(string):
147+
def _tuple_from_string(string):
148148
'''
149149
Accepts well formed seed string, returns tuple (url:port, key)
150150
'''
151-
return tuple(string.split(','))
151+
l = string.split(',')
152+
if len(l) == 1:
153+
l.append(None)
154+
return tuple(l)
152155

153156

154157
cfg = ConfigParser(DEFAULTS)
@@ -162,27 +165,53 @@ def _tuple_from_seed_string(string):
162165
KSIZE = int(cfg.get('CONSTANTS', 'KSIZE'))
163166
ALPHA = int(cfg.get('CONSTANTS', 'ALPHA'))
164167
TRANSACTION_FEE = int(cfg.get('CONSTANTS', 'TRANSACTION_FEE'))
165-
LIBBITCOIN_SERVER = cfg.get('CONSTANTS', 'LIBBITCOIN_SERVER')
166-
LIBBITCOIN_SERVER_TESTNET = cfg.get('CONSTANTS', 'LIBBITCOIN_SERVER_TESTNET')
167168
RESOLVER = cfg.get('CONSTANTS', 'RESOLVER')
168169
SSL = str_to_bool(cfg.get('AUTHENTICATION', 'SSL'))
169170
SSL_CERT = cfg.get('AUTHENTICATION', 'SSL_CERT')
170171
SSL_KEY = cfg.get('AUTHENTICATION', 'SSL_KEY')
171172
USERNAME = cfg.get('AUTHENTICATION', 'USERNAME')
172173
PASSWORD = cfg.get('AUTHENTICATION', 'PASSWORD')
174+
LIBBITCOIN_SERVERS = []
175+
LIBBITCOIN_SERVERS_TESTNET = []
173176
SEEDS = []
174177

175178
items = cfg.items('SEEDS') # this also includes items in DEFAULTS
176179
for item in items:
177-
if _is_seed_tuple(item):
180+
if _is_tuple(item, "seed"):
178181
seed = item[1]
179182
if _is_well_formed_seed_string(seed):
180-
new_seed = _tuple_from_seed_string(seed)
183+
new_seed = _tuple_from_string(seed)
181184
if new_seed not in SEEDS:
182185
SEEDS.append(new_seed)
183186
else:
184187
print 'Warning: please check your configuration file: %s' % seed
185188

189+
items = cfg.items('LIBBITCOIN_SERVERS') # this also includes items in DEFAULTS
190+
for item in items:
191+
if _is_tuple(item, "server"):
192+
server = item[1]
193+
new_server = _tuple_from_string(server)
194+
if item[0] == "server_custom":
195+
LIBBITCOIN_SERVERS = [new_server]
196+
break
197+
elif new_server not in LIBBITCOIN_SERVERS:
198+
LIBBITCOIN_SERVERS.append(new_server)
199+
else:
200+
print 'Warning: please check your configuration file: %s' % server
201+
202+
items = cfg.items('LIBBITCOIN_SERVERS_TESTNET') # this also includes items in DEFAULTS
203+
for item in items:
204+
if _is_tuple(item, "testnet_server"):
205+
server = item[1]
206+
new_server = _tuple_from_string(server)
207+
if item[0] == "testnet_server_custom":
208+
LIBBITCOIN_SERVERS_TESTNET = [new_server]
209+
break
210+
elif new_server not in LIBBITCOIN_SERVERS_TESTNET:
211+
LIBBITCOIN_SERVERS_TESTNET.append(new_server)
212+
else:
213+
print 'Warning: please check your configuration file: %s' % server
214+
186215

187216
def set_value(section, name, value):
188217
config = ConfigParser()
@@ -197,8 +226,19 @@ def get_value(section, name):
197226
config = ConfigParser()
198227
if isfile(CONFIG_FILE):
199228
config.read(CONFIG_FILE)
200-
return config.get(section, name)
229+
try:
230+
return config.get(section, name)
231+
except Exception:
232+
return None
233+
201234

235+
def delete_value(section, name):
236+
config = ConfigParser()
237+
if isfile(CONFIG_FILE):
238+
config.read(CONFIG_FILE)
239+
config.remove_option(section, name)
240+
with open(CONFIG_FILE, 'wb') as configfile:
241+
config.write(configfile)
202242

203243
if __name__ == '__main__':
204244

@@ -221,9 +261,9 @@ def test_is_seed_tuple():
221261
good = ('seed.openbazaar.org:8080', '5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117')
222262
bad_not_tuple = 'seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117'
223263
bad_not_seed_tuple = ('aoioai', 'aoioai')
224-
assert _is_seed_tuple(good)
225-
assert not _is_seed_tuple(bad_not_tuple)
226-
assert not _is_seed_tuple(bad_not_seed_tuple)
264+
assert _is_tuple(good)
265+
assert not _is_tuple(bad_not_tuple)
266+
assert not _is_tuple(bad_not_seed_tuple)
227267

228268

229269
_is_linux()

ob.cfg

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
[CONSTANTS]
2+
ksize = 20
3+
alpha = 3
4+
transaction_fee = 15000
5+
resolver = https://resolver.onename.com/
26

3-
#DATA_FOLDER = /path/to/OpenBazaar/
7+
[LIBBITCOIN_SERVERS]
8+
server1 = tcp://libbitcoin1.openbazaar.org:9091
49

5-
KSIZE = 20
6-
ALPHA = 3
7-
8-
TRANSACTION_FEE = 15000
9-
10-
LIBBITCOIN_SERVER = tcp://libbitcoin1.openbazaar.org:9091
11-
LIBBITCOIN_SERVER_TESTNET = tcp://libbitcoin2.openbazaar.org:9091
12-
13-
RESOLVER = https://resolver.onename.com/
10+
[LIBBITCOIN_SERVERS_TESTNET]
11+
testnet_server2 = tcp://libbitcoin2.openbazaar.org:9091,<Z&{.=LJSPySefIKgCu99w.L%b^6VvuVp0+pbnOM
1412

1513
[AUTHENTICATION]
1614

17-
#SSL = False
18-
19-
#SSL_CERT = /path/to/certificate.crt
20-
#SSL_KEY = /path/to/privkey.key
21-
22-
#USERNAME = username
23-
#PASSWORD = password
24-
2515
[SEEDS]
16+
seed1 = seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117
2617

27-
SEED1 = seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117

openbazaard.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import time
1010
from api.ws import WSFactory, AuthenticatedWebSocketProtocol, AuthenticatedWebSocketFactory
1111
from api.restapi import RestAPI
12-
from config import DATA_FOLDER, KSIZE, ALPHA, LIBBITCOIN_SERVER,\
13-
LIBBITCOIN_SERVER_TESTNET, SSL_KEY, SSL_CERT, SEEDS, SSL
12+
from config import DATA_FOLDER, KSIZE, ALPHA, LIBBITCOIN_SERVERS,\
13+
LIBBITCOIN_SERVERS_TESTNET, SSL_KEY, SSL_CERT, SEEDS, SSL
1414
from daemon import Daemon
1515
from db.datastore import Database
1616
from dht.network import Server
@@ -135,9 +135,9 @@ def on_bootstrap_complete(resp):
135135

136136
# blockchain
137137
if TESTNET:
138-
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVER_TESTNET, log=Logger(service="LibbitcoinClient"))
138+
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVERS_TESTNET, log=Logger(service="LibbitcoinClient"))
139139
else:
140-
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVER, log=Logger(service="LibbitcoinClient"))
140+
libbitcoin_client = LibbitcoinClient(LIBBITCOIN_SERVERS, log=Logger(service="LibbitcoinClient"))
141141
heartbeat_server.libbitcoin = libbitcoin_client
142142

143143
# listeners

0 commit comments

Comments
 (0)