25
25
configure_logging (config_string = config_string )
26
26
27
27
NUM_PARTICIPANTS = 10
28
+ BLOCK_MAKING_PPB = 10
28
29
29
30
print 'Initializing privkeys, addresses and randaos for validators'
30
31
privkeys = [utils .sha3 (str (i )) for i in range (NUM_PARTICIPANTS )]
31
32
addrs = [utils .privtoaddr (k ) for k in privkeys ]
32
33
randaos = [RandaoManager (utils .sha3 (str (i ))) for i in range (NUM_PARTICIPANTS )]
33
- deposit_sizes = [i * 50 + 50 for i in range (NUM_PARTICIPANTS )]
34
+ deposit_sizes = [i * 500 + 500 for i in range (NUM_PARTICIPANTS )]
34
35
vcodes = [generate_validation_code (a ) for a in addrs ]
35
36
vchashes = [utils .sha3 (c ) for c in vcodes ]
36
37
assert len (privkeys ) == len (addrs ) == len (randaos ) == len (deposit_sizes ) == len (vcodes ) == len (vchashes ) == NUM_PARTICIPANTS
39
40
ct = get_casper_ct ()
40
41
assert ct
41
42
print 'Constructing genesis'
42
- s = make_casper_genesis (validators = [(generate_validation_code (a ), ds * 10 ** 18 , r .get (9999 ))
43
+ s = make_casper_genesis (validators = [(generate_validation_code (a ), ds * 10 ** 18 , r .get (9999 ), a )
43
44
for a , ds , r in zip (addrs , deposit_sizes , randaos )][:- 1 ],
44
45
alloc = {a : {'balance' : 10 ** 18 } for a in addrs },
45
46
timestamp = int (time .time () - 99999 ),
46
47
epoch_length = 100 )
47
48
print 'Genesis constructed successfully'
48
49
chains = [Chain (s .to_snapshot (), env = s .env ) for i in range (NUM_PARTICIPANTS )]
50
+ withdrawal_time_1 = call_casper (chains [0 ].state , 'getLockDuration' , [vchashes [0 ]])
51
+
52
+ # List of validator IDs that created each block
53
+ vids = []
49
54
50
55
# Create and sign a block
51
56
def make_block (chain , key , randao , vchash , skips ):
@@ -68,6 +73,7 @@ def make_block(chain, key, randao, vchash, skips):
68
73
assert validate_block_header (s , b .header )
69
74
print 'Validation successful'
70
75
assert chains [0 ].add_block (b )
76
+ vids .append (next_validator_id )
71
77
print 'Block added to chain'
72
78
# Make another block
73
79
next_validator = call_casper (chains [0 ].state , 'getValidator' , [0 ])
@@ -77,6 +83,7 @@ def make_block(chain, key, randao, vchash, skips):
77
83
b2 = make_block (chains [0 ], privkeys [next_validator_id ],
78
84
randaos [next_validator_id ], vchashes [next_validator_id ], skip_count )
79
85
assert chains [0 ].add_block (b2 )
86
+ vids .append (next_validator_id )
80
87
print 'Second block added to chain'
81
88
# Make a dunkle and include it in a transaction
82
89
next_validator = call_casper (chains [1 ].state , 'getValidator' , [1 ])
@@ -108,7 +115,6 @@ def make_block(chain, key, randao, vchash, skips):
108
115
chains [0 ].state .commit ()
109
116
print 'Added new validator "in-flight", indices:' , vchashes [- 1 ].encode ('hex' )
110
117
# Create some blocks
111
- vids = []
112
118
bn = call_casper (chains [0 ].state , 'getBlockNumber' )
113
119
for i in range (bn + 1 , 200 ):
114
120
next_validator = call_casper (chains [0 ].state , 'getValidator' , [0 ])
@@ -128,6 +134,7 @@ def make_block(chain, key, randao, vchash, skips):
128
134
assert call_casper (chains [0 ].state , 'getEndEpoch' , [vchashes [0 ]]) == 4
129
135
chains [0 ].state .commit ()
130
136
print 'Withdrew a validator'
137
+ print '%d blocks before ETH becomes available' % withdrawal_time_1
131
138
for i in range (200 , 400 ):
132
139
next_validator = call_casper (chains [0 ].state , 'getValidator' , [0 ])
133
140
next_validator_id = vchashes .index (next_validator )
@@ -139,14 +146,26 @@ def make_block(chain, key, randao, vchash, skips):
139
146
print 'Created 200 blocks after the deposit, created by validators:' , vids [- 200 :]
140
147
assert len (vchashes ) - 1 in vids
141
148
assert 0 in vids
142
- for i in range (400 , 600 ):
149
+ for i in range (400 , 400 + withdrawal_time_1 + 1 ):
143
150
next_validator = call_casper (chains [0 ].state , 'getValidator' , [0 ])
144
151
next_validator_id = vchashes .index (next_validator )
145
152
b = make_block (chains [0 ], privkeys [next_validator_id ], randaos [next_validator_id ],
146
153
vchashes [next_validator_id ], 0 )
147
154
assert chains [0 ].add_block (b )
148
155
vids .append (next_validator_id )
149
- print 'Created 200 blocks after the withdrawal, created by validators:' , vids [- 200 :]
156
+ print 'Created %d blocks after the withdrawal, created by validators:' % ( withdrawal_time_1 + 1 ) , vids [- 200 :]
150
157
assert len (vchashes ) - 1 in vids
151
158
assert 0 not in vids [- 200 :]
159
+ pre_bal = chains [0 ].state .get_balance (addrs [0 ])
160
+ txdata = ct .encode ('withdraw' , [vchashes [0 ]])
161
+ t4 = Transaction (chains [0 ].state .get_nonce (addrs [0 ]), 0 , 1000000 , casper_config ['CASPER_ADDR' ], 0 , txdata ).sign (privkeys [0 ])
162
+ apply_transaction (chains [0 ].state , t4 )
163
+ post_bal = chains [0 ].state .get_balance (addrs [0 ])
164
+ print 'Wei withdrawn:' , post_bal - pre_bal
165
+ blocks_by_v0_in_stage1 = len ([x for x in vids [:200 ] if x == 0 ])
166
+ expected_revenue_in_stage1 = blocks_by_v0_in_stage1 * max (sum (deposit_sizes [:- 1 ]), 1000000 ) * 10 ** 18 * BLOCK_MAKING_PPB / 10 ** 9
167
+ blocks_by_v0_in_stage2 = len ([x for x in vids [200 :400 ] if x == 0 ])
168
+ expected_revenue_in_stage2 = blocks_by_v0_in_stage2 * max (sum (deposit_sizes ), 1000000 ) * 10 ** 18 * BLOCK_MAKING_PPB / 10 ** 9
169
+
170
+ assert post_bal - pre_bal == deposit_sizes [0 ] * 10 ** 18 + expected_revenue_in_stage1 + expected_revenue_in_stage2
152
171
print 'PoS test fully passed'
0 commit comments