def scheduler(): Alarm = get_contract('Alarm') CallerPool = get_contract('CallerPool') alarm = Alarm(alarm_address, rpc_client) caller_pool = CallerPool(alarm.getCallerPoolAddress.call(), rpc_client) block_sage = BlockSage(rpc_client) pool_manager = PoolManager(caller_pool, block_sage=block_sage) pool_manager.monitor_async() scheduler = Scheduler(alarm, pool_manager, block_sage=block_sage) scheduler.monitor_async() try: while scheduler._thread.is_alive(): time.sleep(1) except KeyboardInterrupt: scheduler.stop() scheduler.block_sage.stop() scheduler.pool_manager.stop() for scheduled_call in scheduler.active_calls.values(): scheduled_call.stop() scheduler._thread.join(5)
def scheduler(address): """ Run the call scheduler. """ Alarm = get_contract('Scheduler') alarm = Alarm(address, rpc_client) block_sage = BlockSage(rpc_client) pool_manager = PoolManager(alarm, block_sage=block_sage) pool_manager.monitor_async() scheduler = Scheduler(alarm, pool_manager, block_sage=block_sage) scheduler.monitor_async() try: while scheduler._thread.is_alive(): time.sleep(1) except KeyboardInterrupt: scheduler.stop() scheduler.block_sage.stop() scheduler.pool_manager.stop() for scheduled_call in scheduler.active_calls.values(): scheduled_call.stop() scheduler._thread.join(5)
def test_enumerate_upcoming_calls_with_no_calls(deployed_contracts, scheduled_calls): scheduler = Scheduler(deployed_contracts.Scheduler) last_target_block = scheduled_calls[-1].targetBlock() actual_calls = tuple( scheduler.enumerate_calls(last_target_block + 1, 1000000)) assert not actual_calls
def test_enumerate_upcoming_calls(deployed_contracts, scheduled_calls): scheduler = Scheduler(deployed_contracts.Scheduler) expected_calls = tuple(call._meta.address for call in scheduled_calls) actual_calls = tuple( scheduler.enumerate_calls( scheduled_calls[0].targetBlock(), scheduled_calls[-1].targetBlock(), )) assert actual_calls == expected_calls
def test_basic_call_enumeration(geth_node, deployed_contracts, scheduled_calls): scheduler = Scheduler(deployed_contracts.Scheduler) left_block = scheduled_calls[1].targetBlock() right_block = scheduled_calls[5].targetBlock() call_addresses = tuple(scheduler.enumerate_calls(left_block, right_block)) assert len(call_addresses) == 5 expected_addresses = tuple(call._meta.address for call in scheduled_calls[1:6]) assert call_addresses == expected_addresses
def test_scheduler(geth_node, geth_node_config, deploy_client, deployed_contracts, contracts, get_call, denoms): block_sage = BlockSage(deploy_client) scheduler = deployed_contracts.Scheduler client_contract = deployed_contracts.TestCallExecution anchor_block = deploy_client.get_block_number() blocks = (1, 4, 4, 8, 30, 40, 50, 60) calls = [] for n in blocks: scheduling_txn = scheduler.scheduleCall( client_contract._meta.address, client_contract.setBool.encoded_abi_signature, anchor_block + 100 + n, 1000000, value=10 * denoms.ether, gas=3000000, ) scheduling_receipt = deploy_client.wait_for_transaction(scheduling_txn) call = get_call(scheduling_txn) calls.append(call) pool_manager = PoolManager(scheduler, block_sage) scheduler = Scheduler(scheduler, pool_manager, block_sage=block_sage) scheduler.monitor_async() final_block = anchor_block + 100 + 80 deploy_client.wait_for_block( final_block, 2 * block_sage.estimated_time_to_block(final_block), ) scheduler.stop() block_sage.stop() did_suicide = [len(deploy_client.get_code(call._meta.address)) <= 2 for call in calls] assert all(did_suicide)
def test_get_next_call_sibling(deployed_contracts, scheduled_calls, mock_blocksage): scheduler = Scheduler(deployed_contracts.Scheduler, block_sage=mock_blocksage) # same block next_call_a = scheduler.get_next_call_sibling( scheduled_calls[1]._meta.address) assert next_call_a == scheduled_calls[2]._meta.address # different blocks next_call_b = scheduler.get_next_call_sibling( scheduled_calls[2]._meta.address) assert next_call_b == scheduled_calls[3]._meta.address # no next call next_call_c = scheduler.get_next_call_sibling( scheduled_calls[-1]._meta.address) assert next_call_c is None
def scheduler(address): """ Run the call scheduler. """ Alarm = get_contract('Alarm') alarm = Alarm(address, rpc_client) block_sage = BlockSage(rpc_client) pool_manager = PoolManager(alarm, block_sage=block_sage) pool_manager.monitor_async() scheduler = Scheduler(alarm, pool_manager, block_sage=block_sage) scheduler.monitor_async() try: while scheduler._thread.is_alive(): time.sleep(1) except KeyboardInterrupt: scheduler.stop() scheduler.block_sage.stop() scheduler.pool_manager.stop() for scheduled_call in scheduler.active_calls.values(): scheduled_call.stop() scheduler._thread.join(5)
def test_scheduler(geth_node, deployed_contracts, deploy_client, scheduled_calls): block_sage = BlockSage(deploy_client) scheduler = Scheduler(deployed_contracts.Scheduler, block_sage=block_sage) scheduler.monitor_async() last_call = scheduled_calls[-1] final_block = last_call.targetBlock() + last_call.gracePeriod() + 1 for call in scheduled_calls: wait_til = call.targetBlock() - 5 deploy_client.wait_for_block( wait_til, block_sage.estimated_time_to_block(wait_til) * 2, ) time.sleep(2) wait_til = scheduled_calls[-1].targetBlock() + 50 deploy_client.wait_for_block( wait_til, block_sage.estimated_time_to_block(wait_til) * 2, ) scheduler.stop() block_sage.stop() was_called = [call.wasCalled() for call in scheduled_calls] assert all(was_called)
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)
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)
def test_get_next_call(deployed_contracts, scheduled_calls): scheduler = Scheduler(deployed_contracts.Scheduler) # target block - 1 next_call_a = scheduler.get_next_call(scheduled_calls[1].targetBlock() - 1) assert next_call_a == scheduled_calls[1]._meta.address # target block - 1 next_call_b = scheduler.get_next_call(scheduled_calls[1].targetBlock()) assert next_call_b == scheduled_calls[1]._meta.address # before all scheduled_calls next_call_c = scheduler.get_next_call(0) assert next_call_c == scheduled_calls[0]._meta.address # after all scheduled_calls next_call_d = scheduler.get_next_call(scheduled_calls[-1].targetBlock() + 1) assert next_call_d is None
def scheduler(address, client, rpchost, rpcport, ipcpath): """ Run the call scheduler. """ if client == 'ipc': blockchain_client = IPCClient(ipc_path=ipcpath) elif client == 'rpc': blockchain_client = RPCClient(host=rpchost, port=rpcport) SchedulerContract = get_contract('Scheduler') scheduler_contract = SchedulerContract(address, blockchain_client) try: api_version = scheduler_contract.callAPIVersion() if api_version != 7: raise click.ClickException( "The scheduling contract address does not appear to have a compatable API" ) except EmptyDataError: raise click.ClickException( "The scheduler address seems to not be correct. Using {0}. You " "may need to specify the address using `--address` if you are " "running the client against a test network".format(address)) block_sage = BlockSage(blockchain_client) scheduler = Scheduler(scheduler_contract, block_sage=block_sage) scheduler.monitor_async() try: while scheduler._thread.is_alive(): time.sleep(1) except KeyboardInterrupt: scheduler.stop() scheduler.block_sage.stop() for scheduled_call in scheduler.active_calls.values(): scheduled_call.stop() scheduler._thread.join(5)