def test_eth_syncing(self, web3): syncing = web3.eth.syncing assert is_boolean(syncing) or is_dict(syncing) if is_boolean(syncing): assert syncing is False elif is_dict(syncing): assert 'startingBlock' in syncing assert 'currentBlock' in syncing assert 'highestBlock' in syncing assert is_integer(syncing['startingBlock']) assert is_integer(syncing['currentBlock']) assert is_integer(syncing['highestBlock'])
def test_eth_getTransactionByBlockNumberAndIndex(self, web3, block_with_txn, mined_txn_hash): transaction = web3.eth.getTransactionByBlock(block_with_txn['number'], 0) assert is_dict(transaction) assert transaction['hash'] == HexBytes(mined_txn_hash)
def generate_cache_key(value): """ Generates a cache key for the *args and **kwargs """ if is_bytes(value): return hashlib.md5(value).hexdigest() elif is_text(value): return generate_cache_key(to_bytes(text=value)) elif is_boolean(value) or is_null(value) or is_number(value): return generate_cache_key(repr(value)) elif is_dict(value): return generate_cache_key(( (key, value[key]) for key in sorted(value.keys()) )) elif is_list_like(value) or isinstance(value, Generator): return generate_cache_key("".join(( generate_cache_key(item) for item in value ))) else: raise TypeError("Cannot generate cache key for value {0} of type {1}".format( value, type(value), ))
def test_eth_getTransactionReceipt_mined(self, web3, block_with_txn, mined_txn_hash): receipt = web3.eth.getTransactionReceipt(mined_txn_hash) assert is_dict(receipt) assert receipt['blockNumber'] == block_with_txn['number'] assert receipt['blockHash'] == block_with_txn['hash'] assert receipt['transactionIndex'] == 0 assert receipt['transactionHash'] == HexBytes(mined_txn_hash)
def middleware(method, params): response = make_request(method, params) if 'result' in response: result = response['result'] if is_dict(result) and not isinstance(result, AttributeDict): return assoc(response, 'result', AttributeDict.recursive(result)) else: return response else: return response
def normalize_keys(keyfile_json): for key, value in keyfile_json.items(): if is_string(key): norm_key = key.lower() else: norm_key = key if is_dict(value): norm_value = normalize_keys(value) else: norm_value = value yield norm_key, norm_value
def decrypt(keyfile_json, password, mode='ECDSA'): ''' Decrypts a private key that was encrypted using an awake006 client or :meth:`~Account.encrypt`. :param keyfile_json: The encrypted key :type keyfile_json: dict or str :param str password: The password that was used to encrypt the key :returns: the raw private key :rtype: ~hexbytes.main.HexBytes .. code-block:: python >>> encrypted = { 'address': '5ce9454909639d2d17a3f753ce7d93fa0b9ab12e', 'crypto': {'cipher': 'aes-128-ctr', 'cipherparams': {'iv': '78f214584844e0b241b433d7c3bb8d5f'}, 'ciphertext': 'd6dbb56e4f54ba6db2e8dc14df17cb7352fdce03681dd3f90ce4b6c1d5af2c4f', 'kdf': 'pbkdf2', 'kdfparams': {'c': 1000000, 'dklen': 32, 'prf': 'hmac-sha256', 'salt': '45cf943b4de2c05c2c440ef96af914a2'}, 'mac': 'f5e1af09df5ded25c96fcf075ada313fb6f79735a914adc8cb02e8ddee7813c3'}, 'id': 'b812f3f9-78cc-462a-9e89-74418aa27cb0', 'version': 3} >>> import getpass >>> Account.decrypt(encrypted, getpass.getpass()) HexBytes('0xb25c7db31feed9122727bf0939dc769a96564b2de4c4726d035b36ecf1e5b364') ''' if isinstance(keyfile_json, str): pass if keyfile_json[1:3] in [MIANNETHRP, TESTNETHRP]: try: keyfile = json.load(keyfile_json + '.json') except Exception as rep: print(rep) keyfile = json.load(keyfile_json) else: keyfile = json.loads(keyfile_json) elif is_dict(keyfile_json): keyfile = keyfile_json else: raise TypeError( "The keyfile should be supplied as a JSON string, or a dictionary." ) password_bytes = text_if_str(to_bytes, password) return HexBytes(decode_keyfile_json(keyfile, password_bytes, mode))
def validate_abi(abi): """ Helper function for validating an ABI """ if not is_list_like(abi): raise ValueError("'abi' is not a list") if not all(is_dict(e) for e in abi): raise ValueError("'abi' is not a list of dictionaries") functions = filter_by_type('function', abi) selectors = groupby(compose(encode_hex, function_abi_to_4byte_selector), functions) duplicates = valfilter(lambda funcs: len(funcs) > 1, selectors) if duplicates: raise ValueError('Abi contains functions with colliding selectors. ' 'Functions {0}'.format( _prepare_selector_collision_msg(duplicates)))
def test_eth_getTransactionReceipt_with_log_entry(self, web3, block_with_txn_with_log, emitter_contract, txn_hash_with_log): receipt = web3.eth.getTransactionReceipt(txn_hash_with_log) assert is_dict(receipt) assert receipt['blockNumber'] == block_with_txn_with_log['number'] assert receipt['blockHash'] == block_with_txn_with_log['hash'] assert receipt['transactionIndex'] == 0 assert receipt['transactionHash'] == HexBytes(txn_hash_with_log) assert len(receipt['logs']) == 1 log_entry = receipt['logs'][0] assert log_entry['blockNumber'] == block_with_txn_with_log['number'] assert log_entry['blockHash'] == block_with_txn_with_log['hash'] assert log_entry['logIndex'] == 0 assert is_same_address(log_entry['address'], emitter_contract.address) assert log_entry['transactionIndex'] == 0 assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
def setup_chain_state(web3): coinbase = web3.eth.coinbase assert is_same_address(coinbase, common.COINBASE) # # Math Contract # math_contract_factory = web3.eth.contract( abi=MATH_ABI, bytecode=MATH_BYTECODE, ) math_deploy_receipt = common.deploy_contract(web3, 'math', math_contract_factory) assert is_dict(math_deploy_receipt) # # Emitter Contract # emitter_contract_factory = web3.eth.contract( abi=EMITTER_ABI, bytecode=EMITTER_BYTECODE, ) emitter_deploy_receipt = common.deploy_contract(web3, 'emitter', emitter_contract_factory) emitter_contract = emitter_contract_factory( emitter_deploy_receipt['contractAddress']) txn_hash_with_log = emitter_contract.functions.logDouble( which=EMITTER_ENUM['LogDoubleWithIndex'], arg0=12345, arg1=54321, ).transact({ 'from': web3.eth.coinbase, }) print('TXN_HASH_WITH_LOG:', txn_hash_with_log) txn_receipt_with_log = common.mine_transaction_hash( web3, txn_hash_with_log) block_with_log = web3.eth.getBlock(txn_receipt_with_log['blockHash']) print('BLOCK_HASH_WITH_LOG:', block_with_log['hash']) # # Empty Block # empty_block_number = common.mine_block(web3) print('MINED_EMPTY_BLOCK') empty_block = web3.eth.getBlock(empty_block_number) assert is_dict(empty_block) assert not empty_block['transactions'] print('EMPTY_BLOCK_HASH:', empty_block['hash']) # # Block with Transaction # web3.personal.unlockAccount(coinbase, common.KEYFILE_PW) web3.miner.start(1) mined_txn_hash = web3.eth.sendTransaction({ 'from': coinbase, 'to': coinbase, 'value': 1, 'gas': 21000, 'gas_price': web3.eth.gasPrice, }) mined_txn_receipt = common.mine_transaction_hash(web3, mined_txn_hash) print('MINED_TXN_HASH:', mined_txn_hash) block_with_txn = web3.eth.getBlock(mined_txn_receipt['blockHash']) print('BLOCK_WITH_TXN_HASH:', block_with_txn['hash']) geth_fixture = { 'math_deploy_txn_hash': math_deploy_receipt['transactionHash'], 'math_address': math_deploy_receipt['contractAddress'], 'emitter_deploy_txn_hash': emitter_deploy_receipt['transactionHash'], 'emitter_address': emitter_deploy_receipt['contractAddress'], 'txn_hash_with_log': txn_hash_with_log, 'block_hash_with_log': block_with_log['hash'], 'empty_block_hash': empty_block['hash'], 'mined_txn_hash': mined_txn_hash, 'block_with_txn_hash': block_with_txn['hash'], } return geth_fixture
def block_with_txn_with_log(web3, geth_fixture_data): block = web3.eth.getBlock(geth_fixture_data['block_hash_with_log']) assert is_dict(block) return block
def empty_block(web3, geth_fixture_data): block = web3.eth.getBlock(geth_fixture_data['empty_block_hash']) assert is_dict(block) return block
def test_eth_getTransactionByHash(self, web3, mined_txn_hash): transaction = web3.eth.getTransaction(mined_txn_hash) assert is_dict(transaction) assert transaction['hash'] == HexBytes(mined_txn_hash)
def test_eth_getTransactionByHash_contract_creation( self, web3, math_contract_deploy_txn_hash): transaction = web3.eth.getTransaction(math_contract_deploy_txn_hash) assert is_dict(transaction) assert transaction['to'] is None, "to field is %r" % transaction['to']
def block_with_txn(web3, parity_fixture_data): block = web3.eth.getBlock(parity_fixture_data['block_with_txn_hash']) assert is_dict(block) return block
def emitter_contract(web3, emitter_contract_factory, emitter_contract_deploy_txn_hash): deploy_receipt = web3.eth.waitForTransactionReceipt(emitter_contract_deploy_txn_hash) assert is_dict(deploy_receipt) contract_address = deploy_receipt['contractAddress'] assert is_checksum_address(contract_address) return emitter_contract_factory(contract_address)
def is_array_of_dicts(value): if not is_list_like(value): return False return all((is_dict(item) for item in value))