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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
import pandas as pd
import random as rand
import string
"""
===============================
priest ledger format:
voted or not, ballot number, promised or not, times led
0/1,n,0/1,x
promised or not explanation: whether or not this priest has promised to not
respond to maxvote requests after this ballot
times led explanation: number of times she's been leader
(see doc for messenger)
===============================
priest messagebook format:
(priests will track these for changes by messenger)
from,code
n,1/2/3
codes (not leader):
1: next ballot
2: begin ballot
3: successful ballot
codes (leader):
===============================
sequence of code writing (may provide intuition for future folks):
- skeleton
- priest init
- leader_main
- god init
- next_ballot
- send_next_ballot
===============================
"""
"""
paxons are very religious; god controls anything and everything
- literally creates priests ie initializes the objects
- assigns ballot number ranges for each priest (so B1 is satisfied)
"""
class god:
num_priests = 0
priests = {}
def __init__(self, num_priests, num_ballots, offset=100):
self.num_priests = num_priests
self.num_ballots = num_ballots
#create and name the priests (quite godly)
for x in range(0, self.num_priests):
self.priests[x] = priest(name, (x+1)*offset)
"""
messenger relays messages to and from the leader and the priest
she accomplishes this by tracking the priests' ledger files for changes
each priest (including the leader) has a personal messenger (instance)
"""
class messenger(god):
"""
leader messenger functions
"""
def send_next_ballot(current_list, ballot_num):
pass
def send_begin_ballot():
pass
def send_on_success():
pass
"""
priest messenger functions
"""
def send_last_vote():
pass
def send_vote():
pass
"""
priests are present (or not present) and vote (or not vote) for ballots proposed
by the leader
they record all ballots they have participated in in individual ledger files
start offset is the offset by which a priest starts assigning ballot numbers when
he is assigned leader to satisfy B1
ex. priests (A, B, C) have offsets (100, 200, 300) (this assumes maximum of 99 ballots)
leader sequence (determined randomly by god): A B A C B A
resulting ballot numbers: 100 200 101 300 201 102
"""
class priest(messenger):
def __init__(self, name, start_offset):
self.name = name #write this name in the ledger in case it gets lost (ledger fileaname)
self.messenger = messenger() #hire a messenger
self.ledger = open("ledgers/" + self.name, "+")
self.offset = offset
#=====================leader functions===================
def leader_main():
times_led = self.ledger.at[self.ledger.shape[0]-1,'times_led'] #last col of last row of ledger
ballot_num = self.offset + times_led #B1 is satisfied assuming num_ballots < difference between offsets
self.next_ballot(ballot_num)
self.begin_ballot()
def next_ballot(ballot_num):
#randomly choose a number of priests: 40% - 100% of total priests
rand_num = random.randrange(num_priests*0.4, num_priests+1)
current_priests = random.sample(priests.keys(), rand_num)
#Send the nextBallot message to the priests and wait for their responses
messenger.send_next_ballot(current_list, ballot_num)
def begin_ballot():
#wait for all threads to respond
#send message to every priest indicating that his vote is requested
def evaluate():
pass
#check if the ballot was a success and note the decree if it was
#send the sucess message to all living priests
#====================regular priest functions======================
def priest_main():
pass
def last_vote():
pass
#responding to a next_ballot request from the leader
#determine the lastVote and send it to the leader (if not promised to another leader) (might
#need another function for this)
#if responded, set promise to 1 for the relevant maxVote in the ledger
def vote():
pass
#choose (randomly) whether or not to vote on this ballot
#send the vote to the leader (another function "voted"?)
def on_success():
pass
#do something if the messenger brings the good news of a ballot success
|