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:
@@ -24,7 +24,7 @@
|
||||
# SOFTWARE.
|
||||
from collections import defaultdict
|
||||
from math import floor, log10
|
||||
from typing import NamedTuple, List, Callable, Sequence, Union, Dict, Tuple, Mapping, Type
|
||||
from typing import NamedTuple, List, Callable, Sequence, Union, Dict, Tuple, Mapping, Type, TYPE_CHECKING
|
||||
from decimal import Decimal
|
||||
|
||||
from .bitcoin import sha256, COIN, is_address
|
||||
@@ -32,6 +32,9 @@ from .transaction import Transaction, TxOutput, PartialTransaction, PartialTxInp
|
||||
from .util import NotEnoughFunds
|
||||
from .logging import Logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .simple_config import SimpleConfig
|
||||
|
||||
|
||||
# A simple deterministic PRNG. Used to deterministically shuffle a
|
||||
# set of coins - the same set of coins should produce the same output.
|
||||
@@ -484,13 +487,13 @@ COIN_CHOOSERS = {
|
||||
'Privacy': CoinChooserPrivacy,
|
||||
} # type: Mapping[str, Type[CoinChooserBase]]
|
||||
|
||||
def get_name(config):
|
||||
kind = config.get('coin_chooser')
|
||||
def get_name(config: 'SimpleConfig') -> str:
|
||||
kind = config.WALLET_COIN_CHOOSER_POLICY
|
||||
if kind not in COIN_CHOOSERS:
|
||||
kind = 'Privacy'
|
||||
kind = config.cv.WALLET_COIN_CHOOSER_POLICY.get_default_value()
|
||||
return kind
|
||||
|
||||
def get_coin_chooser(config) -> CoinChooserBase:
|
||||
def get_coin_chooser(config: 'SimpleConfig') -> CoinChooserBase:
|
||||
klass = COIN_CHOOSERS[get_name(config)]
|
||||
# note: we enable enable_output_value_rounding by default as
|
||||
# - for sacrificing a few satoshis
|
||||
@@ -498,6 +501,6 @@ def get_coin_chooser(config) -> CoinChooserBase:
|
||||
# + it also helps the network as a whole as fees will become noisier
|
||||
# (trying to counter the heuristic that "whole integer sat/byte feerates" are common)
|
||||
coinchooser = klass(
|
||||
enable_output_value_rounding=config.get('coin_chooser_output_rounding', True),
|
||||
enable_output_value_rounding=config.WALLET_COIN_CHOOSER_OUTPUT_ROUNDING,
|
||||
)
|
||||
return coinchooser
|
||||
|
||||
Reference in New Issue
Block a user