示例#1
0
文件: eth.py 项目: firke/web3.py
 def wait_for_transaction_receipt(
     self, transaction_hash: _Hash32, timeout: int = 120, poll_latency: float = 0.1
 ) -> TxReceipt:
     try:
         return wait_for_transaction_receipt(self.web3, transaction_hash, timeout, poll_latency)
     except Timeout:
         raise TimeExhausted(
             "Transaction {} is not in the chain, after {} seconds".format(
                 to_hex(transaction_hash),
                 timeout,
             )
         )
示例#2
0
 def waitForTransactionReceipt(self,
                               chainId,
                               transaction_hash,
                               timeout=5000):
     try:
         return wait_for_transaction_receipt(self.web3, chainId,
                                             transaction_hash, timeout)
     except Timeout:
         raise TimeExhausted(
             "Transaction {} is not in the chain, after {} seconds".format(
                 transaction_hash,
                 timeout,
             ))
示例#3
0
    def wait_for_transaction_receipt(self,
                                     tx_hash,
                                     timeout=180,
                                     poll_latency=0.1):

        try:
            return wait_for_transaction_receipt(self.web3,
                                                tx_hash,
                                                timeout=timeout,
                                                poll_latency=poll_latency)
        except Timeout:
            raise TimeExhausted(
                "Transaction {} is not in the chain, after {} seconds".format(
                    tx_hash,
                    timeout,
                ))
示例#4
0
    def wait_for_transaction_receipt(
        self, transaction_hash: _Hash32, timeout: float = 120, poll_latency: float = 0.1
    ) -> TxReceipt:
        try:
            with Timeout(timeout) as _timeout:
                while True:
                    try:
                        tx_receipt = self._get_transaction_receipt(transaction_hash)
                    except TransactionNotFound:
                        tx_receipt = None
                    if tx_receipt is not None:
                        break
                    _timeout.sleep(poll_latency)
            return tx_receipt

        except Timeout:
            raise TimeExhausted(
                f"Transaction {HexBytes(transaction_hash) !r} is not in the chain "
                f"after {timeout} seconds"
            )
示例#5
0
def test_verify_order_tx(alice_address, bob_address, alice_ocean,
                         alice_wallet):
    """Tests verify_order_tx function."""
    token = alice_ocean.create_data_token("DataToken1",
                                          "DT1",
                                          from_wallet=alice_wallet,
                                          blob="foo_blob")

    token.mint(alice_address, to_wei(100), from_wallet=alice_wallet)
    token.approve(bob_address, to_wei(1), from_wallet=alice_wallet)
    transfer_tx_id = token.transfer(bob_address,
                                    to_wei(5),
                                    from_wallet=alice_wallet)

    with pytest.raises(TimeExhausted):
        with patch("ocean_lib.models.data_token.DataToken.get_tx_receipt"
                   ) as mock:
            # dummy tx id which is not found in the chain
            # catches TimeExhausted exception from web3
            mock.side_effect = TimeExhausted()
            token.verify_order_tx("0x0", "some_did", "some_index",
                                  "some_amount", alice_address)

    transfer_tx_id = token.transfer(bob_address,
                                    to_wei(5),
                                    from_wallet=alice_wallet)
    with pytest.raises(AssertionError):
        # tx id is from transfer, not order
        token.verify_order_tx(transfer_tx_id, "some_did", "some_index",
                              "some_amount", alice_address)

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = V3Asset(json_filename=sample_ddo_path)
    order_tx_id = token.startOrder(alice_address, to_wei(1), 1, ZERO_ADDRESS,
                                   alice_wallet)

    with pytest.raises(AssertionError):
        # the wrong asset did, this is a sample
        token.verify_order_tx(order_tx_id, asset.did, "some_index",
                              "some_amount", alice_address)
示例#6
0
    def test_error_13(self, client, session):
        insert_node_data(session, is_synced=True)

        # トークンリスト登録
        tokenlist = tokenlist_contract()
        config.TOKEN_LIST_CONTRACT_ADDRESS = tokenlist['address']
        issuer = eth_account['issuer']
        coupontoken_1 = issue_coupon_token(
            issuer, {
                "name": "name_test1",
                "symbol": "symbol_test1",
                "totalSupply": 1000000,
                "tradableExchange": config.ZERO_ADDRESS,
                "details": "details_test1",
                "returnDetails": "returnDetails_test1",
                "memo": "memo_test1",
                "expirationDate": "20211201",
                "transferable": True,
                "contactInformation": "contactInformation_test1",
                "privacyPolicy": "privacyPolicy_test1"
            })
        coupon_register_list(issuer, coupontoken_1, tokenlist)

        # Listing,実行可能コントラクト登録
        listing_token(session, coupontoken_1)
        executable_contract_token(session, coupontoken_1)

        token_contract_1 = web3.eth.contract(
            address=to_checksum_address(coupontoken_1["address"]),
            abi=coupontoken_1["abi"],
        )

        local_account_1 = web3.eth.account.create()

        # テスト用のトランザクション実行前の事前準備
        pre_tx = token_contract_1.functions.transfer(
            to_checksum_address(local_account_1.address),
            10).buildTransaction({
                "from":
                to_checksum_address(issuer["account_address"]),
                "gas":
                6000000
            })
        tx_hash = web3.eth.sendTransaction(pre_tx)
        web3.eth.waitForTransactionReceipt(tx_hash)

        tx = token_contract_1.functions.consume(10).buildTransaction({
            "from":
            to_checksum_address(local_account_1.address),
            "gas":
            6000000
        })
        tx["nonce"] = web3.eth.getTransactionCount(
            to_checksum_address(local_account_1.address))
        signed_tx_1 = web3.eth.account.signTransaction(
            tx, local_account_1.privateKey)

        request_params = {
            "raw_tx_hex_list": [signed_tx_1.rawTransaction.hex()]
        }
        headers = {'Content-Type': 'application/json'}
        request_body = json.dumps(request_params)

        # タイムアウト
        # queuedに滞留
        # NOTE: GanacheにはRPCメソッド:txpool_inspectが存在しないためMock化
        with mock.patch(
                "web3.eth.Eth.waitForTransactionReceipt",
                MagicMock(side_effect=TimeExhausted())
        ), mock.patch(
                "web3.geth.GethTxPool.inspect",
                AttributeDict({
                    "pending":
                    AttributeDict({}),
                    "queued":
                    AttributeDict({
                        to_checksum_address(local_account_1.address):
                        AttributeDict({
                            str(tx["nonce"]):
                            "0xffffffffffffffffffffffffffffffffffffffff: 0 wei + 999999 × 11111 gas"
                        })
                    })
                })):
            resp = client.simulate_post(self.apiurl,
                                        headers=headers,
                                        body=request_body)

        assert resp.status_code == 200
        assert resp.json['meta'] == {'code': 200, 'message': 'OK'}
        assert resp.json['data'] == [{"id": 1, "status": 0}]