Exemplo n.º 1
0
async def handle_general_state_tests(fixture_data):
    # Start from Genesis
    params = [fixture_data]
    request_msg = build_request('evm_resetToGenesisFixture', params)
    response = await Connection.get_ipc_response(request_msg)

    if "result" in response:
        assert response[
            "result"] is True, "Unable to Reset Genesis - %s" % response
    else:
        print(response)
        assert False

    # Validate the fixture_data["pre"] states
    await validate_accounts(fixture_data['pre'])

    # Mine the fixture_data["blocks"] on the chain initialized above with genesis
    await mine_and_validate_fixture_blocks(fixture_data["blocks"])

    if fixture_data.get('lastblockhash', None):
        for block_fixture in fixture_data['blocks']:
            if get_in(['blockHeader', 'hash'],
                      block_fixture) == fixture_data['lastblockhash']:
                await validate_last_block(block_fixture)

    # Validate the fixture_data["post"] states
    await validate_accounts(fixture_data['postState'])
    # Validate that mining of above block didn't alter the previous state
    await validate_accounts(fixture_data['pre'], 'earliest')
async def validate_block(block_fixture, at_block):
    if is_by_hash(at_block):
        rpc_method = 'eth_getBlockByHash'
    else:
        rpc_method = 'eth_getBlockByNumber'

    # validate without transaction bodies
    params = [at_block, False]
    request_msg = build_request(rpc_method, params)
    response = await Connection.get_ipc_response(request_msg)
    assert "error" not in response, "Error in getting block: %s" % response

    result = response["result"]
    validate_rpc_block_vs_fixture(result, block_fixture)
    assert len(result['transactions']) == len(block_fixture['transactions'])

    for index, transaction_fixture in enumerate(block_fixture['transactions']):
        await validate_transaction_by_index(transaction_fixture, at_block, index)

    await validate_transaction_count(block_fixture, at_block)

    # TODO validate transaction bodies
    # result, error = await call_rpc(rpc, rpc_method, [at_block, True])
    # assert error is None
    # assert result['transactions'] == block_fixture['transactions']

    await validate_uncles(block_fixture, at_block)
Exemplo n.º 3
0
async def mine_and_validate_fixture_blocks(block_fixture_data):
    """
    This function is responsible to send out RPC calls to the server,
    so that the server can mine the blocks on its already formed (current) local chain
    1) Validate that the response which is rlp of the block as per the client is the same
       as those in the fixture, if the block in the fixture comes with the header
    2) Else check that the output should contain ``error`` as one of its json field
    """
    for block_fixture in block_fixture_data:
        should_be_good_block = 'blockHeader' in block_fixture

        if 'rlp_error' in block_fixture:
            assert not should_be_good_block
            continue

        params = [block_fixture]
        request_msg = build_request('evm_applyBlockFixture', params)
        response = await Connection.get_ipc_response(request_msg)

        if should_be_good_block:
            assert "error" not in response, "Response: %s" % response
            assert response["result"] == block_fixture[
                "rlp"], "RLP values don't match"

            # Validate the contents of the block as per fixture
            await validate_block(block_fixture,
                                 block_fixture['blockHeader']['hash'])
        else:
            assert "error" in response, "Response: %s" % response
async def validate_transaction_by_index(transaction_fixture, at_block, index):
    if is_by_hash(at_block):
        rpc_method = 'eth_getTransactionByBlockHashAndIndex'
    else:
        rpc_method = 'eth_getTransactionByBlockNumberAndIndex'

    params = [at_block, hex(index)]
    request_msg = build_request(rpc_method, params)
    response = await Connection.get_ipc_response(request_msg)
    assert "error" not in response, "Error in getting transaction: %s" % response
    validate_rpc_transaction_vs_fixture(response["result"], transaction_fixture)
async def validate_account_attribute(
        *,
        account_state_key,
        rpc_method,
        state,
        addr,
        at_block):
    params = [addr, at_block]
    request_msg = build_request(rpc_method, params)
    response = await Connection.get_ipc_response(request_msg)

    assert response["result"] == state[account_state_key], "Invalid state - %s" % response
async def validate_uncle_headers(block_fixture, at_block):
    if is_by_hash(at_block):
        rpc_method = 'eth_getUncleByBlockHashAndIndex'
    else:
        rpc_method = 'eth_getUncleByBlockNumberAndIndex'

    for idx, uncle in enumerate(block_fixture['uncleHeaders']):
        params = [at_block, hex(idx)]
        request_msg = build_request(rpc_method, params)
        response = await Connection.get_ipc_response(request_msg)
        assert "error" not in response
        validate_rpc_block_vs_fixture_header(response["result"], uncle)
async def validate_uncle_count(block_fixture, at_block):
    if is_by_hash(at_block):
        rpc_method = 'eth_getUncleCountByBlockHash'
    else:
        rpc_method = 'eth_getUncleCountByBlockNumber'

    num_uncles = len(block_fixture['uncleHeaders'])
    params = [at_block]
    request_msg = build_request(rpc_method, params)
    response = await Connection.get_ipc_response(request_msg)
    assert "error" not in response
    assert response["result"] == hex(num_uncles)
async def validate_transaction_count(block_fixture, at_block):
    if is_by_hash(at_block):
        rpc_method = 'eth_getBlockTransactionCountByHash'
    else:
        rpc_method = 'eth_getBlockTransactionCountByNumber'
    expected_transaction_count = hex(len(block_fixture['transactions']))

    params = [at_block]
    request_msg = build_request(rpc_method, params)
    response = await Connection.get_ipc_response(request_msg)
    assert "error" not in response
    assert response["result"] == expected_transaction_count
async def validate_account_state(account_state, addr, at_block):
    standardized_state = fixture_state_in_rpc_format(account_state)
    for account_state_key, rpc_method in RPC_STATE_LOOKUPS:
        await validate_account_attribute(
            account_state_key=account_state_key,
            rpc_method=rpc_method,
            state=standardized_state,
            addr=addr,
            at_block=at_block,
        )
    for key in account_state['storage']:
        position = '0x0' if key == '0x' else key
        expected_storage = account_state['storage'][key]

        params = [addr, position, at_block]
        request_msg = build_request('eth_getStorageAt', params)
        response = await Connection.get_ipc_response(request_msg)

        assert response["result"] == expected_storage, "Invalid storage - %s" % response["error"]
Exemplo n.º 10
0
def request_trip(update, context):
    user = User.from_dict(update.message.from_user)
    DB.execute(UserSQL.save, user.asdict())
    logger.info(
        f'Starting process find ticket request from {user.username} user: {context.args}'
    )

    request = build_request(user.id, context.args)
    request.id = DB.execute(RequestSQL.create, request.asdict())[0][0]

    message = f'Your request *{request.id}* is processing...'
    context.bot.send_message(user.id, text=message, parse_mode='Markdown')
    context.job_queue.run_repeating(process_request,
                                    interval=JOB_INTERVAL,
                                    first=0,
                                    context=request,
                                    name=request.id)

    DB.execute(RequestSQL.update_status_by_id,
               [RequestStatus.in_progress.value, request.id])
    logger.info(
        f'Request {request.id} from {user.username} user was successfully added to job queue'
    )