Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
test_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 unittest import TestCase
8 from coordinator.crypto import import_verification_key, import_signature
9 from coordinator.server_state import ServerState, initial_server_state
10 from coordinator.contributor_list import ContributorList, Contributor
11 from coordinator.server_configuration import Configuration
12 
13 
14 START_TIME = 8000.0
15 CONTRIBUTION_INTERVAL = 1000.0
16 
17 TEST_KEY_0 = \
18  "30" + \
19  "819b301006072a8648ce3d020106052b81040023038186000400da7cc0d36ec6" + \
20  "496ff55e2c77df8eb944d452a4b9fed9f73f5ba9e9d01b66dc2d221a8b01f11a" + \
21  "f67575b9e8855729e2cf300d8c0addf82a57f0d1396f13117c032b016e7876c2" + \
22  "c9c147d1cb72d15e7295e717421a485c17f40a591fe9a1225d73ce7dd77dd545" + \
23  "7022f4d22f36960ca0036c1f86f2bec569be98455d44e42169b7b01af2"
24 EVIDENCE_0 = \
25  "015bbb49bf4ca076c6cf6668d710bc415483dd2f78f3600e1a9052ed6712e8ca" + \
26  "0b4468ce65d59e8eb9c0d24bcc28ea58fe7fa899f9703cbd2db89f3cf7082268" + \
27  "4cc300fc8cc01eea1c8652fafe158fb378ed9820515ef856503a347db6579f94" + \
28  "5cf4e00f6c2850d5c544db3803561faa2a85d300a7b78d5c95f1f0f9c333aaf4" + \
29  "fb8671d8"
30 TEST_KEY_1 = \
31  "30" + \
32  "819b301006072a8648ce3d020106052b810400230381860004017b68c30b536e" + \
33  "772d3ad84ff46f9a00225fb93ee693e4ee051c122cf05a37efc384f205a596f3" + \
34  "53b5293d8f92aba2ab1319885fff262dd65c8488975097a9963349005d017e20" + \
35  "5b32ed016706a75de2461f57f03fefba4d45339cf34271fbd7557b4ec7cca450" + \
36  "f04dea14de6500dafb8a193a2647705493a70b03fe1e902737e899c7d4"
37 EVIDENCE_1 = \
38  "00150bee7f0b1170f81f5174e7b2c9f72d10da6ea78de03ae02d9019865db375" + \
39  "d53de484a1d440bfdb2054d485888bd5a9e411991e984907f87cd19206ac561b" + \
40  "682c011164524c9895791a8883a2e50a895211a7591504b6ca91b7686610f313" + \
41  "cc03924896b798d519e0469a5dc432a366a4e62ba7d31d6c8b794773eedcaf91" + \
42  "cf6b25d6"
43 TEST_KEY_2 = \
44  "30" + \
45  "819b301006072a8648ce3d020106052b81040023038186000401dad8ae45b05e" + \
46  "b48d71698636925e422722c908ecdf3c4578a19328a23a5088eab4456b995ee9" + \
47  "19091cc82d9b359d39a431af5fd2000b3aa521c3b239c7a638151801ef36d88c" + \
48  "5c7bcc99e65dcaacbe2e2cdb1be5fdd5af917e9cb81f5cb74f67a70476c9d501" + \
49  "e8254c53fb3bfdcfdc31bcb57d5ae4d041a96de6423c6e0e56a0fb6429"
50 EVIDENCE_2 = \
51  "00f1e8edf366b47dca3edcdae3f08cdcfbeec107960eec3ae1c8cc996cdc47d8" + \
52  "a214562f9b8ff7a377ccd6cc9b2240af61f8fc0ba14f967859eff28e1cf74a42" + \
53  "01ac00d342be7d8e0cf512a840bbf1d6cdbb29965b23e7d31304ea20db9441cd" + \
54  "6173d8a5199939e7e5406a1af47078d871c5958a522c6b46c2d8a442d17e93d7" + \
55  "c6d1e0bd"
56 TEST_KEY_3 = \
57  "30" + \
58  "819b301006072a8648ce3d020106052b810400230381860004011ebc2b021f33" + \
59  "f2b71ede076c33f38c75b11146db98c2f922f22c79468277315af4c811dbb859" + \
60  "f65f3a174a5ca31e42a6a27d63161069f6be46097e5afaddf5617a010671b962" + \
61  "7ed80f26ad092a4aef0655dc99e8481946c57dae461daca97fbe32bbb64f3f7f" + \
62  "3ed1153420ab0453a9bbfb278a60b5bcdd2ce2efa99c4fd99f63c093b2"
63 EVIDENCE_3 = \
64  "0156297506fcc4535682943562e19e702e3ae94382057847dc469cb337a4cc6f" + \
65  "4b5c4dbe0da066b29eb716a2c14690db9b38f44a06623cb465cf0461ddab201c" + \
66  "d22c002bd6205d415bf66b7cf8e16e06736fca2e029edd6b0eb1ec741ae299df" + \
67  "02cd27030986b6bbfcb0677003226b1fabb83af29d37765fa514118b0a9422ec" + \
68  "af156c85"
69 
70 
71 class TestServerState(TestCase):
72 
73  def test_configuration(self) -> None:
74  config_1 = self._dummy_config()
75  config_json_1 = config_1.to_json()
76  config_2 = Configuration.from_json(config_json_1)
77  config_json_2 = config_2.to_json()
78  self.assertEqual(config_json_1, config_json_2)
79 
80  def test_state_serialization(self) -> None:
81  config = self._dummy_config()
82  contributors = self._dummy_contributors()
83  state = initial_server_state(config, contributors)
84 
85  # initial state
86  self.assertEqual(0, state.next_contributor_index)
87  self.assertEqual(
88  START_TIME + CONTRIBUTION_INTERVAL,
89  state.next_contributor_deadline)
90 
91  # json serialization
92  self._test_json_serialization(state)
93 
94  # update, deadline not passed
95  state.update(
96  START_TIME + (CONTRIBUTION_INTERVAL / 2),
97  CONTRIBUTION_INTERVAL)
98  self.assertEqual(0, state.next_contributor_index)
99  self.assertEqual(
100  START_TIME + CONTRIBUTION_INTERVAL,
101  state.next_contributor_deadline)
102  self.assertFalse(state.have_all_contributions())
103 
104  # json serialization
105  self._test_json_serialization(state)
106 
107  # deadline passed with no contribution
108  state.update(
109  START_TIME + CONTRIBUTION_INTERVAL + 0.1,
110  CONTRIBUTION_INTERVAL)
111  self.assertEqual(1, state.next_contributor_index)
112  self.assertEqual(
113  START_TIME + 0.1 + 2 * CONTRIBUTION_INTERVAL,
114  state.next_contributor_deadline)
115  self.assertFalse(state.have_all_contributions())
116 
117  # json serialization
118  self._test_json_serialization(state)
119 
120  # got contribution part way through interval
121  state.received_contribution(
122  START_TIME + 0.1 + 2.5 * CONTRIBUTION_INTERVAL)
123  self.assertEqual(2, state.next_contributor_index)
124  self.assertEqual(
125  START_TIME + 0.1 + 2.5 * CONTRIBUTION_INTERVAL,
126  state.next_contributor_deadline)
127  self.assertFalse(state.have_all_contributions())
128 
129  # json serialization
130  self._test_json_serialization(state)
131 
132  # 3rd contribution deadline
133  state.update(
134  START_TIME + 0.2 + 2.5 * CONTRIBUTION_INTERVAL,
135  CONTRIBUTION_INTERVAL)
136  self.assertEqual(3, state.next_contributor_index)
137  self.assertEqual(
138  START_TIME + 0.2 + 3.5 * CONTRIBUTION_INTERVAL,
139  state.next_contributor_deadline)
140  self.assertFalse(state.have_all_contributions())
141 
142  # final contribution
143  state.received_contribution(
144  START_TIME + 0.2 + 3.0 * CONTRIBUTION_INTERVAL)
145  self.assertTrue(state.have_all_contributions())
146  self.assertEqual(0, state.next_contributor_deadline)
147 
148  # json serialization
149  self._test_json_serialization(state)
150 
151  @staticmethod
152  def _dummy_contributors() -> ContributorList:
153  return ContributorList(
154  [
155  Contributor(
156  "c1@mpc.com",
157  import_verification_key(TEST_KEY_0),
158  import_signature(EVIDENCE_0)),
159  Contributor(
160  "c2@mpc.com",
161  import_verification_key(TEST_KEY_1),
162  import_signature(EVIDENCE_1)),
163  Contributor(
164  "c3@mpc.com",
165  import_verification_key(TEST_KEY_2),
166  import_signature(EVIDENCE_2)),
167  Contributor(
168  "c4@mpc.com",
169  import_verification_key(TEST_KEY_3),
170  import_signature(EVIDENCE_3)),
171  ])
172 
173  @staticmethod
174  def _dummy_config() -> Configuration:
175  return Configuration(
176  "contributors.json",
177  START_TIME,
178  CONTRIBUTION_INTERVAL,
179  "key.pem",
180  "cert.pem",
181  8001
182  )
183 
184  def _test_json_serialization(self, state: ServerState) -> None:
185  state_1_json = state.to_json()
186  state_2 = ServerState.from_json(state_1_json)
187  state_2_json = state_2.to_json()
188  self.assertEqual(state_1_json, state_2_json)
coordinator.crypto
Definition: crypto.py:1
coordinator.server_state
Definition: server_state.py:1
coordinator.server_configuration
Definition: server_configuration.py:1
coordinator.contributor_list.Contributor
Definition: contributor_list.py:16
test.test_server_state.TestServerState._test_json_serialization
None _test_json_serialization(self, ServerState state)
Definition: test_server_state.py:184
coordinator.server_state.initial_server_state
ServerState initial_server_state(Configuration configuration, ContributorList contributors)
Definition: server_state.py:86
test.test_server_state.TestServerState.test_state_serialization
None test_state_serialization(self)
Definition: test_server_state.py:80
coordinator.crypto.import_verification_key
ecdsa.VerifyingKey import_verification_key(str vk_s)
Definition: crypto.py:70
test.test_server_state.TestServerState
Definition: test_server_state.py:71
coordinator.contributor_list
Definition: contributor_list.py:1
coordinator.server_configuration.Configuration
Definition: server_configuration.py:19
test.test_server_state.TestServerState._dummy_config
Configuration _dummy_config()
Definition: test_server_state.py:174
test.test_server_state.TestServerState._dummy_contributors
ContributorList _dummy_contributors()
Definition: test_server_state.py:152
coordinator.crypto.import_signature
bytes import_signature(str sig_s)
Definition: crypto.py:78
test.test_server_state.TestServerState.test_configuration
None test_configuration(self)
Definition: test_server_state.py:73
coordinator.contributor_list.ContributorList
Definition: contributor_list.py:44