def test_extra_call_gas_constant_when_gas_price_higher(deploy_client,
                                                       deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesInt

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(
        alarm._meta.address, -12345)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    base_gas_price = alarm.getCallBaseGasPrice(call_key)

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key,
                                                 gas_price=base_gas_price + 10)

    call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash)

    assert alarm.checkIfCalled(call_key) is True

    recorded_gas_used = alarm.getCallGasUsed(call_key)
    actual_gas_used = int(call_txn_receipt['gasUsed'], 16)

    assert recorded_gas_used >= actual_gas_used

    assert abs(recorded_gas_used - actual_gas_used) in {0, 64}
def test_caller_payout(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    coinbase = deploy_client.get_coinbase()

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, 3)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() == 0

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.getCallPayout(call_key) == 0
    assert alarm.getAccountBalance(coinbase) == 0

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    gas_used = alarm.getCallGasUsed(call_key)
    gas_price = alarm.getCallGasPrice(call_key)
    base_gas_price = alarm.getCallBaseGasPrice(call_key)

    scalar = 100 * base_gas_price / (abs(gas_price - base_gas_price) + base_gas_price)
    expected_payout = gas_used * gas_price * scalar * 101 / 10000

    balance = alarm.getAccountBalance(coinbase)
    assert balance == expected_payout
    assert alarm.getCallPayout(call_key) == balance
Пример #3
0
def test_call_fee(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, 3)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() == 0

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    owner = '0xd3cda913deb6f67967b99d67acdfa1712c293601'

    assert alarm.getCallFee(call_key) == 0
    assert alarm.getAccountBalance(owner) == 0

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    balance = alarm.getAccountBalance(owner)
    assert balance > 0
    assert alarm.getCallFee(call_key) == balance
def test_entering_pool(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getBondBalance(coinbase) == 0
    deposit_amount = alarm.getMinimumBond() * 10

    txn_1_hash = alarm.depositBond.sendTransaction(value=deposit_amount)
    wait_for_transaction(deploy_client, txn_1_hash)

    assert alarm.isInPool(coinbase) is False
    assert alarm.canEnterPool(coinbase) is True

    wait_for_transaction(deploy_client, alarm.enterPool.sendTransaction())

    # Now queued to be in the next pool.
    assert alarm.getCurrentGenerationId() == 0
    assert alarm.isInPool(coinbase) is True
    assert alarm.canEnterPool(coinbase) is False

    next_generation_id = alarm.getNextGenerationId()
    assert next_generation_id > 0
    assert alarm.isInGeneration(coinbase, next_generation_id) is True

    deploy_client.wait_for_block(
        alarm.getGenerationStartAt(next_generation_id), 18)

    assert alarm.isInPool(coinbase) is True
    assert alarm.canEnterPool(coinbase) is False
def test_check_if_call_successful(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() is False

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.checkIfCalled(call_key) is False
    assert alarm.checkIfSuccess(call_key) is False

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key))
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    assert client_contract.value() is True
    assert alarm.checkIfCalled(call_key) is True
    assert alarm.checkIfSuccess(call_key) is True
def test_extra_call_gas_constant_when_gas_price_lower(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesInt

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, -12345)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    base_gas_price = alarm.getCallBaseGasPrice(call_key)

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)

    call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash)

    assert alarm.checkIfCalled(call_key) is True

    recorded_gas_used = alarm.getCallGasUsed(call_key)
    actual_gas_used = int(call_txn_receipt['gasUsed'], 16)

    assert recorded_gas_used >= actual_gas_used

    assert abs(recorded_gas_used - actual_gas_used) in {0, 64}
def test_getting_called_at_block(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() is False

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.getCallGasUsed(call_key) == 0

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    call_txn = deploy_client.get_transaction_by_hash(call_txn_hash)

    assert client_contract.value() is True
    assert alarm.getCallCalledAtBlock(call_key) == int(call_txn['blockNumber'], 16)
Пример #8
0
def test_infinite_loop_protection(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.InfiniteLoop

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)
    deploy_client.send_transaction(to=client_contract._meta.address,
                                   value=1000000000)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None
    deploy_client.wait_for_block(alarm.getCallTargetBlock.call(callKey), 300)
    call_txn_hash = alarm.doCall.sendTransaction(callKey)
    call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash)
    call_txn = deploy_client.get_transaction_by_hash(call_txn_hash)

    assert alarm.checkIfCalled.call(callKey) is True
    assert alarm.checkIfSuccess.call(callKey) is False

    gas_used = int(call_txn_receipt['gasUsed'], 16)
    expected = int(call_txn['gas'], 16)
    assert gas_used == expected
def test_caller_payout(geth_node, geth_coinbase, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    deposit_amount = get_max_gas(rpc_client) * rpc_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, 3)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.value.call() == 0

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    assert alarm.getCallPayout.call(callKey) == 0
    assert alarm.accountBalances.call(geth_coinbase) == 0

    wait_for_block(rpc_client, alarm.getCallTargetBlock.call(callKey), 120)
    call_txn_hash = alarm.doCall.sendTransaction(callKey)
    wait_for_transaction(alarm._meta.rpc_client, call_txn_hash)

    gas_used = alarm.getCallGasUsed.call(callKey)
    gas_price = alarm.getCallGasPrice.call(callKey)
    base_gas_price = alarm.getCallBaseGasPrice.call(callKey)

    scalar = 100 * base_gas_price / (abs(gas_price - base_gas_price) + base_gas_price)
    expected_payout = gas_used * gas_price * scalar * 101 / 10000

    balance = alarm.accountBalances.call(geth_coinbase)
    assert balance == expected_payout
    assert alarm.getCallPayout.call(callKey) == balance
def test_what_happens_when_call_throws_exception(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.Fails

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert client_contract.value() is False
    assert alarm.checkIfCalled(call_key) is False
    assert alarm.checkIfSuccess(call_key) is False

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 300)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash)
    call_txn = deploy_client.get_transaction_by_hash(call_txn_hash)

    assert client_contract.value() is False
    assert alarm.checkIfCalled(call_key) is True
    assert alarm.checkIfSuccess(call_key) is False

    gas_used = int(call_txn_receipt['gasUsed'], 16)
    gas = int(call_txn['gas'], 16)
    gas_recorded = alarm.getCallGasUsed(call_key)

    assert gas == gas_used
    assert gas == gas_recorded
def test_what_happens_when_call_throws_exception(deploy_client,
                                                 deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.Fails

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert client_contract.value() is False
    assert alarm.checkIfCalled(call_key) is False
    assert alarm.checkIfSuccess(call_key) is False

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 300)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash)
    call_txn = deploy_client.get_transaction_by_hash(call_txn_hash)

    assert client_contract.value() is False
    assert alarm.checkIfCalled(call_key) is True
    assert alarm.checkIfSuccess(call_key) is False

    gas_used = int(call_txn_receipt['gasUsed'], 16)
    gas = int(call_txn['gas'], 16)
    gas_recorded = alarm.getCallGasUsed(call_key)

    assert gas == gas_used
    assert gas == gas_recorded
Пример #12
0
    def manage_membership(self):
        if self.in_any_pool:
            return

        # check bond balance.
        if self.bond_balance < self.minimum_bond:
            self.logger.error(
                "Insufficient bond balance to enter the caller pool")
            self.stop()

        # check the next pool is not currently frozen.
        if self.next_generation_id and self.is_generation_frozen(
                self.next_generation_id):
            self.logger.info("Next pool is frozen and cannot be joined")
            return

        # double check the API agrees with our other checks.
        if not self.can_enter_pool:
            self.logger.warning(
                "CallerPool unexpectedly said we couldn't enter")
            return

        self.logger.info("Entering caller pool")
        txn_hash = self.enter_pool()
        self.logger.debug("Entered caller pool with txn: %s", txn_hash)
        wait_for_transaction(self.rpc_client, txn_hash, 60)
        self.logger.info("Entered caller pool at generation #%s",
                         self.next_generation_id)
def test_entering_pool(geth_node, geth_coinbase, rpc_client, deployed_contracts):
    caller_pool = deployed_contracts.CallerPool

    assert caller_pool.callerBonds.call(geth_coinbase) == 0
    deposit_amount = caller_pool.getMinimumBond.call() * 10

    txn_1_hash = caller_pool.depositBond.sendTransaction(value=deposit_amount)
    wait_for_transaction(rpc_client, txn_1_hash)

    assert caller_pool.isInAnyPool.call() is False
    assert caller_pool.canEnterPool.call() is True

    wait_for_transaction(rpc_client, caller_pool.enterPool.sendTransaction())

    # Now queued to be in the next pool.
    assert caller_pool.getActivePoolKey.call() == 0
    assert caller_pool.isInAnyPool.call() is True
    assert caller_pool.canEnterPool.call() is False

    next_pool_key = caller_pool.getNextPoolKey.call()
    assert next_pool_key > 0
    assert caller_pool.isInPool.call(next_pool_key) is True

    wait_for_block(rpc_client, next_pool_key, 180)

    assert caller_pool.isInAnyPool.call() is True
    assert caller_pool.canEnterPool.call() is False
def test_check_if_call_successful_for_failed_call(deploy_client,
                                                  deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.InfiniteLoop

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.checkIfCalled(call_key) is False
    assert alarm.checkIfSuccess(call_key) is False

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    call_txn_receipt = wait_for_transaction(deploy_client, call_txn_hash)
    call_txn = deploy_client.get_transaction_by_hash(call_txn_hash)

    assert alarm.checkIfCalled(call_key) is True
    assert alarm.checkIfSuccess(call_key) is False
    assert int(call_txn_receipt['gasUsed'], 16) == int(call_txn['gas'], 16)
def test_extra_call_gas_constant_when_gas_price_lower(geth_node, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesInt

    deposit_amount = get_max_gas(rpc_client) * rpc_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, -12345)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    base_gas_price = alarm.getCallBaseGasPrice.call(callKey)

    wait_for_block(rpc_client, alarm.getCallTargetBlock.call(callKey), 120)
    call_txn_hash = alarm.doCall.sendTransaction(callKey, gas_price=base_gas_price - 10)

    call_txn_receipt = wait_for_transaction(alarm._meta.rpc_client, call_txn_hash)

    assert alarm.checkIfCalled.call(callKey) is True

    recorded_gas_used = alarm.getCallGasUsed.call(callKey)
    actual_gas_used = int(call_txn_receipt['gasUsed'], 16)

    try:
        assert actual_gas_used == recorded_gas_used
    except AssertionError:
        assert actual_gas_used == recorded_gas_used + 64
def test_registering_many_values(geth_node, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.TestDataRegistry

    assert client_contract.wasSuccessful.call() == 0

    txn_hash = client_contract.registerMany.sendTransaction(
        alarm._meta.address,
        2 ** 256 - 1,
        -1 * (2 ** 128 - 1),
        2 ** 128 - 1,
        '1234567890\00\00\00abcdefg',
        '0xc948453368e5ddc7bc00bb52b5809138217a068d',
        '123456789012345678901234567890123456789012345678901234567890',
    )

    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.wasSuccessful.call() == 1

    data_hash = alarm.getLastDataHash.call()
    assert data_hash is not None

    data = alarm.getLastData.call()
    assert data == (
        '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
        '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
        '1234567890\x00\x00\x00abcdefg\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc9HE3h\xe5\xdd\xc7\xbc\x00\xbbR\xb5\x80\x918!z\x06\x8d'
        '123456789012345678901234567890123456789012345678901234567890\x00\x00\x00\x00'
    )
Пример #17
0
def test_registering_using_local_abstraction(deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.TestDataRegistry

    assert client_contract.wasSuccessful.call() == 0

    # This is for a weird bug that I'm not sure how to fix.
    evm = alarm._meta.rpc_client.evm
    evm.mine()

    txn_hash = client_contract.registerData.sendTransaction(
        alarm._meta.address,
        -1 * (2**128 - 1),
        '1234567890\00\00\00abcdefg',
        '0xc948453368e5ddc7bc00bb52b5809138217a068d',
    )

    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.wasSuccessful.call() == 1

    data_hash = alarm.getLastDataHash.call()
    assert data_hash is not None

    data = alarm.getLastData.call()
    assert data == (
        '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
        '1234567890\x00\x00\x00abcdefg\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc9HE3h\xe5\xdd\xc7\xbc\x00\xbbR\xb5\x80\x918!z\x06\x8d'
    )
def test_call_fee(geth_node, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    deposit_amount = get_max_gas(rpc_client) * rpc_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, 3)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.value.call() == 0

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    before_balance = alarm.accountBalances.call(client_contract._meta.address)

    wait_for_block(rpc_client, alarm.getCallTargetBlock.call(callKey), 120)
    call_txn_hash = alarm.doCall.sendTransaction(callKey)
    wait_for_transaction(alarm._meta.rpc_client, call_txn_hash)

    after_balance = alarm.accountBalances.call(client_contract._meta.address)
    fee = alarm.getCallFee.call(callKey)
    payout = alarm.getCallPayout.call(callKey)

    assert after_balance == before_balance - payout - fee
def test_scheduled_call_execution_without_pool(geth_node, geth_coinbase, rpc_client, deployed_contracts, contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.SpecifyBlock
    caller_pool = contracts.CallerPool(alarm.getCallerPoolAddress.call(), rpc_client)

    deposit_amount = get_max_gas(rpc_client) * rpc_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    target_block = rpc_client.get_block_number() + 45

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, target_block)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    pool_manager = PoolManager(caller_pool)
    scheduled_call = ScheduledCall(alarm, pool_manager, callKey)

    assert pool_manager.in_active_pool is False
    scheduled_call.execute_async()

    wait_for_block(rpc_client, scheduled_call.target_block + 4, 180)
    assert pool_manager.in_active_pool is False

    assert scheduled_call.txn_hash
    assert scheduled_call.txn_receipt
    assert scheduled_call.txn

    assert alarm.checkIfCalled.call(scheduled_call.call_key)
    assert scheduled_call.was_called
    assert scheduled_call.target_block <= scheduled_call.called_at_block
    assert scheduled_call.called_at_block <= scheduled_call.target_block + scheduled_call.grace_period
def test_entering_pool(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getBondBalance(coinbase) == 0
    deposit_amount = alarm.getMinimumBond() * 10

    txn_1_hash = alarm.depositBond.sendTransaction(value=deposit_amount)
    wait_for_transaction(deploy_client, txn_1_hash)

    assert alarm.isInPool(coinbase) is False
    assert alarm.canEnterPool(coinbase) is True

    wait_for_transaction(deploy_client, alarm.enterPool.sendTransaction())

    # Now queued to be in the next pool.
    assert alarm.getCurrentGenerationId() == 0
    assert alarm.isInPool(coinbase) is True
    assert alarm.canEnterPool(coinbase) is False

    next_generation_id = alarm.getNextGenerationId()
    assert next_generation_id > 0
    assert alarm.isInGeneration(coinbase, next_generation_id) is True

    deploy_client.wait_for_block(alarm.getGenerationStartAt(next_generation_id), 18)

    assert alarm.isInPool(coinbase) is True
    assert alarm.canEnterPool(coinbase) is False
Пример #21
0
def test_call_fee_and_payout_deducted_from_account_balance(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, 3)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() == 0

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    before_balance = alarm.getAccountBalance(client_contract._meta.address)

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    after_balance = alarm.getAccountBalance(client_contract._meta.address)
    fee = alarm.getCallFee(call_key)
    payout = alarm.getCallPayout(call_key)

    assert after_balance == before_balance - payout - fee
def test_enumerate_upcoming_tree_positions(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.SpecifyBlock

    anchor_block = deploy_client.get_block_number()

    blocks = (1, 4, 4, 8, 15, 25, 25, 25, 30, 40, 50, 60)

    call_keys = []

    for n in blocks:
        txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, anchor_block + 100 + n)
        wait_for_transaction(deploy_client, txn_hash)

        last_call_key = alarm.getLastCallKey()
        assert last_call_key is not None

        call_keys.append(last_call_key)

    expected_calls = tuple(utils.encode_hex(c) for c in call_keys[1:10])

    actual_calls = tuple(
        utils.encode_hex(c)
        for c in enumerate_upcoming_calls(alarm, anchor_block + 100 + 4)
    )
    assert actual_calls == expected_calls
def test_cost_of_duplicate_call(geth_node, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(rpc_client) * rpc_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.value.call() is False

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    assert alarm.checkIfCalled.call(callKey) is False

    wait_for_block(rpc_client, alarm.getCallTargetBlock.call(callKey), 120)
    call_txn_hash = alarm.doCall.sendTransaction(callKey)
    call_txn_receipt = wait_for_transaction(alarm._meta.rpc_client, call_txn_hash)

    assert alarm.checkIfCalled.call(callKey) is True

    call_txn_hash = alarm.doCall.sendTransaction(callKey)
    call_txn_receipt = wait_for_transaction(alarm._meta.rpc_client, call_txn_hash)

    assert call_txn_receipt['gasUsed'] == '0x67f0'
Пример #24
0
def test_getting_gas_used(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() is False

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.getCallGasUsed(call_key) == 0

    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    call_txn = deploy_client.get_transaction_by_hash(call_txn_hash)

    assert client_contract.value() is True
    assert alarm.getCallGasPrice(call_key) == int(call_txn['gasPrice'], 16)
def test_registering_using_local_abstraction(deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.TestDataRegistry

    assert client_contract.wasSuccessful.call() == 0

    # This is for a weird bug that I'm not sure how to fix.
    evm = alarm._meta.rpc_client.evm
    evm.mine()

    txn_hash = client_contract.registerData.sendTransaction(
        alarm._meta.address,
        -1 * (2 ** 128 - 1),
        '1234567890\00\00\00abcdefg',
        '0xc948453368e5ddc7bc00bb52b5809138217a068d',
    )

    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.wasSuccessful.call() == 1

    data_hash = alarm.getLastDataHash.call()
    assert data_hash is not None

    data = alarm.getLastData.call()
    assert data == (
        '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01'
        '1234567890\x00\x00\x00abcdefg\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
        '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc9HE3h\xe5\xdd\xc7\xbc\x00\xbbR\xb5\x80\x918!z\x06\x8d'
    )
Пример #26
0
def test_executing_scheduled_call_with_bytes32(deploy_client,
                                               deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesBytes32

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(
        alarm._meta.address,
        'abc\x00\x00\x00\x00\x00abc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    )
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() is None

    call_key = alarm.getLastCallKey()
    assert call_key is not None
    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    assert client_contract.value() == 'abc\x00\x00\x00\x00\x00abc'
def test_authorizing_other_address(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.AuthorizesOthers
    coinbase = deploy_client.get_coinbase()

    authed_addr = alarm.authorizedAddress()
    unauthed_addr = alarm.unauthorizedAddress()

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(coinbase, value=deposit_amount)

    assert alarm.checkAuthorization(coinbase,
                                    client_contract._meta.address) is False

    txn_1_hash = alarm.scheduleCall.sendTransaction(
        client_contract._meta.address,
        client_contract.doIt.encoded_abi_signature,
        utils.decode_hex(
            'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
        ),
        deploy_client.get_block_number() + 50, 255, 0)
    wait_for_transaction(deploy_client, txn_1_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None
    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    assert alarm.checkIfCalled(call_key) is True
    assert alarm.checkIfSuccess(call_key) is True
    assert client_contract.calledBy() == unauthed_addr

    wait_for_transaction(
        deploy_client,
        client_contract.authorize.sendTransaction(alarm._meta.address))
    assert alarm.checkAuthorization(coinbase,
                                    client_contract._meta.address) is True

    txn_2_hash = alarm.scheduleCall.sendTransaction(
        client_contract._meta.address,
        client_contract.doIt.encoded_abi_signature,
        utils.decode_hex(
            'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
        ),
        deploy_client.get_block_number() + 50, 255, 0)
    wait_for_transaction(deploy_client, txn_2_hash)

    assert call_key != alarm.getLastCallKey()
    call_key = alarm.getLastCallKey()
    assert call_key is not None
    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    assert alarm.checkIfCalled(call_key) is True
    assert alarm.checkIfSuccess(call_key) is True
    assert client_contract.calledBy() == authed_addr
def test_get_next_call_with_no_duplicate_block_numbers(geth_node, rpc_client, deployed_contracts):
    """
              8
             / \
            /   \
           /     \
          7       13
         /       /  \
        4       9    15
       / \       \
      1   5       11
           \
            6
    """
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.SpecifyBlock

    anchor_block = rpc_client.get_block_number()

    call_keys = []

    blocks = [anchor_block + 1000 + n for n in (8, 7, 13, 4, 5, 1, 9, 6, 11, 15)]

    for n in blocks:
        wait_for_transaction(rpc_client, client_contract.scheduleIt.sendTransaction(alarm._meta.address, n))

        last_call_key = alarm.getLastCallKey.call()
        assert last_call_key is not None

        call_keys.append(last_call_key)

    key_to_block = dict(zip(call_keys, [b - 1000 - anchor_block for b in blocks]))
    key_to_block[None] = None

    expected_next_blocks = {
        1: 1,
        2: 4,
        3: 4,
        4: 4,
        5: 5,
        6: 6,
        7: 7,
        8: 8,
        9: 9,
        10: 11,
        11: 11,
        12: 13,
        13: 13,
        14: 15,
        15: 15,
        16: None,
    }

    actual_next_blocks = {
        n: key_to_block[alarm.getNextCallKey.call(n + 1000 + anchor_block)]
        for n in expected_next_blocks.keys()
    }

    assert actual_next_blocks == expected_next_blocks
def test_get_call_signature(geth_node, geth_coinbase, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    call_key = alarm.getLastCallKey.call()
    assert call_key

    alarm.getCallABISignature.call(call_key) == client_contract.doIt.encoded_abi_function_signature
def test_get_call_signature(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey.call()
    assert call_key

    alarm.getCallABISignature.call(call_key) == '\xb2\x9f\x085'
    alarm.getCallABISignature.call(call_key) == client_contract.doIt.encoded_abi_signature
def test_getting_contract_address(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() is False

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.getCallContractAddress(call_key) == client_contract._meta.address
def test_authorizing_other_address(geth_node, geth_coinbase, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.AuthorizesOthers

    assert alarm.checkAuthorization.call(geth_coinbase, client_contract._meta.address) is False

    wait_for_transaction(rpc_client, client_contract.authorize.sendTransaction(alarm._meta.address))

    assert alarm.checkAuthorization.call(geth_coinbase, client_contract._meta.address) is True

    wait_for_transaction(rpc_client, client_contract.unauthorize.sendTransaction(alarm._meta.address))

    assert alarm.checkAuthorization.call(geth_coinbase, client_contract._meta.address) is False
Пример #33
0
def test_get_call_signature(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey.call()
    assert call_key

    alarm.getCallABISignature.call(call_key) == '\xb2\x9f\x085'
    alarm.getCallABISignature.call(
        call_key) == client_contract.doIt.encoded_abi_signature
def test_withdrawing_bond_restricted_when_in_pool(deploy_client,
                                                  deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getBondBalance(coinbase) == 0
    deposit_amount = alarm.getMinimumBond() * 10

    txn_1_hash = alarm.depositBond.sendTransaction(value=deposit_amount)
    wait_for_transaction(deploy_client, txn_1_hash)

    assert alarm.getBondBalance(coinbase) == deposit_amount

    assert alarm.isInPool(coinbase) is False
    assert alarm.canEnterPool(coinbase) is True
    wait_for_transaction(deploy_client, alarm.enterPool.sendTransaction())

    txn_2_hash = alarm.withdrawBond.sendTransaction(deposit_amount)
    wait_for_transaction(deploy_client, txn_2_hash)

    assert alarm.isInPool(coinbase) is True

    # Withdrawl of full amount not allowed
    assert alarm.getBondBalance(coinbase) == deposit_amount

    # wi
    minimum_bond = alarm.getMinimumBond()
    txn_3_hash = alarm.withdrawBond.sendTransaction(
        deposit_amount - 2 * minimum_bond, )
    wait_for_transaction(deploy_client, txn_3_hash)

    # Withdrawl of amount above minimum bond amount is allowed
    assert alarm.isInPool(coinbase) is True
    assert alarm.getBondBalance(coinbase) == 2 * minimum_bond
def test_getting_target_block(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)
    txn = deploy_client.get_transaction_by_hash(txn_hash)

    created_at_block = int(txn['blockNumber'], 16)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.getCallTargetBlock(call_key) == created_at_block + 40
def test_get_scheduled_by(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key

    alarm.getCallScheduledBy(call_key) == deploy_client.get_coinbase()
def test_first_call_registered_is_set_as_root(geth_node, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.SpecifyBlock

    assert alarm.rootNodeCallKey.call(raw=True) == "0x0000000000000000000000000000000000000000000000000000000000000000"

    current_block = rpc_client.get_block_number()

    wait_for_transaction(rpc_client, client_contract.scheduleIt.sendTransaction(alarm._meta.address, current_block + 100))

    last_call_key = alarm.getLastCallKey.call()
    assert last_call_key is not None

    assert alarm.rootNodeCallKey.call() == last_call_key
def test_depositing_bond(geth_node, geth_coinbase, deployed_contracts):
    caller_pool = deployed_contracts.CallerPool

    assert caller_pool.callerBonds.call(geth_coinbase) == 0

    txn_1_hash = caller_pool.depositBond.sendTransaction(value=123)
    wait_for_transaction(caller_pool._meta.rpc_client, txn_1_hash)

    assert caller_pool.callerBonds.call(geth_coinbase) == 123

    txn_2_hash = caller_pool.depositBond.sendTransaction(value=456)
    wait_for_transaction(caller_pool._meta.rpc_client, txn_2_hash)

    assert caller_pool.callerBonds.call(geth_coinbase) == 579
def test_getting_target_block(geth_node, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)
    txn = client_contract._meta.rpc_client.get_transaction_by_hash(txn_hash)

    created_at_block = int(txn["blockNumber"], 16)

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    assert alarm.getCallTargetBlock.call(callKey) == created_at_block + 40
def test_depositing_funds(geth_node, geth_coinbase, deployed_contracts):
    alarm = deployed_contracts.Alarm

    assert alarm.accountBalances.call(geth_coinbase) == 0

    txn_1_hash = alarm.deposit.sendTransaction(geth_coinbase, value=123)
    wait_for_transaction(alarm._meta.rpc_client, txn_1_hash)

    assert alarm.accountBalances.call(geth_coinbase) == 123

    txn_2_hash = alarm.deposit.sendTransaction(geth_coinbase, value=456)
    wait_for_transaction(alarm._meta.rpc_client, txn_2_hash)

    assert alarm.accountBalances.call(geth_coinbase) == 579
def test_withdrawing_bond_restricted_when_in_pool(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getBondBalance(coinbase) == 0
    deposit_amount = alarm.getMinimumBond() * 10

    txn_1_hash = alarm.depositBond.sendTransaction(value=deposit_amount)
    wait_for_transaction(deploy_client, txn_1_hash)

    assert alarm.getBondBalance(coinbase) == deposit_amount

    assert alarm.isInPool(coinbase) is False
    assert alarm.canEnterPool(coinbase) is True
    wait_for_transaction(deploy_client, alarm.enterPool.sendTransaction())

    txn_2_hash = alarm.withdrawBond.sendTransaction(deposit_amount)
    wait_for_transaction(deploy_client, txn_2_hash)

    assert alarm.isInPool(coinbase) is True

    # Withdrawl of full amount not allowed
    assert alarm.getBondBalance(coinbase) == deposit_amount

    # wi
    minimum_bond = alarm.getMinimumBond()
    txn_3_hash = alarm.withdrawBond.sendTransaction(
        deposit_amount - 2 * minimum_bond,
    )
    wait_for_transaction(deploy_client, txn_3_hash)

    # Withdrawl of amount above minimum bond amount is allowed
    assert alarm.isInPool(coinbase) is True
    assert alarm.getBondBalance(coinbase) == 2 * minimum_bond
def test_depositing(deploy_client, deployed_contracts):
    bank = deployed_contracts.TestAccounting
    coinbase = deploy_client.get_coinbase()

    assert bank.getAccountBalance(coinbase) == 0

    txn_hash = bank.deposit(value=1234)
    wait_for_transaction(deploy_client, txn_hash)

    assert bank.getAccountBalance(coinbase) == 1234

    txn_hash = bank.deposit(value=1234)
    wait_for_transaction(deploy_client, txn_hash)

    assert bank.getAccountBalance(coinbase) == 2468
def test_depositing_bond(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getBondBalance.call(coinbase) == 0

    txn_1_hash = alarm.depositBond.sendTransaction(value=123)
    wait_for_transaction(deploy_client, txn_1_hash)

    assert alarm.getBondBalance.call(coinbase) == 123

    txn_2_hash = alarm.depositBond.sendTransaction(value=456)
    wait_for_transaction(deploy_client, txn_2_hash)

    assert alarm.getBondBalance.call(coinbase) == 579
def test_executing_scheduled_call(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, 3)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    call_data = alarm.getCallData(call_key)
    assert call_data == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03'
def test_getting_base_gas_used(geth_node, rpc_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(rpc_client) * rpc_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)
    txn = rpc_client.get_transaction_by_hash(txn_hash)

    callKey = alarm.getLastCallKey.call()
    assert callKey is not None

    assert alarm.getCallBaseGasPrice.call(callKey) == int(txn['gasPrice'], 16)
def test_depositing_bond(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getBondBalance.call(coinbase) == 0

    txn_1_hash = alarm.depositBond.sendTransaction(value=123)
    wait_for_transaction(deploy_client, txn_1_hash)

    assert alarm.getBondBalance.call(coinbase) == 123

    txn_2_hash = alarm.depositBond.sendTransaction(value=456)
    wait_for_transaction(deploy_client, txn_2_hash)

    assert alarm.getBondBalance.call(coinbase) == 579
def test_depositing_funds(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    coinbase = deploy_client.get_coinbase()

    assert alarm.getAccountBalance.call(coinbase) == 0

    txn_1_hash = alarm.deposit.sendTransaction(coinbase, value=123)
    wait_for_transaction(alarm._meta.rpc_client, txn_1_hash)

    assert alarm.getAccountBalance.call(coinbase) == 123

    txn_2_hash = alarm.deposit.sendTransaction(coinbase, value=456)
    wait_for_transaction(alarm._meta.rpc_client, txn_2_hash)

    assert alarm.getAccountBalance.call(coinbase) == 579
Пример #48
0
def test_get_scheduled_by(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key

    alarm.getCallScheduledBy(call_key) == deploy_client.get_coinbase()
Пример #49
0
def test_scheduled_call_execution_without_pool(geth_node, geth_node_config, deploy_client, deployed_contracts, contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.SpecifyBlock

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    target_block = deploy_client.get_block_number() + 45

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, target_block)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    scheduled_call = ScheduledCall(alarm, call_key)
    block_sage = scheduled_call.block_sage
    pool_manager = PoolManager(alarm, block_sage=block_sage)

    time.sleep(1)

    assert pool_manager.in_any_pool is False
    scheduled_call.execute_async()

    # let the scheduled call do it's thing.
    assert block_sage.current_block_number < target_block

    wait_till = scheduled_call.target_block + 10
    wait_for_block(
        deploy_client,
        wait_till,
        2 * block_sage.estimated_time_to_block(wait_till),
    )

    for i in range(5):
        if scheduled_call.txn_hash:
            break
        time.sleep(block_sage.block_time)

    assert scheduled_call.txn_hash
    assert scheduled_call.txn_receipt
    assert scheduled_call.txn

    assert alarm.checkIfCalled(scheduled_call.call_key)
    assert scheduled_call.was_called
    assert scheduled_call.target_block <= scheduled_call.called_at_block
    assert scheduled_call.called_at_block <= scheduled_call.target_block + scheduled_call.grace_period
def test_getting_base_gas_used(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.NoArgs

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)
    txn = deploy_client.get_transaction_by_hash(txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.getCallBaseGasPrice(call_key) == int(txn['gasPrice'], 16)
Пример #51
0
def test_scheduler(geth_node, geth_node_config, deploy_client,
                   deployed_contracts, contracts):
    block_sage = BlockSage(deploy_client)

    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.SpecifyBlock

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    anchor_block = deploy_client.get_block_number()

    blocks = (1, 4, 4, 8, 30, 40, 50, 60)

    call_keys = []

    for n in blocks:
        wait_for_transaction(
            deploy_client,
            client_contract.scheduleIt.sendTransaction(alarm._meta.address,
                                                       anchor_block + 100 + n))

        last_call_key = alarm.getLastCallKey()
        assert last_call_key is not None

        call_keys.append(last_call_key)

    pool_manager = PoolManager(alarm, block_sage)
    scheduler = Scheduler(alarm, pool_manager, block_sage=block_sage)
    scheduler.monitor_async()

    final_block = anchor_block + 100 + 70
    wait_for_block(
        deploy_client,
        final_block,
        2 * block_sage.estimated_time_to_block(final_block),
    )

    scheduler.stop()
    block_sage.stop()

    results = [alarm.checkIfCalled(k) for k in call_keys]
    assert all(results)
Пример #52
0
def test_executing_scheduled_call(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesUInt

    deposit_amount = get_max_gas(
        deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address,
                                  value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(
        alarm._meta.address, 3)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    call_data = alarm.getCallData(call_key)
    assert call_data == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03'
Пример #53
0
def test_cancelling_a_call(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.CancelsCall

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address)
    wait_for_transaction(deploy_client, txn_hash)

    call_key = alarm.getLastCallKey()
    assert call_key is not None

    assert alarm.checkIfCancelled(call_key) is False

    cancel_txn_hash = client_contract.cancelIt.sendTransaction(alarm._meta.address, call_key)
    wait_for_transaction(deploy_client, cancel_txn_hash)

    assert alarm.checkIfCancelled(call_key) is True
def test_registering_bytes(deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.TestDataRegistry

    assert client_contract.wasSuccessful.call() == 0

    # This is for a weird bug that I'm not sure how to fix.
    evm = alarm._meta.rpc_client.evm
    evm.mine()

    txn_hash = client_contract.registerBytes.sendTransaction(alarm._meta.address, 'abcd')
    wait_for_transaction(client_contract._meta.rpc_client, txn_hash)

    assert client_contract.wasSuccessful.call() == 1

    data_hash = alarm.getLastDataHash.call()
    assert data_hash is not None

    data = alarm.getLastData.call()
    assert data == 'abcd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Пример #55
0
def test_authorizing_other_address(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.AuthorizesOthers
    coinbase = deploy_client.get_coinbase()

    assert alarm.checkAuthorization.call(
        coinbase, client_contract._meta.address) is False

    wait_for_transaction(
        deploy_client,
        client_contract.authorize.sendTransaction(alarm._meta.address))

    assert alarm.checkAuthorization.call(coinbase,
                                         client_contract._meta.address) is True

    wait_for_transaction(
        deploy_client,
        client_contract.unauthorize.sendTransaction(alarm._meta.address))

    assert alarm.checkAuthorization.call(
        coinbase, client_contract._meta.address) is False
def test_executing_scheduled_call_with_address(deploy_client, deployed_contracts):
    alarm = deployed_contracts.Alarm
    client_contract = deployed_contracts.PassesAddress

    deposit_amount = get_max_gas(deploy_client) * deploy_client.get_gas_price() * 20
    alarm.deposit.sendTransaction(client_contract._meta.address, value=deposit_amount)

    address = '0xc948453368e5ddc7bc00bb52b5809138217a068d'

    txn_hash = client_contract.scheduleIt.sendTransaction(alarm._meta.address, address)
    wait_for_transaction(deploy_client, txn_hash)

    assert client_contract.value() == '0x0000000000000000000000000000000000000000'

    call_key = alarm.getLastCallKey()
    assert call_key is not None
    deploy_client.wait_for_block(alarm.getCallTargetBlock(call_key), 120)
    call_txn_hash = alarm.doCall.sendTransaction(call_key)
    wait_for_transaction(deploy_client, call_txn_hash)

    assert client_contract.value() == address