def test_async_requests():
    client = EthTesterClient(async=True, async_timeout=60)

    threads = []
    errors = []
    to_addr = tester.encode_hex(tester.accounts[1])

    def spam_block_number():
        for i in range(5):
            try:
                client.send_transaction(
                    to=to_addr,
                    value=1,
                )
            except Exception as e:
                errors.append(e)
                pytest.fail(e.message)

    for i in range(5):
        thread = threading.Thread(target=spam_block_number)
        thread.daemon = True
        threads.append(thread)

    [thread.start() for thread in threads]

    [thread.join() for thread in threads]

    assert not errors
def test_get_accounts(rpc_server, rpc_client):
    accounts = rpc_client.get_accounts()

    assert len(accounts) == len(tester.accounts)

    for _a, a in zip(accounts, tester.accounts):
        assert _a == "0x" + tester.encode_hex(a)
def test_cannot_execute_if_claimed_by_other(deploy_client, deployed_contracts,
                                            deploy_coinbase,
                                            deploy_future_block_call):
    client_contract = deployed_contracts.TestCallExecution

    target_block = deploy_client.get_block_number() + 300

    call = deploy_future_block_call(
        client_contract.setBool,
        target_block=target_block,
    )

    deploy_client.wait_for_block(target_block - 10 - 255)

    # claim it
    claim_txn_h = call.claim(value=2 * call.basePayment())
    claim_txn_r = deploy_client.wait_for_transaction(claim_txn_h)

    assert call.claimer() == deploy_coinbase

    deploy_client.wait_for_block(call.targetBlock())

    assert call.wasCalled() is False

    not_allowed_txn_h = call.execute(_from=encode_hex(accounts[1]))
    not_allowed_txn_r = deploy_client.wait_for_transaction(not_allowed_txn_h)

    assert call.wasCalled() is False

    execute_txn_h = call.execute()
    execute_txn_r = deploy_client.wait_for_transaction(execute_txn_h)

    assert call.wasCalled() is True
def test_claim_deposit_goes_to_caller(deploy_client, deployed_contracts,
                                      deploy_future_block_call, denoms,
                                      deploy_coinbase, FutureBlockCall,
                                      CallLib, SchedulerLib):
    scheduler = deployed_contracts.Scheduler
    client_contract = deployed_contracts.TestCallExecution

    target_block = deploy_client.get_block_number() + 1000

    call = deploy_future_block_call(
        client_contract.setBool,
        target_block=target_block,
        payment=12345,
        donation=54321,
        endowment=denoms.ether * 100,
    )

    deploy_client.wait_for_block(target_block - 10 - 255)

    # claim it
    deposit_amount = 2 * call.basePayment()
    claim_txn_h = call.claim(value=deposit_amount)
    claim_txn_r = deploy_client.wait_for_transaction(claim_txn_h)

    deploy_client.wait_for_block(
        call.targetBlock() + deployed_contracts.Scheduler.getCallWindowSize() + 1
    )
    exe_addr = "0x" + encode_hex(accounts[1])
    before_balance = deploy_client.get_balance(exe_addr)
    before_call_balance = call.get_balance()

    assert call.wasCalled() is False
    assert call.claimer() == deploy_coinbase
    assert call.claimerDeposit() == deposit_amount

    ffa_txn_h = call.execute(_from=exe_addr)
    ffa_txn_r = deploy_client.wait_for_transaction(ffa_txn_h)
    ffa_txn = deploy_client.get_transaction_by_hash(ffa_txn_h)

    assert call.wasCalled() is True
    assert call.claimer() == deploy_coinbase
    assert call.claimerDeposit() == 0

    execute_logs = CallLib.CallExecuted.get_transaction_logs(ffa_txn_h)
    assert len(execute_logs) == 1
    execute_data = CallLib.CallExecuted.get_log_data(execute_logs[0])

    assert exe_addr in ffa_txn_r['logs'][0]['topics']

    after_balance = deploy_client.get_balance(exe_addr)
    expected_payout = deposit_amount + call.basePayment() + execute_data['gasCost']

    assert abs(execute_data['payment'] - expected_payout) < 100

    computed_payout = after_balance - before_balance
    actual_gas = int(ffa_txn_r['gasUsed'], 16)
    gas_diff = execute_data['gasCost'] - actual_gas

    assert computed_payout == deposit_amount + 12345 + gas_diff
def test_get_block(rpc_server, rpc_client, eth_coinbase):
    block_number = rpc_client.get_block_number()
    assert block_number == 0

    to_addr = "0x" + tester.encode_hex(tester.accounts[1])
    txn_hash = rpc_client.send_transaction(_from=eth_coinbase, to=to_addr, value=100)
    assert txn_hash

    block_number = rpc_client.get_block_number()
    assert block_number == 1
示例#6
0
def accounts(populus_config, request):
    client_type = populus_config.get_value(request, 'deploy_client_type')
    if client_type == 'ethtester':
        from ethereum import tester
        return tuple("0x" + tester.encode_hex(account) for account in tester.accounts)
    elif client_type == 'rpc':
        raise ValueError("Not supported")
    elif client_type == 'ipc':
        raise ValueError("Not supported")
    else:
        raise ValueError("Unknown client type")
def test_get_block(rpc_server, rpc_client, eth_coinbase):
    block_number = rpc_client.get_block_number()
    assert block_number == 0

    to_addr = "0x" + tester.encode_hex(tester.accounts[1])
    txn_hash = rpc_client.send_transaction(_from=eth_coinbase,
                                           to=to_addr,
                                           value=100)
    assert txn_hash

    block_number = rpc_client.get_block_number()
    assert block_number == 1
def test_get_transaction_by_hash(rpc_server, eth_coinbase):
    client = Client("127.0.0.1", "8545")

    to_addr = tester.encode_hex(tester.accounts[1])

    txn_hash = client.send_transaction(_from=eth_coinbase, to=to_addr, value=12345)

    txn = client.get_transaction_by_hash(txn_hash)

    assert txn["from"].endswith(eth_coinbase)
    assert txn["to"].endswith(to_addr)
    assert int(txn["value"], 16) == 12345
def test_send_a_transaction_uses_coinbase_as_from(rpc_server, eth_coinbase):
    client = Client('127.0.0.1', '8545')

    to_addr = tester.encode_hex(tester.accounts[1])

    txn_hash = client.send_transaction(
        to=to_addr,
        value=12345,
    )

    after_balance = client.get_balance(eth_coinbase)

    assert after_balance == 1000004999999999999987655L
def test_get_block_by_hash(rpc_server, rpc_client, eth_coinbase):
    block_number = rpc_client.get_block_number()
    assert block_number == 0

    to_addr = "0x" + tester.encode_hex(tester.accounts[1])
    txn_hash = rpc_client.send_transaction(_from=eth_coinbase, to=to_addr, value=100)
    assert txn_hash

    txn_receipt = rpc_client.get_transaction_receipt(txn_hash)
    block_hash = txn_receipt['blockHash']

    block = rpc_client.get_block_by_hash(block_hash)
    assert block
def test_get_block_by_hash(rpc_server, rpc_client, eth_coinbase):
    block_number = rpc_client.get_block_number()
    assert block_number == 0

    to_addr = "0x" + tester.encode_hex(tester.accounts[1])
    txn_hash = rpc_client.send_transaction(_from=eth_coinbase,
                                           to=to_addr,
                                           value=100)
    assert txn_hash

    txn_receipt = rpc_client.get_transaction_receipt(txn_hash)
    block_hash = txn_receipt['blockHash']

    block = rpc_client.get_block_by_hash(block_hash)
    assert block
def test_scheduler_gets_what_is_leftover(deploy_client, deployed_contracts,
                                         deploy_future_block_call, denoms,
                                         deploy_coinbase, FutureBlockCall,
                                         CallLib, SchedulerLib):
    scheduler = deployed_contracts.Scheduler
    client_contract = deployed_contracts.TestCallExecution

    scheduler_address = "0x" + encode_hex(accounts[1])
    deploy_client.send_transaction(to=scheduler_address, value=20 * denoms.ether)

    target_block = deploy_client.get_block_number() + 1000

    call = deploy_future_block_call(
        client_contract.setBool,
        target_block=target_block,
        payment=12345,
        donation=54321,
        endowment=denoms.ether * 10,
        scheduler_address=scheduler_address,
    )

    deploy_client.wait_for_block(target_block)

    before_balance = deploy_client.get_balance(scheduler_address)
    before_call_balance = call.get_balance()

    assert call.wasCalled() is False
    assert before_call_balance == 10 * denoms.ether

    ffa_txn_h = call.execute(_from=deploy_coinbase)
    ffa_txn_r = deploy_client.wait_for_transaction(ffa_txn_h)
    ffa_txn = deploy_client.get_transaction_by_hash(ffa_txn_h)

    assert call.wasCalled() is True
    assert call.get_balance() == 0

    execute_logs = CallLib.CallExecuted.get_transaction_logs(ffa_txn_h)
    assert len(execute_logs) == 1
    execute_data = CallLib.CallExecuted.get_log_data(execute_logs[0])

    after_balance = deploy_client.get_balance(scheduler_address)
    payout = execute_data['payment']
    donation = execute_data['donation']

    computed_reimbursement = after_balance - before_balance
    expected_reimbursement = before_call_balance - payout - donation

    assert computed_reimbursement == expected_reimbursement
def test_get_transaction_by_hash(rpc_server, eth_coinbase):
    client = Client('127.0.0.1', '8545')

    to_addr = tester.encode_hex(tester.accounts[1])

    txn_hash = client.send_transaction(
        _from=eth_coinbase,
        to=to_addr,
        value=12345,
    )

    txn = client.get_transaction_by_hash(txn_hash)

    assert txn['from'].endswith(eth_coinbase)
    assert txn['to'].endswith(to_addr)
    assert int(txn['value'], 16) == 12345
def test_only_scheduler_can_cancel_prior_to_target_block(deploy_client,
                                                         deployed_contracts,
                                                         deploy_future_block_call,
                                                         CallLib):
    client_contract = deployed_contracts.TestCallExecution

    target_block = deploy_client.get_block_number() + 300
    call = deploy_future_block_call(
        client_contract.setBool,
        target_block=target_block,
    )

    assert call.isCancelled() is False

    cancel_txn_hash = call.cancel(_from=encode_hex(accounts[1]))
    cancel_txn_receipt = deploy_client.wait_for_transaction(cancel_txn_hash)
    assert len(CallLib.Cancelled.get_transaction_logs(cancel_txn_hash)) == 0
示例#15
0
    def __init__(self, state, code, sender, gas, endowment, language):
        if language not in _t.languages:
            _t.languages[language] = __import__(language)
        language = _t.languages[language]
        
        if os.path.exists(code):
            cache = code.replace('.se', '.sec')
            if os.path.exists(cache):
                cache_made = os.path.getmtime(cache)
                code_made = os.path.getmtime(code)
                if code_made > cache_made:
                    with open(cache, 'wb') as f:
                        print Fore.YELLOW + Style.BRIGHT + 'Stale cache, recompiling...' + Style.RESET_ALL
                        with timer():
                            evm = language.compile(code)
                            sig = language.mk_full_signature(code)
                            evm_len = encode(len(evm), 256, 2)
                            sig_len = encode(len(sig), 256, 2)
                            f.write(evm_len + evm + sig_len + sig)
                else:
                    print Fore.YELLOW + Style.BRIGHT + 'Loading from cache...' + Style.RESET_ALL
                    with open(cache, 'rb') as f:
                        with timer():
                            evm_len = decode(f.read(2), 256)
                            evm = f.read(evm_len)
                            sig_len = decode(f.read(2), 256)
                            sig = f.read(sig_len)
            else:
                with open(cache, 'wb') as f:
                    print Fore.YELLOW + Style.BRIGHT + 'Generating cache...' + Style.RESET_ALL
                    with timer():
                        evm = language.compile(code)
                        sig = language.mk_full_signature(code)
                        evm_len = encode(len(evm), 256, 2)
                        sig_len = encode(len(sig), 256, 2)
                        f.write(evm_len + evm + sig_len + sig)

            with suppressed_output():
                self.address = _t.encode_hex(state.evm(evm, sender, endowment, gas))
            self._translator = _t.abi.ContractTranslator(sig)
            assert len(state.block.get_code(self.address)), "Contract code empty"
            for funcname in self._translator.function_data:
                vars(self)[funcname] = make_dispatcher(state, self, funcname)
def test_anyone_can_cancel_after_call_window(deploy_client, deployed_contracts,
                                             deploy_future_block_call, deploy_coinbase,
                                             CallLib):
    client_contract = deployed_contracts.TestCallExecution

    target_block = deploy_client.get_block_number() + 300

    call = deploy_future_block_call(
        client_contract.setBool,
        target_block=target_block,
    )

    deploy_client.wait_for_block(call.targetBlock() + call.gracePeriod() + 1)

    assert call.isCancelled() is False

    txn_h = call.cancel(_from=encode_hex(accounts[1]))
    txn_r = deploy_client.wait_for_transaction(txn_h)
    txn = deploy_client.get_transaction_by_hash(txn_h)

    assert txn['from'] != deploy_coinbase
    assert call.isCancelled() is True
示例#17
0
文件: plugin.py 项目: Quiark/populus
def eth_coinbase():
    from ethereum import tester
    return tester.encode_hex(tester.accounts[0])
示例#18
0
def ethtester_coinbase():
    from ethereum import tester
    return tester.encode_hex(tester.accounts[0])