def test_genesis(db, alt_db): k, v, k2, v2 = accounts() blk = blocks.genesis(env(db), start_alloc={v: { "balance": utils.denoms.ether * 1 }}) # sr = blk.state_root assert blk.state.db.db == db.db db.put(blk.hash, rlp.encode(blk)) blk.state.db.commit() # assert sr in db db.commit() # assert sr in db blk2 = blocks.genesis(env(db), start_alloc={v: { "balance": utils.denoms.ether * 1 }}) blk3 = blocks.genesis(env(db)) assert blk == blk2 assert blk != blk3 blk2 = blocks.genesis(env(alt_db), start_alloc={v: { "balance": utils.denoms.ether * 1 }}) blk3 = blocks.genesis(env(alt_db)) assert blk == blk2 assert blk != blk3
def load_block_tests(data, db): """Load blocks from json file. :param data: the data from the json file as dictionary :param db: the db in which the blocks will be stored :raises: :exc:`ValueError` if the file contains invalid blocks :raises: :exc:`KeyError` if the file is missing required data fields :returns: a list of blocks in an ephem db """ scanners = ethereum.utils.scanners initial_alloc = {} for address, acct_state in data['pre'].items(): address = ethereum.utils.decode_hex(address) balance = scanners['int256b'](acct_state['balance'][2:]) nonce = acct_state['nonce'][2:] if nonce != '0': nonce = scanners['int256b'](nonce), initial_alloc[address] = { 'balance': balance, 'code': acct_state['code'], 'nonce': nonce, 'storage': acct_state['storage'] } genesis(db, initial_alloc) # builds the state trie genesis_block = rlp.decode(ethereum.utils.decode_hex( data['genesisRLP'][2:]), Block, db=db) blocks = [genesis_block] for blk in data['blocks']: rlpdata = ethereum.utils.decode_hex(blk['rlp'][2:]) blocks.append(rlp.decode(rlpdata, Block, db=db, parent=blocks[-1])) return blocks
def load_block_tests(data, db): """Load blocks from json file. :param data: the data from the json file as dictionary :param db: the db in which the blocks will be stored :raises: :exc:`ValueError` if the file contains invalid blocks :raises: :exc:`KeyError` if the file is missing required data fields :returns: a list of blocks in an ephem db """ scanners = ethereum.utils.scanners initial_alloc = {} for address, acct_state in data['pre'].items(): address = ethereum.utils.decode_hex(address) balance = scanners['int256b'](acct_state['balance'][2:]) nonce = scanners['int256b'](acct_state['nonce'][2:]) initial_alloc[address] = { 'balance': balance, 'code': acct_state['code'], 'nonce': nonce, 'storage': acct_state['storage'] } genesis(db, start_alloc=initial_alloc) # builds the state trie genesis_block = rlp.decode(ethereum.utils.decode_hex(data['genesisRLP'][2:]), Block, db=db) blocks = {genesis_block.hash: genesis_block} for blk in data['blocks']: rlpdata = ethereum.utils.decode_hex(blk['rlp'][2:]) assert ethereum.utils.decode_hex(blk['blockHeader']['parentHash']) in blocks parent = blocks[ethereum.utils.decode_hex(blk['blockHeader']['parentHash'])] block = rlp.decode(rlpdata, Block, db=db, parent=parent) blocks[block.hash] = block return sorted(blocks.values(), key=lambda b: b.number)
def load_block_tests(data, db): """Load blocks from json file. :param data: the data from the json file as dictionary :param db: the db in which the blocks will be stored :raises: :exc:`ValueError` if the file contains invalid blocks :raises: :exc:`KeyError` if the file is missing required data fields :returns: a list of blocks in an ephem db """ scanners = ethereum.utils.scanners initial_alloc = {} for address, acct_state in data['pre'].items(): address = ethereum.utils.decode_hex(address) balance = scanners['int256b'](acct_state['balance'][2:]) nonce = scanners['int256b'](acct_state['nonce'][2:]) initial_alloc[address] = { 'balance': balance, 'code': acct_state['code'], 'nonce': nonce, 'storage': acct_state['storage'] } genesis(db, initial_alloc) # builds the state trie genesis_block = rlp.decode(ethereum.utils.decode_hex(data['genesisRLP'][2:]), Block, db=db) blocks = [genesis_block] for blk in data['blocks']: rlpdata = ethereum.utils.decode_hex(blk['rlp'][2:]) blocks.append(rlp.decode(rlpdata, Block, db=db, parent=blocks[-1])) return blocks
def test_genesis_db(db, alt_db): k, v, k2, v2 = accounts() blk = blocks.genesis(env(db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) store_block(blk) blk2 = blocks.genesis(env(db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) blk3 = blocks.genesis(env(db)) assert blk == blk2 assert blk != blk3 blk2 = blocks.genesis(env(alt_db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) blk3 = blocks.genesis(env(alt_db)) assert blk == blk2 assert blk != blk3
def load_block_tests(data, db): """Load blocks from json file. :param data: the data from the json file as dictionary :param db: the db in which the blocks will be stored :raises: :exc:`ValueError` if the file contains invalid blocks :raises: :exc:`KeyError` if the file is missing required data fields :returns: a list of blocks in an ephem db """ b = genesis(db, data['pre']) gbh = data["genesisBlockHeader"] b.bloom = ethereum.utils.scanners['int256b'](gbh['bloom']) b.timestamp = ethereum.utils.scanners['int'](gbh['timestamp']) b.nonce = ethereum.utils.scanners['bin'](gbh['nonce']) b.extra_data = ethereum.utils.scanners['bin'](gbh['extraData']) b.gas_limit = ethereum.utils.scanners['int'](gbh['gasLimit']) b.gas_used = ethereum.utils.scanners['int'](gbh['gasUsed']) b.coinbase = ethereum.utils.decode_hex(gbh['coinbase']) b.difficulty = int(gbh['difficulty']) b.prevhash = ethereum.utils.scanners['bin'](gbh['parentHash']) b.mixhash = ethereum.utils.scanners['bin'](gbh['mixHash']) if (b.hash != ethereum.utils.scanners['bin'](gbh['hash']) or b.receipts.root_hash != ethereum.utils.scanners['bin'](gbh['receiptTrie']) or b.transactions.root_hash != ethereum.utils.scanners['bin'](gbh['transactionsTrie']) or b.state.root_hash != ethereum.utils.scanners['bin'](gbh['stateRoot']) or ethereum.utils.sha3rlp(b.uncles) != ethereum.utils.scanners['bin'](gbh['uncleHash']) or not b.header.check_pow()): raise ValueError('Invalid genesis block') blocks = [b] for blk in data['blocks']: rlpdata = ethereum.utils.decode_hex(blk['rlp'][2:]) blocks.append(rlp.decode(rlpdata, Block, db=db, parent=blocks[-1])) return blocks
def test_genesis(): for profile in ['frontier']: # fixme olympics config = dict(eth=dict()) # Set config values based on profile selection merge_dict(config, PROFILES[profile]) # Load genesis config update_config_from_genesis_json(config, config['eth']['genesis']) konfig.update_config_with_defaults( config, {'eth': { 'block': blocks.default_config }}) print config['eth'].keys() bc = config['eth']['block'] print bc.keys() env = Env(DB(), bc) genesis = blocks.genesis(env) print 'genesis.hash', genesis.hash.encode('hex') print 'expected', config['eth']['genesis_hash'] assert genesis.hash == config['eth']['genesis_hash'].decode('hex')
def test_genesis_config(): "test setting genesis alloc using the config" alloc = { '1' * 40: { 'wei': 1 }, # wei '2' * 40: { 'balance': 2 }, # balance '3' * 20: { 'balance': 3 }, # 20 bytes } config = dict(eth=dict(genesis=dict(alloc=alloc))) konfig.update_config_with_defaults( config, {'eth': { 'block': blocks.default_config }}) # Load genesis config update_config_from_genesis_json(config, config['eth']['genesis']) bc = config['eth']['block'] pprint(bc) env = Env(DB(), bc) genesis = blocks.genesis(env) for address, value_dict in alloc.items(): value = value_dict.values()[0] assert genesis.get_balance(address) == value
def test_genesis(db, alt_db): k, v, k2, v2 = accounts() blk = blocks.genesis(env(db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) # sr = blk.state_root assert blk.state.db.db == db.db db.put(blk.hash, rlp.encode(blk)) blk.state.db.commit() # assert sr in db db.commit() # assert sr in db blk2 = blocks.genesis(env(db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) blk3 = blocks.genesis(env(db)) assert blk == blk2 assert blk != blk3 blk2 = blocks.genesis(env(alt_db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) blk3 = blocks.genesis(env(alt_db)) assert blk == blk2 assert blk != blk3
def test_transfer(db): k, v, k2, v2 = accounts() blk = blocks.genesis(env(db), start_alloc={v: {"balance": utils.denoms.ether * 1}}) b_v = blk.get_balance(v) b_v2 = blk.get_balance(v2) value = 42 success = blk.transfer_value(v, v2, value) assert success assert blk.get_balance(v) == b_v - value assert blk.get_balance(v2) == b_v2 + value
def test_failing_transfer(db): k, v, k2, v2 = accounts() blk = blocks.genesis(db, {v: {"balance": utils.denoms.ether * 1}}) b_v = blk.get_balance(v) b_v2 = blk.get_balance(v2) value = utils.denoms.ether * 2 # should fail success = blk.transfer_value(v, v2, value) assert not success assert blk.get_balance(v) == b_v assert blk.get_balance(v2) == b_v2
def _initialize_blockchain(self, genesis=None): log.info('Initializing new chain') if not genesis: genesis = blocks.genesis(self.blockchain, difficulty=blocks.GENESIS_DIFFICULTY) log.info('new genesis', genesis_hash=genesis, difficulty=genesis.difficulty) self.index.add_block(genesis) self._store_block(genesis) assert genesis == blocks.get_block(self.blockchain, genesis.hash) self._update_head(genesis) assert genesis.hash in self self.commit()
def _initialize_blockchain(self, genesis=None): log.info('Initializing new chain') if not genesis: genesis = blocks.genesis(self.env) log.info('new genesis', genesis_hash=genesis, difficulty=genesis.difficulty) self.index.add_block(genesis) self._store_block(genesis) assert genesis == blocks.get_block(self.env, genesis.hash) self._update_head(genesis) assert genesis.hash in self self.commit()
def run_ethash_test(params, mode): if 'header' not in params: b = blocks.genesis(db) b.nonce = decode_hex(params['nonce']) b.number = params.get('number', 0) header = b.header params['header'] = encode_hex(rlp.encode(b.header)) else: header = blocks.BlockHeader(decode_hex(params['header'])) header_hash = header.mining_hash cache_size = ethash.get_cache_size(header.number) full_size = ethash.get_full_size(header.number) seed = b'\x00' * 32 for i in range(header.number // ethash_utils.EPOCH_LENGTH): seed = utils.sha3(seed) nonce = header.nonce assert len(nonce) == 8 assert len(seed) == 32 t1 = time.time() cache = ethash.mkcache(cache_size, seed) t2 = time.time() cache_hash = encode_hex(utils.sha3(ethash.serialize_cache(cache))) t6 = time.time() light_verify = ethash.hashimoto_light(full_size, cache, header_hash, nonce) t7 = time.time() # assert full_mine == light_mine out = { "seed": encode_hex(seed), "header_hash": encode_hex(header_hash), "nonce": encode_hex(nonce), "cache_size": cache_size, "full_size": full_size, "cache_hash": cache_hash, "mixhash": encode_hex(light_verify["mix digest"]), "result": encode_hex(light_verify["result"]), } if mode == FILL: header.mixhash = light_verify["mixhash"] params["header"] = encode_hex(rlp.encode(header)) for k, v in list(out.items()): params[k] = v return params elif mode == VERIFY: should, actual = header.mixhash, light_verify['mixhash'] assert should == actual, "Mismatch: mixhash %r %r" % (should, actual) for k, v in list(out.items()): assert params[k] == v, "Mismatch: " + k + ' %r %r' % (params[k], v) elif mode == TIME: return { "cache_gen": t2 - t1, "verification_time": t7 - t6 }
def load_block_tests(data, db): """Load blocks from json file. :param data: the data from the json file as dictionary :param db: the db in which the blocks will be stored :raises: :exc:`ValueError` if the file contains invalid blocks :raises: :exc:`KeyError` if the file is missing required data fields :returns: a list of blocks in an ephem db """ scanners = ethereum.utils.scanners initial_alloc = {} for address, acct_state in data['pre'].items(): address = ethereum.utils.decode_hex(address) balance = scanners['int256b'](acct_state['balance'][2:]) nonce = scanners['int256b'](acct_state['nonce'][2:]) initial_alloc[address] = { 'balance': balance, 'code': acct_state['code'], 'nonce': nonce, 'storage': acct_state['storage'] } env = ethereum.config.Env(db=db) genesis(env, start_alloc=initial_alloc) # builds the state trie genesis_block = rlp.decode(ethereum.utils.decode_hex( data['genesisRLP'][2:]), Block, env=env) blocks = {genesis_block.hash: genesis_block} for blk in data['blocks']: rlpdata = ethereum.utils.decode_hex(blk['rlp'][2:]) assert ethereum.utils.decode_hex( blk['blockHeader']['parentHash']) in blocks parent = blocks[ethereum.utils.decode_hex( blk['blockHeader']['parentHash'])] block = rlp.decode(rlpdata, Block, parent=parent, env=env) blocks[block.hash] = block return sorted(blocks.values(), key=lambda b: b.number)
def run_block_test(params): b = blocks.genesis(env, start_alloc=params["pre"]) gbh = params["genesisBlockHeader"] b.bloom = utils.scanners['int256b'](gbh["bloom"]) b.timestamp = utils.scanners['int'](gbh["timestamp"]) b.nonce = utils.scanners['bin'](gbh["nonce"]) b.extra_data = utils.scanners['bin'](gbh["extraData"]) b.gas_limit = utils.scanners['int'](gbh["gasLimit"]) b.gas_used = utils.scanners['int'](gbh["gasUsed"]) b.coinbase = utils.scanners['addr'](decode_hex(gbh["coinbase"])) b.difficulty = utils.parse_int_or_hex(gbh["difficulty"]) b.prevhash = utils.scanners['bin'](gbh["parentHash"]) b.mixhash = utils.scanners['bin'](gbh["mixHash"]) assert b.receipts.root_hash == \ utils.scanners['bin'](gbh["receiptTrie"]) assert b.transactions.root_hash == \ utils.scanners['bin'](gbh["transactionsTrie"]) assert utils.sha3rlp(b.uncles) == \ utils.scanners['bin'](gbh["uncleHash"]) h = encode_hex(b.state.root_hash) if h != str_to_bytes(gbh["stateRoot"]): raise Exception("state root mismatch") if b.hash != utils.scanners['bin'](gbh["hash"]): raise Exception("header hash mismatch") assert b.header.check_pow() blockmap = {b.hash: b} env.db.put(b.hash, rlp.encode(b)) for blk in params["blocks"]: if 'blockHeader' not in blk: try: rlpdata = decode_hex(blk["rlp"][2:]) blkparent = rlp.decode( rlp.encode(rlp.decode(rlpdata)[0]), blocks.BlockHeader).prevhash b2 = rlp.decode(rlpdata, blocks.Block, parent=blockmap[blkparent], env=env) success = b2.validate_uncles() except (ValueError, TypeError, AttributeError, VerificationFailed, DecodingError, DeserializationError, InvalidTransaction, KeyError): success = False assert not success else: rlpdata = decode_hex(blk["rlp"][2:]) blkparent = rlp.decode(rlp.encode(rlp.decode(rlpdata)[0]), blocks.BlockHeader).prevhash b2 = rlp.decode(rlpdata, blocks.Block, parent=blockmap[blkparent], env=env) assert b2.validate_uncles() blockmap[b2.hash] = b2 env.db.put(b2.hash, rlp.encode(b2))
def run_genesis_test(params, mode): params = copy.deepcopy(params) if 'difficulty' not in params: params['difficulty'] = int_to_hex(2 ** 34) if 'mixhash' not in params: params['mixhash'] = '0x' + '0' * 64 if 'nonce' not in params: params['nonce'] = '0x0000000000000042' if 'timestamp' not in params: params['timestamp'] = int_to_hex(5000) if 'parentHash' not in params: params['parentHash'] = '0x' + '0' * 64 if 'gasLimit' not in params: params['gasLimit'] = int_to_hex(5000) if 'extraData' not in params: params['extraData'] = '0x' if 'coinbase' not in params: params['coinbase'] = '0x' + '3' * 40 x = time.time() b = blocks.genesis(EphemDB(), start_alloc=params['alloc'], difficulty=parse_int_or_hex(params['difficulty']), timestamp=parse_int_or_hex(params['timestamp']), extra_data=decode_hex(remove_0x_head(params['extraData'])), gas_limit=parse_int_or_hex(params['gasLimit']), mixhash=decode_hex(remove_0x_head(params['mixhash'])), prevhash=decode_hex(remove_0x_head(params['parentHash'])), coinbase=decode_hex(remove_0x_head(params['coinbase'])), nonce=decode_hex(remove_0x_head(params['nonce']))) assert b.difficulty == parse_int_or_hex(params['difficulty']) assert b.timestamp == parse_int_or_hex(params['timestamp']) assert b.extra_data == decode_hex(remove_0x_head(params['extraData'])) assert b.gas_limit == parse_int_or_hex(params['gasLimit']) assert b.mixhash == decode_hex(remove_0x_head(params['mixhash'])) assert b.prevhash == decode_hex(remove_0x_head(params['parentHash'])) assert b.nonce == decode_hex(remove_0x_head(params['nonce'])) print(9) if mode == FILL: params['result'] = encode_hex(rlp.encode(b)) return params elif mode == VERIFY: assert params['result'] == encode_hex(rlp.encode(b)) elif mode == TIME: return { 'creation': time.time() - x }
def run_genesis_test(params, mode): params = copy.deepcopy(params) if 'difficulty' not in params: params['difficulty'] = int_to_hex(2**34) if 'mixhash' not in params: params['mixhash'] = '0x' + '0' * 64 if 'nonce' not in params: params['nonce'] = '0x0000000000000042' if 'timestamp' not in params: params['timestamp'] = int_to_hex(5000) if 'parentHash' not in params: params['parentHash'] = '0x' + '0' * 64 if 'gasLimit' not in params: params['gasLimit'] = int_to_hex(5000) if 'extraData' not in params: params['extraData'] = '0x' if 'coinbase' not in params: params['coinbase'] = '0x' + '3' * 40 x = time.time() b = blocks.genesis( EphemDB(), start_alloc=params['alloc'], difficulty=parse_int_or_hex(params['difficulty']), timestamp=parse_int_or_hex(params['timestamp']), extra_data=decode_hex(remove_0x_head(params['extraData'])), gas_limit=parse_int_or_hex(params['gasLimit']), mixhash=decode_hex(remove_0x_head(params['mixhash'])), prevhash=decode_hex(remove_0x_head(params['parentHash'])), coinbase=decode_hex(remove_0x_head(params['coinbase'])), nonce=decode_hex(remove_0x_head(params['nonce']))) assert b.difficulty == parse_int_or_hex(params['difficulty']) assert b.timestamp == parse_int_or_hex(params['timestamp']) assert b.extra_data == decode_hex(remove_0x_head(params['extraData'])) assert b.gas_limit == parse_int_or_hex(params['gasLimit']) assert b.mixhash == decode_hex(remove_0x_head(params['mixhash'])) assert b.prevhash == decode_hex(remove_0x_head(params['parentHash'])) assert b.nonce == decode_hex(remove_0x_head(params['nonce'])) print 9 if mode == FILL: params['result'] = encode_hex(rlp.encode(b)) return params elif mode == VERIFY: assert params['result'] == encode_hex(rlp.encode(b)) elif mode == TIME: return {'creation': time.time() - x}
def __init__(self, app): self.config = app.config sce = self.config['eth'] if int(sce['pruning']) >= 0: self.db = RefcountDB(app.services.db) if "I am not pruning" in self.db.db: raise Exception("This database was initialized as non-pruning." " Kinda hard to start pruning now.") self.db.ttl = int(sce['pruning']) self.db.db.put("I am pruning", "1") else: self.db = app.services.db if "I am pruning" in self.db: raise Exception("This database was initialized as pruning." " Kinda hard to stop pruning now.") self.db.put("I am not pruning", "1") assert self.db is not None super(ChainService, self).__init__(app) log.info('initializing chain') coinbase = app.services.accounts.coinbase if sce['genesis']: log.info('loading genesis', path=sce['genesis']) _json = json.load(open(sce['genesis'])) else: log.info('loaded default genesis alloc') _json = None _genesis = genesis(self.db, json=_json) log.info('created genesis block', hash=encode_hex(_genesis.hash)) self.chain = Chain(self.db, genesis=_genesis, new_head_cb=self._on_new_head, coinbase=coinbase) log.info('chain at', number=self.chain.head.number) self.synchronizer = Synchronizer(self, force_sync=None) self.block_queue = Queue(maxsize=self.block_queue_size) self.transaction_queue = Queue(maxsize=self.transaction_queue_size) self.add_blocks_lock = False self.add_transaction_lock = gevent.lock.Semaphore() self.broadcast_filter = DuplicatesFilter() self.on_new_head_cbs = [] self.on_new_head_candidate_cbs = [] self.newblock_processing_times = deque(maxlen=1000)
def __init__(self, app): self.config = app.config sce = self.config['eth'] if int(sce['pruning']) >= 0: self.db = RefcountDB(app.services.db) if "I am not pruning" in self.db.db: raise Exception("This database was initialized as non-pruning." " Kinda hard to start pruning now.") self.db.ttl = int(sce['pruning']) self.db.db.put("I am pruning", "1") else: self.db = app.services.db if "I am pruning" in self.db: raise Exception("This database was initialized as pruning." " Kinda hard to stop pruning now.") self.db.put("I am not pruning", "1") assert self.db is not None super(ChainService, self).__init__(app) log.info('initializing chain') coinbase = app.services.accounts.coinbase try: _json = json.load(open(sce['genesis'])) log.info('loading genesis', filename=sce['genesis']) except Exception as e: log.warn(str(e)) _json = GENESIS_JSON log.info('loaded default genesis alloc') _genesis = genesis(self.db, json=_json) log.info('created genesis block', hash=encode_hex(_genesis.hash)) self.chain = Chain(self.db, genesis=_genesis, new_head_cb=self._on_new_head, coinbase=coinbase) log.info('chain at', number=self.chain.head.number) self.synchronizer = Synchronizer(self, force_sync=None) self.block_queue = Queue(maxsize=self.block_queue_size) self.transaction_queue = Queue(maxsize=self.transaction_queue_size) self.add_blocks_lock = False self.add_transaction_lock = gevent.lock.Semaphore() self.broadcast_filter = DuplicatesFilter() self.on_new_head_cbs = [] self.on_new_head_candidate_cbs = [] self.newblock_processing_times = deque(maxlen=1000)
def test_profile(profile): config = dict(eth=dict()) konfig.update_config_with_defaults( config, {'eth': { 'block': blocks.default_config }}) # Set config values based on profile selection merge_dict(config, PROFILES[profile]) # Load genesis config update_config_from_genesis_json(config, config['eth']['genesis']) bc = config['eth']['block'] pprint(bc) env = Env(DB(), bc) genesis = blocks.genesis(env) assert genesis.hash.encode('hex') == config['eth']['genesis_hash']
def __init__(self, app): self.config = app.config self.db = app.services.db assert self.db is not None super(ChainService, self).__init__(app) log.info('initializing chain') coinbase = app.services.accounts.coinbase _genesis = genesis(self.db, nonce=self.config['eth']['genesis_nonce'].decode('hex')) self.chain = Chain(self.db, genesis=_genesis, new_head_cb=self._on_new_head, coinbase=coinbase) log.info('chain at', number=self.chain.head.number) self.synchronizer = Synchronizer(self, force_sync=None) self.block_queue = Queue(maxsize=self.block_queue_size) self.transaction_queue = Queue(maxsize=self.transaction_queue_size) self.add_blocks_lock = False self.add_transaction_lock = gevent.lock.Semaphore() self.broadcast_filter = DuplicatesFilter() self.on_new_head_cbs = [] self.on_new_head_candidate_cbs = [] self.newblock_processing_times = deque(maxlen=1000)
def test_genesis(): for profile in ['frontier']: # fixme olympics config = dict(eth=dict()) # Set config values based on profile selection merge_dict(config, PROFILES[profile]) # Load genesis config update_config_from_genesis_json(config, config['eth']['genesis']) konfig.update_config_with_defaults(config, {'eth': {'block': blocks.default_config}}) print config['eth'].keys() bc = config['eth']['block'] print bc.keys() env = Env(DB(), bc) genesis = blocks.genesis(env) print 'genesis.hash', genesis.hash.encode('hex') print 'expected', config['eth']['genesis_hash'] assert genesis.hash == config['eth']['genesis_hash'].decode('hex')
def __init__(self, num_accounts=len(keys)): self.temp_data_dir = tempfile.mkdtemp() self.db = db.EphemDB() self.env = Env(self.db) self.last_tx = None initial_balances = {} for i in range(num_accounts): account = accounts[i] initial_balances[account] = {'wei': 10**24} for i in range(1, 5): address = int_to_addr(i) initial_balances[address] = {'wei': 1} self.block = blocks.genesis( self.env, start_alloc=initial_balances, ) self.blocks = [self.block] self.block.timestamp = 1410973349 self.block.coinbase = DEFAULT_ACCOUNT self.block.gas_limit = 10**9
def __init__(self, num_accounts=len(keys)): self.temp_data_dir = tempfile.mkdtemp() self.db = db.EphemDB() self.env = Env(self.db) self.last_tx = None initial_balances = {} for i in range(num_accounts): account = accounts[i] initial_balances[account] = {'wei': 10 ** 24} for i in range(1, 5): address = int_to_addr(i) initial_balances[address] = {'wei': 1} self.block = blocks.genesis( self.env, start_alloc=initial_balances, ) self.blocks = [self.block] self.block.timestamp = 1410973349 self.block.coinbase = DEFAULT_ACCOUNT self.block.gas_limit = 10 ** 9
def test_serialize_block(db): blk = blocks.genesis(env(db)) tb_blk = blocks.BlockHeader.from_block_rlp(rlp.encode(blk)) assert blk.hash == tb_blk.hash assert blk.number == tb_blk.number
def test_genesis_hash(genesis_fixture): genesis = blocks.genesis(new_db()) assert genesis.hex_hash() == genesis_fixture['genesis_hash']
def test_alloc_too_big(db): k, v, k2, v2 = accounts() blk = None with pytest.raises(ValueError): blk = blocks.genesis(env(db), start_alloc={v: {"balance": 2**256}}) assert blk is None
def mkgenesis(initial_alloc={}, db=None): assert db is not None o = blocks.genesis(env(db), start_alloc=initial_alloc, difficulty=1) assert o.difficulty == 1 return o
def test_genesis_initial_alloc(genesis_fixture): genesis = blocks.genesis(new_db()) for k, v in list(blocks.GENESIS_INITIAL_ALLOC.items()): assert genesis.get_balance(k) == v.get("balance", 0) or v.get("wei", 0)
def mkgenesis(initial_alloc={}, db=None): assert db is not None return blocks.genesis(db, initial_alloc, difficulty=1)
def test_genesis_initial_alloc(genesis_fixture): env = new_env() genesis = blocks.genesis(env) for k, v in list(env.config['GENESIS_INITIAL_ALLOC'].items()): assert genesis.get_balance(k) == v.get("balance", 0) or v.get("wei", 0)
with open(fn, 'r') as f: genesis_fixture = json.load(f) assert genesis_fixture is not None, "Could not read genesishashtest.json from fixtures. Make sure you did 'git submodule init'!" # FIXME: assert that link is uptodate for k in ('genesis_rlp_hex', 'genesis_state_root', 'genesis_hash'): assert k in genesis_fixture return fixture_to_bytes(genesis_fixture) @pytest.mark.xfail # code not in sync with genesis fixtures def test_genesis_state_root(genesis_fixture): genesis = blocks.genesis(new_env()) assert encode_hex(genesis.state_root) == utils.to_string(genesis_fixture['genesis_state_root']) def test_genesis_initial_alloc(genesis_fixture): env = new_env() genesis = blocks.genesis(env) for k, v in list(env.config['GENESIS_INITIAL_ALLOC'].items()): assert genesis.get_balance(k) == v.get("balance", 0) or v.get("wei", 0) @pytest.mark.xfail # code not in sync with genesis fixtures def test_genesis_hash(genesis_fixture): genesis = blocks.genesis(new_env()) assert genesis.hex_hash() == utils.to_string(genesis_fixture['genesis_hash']) if __name__ == '__main__': print('current genesis:', blocks.genesis(new_env()).hex_hash())
def test_deserialize_commit(db): k, v, k2, v2 = accounts() blk = blocks.genesis(env(db)) db.put(blk.hash, rlp.encode(blk)) db.commit() assert blk == blocks.get_block(env(db), blk.hash)
def test_genesis_state_root(genesis_fixture): genesis = blocks.genesis(new_db()) assert encode_hex(genesis.state_root) == genesis_fixture['genesis_state_root']
def mkquickgenesis(initial_alloc={}, db=None): "set INITIAL_DIFFICULTY to a value that is quickly minable" assert db is not None return blocks.genesis(env(db), start_alloc=initial_alloc, difficulty=1)
def test_genesis_state_root(genesis_fixture): genesis = blocks.genesis(new_db()) assert encode_hex( genesis.state_root) == genesis_fixture['genesis_state_root']
def run_block_test(params, config_overrides = {}): b = blocks.genesis(env, start_alloc=params["pre"]) gbh = params["genesisBlockHeader"] b.bloom = utils.scanners['int256b'](gbh["bloom"]) b.timestamp = utils.scanners['int'](gbh["timestamp"]) b.nonce = utils.scanners['bin'](gbh["nonce"]) b.extra_data = utils.scanners['bin'](gbh["extraData"]) b.gas_limit = utils.scanners['int'](gbh["gasLimit"]) b.gas_used = utils.scanners['int'](gbh["gasUsed"]) b.coinbase = utils.scanners['addr'](decode_hex(gbh["coinbase"])) b.difficulty = utils.parse_int_or_hex(gbh["difficulty"]) b.prevhash = utils.scanners['bin'](gbh["parentHash"]) b.mixhash = utils.scanners['bin'](gbh["mixHash"]) assert b.receipts.root_hash == \ utils.scanners['bin'](gbh["receiptTrie"]) assert b.transactions.root_hash == \ utils.scanners['bin'](gbh["transactionsTrie"]) assert utils.sha3rlp(b.uncles) == \ utils.scanners['bin'](gbh["uncleHash"]) h = encode_hex(b.state.root_hash) if h != str_to_bytes(gbh["stateRoot"]): raise Exception("state root mismatch") if b.hash != utils.scanners['bin'](gbh["hash"]): raise Exception("header hash mismatch") # assert b.header.check_pow() blockmap = {b.hash: b} env.db.put(b.hash, rlp.encode(b)) old_config = copy.deepcopy(env.config) for k, v in config_overrides.items(): env.config[k] = v b2 = None for blk in params["blocks"]: if 'blockHeader' not in blk: try: rlpdata = decode_hex(blk["rlp"][2:]) blkparent = rlp.decode( rlp.encode(rlp.decode(rlpdata)[0]), blocks.BlockHeader).prevhash b2 = rlp.decode(rlpdata, blocks.Block, parent=blockmap[blkparent], env=env) success = b2.validate_uncles() except (ValueError, TypeError, AttributeError, VerificationFailed, DecodingError, DeserializationError, InvalidTransaction, KeyError): success = False assert not success else: rlpdata = decode_hex(blk["rlp"][2:]) blkparent = rlp.decode(rlp.encode(rlp.decode(rlpdata)[0]), blocks.BlockHeader).prevhash b2 = rlp.decode(rlpdata, blocks.Block, parent=blockmap[blkparent], env=env) assert b2.validate_uncles() blockmap[b2.hash] = b2 env.db.put(b2.hash, rlp.encode(b2)) if b2: print('Block %d with state root %s' % (b2.number, encode_hex(b2.state.root_hash))) # blkdict = b.to_dict(False, True, False, True) # assert blk["blockHeader"] == \ # translate_keys(blkdict["header"], translator_list, lambda y, x: x, []) # assert blk["transactions"] == \ # [translate_keys(t, translator_list, valueconv, ['hash']) # for t in blkdict["transactions"]] # assert blk["uncleHeader"] == \ # [translate_keys(u, translator_list, lambda x: x, []) # for u in blkdict["uncles"]] env.config = old_config
def test_deserialize(db): k, v, k2, v2 = accounts() blk = blocks.genesis(db) db.put(blk.hash, rlp.encode(blk)) assert blk == blocks.get_block(db, blk.hash)
def test_genesis_state_root(genesis_fixture): genesis = blocks.genesis(new_env()) assert encode_hex(genesis.state_root) == utils.to_string(genesis_fixture['genesis_state_root'])
def run_block_test(params, config_overrides={}): b = blocks.genesis(env, start_alloc=params["pre"]) gbh = params["genesisBlockHeader"] b.bloom = utils.scanners['int256b'](gbh["bloom"]) b.timestamp = utils.scanners['int'](gbh["timestamp"]) b.nonce = utils.scanners['bin'](gbh["nonce"]) b.extra_data = utils.scanners['bin'](gbh["extraData"]) b.gas_limit = utils.scanners['int'](gbh["gasLimit"]) b.gas_used = utils.scanners['int'](gbh["gasUsed"]) b.coinbase = utils.scanners['addr'](decode_hex(gbh["coinbase"])) b.difficulty = utils.parse_int_or_hex(gbh["difficulty"]) b.prevhash = utils.scanners['bin'](gbh["parentHash"]) b.mixhash = utils.scanners['bin'](gbh["mixHash"]) assert b.receipts.root_hash == \ utils.scanners['bin'](gbh["receiptTrie"]) assert b.transactions.root_hash == \ utils.scanners['bin'](gbh["transactionsTrie"]) assert utils.sha3rlp(b.uncles) == \ utils.scanners['bin'](gbh["uncleHash"]) h = encode_hex(b.state.root_hash) if h != str_to_bytes(gbh["stateRoot"]): raise Exception("state root mismatch") if b.hash != utils.scanners['bin'](gbh["hash"]): raise Exception("header hash mismatch") # assert b.header.check_pow() blockmap = {b.hash: b} env.db.put(b.hash, rlp.encode(b)) old_config = copy.deepcopy(env.config) try: for k, v in config_overrides.items(): env.config[k] = v b2 = None for blk in params["blocks"]: if 'blockHeader' not in blk: try: rlpdata = decode_hex(blk["rlp"][2:]) blkparent = rlp.decode(rlp.encode(rlp.decode(rlpdata)[0]), blocks.BlockHeader).prevhash b2 = rlp.decode(rlpdata, blocks.Block, parent=blockmap[blkparent], env=env) success = b2.validate_uncles() except (ValueError, TypeError, AttributeError, VerificationFailed, DecodingError, DeserializationError, InvalidTransaction, KeyError): success = False assert not success else: rlpdata = decode_hex(blk["rlp"][2:]) blkparent = rlp.decode(rlp.encode(rlp.decode(rlpdata)[0]), blocks.BlockHeader).prevhash b2 = rlp.decode(rlpdata, blocks.Block, parent=blockmap[blkparent], env=env) assert b2.validate_uncles() blockmap[b2.hash] = b2 env.db.put(b2.hash, rlp.encode(b2)) if b2: print('Block %d with state root %s' % (b2.number, encode_hex(b2.state.root_hash))) # blkdict = b.to_dict(False, True, False, True) # assert blk["blockHeader"] == \ # translate_keys(blkdict["header"], translator_list, lambda y, x: x, []) # assert blk["transactions"] == \ # [translate_keys(t, translator_list, valueconv, ['hash']) # for t in blkdict["transactions"]] # assert blk["uncleHeader"] == \ # [translate_keys(u, translator_list, lambda x: x, []) # for u in blkdict["uncles"]] finally: env.config = old_config
def test_genesis_hash(genesis_fixture): genesis = blocks.genesis(new_env()) assert genesis.hex_hash() == utils.to_string(genesis_fixture['genesis_hash'])