def check_snapshot_consistency(snapshot, env=None): if env: c = chain.Chain(env=env) else: c = chain.Chain(snapshot, Env()) snapshot2 = c.state.to_snapshot() if snapshot != snapshot2: # FIXME for i, ss in enumerate([snapshot, snapshot2]): fn = '/tmp/{}_{}'.format(STATE_STORE_FN, i) open(fn, 'w').write(json.dumps(snapshot)) raise Exception("snapshot difference, see {}*".format(fn[:-1]))
def __init__(self, alloc=base_alloc, env=None, genesis=None): if genesis: self.chain = pow_chain.Chain(genesis, reset_genesis=True) else: self.chain = pow_chain.Chain(mk_basic_state( alloc, None, get_env(env)), reset_genesis=True) self.cs = get_consensus_strategy(self.chain.env.config) self.block = mk_block_from_prevstate( self.chain, timestamp=self.chain.state.timestamp + 1) self.head_state = self.chain.state.ephemeral_clone() self.cs.initialize(self.head_state, self.block) self.last_sender = None self.last_tx = None
def __init__(self, alloc=base_alloc, env=None, genesis=None): from ethereum.pow import chain as pow_chain if genesis: if type(genesis)!=dict and genesis.env.config['CONSENSUS_STRATEGY'] == 'hybrid_casper': from ethereum.hybrid_casper import chain as hybrid_casper_chain self.chain = hybrid_casper_chain.Chain(genesis, reset_genesis=True) else: self.chain = pow_chain.Chain(genesis, env=env, reset_genesis=True) else: self.chain = pow_chain.Chain(mk_basic_state(alloc, None, get_env(env)), reset_genesis=True) self.cs = get_consensus_strategy(self.chain.env.config) self.block = mk_block_from_prevstate( self.chain, timestamp=self.chain.state.timestamp + 1) self.head_state = self.chain.state.ephemeral_clone() self.cs.initialize(self.head_state, self.block) self.last_sender = None self.last_tx = None
def run_block_test(params, config_overrides=None): if config_overrides is None: config_overrides = {} env = Env(db.EphemDB()) genesis_decl = {} for param in ("bloom", "timestamp", "nonce", "extraData", "gasLimit", "coinbase", "difficulty", "parentHash", "mixHash", "gasUsed"): genesis_decl[param] = params["genesisBlockHeader"][param] genesis_decl["alloc"] = params["pre"] old_config = copy.deepcopy(env.config) for k, v in config_overrides.items(): env.config[k] = v # print('overrides', config_overrides) s = state_from_genesis_declaration(genesis_decl, env, allow_empties=True) initialize_genesis_keys(s, Block(s.prev_headers[0], [], [])) c = chain.Chain(genesis=s, localtime=2**99) # print('h', encode_hex(c.state.prev_headers[0].state_root)) # print(c.state.to_dict()) # print(params["pre"]) assert c.state.env == env assert c.state.prev_headers[0].state_root == safe_decode( params["genesisBlockHeader"]["stateRoot"]), ( encode_hex(c.state.prev_headers[0].state_root), params["genesisBlockHeader"]["stateRoot"]) assert c.state.trie.root_hash == safe_decode( params["genesisBlockHeader"]["stateRoot"]) assert c.state.prev_headers[0].hash == safe_decode( params["genesisBlockHeader"]["hash"]) #print('std', c.state.to_dict()) for blk in params["blocks"]: if 'blockHeader' not in blk: success = True try: rlpdata = safe_decode(blk["rlp"][2:]) success = c.add_block(rlp.decode(rlpdata, Block)) except (ValueError, TypeError, AttributeError, VerificationFailed, DecodingError, DeserializationError, InvalidTransaction, InvalidNonce, KeyError) as e: success = False assert not success else: rlpdata = safe_decode(blk["rlp"][2:]) assert c.add_block(rlp.decode(rlpdata, Block)) env.config = old_config
if '--rlp_blocks' in sys.argv: RLP_BLOCKS_FILE = sys.argv[sys.argv.index('--rlp_blocks') + 1] BENCHMARK = 0 if '--benchmark' in sys.argv: BENCHMARK = int(sys.argv[sys.argv.index('--benchmark') + 1]) DB_DIR = '/tmp/%d' % random.randrange(int(time.time() * 1000000)) if '--db' in sys.argv: DB_DIR = int(sys.argv[sys.argv.index('--db') + 1]) _path, _file = os.path.split(STATE_LOAD_FN) if _file in os.listdir(os.path.join(os.getcwd(), _path)): print('loading state from %s ...' % STATE_LOAD_FN) c = chain.Chain(json.load(open(STATE_LOAD_FN)), Env()) print('loaded.') elif 'genesis_frontier.json' not in os.listdir(os.getcwd()): print('Please download genesis_frontier.json from ' + 'http://vitalik.ca/files/genesis_frontier.json') sys.exit() else: c = chain.Chain(json.load(open('genesis_frontier.json')), Env(LevelDB(DB_DIR))) assert c.state.trie.root_hash.encode('hex') == \ 'd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544' assert c.state.prev_headers[0].hash.encode('hex') == \ 'd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3' print('state generated from genesis') print('Attempting to open %s' % RLP_BLOCKS_FILE) _path, _file = os.path.split(RLP_BLOCKS_FILE)