Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
server_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
4 #
5 # SPDX-License-Identifier: LGPL-3.0+
6 
7 from __future__ import annotations
8 from .server_configuration import JsonDict, Configuration
9 from .contributor_list import ContributorList
10 from typing import cast
11 import json
12 
13 
15  """
16  Current state of the server
17  """
18  def __init__(
19  self,
20  next_contributor_index: int,
21  num_contributors: int,
22  next_contributor_deadline: float):
23  self.next_contributor_index: int = next_contributor_index
24  self.num_contributors: int = num_contributors
25  self.next_contributor_deadline: float = next_contributor_deadline
26  assert self.num_contributors != 0
27 
28  def to_json(self) -> str:
29  return json.dumps(self._to_json_dict())
30 
31  @staticmethod
32  def from_json(state_json: str) -> ServerState:
33  return ServerState._from_json_dict(
34  json.loads(state_json))
35 
36  def have_all_contributions(self) -> bool:
37  """
38  returns True if all contributions have been received
39  """
40  return self.num_contributors <= self.next_contributor_index
41 
42  def received_contribution(self, next_deadline: float) -> None:
43  """
44  Update the state after new contribution has been successfully received.
45  """
46  assert not self.have_all_contributions()
47  self._next_contributor(next_deadline)
48 
49  def update(self, now: float, interval: float) -> bool:
50  """
51  Check whether a contributor has missed his chance and update internal
52  state accordingly. If the deadline has passed, return True. Otherwise
53  return False.
54  """
55  # If the next contributor deadline has passed, update
56  if self.next_contributor_deadline <= 0.0 or \
57  now < self.next_contributor_deadline:
58  return False
59 
60  self._next_contributor(now + interval)
61  return True
62 
63  def _next_contributor(self, next_deadline: float) -> None:
65  if self.have_all_contributions():
67  else:
68  self.next_contributor_deadline = next_deadline
69 
70  def _to_json_dict(self) -> JsonDict:
71  return {
72  "next_contributor_index": self.next_contributor_index,
73  "num_contributors": self.num_contributors,
74  "next_contributor_deadline": str(self.next_contributor_deadline),
75  }
76 
77  @staticmethod
78  def _from_json_dict(json_dict: JsonDict) -> ServerState:
79  return ServerState(
80  next_contributor_index=cast(int, json_dict["next_contributor_index"]),
81  num_contributors=cast(int, json_dict["num_contributors"]),
82  next_contributor_deadline=float(
83  cast(str, json_dict["next_contributor_deadline"])))
84 
85 
87  configuration: Configuration,
88  contributors: ContributorList) -> ServerState:
89  """
90  Create an initial server state, given a configuration and contributor list.
91  """
92  assert configuration.start_time_utc != 0.0
93  assert configuration.contribution_interval != 0.0
94  assert len(contributors) != 0
95  state = ServerState(
96  0,
97  len(contributors),
98  configuration.start_time_utc + configuration.contribution_interval)
99  return state
coordinator.server_state.ServerState.to_json
str to_json(self)
Definition: server_state.py:28
test_commands.mock.str
str
Definition: mock.py:18
coordinator.server_state.ServerState.next_contributor_deadline
next_contributor_deadline
Definition: server_state.py:66
coordinator.server_state.ServerState.update
bool update(self, float now, float interval)
Definition: server_state.py:49
coordinator.server_state.initial_server_state
ServerState initial_server_state(Configuration configuration, ContributorList contributors)
Definition: server_state.py:86
coordinator.server_state.ServerState.from_json
ServerState from_json(str state_json)
Definition: server_state.py:32
coordinator.server_state.ServerState.received_contribution
None received_contribution(self, float next_deadline)
Definition: server_state.py:42
coordinator.server_state.ServerState.next_contributor_index
next_contributor_index
Definition: server_state.py:64
coordinator.server_state.ServerState._next_contributor
None _next_contributor(self, float next_deadline)
Definition: server_state.py:63
coordinator.server_state.ServerState.have_all_contributions
bool have_all_contributions(self)
Definition: server_state.py:36
coordinator.server_state.ServerState.__init__
def __init__(self, int next_contributor_index, int num_contributors, float next_contributor_deadline)
Definition: server_state.py:18
coordinator.server_state.ServerState
Definition: server_state.py:14
coordinator.server_state.ServerState._to_json_dict
JsonDict _to_json_dict(self)
Definition: server_state.py:70