Contract upgrade
Created On | NaN secs ago - Invalid Date UTC+0 |
Contract Code
1
import election_house
2
ELECTION_WINDOW = datetime.WEEKS * 1
3
__upgrade_state = Hash(contract='upgrade', name='upgrade_state')
4
__has_voted = Hash(default_value=False, contract='upgrade', name='has_voted')
5
6
7
def ____():
8
__upgrade_state['locked'] = False
9
__upgrade_state['consensus'] = False
10
__upgrade_state['votes'] = 0
11
__upgrade_state['voters'] = 0
12
13
14
def __start_vote(cilantro_branch_name: str, contracting_branch_name: str,
15
pepper: str):
16
__upgrade_state['locked'] = True
17
__upgrade_state['pepper'] = pepper
18
__upgrade_state['cilantro_branch_name'] = cilantro_branch_name
19
__upgrade_state['contracting_branch_name'] = contracting_branch_name
20
__upgrade_state['votes'] = 0
21
__upgrade_state['voters'] = len(election_house.current_value_for_policy
22
('masternodes')) + len(election_house.current_value_for_policy(
23
'delegates'))
24
__upgrade_state['started'] = now
25
26
27
def __is_valid_voter(address: str):
28
if address in election_house.current_value_for_policy('masternodes'):
29
return True
30
elif address in election_house.current_value_for_policy('delegates'):
31
return True
32
return False
33
34
35
@__export('upgrade')
36
def vote(**kwargs):
37
assert not __has_voted[ctx.caller], 'Cannot vote twice!'
38
assert __is_valid_voter(ctx.caller), 'Invalid voter!'
39
assert not __upgrade_state['consensus'], 'Consensus already achieved!'
40
if __upgrade_state['started'] is not None and now - __upgrade_state[
41
'started'] > ELECTION_WINDOW:
42
__upgrade_state.clear()
43
__has_voted.clear()
44
if not __upgrade_state['locked']:
45
__start_vote(**kwargs)
46
__upgrade_state['votes'] += 1
47
__has_voted[ctx.caller] = True
48
elif __upgrade_state['votes'] + 1 >= __upgrade_state['voters'] * 2 // 3:
49
__upgrade_state['consensus'] = True
50
__has_voted.clear()
51
else:
52
__upgrade_state['votes'] += 1
53
__has_voted[ctx.caller] = True
54