config: introduce ConfigVars
A new config API is introduced, and ~all of the codebase is adapted to it.
The old API is kept but mainly only for dynamic usage where its extra flexibility is needed.
Using examples, the old config API looked this:
```
>>> config.get("request_expiry", 86400)
604800
>>> config.set_key("request_expiry", 86400)
>>>
```
The new config API instead:
```
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS
604800
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS = 86400
>>>
```
The old API operated on arbitrary string keys, the new one uses
a static ~enum-like list of variables.
With the new API:
- there is a single centralised list of config variables, as opposed to
these being scattered all over
- no more duplication of default values (in the getters)
- there is now some (minimal for now) type-validation/conversion for
the config values
closes https://github.com/spesmilo/electrum/pull/5640
closes https://github.com/spesmilo/electrum/pull/5649
Note: there is yet a third API added here, for certain niche/abstract use-cases,
where we need a reference to the config variable itself.
It should only be used when needed:
```
>>> var = config.cv.WALLET_PAYREQ_EXPIRY_SECONDS
>>> var
<ConfigVarWithConfig key='request_expiry'>
>>> var.get()
604800
>>> var.set(3600)
>>> var.get_default_value()
86400
>>> var.is_set()
True
>>> var.is_modifiable()
True
```
This commit is contained in:
@@ -68,8 +68,6 @@ ca_path = certifi.where()
|
||||
|
||||
BUCKET_NAME_OF_ONION_SERVERS = 'onion'
|
||||
|
||||
MAX_INCOMING_MSG_SIZE = 1_000_000 # in bytes
|
||||
|
||||
_KNOWN_NETWORK_PROTOCOLS = {'t', 's'}
|
||||
PREFERRED_NETWORK_PROTOCOL = 's'
|
||||
assert PREFERRED_NETWORK_PROTOCOL in _KNOWN_NETWORK_PROTOCOLS
|
||||
@@ -216,8 +214,8 @@ class NotificationSession(RPCSession):
|
||||
|
||||
def default_framer(self):
|
||||
# overridden so that max_size can be customized
|
||||
max_size = int(self.interface.network.config.get('network_max_incoming_msg_size',
|
||||
MAX_INCOMING_MSG_SIZE))
|
||||
max_size = self.interface.network.config.NETWORK_MAX_INCOMING_MSG_SIZE
|
||||
assert max_size > 500_000, f"{max_size=} (< 500_000) is too small"
|
||||
return NewlineFramer(max_size=max_size)
|
||||
|
||||
async def close(self, *, force_after: int = None):
|
||||
@@ -604,7 +602,7 @@ class Interface(Logger):
|
||||
|
||||
def _get_expected_fingerprint(self) -> Optional[str]:
|
||||
if self.is_main_server():
|
||||
return self.network.config.get("serverfingerprint")
|
||||
return self.network.config.NETWORK_SERVERFINGERPRINT
|
||||
|
||||
def _verify_certificate_fingerprint(self, certificate):
|
||||
expected_fingerprint = self._get_expected_fingerprint()
|
||||
|
||||
Reference in New Issue
Block a user