-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
72 lines (53 loc) · 1.8 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import logging
from konsensus.models.roles.seed import Seed
from konsensus.models.roles.bootstrap import Bootstrap
from konsensus.models.roles.requester import Requester
from konsensus.network import Network
def key_value_state_machine(state, input_value):
if input_value[0] == 'get':
return state, state.get(input_value[1], None)
elif input_value[0] == 'set':
state[input_value[1]] = input_value[2]
return state, input_value[2]
sequences_running = 0
def do_sequence(network, node, key):
global sequences_running
sequences_running += 1
reqs = [
(('get', key), None),
(('set', key, 10), 10),
(('get', key), 10),
(('set', key, 20), 20),
(('set', key, 30), 30),
(('get', key), 30),
]
def request():
if not reqs:
global sequences_running
sequences_running -= 1
if not sequences_running:
network.stop()
return
input_, exp_output = reqs.pop(0)
def req_done(output):
assert output == exp_output, "%r != %r" % (output, exp_output)
request()
Requester(node, input_, req_done).start()
network.set_timer(None, 1.0, request)
def main():
logging.basicConfig(
format="%(name)s - %(message)s", level=logging.DEBUG)
network = Network(int(sys.argv[1]))
peers = ['N%d' % i for i in range(7)]
for p in peers:
node = network.new_node(address=p)
if p == 'N0':
Seed(node, initial_state={}, peers=peers, execute_fn=key_value_state_machine)
else:
Bootstrap(node, execute_fn=key_value_state_machine, peers=peers).start()
for key in 'abcdefg':
do_sequence(network, node, key)
network.run()
if __name__ == "__main__":
main()