Skip to content

Commit

Permalink
Enhance PEER_DISCOVERY environment variable
Browse files Browse the repository at this point in the history
It is now tri-state - ON, OFF or SELF.

Fixes #287
  • Loading branch information
Neil Booth committed Oct 11, 2017
1 parent 585e7be commit 81947d7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
12 changes: 9 additions & 3 deletions docs/ENVIRONMENT.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,15 @@ some of this.

* **PEER_DISCOVERY**

If not defined, or non-empty, ElectrumX will occasionally connect to
and verify its network of peer servers. Set to empty to disable
peer discovery.
This environment variable is case-insensitive and defaults to `on`.

If `on`, ElectrumX will occasionally connect to and verify its
network of peer servers.

If `off`, peer discovery is disabled and a hard-coded default list
of servers will be read in and served. If set to `self` then peer
discovery is disabled and the server will only return itself in the
peers list.

* **PEER_ANNOUNCE**

Expand Down
14 changes: 13 additions & 1 deletion server/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
class Env(EnvBase):
'''Wraps environment configuration.'''

# Peer discovery
PD_OFF, PD_SELF, PD_ON = range(3)

def __init__(self):
super().__init__()
self.obsolete(['UTXO_MB', 'HIST_MB', 'NETWORK'])
Expand All @@ -49,7 +52,7 @@ def __init__(self):
self.anon_logs = self.boolean('ANON_LOGS', False)
self.log_sessions = self.integer('LOG_SESSIONS', 3600)
# Peer discovery
self.peer_discovery = self.boolean('PEER_DISCOVERY', True)
self.peer_discovery = self.peer_discovery_enum()
self.peer_announce = self.boolean('PEER_ANNOUNCE', True)
self.force_proxy = self.boolean('FORCE_PROXY', False)
self.tor_proxy_host = self.default('TOR_PROXY_HOST', 'localhost')
Expand Down Expand Up @@ -143,3 +146,12 @@ def port(port_kind):
ssl_port,
'_tor',
)

def peer_discovery_enum(self):
pd = self.default('PEER_DISCOVERY', 'on').strip().lower()
if pd in ('off', ''):
return self.PD_OFF
elif pd == 'self':
return self.PD_SELF
else:
return self.PD_ON
13 changes: 7 additions & 6 deletions server/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,13 @@ def on_peers_subscribe(self, is_tor):
def import_peers(self):
'''Import hard-coded peers from a file or the coin defaults.'''
self.add_peers(self.myselves)
coin_peers = self.env.coin.PEERS

# Add the hard-coded ones
peers = [Peer.from_real_name(real_name, 'coins.py')
for real_name in coin_peers]
self.add_peers(peers, limit=None)
# Add the hard-coded ones unless only returning self
if self.env.peer_discovery != self.env.PD_SELF:
coin_peers = self.env.coin.PEERS
peers = [Peer.from_real_name(real_name, 'coins.py')
for real_name in coin_peers]
self.add_peers(peers, limit=None)

def connect_to_irc(self):
'''Connect to IRC if not disabled.'''
Expand Down Expand Up @@ -459,7 +460,7 @@ async def main_loop(self):
3) Retrying old peers at regular intervals.
'''
self.connect_to_irc()
if not self.env.peer_discovery:
if self.env.peer_discovery != self.env.PD_ON:
self.logger.info('peer discovery is disabled')
return

Expand Down
12 changes: 11 additions & 1 deletion tests/server/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ def test_ANON_LOGS():
assert_boolean('ANON_LOGS', 'anon_logs', False)

def test_PEER_DISCOVERY():
assert_boolean('PEER_DISCOVERY', 'peer_discovery', True)
e = Env()
assert e.peer_discovery == Env.PD_ON
os.environ['PEER_DISCOVERY'] = ' '
e = Env()
assert e.peer_discovery == Env.PD_OFF
os.environ['PEER_DISCOVERY'] = 'ON'
e = Env()
assert e.peer_discovery == Env.PD_ON
os.environ['PEER_DISCOVERY'] = 'self'
e = Env()
assert e.peer_discovery == Env.PD_SELF

def test_PEER_ANNOUNCE():
assert_boolean('PEER_ANNOUNCE', 'peer_announce', True)
Expand Down

0 comments on commit 81947d7

Please sign in to comment.