def custom_chain(tester, alloc={}, genesis_gas_limit=4712388, min_gas_limit=5000, startgas=3141592): # alloc for i in range(9): alloc[utils.int_to_addr(i)] = {'balance': 1} # genesis from ethereum.genesis_helpers import mk_basic_state header = { "number": 0, "gas_limit": genesis_gas_limit, "gas_used": 0, "timestamp": 1467446877, "difficulty": 1, "uncles_hash": '0x' + utils.encode_hex(utils.sha3(rlp.encode([]))) } genesis = mk_basic_state(alloc, header, tester.get_env(None)) # tester tester.languages['viper'] = compiler.Compiler() tester.STARTGAS = startgas c = tester.Chain(alloc=alloc, genesis=genesis) c.chain.env.config['MIN_GAS_LIMIT'] = min_gas_limit c.mine(1) return c
def test_chain(alloc={}, genesis_gas_limit=9999999, min_gas_limit=5000, startgas=3141592): # alloc alloc[tester.a0] = {'balance': 100000 * utils.denoms.ether} for i in range(9): alloc[utils.int_to_addr(i)] = {'balance': 1} # genesis header = { "number": 0, "gas_limit": genesis_gas_limit, "gas_used": 0, "timestamp": 1467446877, "difficulty": 1, "uncles_hash": '0x' + utils.encode_hex(utils.sha3(rlp.encode([]))) } genesis = mk_basic_state(alloc, header, tester.get_env(None)) # tester tester.languages['viper'] = compiler.Compiler() tester.STARTGAS = startgas chain = tester.Chain(alloc=alloc, genesis=genesis) chain.chain.env.config['MIN_GAS_LIMIT'] = min_gas_limit chain.mine(1) return chain
def make_casper_genesis(alloc, epoch_length, withdrawal_delay, base_interest_factor, base_penalty_factor): # The Casper-specific config declaration casper_config = copy.deepcopy(config.default_config) casper_config['HOMESTEAD_FORK_BLKNUM'] = 0 casper_config['ANTI_DOS_FORK_BLKNUM'] = 0 casper_config['CLEARING_FORK_BLKNUM'] = 0 casper_config['CONSENSUS_STRATEGY'] = 'hybrid_casper' casper_config['NULL_SENDER'] = utils.sha3('NULL_SENDER') casper_config['EPOCH_LENGTH'] = epoch_length casper_config['WITHDRAWAL_DELAY'] = withdrawal_delay casper_config['OWNER'] = a0 casper_config['BASE_INTEREST_FACTOR'] = base_interest_factor casper_config['BASE_PENALTY_FACTOR'] = base_penalty_factor # Get initialization txs init_txs, casper_address = mk_initializers(casper_config, casper_config['NULL_SENDER']) casper_config['CASPER_ADDRESS'] = casper_address # Create state and apply required state_transitions for initializing Casper state = genesis_helpers.mk_basic_state( alloc, None, env=config.Env(config=casper_config)) state.gas_limit = 10**8 for tx in init_txs: state.set_balance(utils.privtoaddr(casper_config['NULL_SENDER']), 15**18) success, output = apply_transaction(state, tx) assert success state.gas_used = 0 state.set_balance(utils.privtoaddr(casper_config['NULL_SENDER']), 0) consensus.initialize(state) state.commit() return state
def __init__(self, alloc=None, env=None, deploy_sharding_contracts=False, genesis=None): # MainChain if genesis is None: genesis = mk_basic_state(base_alloc if alloc is None else alloc, None, get_env(env)) self.chain = MainChain(genesis=genesis, 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 # ShardChains self.collation = {} self.shard_head_state = {} self.shard_last_sender = {} self.shard_last_tx = {} self.add_header_logs = [] # validator manager contract and other pre-compiled contracts self.is_sharding_contracts_deployed = False if deploy_sharding_contracts: self.is_sharding_contracts_deployed = True self.deploy_initializing_contracts(k0) self.last_sender = k0 self.mine(1)
def __init__(self, alloc=None, env=None): self.chain = chain.Chain(genesis=mk_basic_state( base_alloc if alloc is None else 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 test_chain(alloc=tester.base_alloc, genesis_gas_limit=9999999, min_gas_limit=5000, startgas=3141592): # genesis header = { "number": 0, "gas_limit": genesis_gas_limit, "gas_used": 0, "timestamp": 1467446877, "difficulty": 1, "uncles_hash": '0x'+utils.encode_hex(utils.sha3(rlp.encode([]))) } genesis = mk_basic_state(alloc, header, tester.get_env(None)) # tester tester.languages['vyper'] = compiler.Compiler() tester.STARTGAS = startgas chain = tester.Chain(alloc=alloc, genesis=genesis) chain.chain.env.config['MIN_GAS_LIMIT'] = min_gas_limit chain.mine(1) return chain
def test_genesis_from_state_snapshot(): """ Test if Chain could be initilaized from State snapshot """ # Customize a state k, v, k2, v2 = accounts() alloc = {v: {"balance": utils.denoms.ether * 1}} state = mk_basic_state(alloc, None) state.block_difficulty = 1 # Initialize another chain from state.to_snapshot() genesis = state.to_snapshot() new_chain = Chain(genesis=genesis) assert new_chain.state.trie.root_hash == state.trie.root_hash assert new_chain.state.block_difficulty == state.block_difficulty assert new_chain.head.number == state.block_number
def add_test_shard(self, shard_id, setup_urs_contracts=True, alloc=None): """Initial shard with fake accounts """ assert not self.chain.has_shard(shard_id) initial_state = mk_basic_state(base_alloc if alloc is None else alloc, None, self.chain.env) initial_state.delta_balance( used_receipt_store_utils.get_urs_contract(shard_id)['addr'], (10**9) * utils.denoms.ether) initial_state.commit() shard = ShardChain(shard_id=shard_id, initial_state=initial_state) self.chain.add_shard(shard) self.__init_shard_var(shard_id) if setup_urs_contracts: self.setup_and_deploy_urs_contracts(k0, shard_id)
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 custom_chain(tester, alloc={}, genesis_gas_limit=4712388, min_gas_limit=5000, startgas=3141592): # alloc for i in range(9): alloc[utils.int_to_addr(i)] = {'balance': 1} # genesis from ethereum.genesis_helpers import mk_basic_state header = { "number": 0, "gas_limit": genesis_gas_limit, "gas_used": 0, "timestamp": 1467446877, "difficulty": 1, "uncles_hash": '0x'+utils.encode_hex(utils.sha3(rlp.encode([]))) } genesis = mk_basic_state(alloc, header, tester.get_env(None)) # tester tester.languages['viper'] = compiler.Compiler() tester.STARTGAS = startgas c = tester.Chain(alloc=alloc, genesis=genesis) c.chain.env.config['MIN_GAS_LIMIT'] = min_gas_limit c.mine(1) return c
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