def __init__(self, genesis): env = Env(EphemDB(), konfig) if not genesis: genesis = mk_genesis_data(env) if 'config' in genesis: if 'homesteadBlock' in genesis['config']: env.config['HOMESTEAD_FORK_BLKNUM'] = int(genesis['config']['homesteadBlock']) env.config['DAO_FORK_BLKNUM'] = int(genesis['config']['homesteadBlock']) env.config['ANTI_DOS_FORK_BLKNUM'] = int(genesis['config']['homesteadBlock']) env.config['SPURIOUS_DRAGON_FORK_BLKNUM'] = int(genesis['config']['homesteadBlock']) self.state = state_from_genesis_declaration(genesis, env) initialize_genesis_keys(self.state, Block(self.state.prev_headers[0], [], []))
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
def __init__(self, genesis=None, env=None, coinbase=b'\x00' * 20, new_head_cb=None, reset_genesis=False, localtime=None, **kwargs): self.env = env or Env() # Initialize the state if b'head_hash' in self.db: # new head tag self.state = self.mk_poststate_of_blockhash( self.db.get(b'head_hash')) print('Initializing chain from saved head, #%d (%s)' % (self.state.prev_headers[0].number, encode_hex(self.state.prev_headers[0].hash))) elif genesis is None: raise Exception("Need genesis decl!") elif isinstance(genesis, State): assert env is None self.state = genesis self.env = self.state.env print('Initializing chain from provided state') elif "extraData" in genesis: self.state = state_from_genesis_declaration(genesis, self.env) reset_genesis = True print('Initializing chain from provided genesis declaration') elif "prev_headers" in genesis: self.state = State.from_snapshot(genesis, self.env) reset_genesis = True print('Initializing chain from provided state snapshot, %d (%s)' % (self.state.block_number, encode_hex(self.state.prev_headers[0].hash[:8]))) else: print('Initializing chain from new state based on alloc') self.state = mk_basic_state( genesis, { "number": kwargs.get('number', 0), "gas_limit": kwargs.get('gas_limit', 4712388), "gas_used": kwargs.get('gas_used', 0), "timestamp": kwargs.get('timestamp', 1467446877), "difficulty": kwargs.get('difficulty', 2**25), "hash": kwargs.get('prevhash', '00' * 32), "uncles_hash": kwargs.get('uncles_hash', '0x' + encode_hex(BLANK_UNCLES_HASH)) }, self.env) reset_genesis = True assert self.env.db == self.state.db initialize(self.state) self.new_head_cb = new_head_cb self.head_hash = self.state.prev_headers[0].hash self.checkpoint_head_hash = b'\x00' * 32 self.db.put(b'cp_subtree_score' + b'\x00' * 32, 2 / 3.) self.commit_logs = [] self.casper_address = self.config['CASPER_ADDRESS'] self.db.put(b'GENESIS_NUMBER', to_string(self.state.block_number)) assert self.state.block_number == self.state.prev_headers[0].number if reset_genesis: self.genesis = Block(self.state.prev_headers[0], [], []) initialize_genesis_keys(self.state, self.genesis) else: self.genesis = self.get_block_by_number(0) self.db.put(b'cp_subtree_score' + self.genesis.hash, 2 / 3.) self.min_gasprice = kwargs.get('min_gasprice', 5 * 10**9) self.coinbase = coinbase self.extra_data = 'moo ha ha says the laughing cow.' self.time_queue = [] self.parent_queue = {} self.localtime = time.time() if localtime is None else localtime
def __init__(self, genesis=None, env=None, new_head_cb=None, reset_genesis=False, localtime=None, max_history=1000, **kwargs): self.env = env or Env() # Initialize the state if b'head_hash' in self.db: # new head tag self.state = self.mk_poststate_of_blockhash( self.db.get('head_hash')) self.state.executing_on_head = True print('Initializing chain from saved head, #%d (%s)' % (self.state.prev_headers[0].number, encode_hex(self.state.prev_headers[0].hash))) elif genesis is None: raise Exception("Need genesis decl!") elif isinstance(genesis, State): assert env is None self.state = genesis self.env = self.state.env print('Initializing chain from provided state') reset_genesis = True elif "extraData" in genesis: self.state = state_from_genesis_declaration(genesis, self.env, executing_on_head=True) reset_genesis = True print('Initializing chain from provided genesis declaration') elif "prev_headers" in genesis: self.state = State.from_snapshot(genesis, self.env, executing_on_head=True) reset_genesis = True print('Initializing chain from provided state snapshot, %d (%s)' % (self.state.block_number, encode_hex(self.state.prev_headers[0].hash[:8]))) elif isinstance(genesis, dict): print('Initializing chain from new state based on alloc') self.state = mk_basic_state( genesis, { "number": kwargs.get('number', 0), "gas_limit": kwargs.get('gas_limit', self.env.config['BLOCK_GAS_LIMIT']), "gas_used": kwargs.get('gas_used', 0), "timestamp": kwargs.get('timestamp', 1467446877), "difficulty": kwargs.get('difficulty', 2**25), "hash": kwargs.get('prevhash', '00' * 32), "uncles_hash": kwargs.get('uncles_hash', '0x' + encode_hex(BLANK_UNCLES_HASH)) }, self.env) reset_genesis = True assert self.env.db == self.state.db initialize(self.state) self.new_head_cb = new_head_cb if self.state.block_number == 0: assert self.state.block_number == self.state.prev_headers[0].number else: assert self.state.block_number - 1 == self.state.prev_headers[ 0].number if reset_genesis: if isinstance(self.state.prev_headers[0], FakeHeader): header = self.state.prev_headers[0].to_block_header() else: header = self.state.prev_headers[0] self.genesis = Block(header) self.state.prev_headers[0] = header initialize_genesis_keys(self.state, self.genesis) else: self.genesis = self.get_block_by_number(0) self.head_hash = self.state.prev_headers[0].hash self.time_queue = [] self.parent_queue = {} self.localtime = time.time() if localtime is None else localtime self.max_history = max_history