client/authority-discovery: Limit number of connections to authorities (#4487)

* client/authority-discovery: Limit number of connections to authorities

Instead of connecting to all sentry nodes of all authorities, with this
patch the authority discovery module does the following:

- Choose one sentry node per authority at random.

- Choose MAX_NUM_AUTHORITY_CONN out of the above at random.

The module uses randomness to prevent hot spots, e.g. all nodes trying
to connect to a single node. If the authority discovery module would
choose the nodes to connect to at random on each new address that it
learns of, the node would go through a lot of connection churn.  Instead
it creates a random seed at start up and uses this seed for its RNG on
each update cycle.

* client/authority-discovery: Extract address cache into own module

* client/authority-discovery/src/addr_cache: Add basic unit tests

* client/authority-discovery: Replace unwrap with expect on [u8] cmp

* .maintain/sentry-node/docker-compose.yml: Prefix endpoint flags

* client/authority-discovery/src/addr_cache: Use sort_unstable and cmp

* client/authority-discovery: Use BTreeMap in addr_cache for sorted iter

To reduce connection churn it is preferrable to have `get_subset` of the
`addr_cache` to return the same result on repeated calls. `get_subset`
iterates a map. To make the process of iteration deterministic, use a
`BTreeMap` instead of a `HashMap`.
This commit is contained in:
Max Inden
2020-01-03 21:40:19 +01:00
committed by Gavin Wood
parent fa1e42a2a1
commit 5cf682cece
7 changed files with 301 additions and 117 deletions
@@ -47,8 +47,8 @@ services:
- "--reserved-nodes"
- "/dns4/sentry-a/tcp/30333/p2p/QmV7EhW6J6KgmNdr558RH1mPx2xGGznW7At4BhXzntRFsi"
# Not only bind to localhost.
- "--ws-external"
- "--rpc-external"
- "--unsafe-ws-external"
- "--unsafe-rpc-external"
# - "--log"
# - "sub-libp2p=trace"
# - "--log"
@@ -88,8 +88,8 @@ services:
- "--rpc-cors"
- "all"
# Not only bind to localhost.
- "--ws-external"
- "--rpc-external"
- "--unsafe-ws-external"
- "--unsafe-rpc-external"
- "--log"
- "sub-authority-discovery=trace"
- "--sentry"
@@ -121,8 +121,8 @@ services:
- "--rpc-cors"
- "all"
# Not only bind to localhost.
- "--ws-external"
- "--rpc-external"
- "--unsafe-ws-external"
- "--unsafe-rpc-external"
- "--log"
- "sub-authority-discovery=trace"