/ casper3 / test.py
test.py
 1  import networksim
 2  from casper import Validator
 3  import casper
 4  from ethereum.parse_genesis_declaration import mk_basic_state
 5  from ethereum.config import Env
 6  from ethereum.casper_utils import RandaoManager, generate_validation_code, call_casper, \
 7      get_skips_and_block_making_time, sign_block, make_block, get_contract_code, \
 8      casper_config, get_casper_ct, get_casper_code, get_rlp_decoder_code, \
 9      get_hash_without_ed_code, make_casper_genesis, validator_sizes, find_indices
10  from ethereum.utils import sha3, privtoaddr
11  from ethereum.transactions import Transaction
12  from ethereum.state_transition import apply_transaction
13  
14  from ethereum.slogging import LogRecorder, configure_logging, set_level
15  # config_string = ':info,eth.vm.log:trace,eth.vm.op:trace,eth.vm.stack:trace,eth.vm.exit:trace,eth.pb.msg:trace,eth.pb.tx:debug'
16  config_string = ':info,eth.vm.log:trace'
17  configure_logging(config_string=config_string)
18  
19  n = networksim.NetworkSimulator(latency=150)
20  n.time = 2
21  print 'Generating keys'
22  keys = [sha3(str(i)) for i in range(20)]
23  print 'Initializing randaos'
24  randaos = [RandaoManager(sha3(k)) for k in keys]
25  deposit_sizes = [128] * 15 + [256] * 5
26  
27  print 'Creating genesis state'
28  s = make_casper_genesis(validators=[(generate_validation_code(privtoaddr(k)), ds * 10**18, r.get(9999))
29                                      for k, ds, r in zip(keys, deposit_sizes, randaos)],
30                          alloc={privtoaddr(k): {'balance': 10**18} for k in keys},
31                          timestamp=2,
32                          epoch_length=40)
33  g = s.to_snapshot()
34  print 'Genesis state created'
35  
36  validators = [Validator(g, k, n, Env(config=casper_config), time_offset=4) for k in keys]
37  n.agents = validators
38  n.generate_peers()
39  lowest_shared_height = -1
40  made_101_check = 0
41  
42  for i in range(100000):
43      # print 'ticking'
44      n.tick()
45      if i % 100 == 0:
46          print '%d ticks passed' % i
47          print 'Validator heads:', [v.chain.head.header.number if v.chain.head else None for v in validators]
48          print 'Total blocks created:', casper.global_block_counter
49          print 'Dunkle count:', call_casper(validators[0].chain.state, 'getTotalDunklesIncluded', [])
50          lowest_shared_height = min([v.chain.head.header.number if v.chain.head else -1 for v in validators])
51          if lowest_shared_height >= 101 and not made_101_check:
52              made_101_check = True
53              print 'Checking that withdrawn validators are inactive'
54              assert len([v for v in validators if v.active]) == len(validators) - 5, len([v for v in validators if v.active])
55              print 'Check successful'
56              break
57      if i == 1:
58          print 'Checking that all validators are active'
59          assert len([v for v in validators if v.active]) == len(validators)
60          print 'Check successful'
61      if i == 2000:
62          print 'Withdrawing a few validators'
63          for v in validators[:5]:
64              v.withdraw()
65      if i == 4000:
66          print 'Checking that validators have withdrawn'
67          for v in validators[:5]:
68              assert call_casper(v.chain.state, 'getEndEpoch', []) <= 2
69          print 'Check successful'