def middleware(method, params): if method in request_formatters: formatter = request_formatters[method] formatted_params = formatter(params) response = make_request(method, formatted_params) else: response = make_request(method, params) if 'result' in response and method in result_formatters: formatter = result_formatters[method] formatted_response = assoc( response, 'result', formatter(response['result']), ) return formatted_response elif 'error' in response and method in error_formatters: formatter = error_formatters[method] formatted_response = assoc( response, 'error', formatter(response['error']), ) return formatted_response else: return response
def test_assoc(): assert assoc({}, "a", 1) == {"a": 1} assert assoc({"a": 1}, "a", 3) == {"a": 3} assert assoc({"a": 1}, "b", 3) == {"a": 1, "b": 3} # Verify immutability: d = {"x": 1} oldd = d assoc(d, "x", 2) assert d is oldd
def test_assoc(): assert assoc({}, "a", 1) == {"a": 1} assert assoc({"a": 1}, "a", 3) == {"a": 3} assert assoc({"a": 1}, "b", 3) == {"a": 1, "b": 3} # Verify immutability: d = {'x': 1} oldd = d assoc(d, 'x', 2) assert d is oldd
def test_assoc(self): D, kw = self.D, self.kw assert assoc(D({}), "a", 1, **kw) == D({"a": 1}) assert assoc(D({"a": 1}), "a", 3, **kw) == D({"a": 3}) assert assoc(D({"a": 1}), "b", 3, **kw) == D({"a": 1, "b": 3}) # Verify immutability: d = D({'x': 1}) oldd = d assoc(d, 'x', 2, **kw) assert d is oldd
def add_direct_dependencies_to_compiled_contracts(compiled_contracts): for contract_data in compiled_contracts: direct_dependencies = set(ref['name'] for ref in itertools.chain( contract_data['linkrefs'], contract_data['linkrefs_runtime'])) yield assoc(contract_data, key='direct_dependencies', value=direct_dependencies)
async def read_meta(sftp, dir_meta): """ Read metadata from a reMarkable tablet. """ await sftp.mget(BASE_DIR + '/*.metadata', dir_meta) await sftp.mget(BASE_DIR + '/*.content', dir_meta) to_uuid = compose( operator.itemgetter(0), os.path.splitext, os.path.basename, ) load_json = compose(json.load, open) files_content = glob.glob(dir_meta + '/*.content') files_content = {to_uuid(fn): fn for fn in files_content} files = glob.glob(dir_meta + '/*.metadata') files = ((fn, to_uuid(fn)) for fn in files) files = ((fn, u) for fn, u in files if u in files_content) # load metadata file and content file data = ((u, load_json(fn), load_json(files_content[u])) for fn, u in files) meta = {u: assoc(m, 'content', c) for u, m, c in data} return resolve_uuid(meta)
def test_manual_mine_pending_transactions(self, eth_tester, test_transaction): accounts = eth_tester.get_accounts() assert accounts, "No accounts available for transaction sending" complete_transaction = assoc(test_transaction, 'from', accounts[0]) self.skip_if_no_evm_execution() eth_tester.mine_blocks() eth_tester.disable_auto_mine_transactions() txn_hash = eth_tester.send_transaction(complete_transaction) with pytest.raises(TransactionNotFound): eth_tester.get_transaction_receipt(txn_hash) pending_transaction = eth_tester.get_transaction_by_hash(txn_hash) self._check_transactions(complete_transaction, pending_transaction) eth_tester.mine_block() receipt = eth_tester.get_transaction_receipt(txn_hash) assert receipt['transaction_hash'] == txn_hash assert receipt['block_number'] mined_transaction = eth_tester.get_transaction_by_hash(txn_hash) self._check_transactions(complete_transaction, mined_transaction)
def serialize_block(block, transaction_serializer, is_pending): serialized_transactions = tuple( transaction_serializer(transaction, block, transaction_index, is_pending=is_pending) for transaction_index, transaction in enumerate(block['transactions']) ) return assoc(block, 'transactions', serialized_transactions)
def sendTransaction(self, transaction): # TODO: move to middleware if 'from' not in transaction and is_address(self.defaultAccount): transaction = assoc(transaction, 'from', self.defaultAccount) # TODO: move gas estimation in middleware if 'gas' not in transaction: transaction = assoc( transaction, 'gas', get_buffered_gas_estimate(self.web3, transaction), ) return self.web3.manager.request_blocking( "eth_sendTransaction", [transaction], )
def test_keystore_sign_transaction(good_signer, mock_account): transaction_dict = assoc(TRANSACTION_DICT, 'from', value=mock_account.address) signed_transaction = good_signer.sign_transaction(transaction_dict=transaction_dict) assert isinstance(signed_transaction, HexBytes) # assert valid transaction transaction = Transaction.from_bytes(signed_transaction) assert to_checksum_address(transaction.to) == transaction_dict['to']
def sendTransaction(self, transaction): # TODO: move to middleware if 'from' not in transaction and is_checksum_address(self.defaultAccount): transaction = assoc(transaction, 'from', self.defaultAccount) # TODO: move gas estimation in middleware if 'gas' not in transaction: transaction = assoc( transaction, 'gas', get_buffered_gas_estimate(self.web3, transaction), ) return self.web3.manager.request_blocking( "eth_sendTransaction", [transaction], )
def estimateGas(self, transaction): # TODO: move to middleware if 'from' not in transaction and is_address(self.defaultAccount): transaction = assoc(transaction, 'from', self.defaultAccount) return self.web3.manager.request_blocking( "eth_estimateGas", [transaction], )
def estimateGas(self, transaction): # TODO: move to middleware if 'from' not in transaction and is_checksum_address(self.defaultAccount): transaction = assoc(transaction, 'from', self.defaultAccount) return self.web3.manager.request_blocking( "eth_estimateGas", [transaction], )
def middleware(method, params): if method == 'eth_sendTransaction': transaction = params[0] if 'gasPrice' not in transaction: generated_gas_price = web3.eth.generateGasPrice(transaction) if generated_gas_price is not None: transaction = assoc(transaction, 'gasPrice', generated_gas_price) return make_request(method, [transaction]) return make_request(method, params)
def test_get_transaction_receipt_pre_byzantium(self, eth_tester, test_transaction): accounts = eth_tester.get_accounts() assert accounts, "No accounts available for transaction sending" transaction = assoc(test_transaction, 'from', accounts[0]) txn_hash = eth_tester.send_transaction(transaction) txn = eth_tester.get_transaction_receipt(txn_hash) assert 'status' not in txn
def middleware(method, params): if method == 'eth_sendTransaction': transaction = params[0] if 'gasPrice' not in transaction: generated_gas_price = webu.eth.generateGasPrice(transaction) if generated_gas_price is not None: transaction = assoc(transaction, 'gasPrice', generated_gas_price) return make_request(method, [transaction]) return make_request(method, params)
def _send_and_check_transaction(self, eth_tester, test_transaction, _from): transaction = assoc(test_transaction, 'from', _from) txn_hash = eth_tester.send_transaction(transaction) txn = eth_tester.get_transaction_by_hash(txn_hash) assert is_same_address(txn['from'], transaction['from']) assert is_same_address(txn['to'], transaction['to']) assert txn['gas_price'] == transaction['gas_price'] assert txn['gas'] == transaction['gas'] assert txn['value'] == transaction['value']
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 create_transaction(transaction, block, transaction_index, is_pending, overrides=None): filled_txn = _fill_transaction(transaction, block, transaction_index, is_pending, overrides) if 'hash' in filled_txn: return filled_txn else: return assoc(filled_txn, 'hash', fake_rlp_hash(filled_txn))
def contract(): eth_tester = EthereumTester( backend=PyEVMBackend(), auto_mine_transactions=False, ) provider = EthereumTesterProvider(eth_tester) w3 = Web3(provider) tx_hash = w3.eth.sendTransaction(assoc(default_tx_detail, 'data', bytecode)) mine(w3, 1) receipt = w3.eth.getTransactionReceipt(tx_hash) contract_address = receipt['contractAddress'] return w3.eth.contract(contract_address, abi=abi, bytecode=bytecode)
def call(self, transaction, block_identifier=None): # TODO: move to middleware if 'from' not in transaction and is_address(self.defaultAccount): transaction = assoc(transaction, 'from', self.defaultAccount) # TODO: move to middleware if block_identifier is None: block_identifier = self.defaultBlock return self.web3.manager.request_blocking( "eth_call", [transaction, block_identifier], )
def call(self, transaction, block_identifier=None): # TODO: move to middleware if 'from' not in transaction and is_checksum_address(self.defaultAccount): transaction = assoc(transaction, 'from', self.defaultAccount) # TODO: move to middleware if block_identifier is None: block_identifier = self.defaultBlock return self.web3.manager.request_blocking( "eth_call", [transaction, block_identifier], )
def test_get_transaction_receipt_byzantium(self, eth_tester, test_transaction): backend = eth_tester.backend.__class__() byzantium_eth_tester = eth_tester.__class__(backend=backend) backend.set_fork_block(FORK_BYZANTIUM, 0) accounts = byzantium_eth_tester.get_accounts() assert accounts, "No accounts available for transaction sending" transaction = assoc(test_transaction, 'from', accounts[0]) txn_hash = byzantium_eth_tester.send_transaction(transaction) txn = byzantium_eth_tester.get_transaction_receipt(txn_hash) assert 'status' in txn assert txn['status'] == 1
def test_estimate_gas(self, eth_tester): self.skip_if_no_evm_execution() math_address = _deploy_math(eth_tester) estimate_call_math_transaction = _make_call_math_transaction( eth_tester, math_address, 'increment', ) gas_estimation = eth_tester.estimate_gas(estimate_call_math_transaction) call_math_transaction = assoc(estimate_call_math_transaction, 'gas', gas_estimation) transaction_hash = eth_tester.send_transaction(call_math_transaction) receipt = eth_tester.get_transaction_receipt(transaction_hash) assert receipt['gas_used'] == gas_estimation
def mine_blocks(self, num_blocks=1, coinbase=None): for _ in range(num_blocks): block_to_mine = dissoc(self.block, 'hash') block_hash = fake_rlp_hash(block_to_mine) mined_block = assoc(block_to_mine, 'hash', block_hash) assign_block_info = compose( partial(assoc, key='block_number', value=mined_block['number']), partial(assoc, key='block_hash', value=mined_block['hash']), ) mined_block['transactions'] = tuple( assign_block_info(transaction) for transaction in mined_block['transactions']) self.blocks.append(mined_block) self.block = make_block_from_parent(mined_block) yield block_hash
def test_estimate_gas(self, eth_tester): self.skip_if_no_evm_execution() math_address = _deploy_math(eth_tester) estimate_call_math_transaction = _make_call_math_transaction( eth_tester, math_address, 'increment', ) gas_estimation = eth_tester.estimate_gas(estimate_call_math_transaction) call_math_transaction = assoc(estimate_call_math_transaction, 'gas', gas_estimation) transaction_hash = eth_tester.send_transaction(call_math_transaction) receipt = eth_tester.get_transaction_receipt(transaction_hash) assert receipt['gas_used'] <= gas_estimation # Tolerance set to the default py-evm tolerance: # https://github.com/ethereum/py-evm/blob/f0276e684edebd7cd9e84cd04b3229ab9dd958b9/evm/estimators/gas.py#L77 # https://github.com/ethereum/py-evm/blob/f0276e684edebd7cd9e84cd04b3229ab9dd958b9/evm/estimators/__init__.py#L11 assert receipt['gas_used'] >= gas_estimation - 21000
def resolve_uuid(meta): meta = {k: assoc(v, 'uuid', k) for k, v in meta.items()} return {to_path(data, meta): data for data in meta.values()}
def inner(*args, **kwargs): value = fn(*args, **kwargs) if 'hash' in value: return value else: return assoc(value, 'hash', keccak(stringify(value)))
def inner(*args, **kwargs): value = fn(*args, **kwargs) if 'hash' in value: return value else: return assoc(value, 'hash', keccak(bytes_repr(value)))
async def fetch_survey_stats(req): try: dset = req.args.get('d') except KeyError as e: raise SurveyError(str(e), info={'datasets': list(dset.keys())}) d = st.dset[dset] qs = d.meta.qns fs = d.meta.facet_map qn = req.args.get('q') if qs.qid.eq(qn).sum() == 0: raise SurveyError("Cannot find qid: %s in dataset: %s" % (qn, dset), info={'questions': set(qs.qid)}) vars = [] if 'v' not in req.args else req.args.get('v').split(',') for v in vars: if not v in d.meta.vars: raise SurveyError("Cannot find var facet v: %s in dataset: %s" % (v, dset), info={'facets': fs}) filt = {} if 'f' not in req.args else parse_filter(req.args.get('f')) for k, vals in filt.items(): if not k in fs: raise SurveyError("Cannot find filter facet: %s in dataset: %s" % (k, dset), info={'facets': fs}) for v in vals: typ = type(fs[k][0]) if not typ(v) in fs[k]: raise SurveyError( "Cannot find value: %s for filter facet: %s in dataset: %s" % (v, k, dset), info={'facets': fs}) use_socrata = False if 's' not in req.args else not 0**int( req.args.get('s'), 2) if use_socrata and not d.meta.has_socrata: raise SurveyError( "Socrata pre-computed data not available for dataset: %s" % dset, info={'facets': fs}) if not use_socrata and not d.meta.has_surveys: raise SurveyError("Surveys data not available for dataset: %s" % dset, info={'facets': fs}) question = qn # meta.qnmeta[qn] results = None # fetch_socrata(qn, resp, vars, filt, national, meta) error = None try: if not use_socrata: results = await fetch_stats(dset, qn, vars, filt) results = concatv(*results) for v in vars: results = map(lambda d: d if v in d else assoc(d, v, 'Total'), results) else: results = d.fetch_socrata(qn, vars, filt) results = results.to_dict(orient='records') except Exception as e: raise ServerError(e) # logger.info('dumping result', res=results) return json({ 'error': error, 'q': qn, 'filter': filt, 'question': question, 'vars': vars, 'results': results })
def _send_and_check_transaction(self, eth_tester, test_transaction, _from): transaction = assoc(test_transaction, 'from', _from) txn_hash = eth_tester.send_transaction(transaction) txn = eth_tester.get_transaction_by_hash(txn_hash) self._check_transactions(transaction, txn)
PK_A = '0x58d23b55bc9cdce1f18c2500f40ff4ab7245df9a89505e9b1fa4851f623d241d' PK_A_ADDRESS = '0xdc544d1aa88ff8bbd2f2aec754b1f1e99e1812fd' NON_DEFAULT_GAS_PRICE = 504 SIMPLE_TRANSACTION = { "to": BURN_ADDRESS, "gas_price": NON_DEFAULT_GAS_PRICE, "value": 0, "gas": 21000, } TRANSACTION_WTH_NONCE = assoc(SIMPLE_TRANSACTION, 'nonce', 0) CONTRACT_TRANSACTION_EMPTY_TO = { "to": '', "gas_price": NON_DEFAULT_GAS_PRICE, "value": 0, "gas": 100000, } CONTRACT_TRANSACTION_MISSING_TO = dissoc(CONTRACT_TRANSACTION_EMPTY_TO, 'to') BLOCK_KEYS = { "number", "hash", "parent_hash", "nonce", "sha3_uncles",
def _call_evm_transaction(tester_module, evm, transaction): if 'gas' not in transaction: transaction_for_call = assoc(transaction, 'gas', 50000000) else: transaction_for_call = transaction return _send_evm_transaction(tester_module, evm, transaction_for_call)
def _estimate_evm_transaction(tester_module, evm, transaction): transaction_for_estimate = assoc(transaction, 'gas', 50000000) return _send_evm_transaction(tester_module, evm, transaction_for_estimate)