mirror of https://github.com/nucypher/nucypher.git
AliceConfiguration file I/O and character production with policy defaults
parent
c527158e4a
commit
ad8bb56cff
|
@ -109,71 +109,36 @@ class AliceConfiguration(NodeConfiguration):
|
||||||
DEFAULT_CONFIG_FILE_LOCATION = os.path.join(DEFAULT_CONFIG_ROOT, CONFIG_FILENAME)
|
DEFAULT_CONFIG_FILE_LOCATION = os.path.join(DEFAULT_CONFIG_ROOT, CONFIG_FILENAME)
|
||||||
DEFAULT_REST_PORT = 8151
|
DEFAULT_REST_PORT = 8151
|
||||||
|
|
||||||
|
# TODO: Best (Sane) Defaults
|
||||||
DEFAULT_M = 2
|
DEFAULT_M = 2
|
||||||
DEFAULT_N = 3
|
DEFAULT_N = 3
|
||||||
DEFAULT_RATE = int(1e14) # wei # TODO: Best Sane Default
|
DEFAULT_RATE = int(1e14) # wei
|
||||||
DEFAULT_FIRST_PERIOD_RATE = DEFAULT_RATE * 0.25 # TODO:
|
DEFAULT_FIRST_PERIOD_RATE = 0.25 # of rate
|
||||||
DEFAULT_EXPIRATION = datetime.timedelta(days=3) # TODO:
|
DEFAULT_DURATION = 3 # periods
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
m: int = None,
|
m: int = None,
|
||||||
n: int = None,
|
n: int = None,
|
||||||
rate: int = None,
|
rate: int = None,
|
||||||
first_period_rate: float = None,
|
first_period_rate: float = None,
|
||||||
|
policy_duration = None,
|
||||||
*args, **kwargs):
|
*args, **kwargs):
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.m = m
|
self.m = m or self.DEFAULT_M
|
||||||
self.n = n
|
self.n = n or self.DEFAULT_N
|
||||||
self.rate = rate
|
self.rate = rate or self.DEFAULT_RATE
|
||||||
self.first_period_rate = first_period_rate
|
self.first_period_rate = first_period_rate or self.DEFAULT_FIRST_PERIOD_RATE
|
||||||
|
self.policy_duration = policy_duration or self.DEFAULT_DURATION
|
||||||
|
|
||||||
@classmethod
|
@property
|
||||||
def from_configuration_file(cls,
|
def static_payload(self) -> dict:
|
||||||
filepath: str = None,
|
payload = dict(m=self.m,
|
||||||
provider_process=None,
|
n=self.n,
|
||||||
**overrides) -> 'NodeConfiguration':
|
rate=self.rate,
|
||||||
|
first_period_rate=self.first_period_rate,
|
||||||
"""Initialize a NodeConfiguration from a JSON file."""
|
duration=self.policy_duration)
|
||||||
|
return {**super().static_payload, **payload}
|
||||||
payload = cls.get_configuration_payload(filepath=filepath, **overrides)
|
|
||||||
|
|
||||||
# Instantiate from merged params
|
|
||||||
node_configuration = cls(config_file_location=filepath,
|
|
||||||
provider_process=provider_process,
|
|
||||||
**payload)
|
|
||||||
|
|
||||||
return node_configuration
|
|
||||||
|
|
||||||
def to_configuration_file(self, filepath: str = None) -> str:
|
|
||||||
"""Write the static_payload to a JSON file."""
|
|
||||||
if not filepath:
|
|
||||||
filename = f'{self._NAME.lower()}{self._NAME.lower(), }' # FIXME: Support multiple Alice configs
|
|
||||||
filepath = os.path.join(self.config_root, filename)
|
|
||||||
|
|
||||||
if os.path.isfile(filepath):
|
|
||||||
# Avoid overriding an existing default configuration
|
|
||||||
filename = f'{self._NAME.lower()}-{self.checksum_public_address[:6]}{self.__CONFIG_FILE_EXT}'
|
|
||||||
filepath = os.path.join(self.config_root, filename)
|
|
||||||
|
|
||||||
payload = self.static_payload
|
|
||||||
del payload['is_me']
|
|
||||||
|
|
||||||
# Serialize domains
|
|
||||||
domains = list(str(domain) for domain in self.domains)
|
|
||||||
|
|
||||||
# Save node connection data
|
|
||||||
payload.update(dict(
|
|
||||||
node_storage=self.node_storage.payload(),
|
|
||||||
domains=domains,
|
|
||||||
m=self.m,
|
|
||||||
n=self.n,
|
|
||||||
rate=self.rate,
|
|
||||||
first_period_rate=self.first_period_rate
|
|
||||||
))
|
|
||||||
|
|
||||||
with open(filepath, 'w') as config_file:
|
|
||||||
config_file.write(json.dumps(payload, indent=4))
|
|
||||||
return filepath
|
|
||||||
|
|
||||||
def write_keyring(self, password: str, **generation_kwargs) -> NucypherKeyring:
|
def write_keyring(self, password: str, **generation_kwargs) -> NucypherKeyring:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue