Constant QUERY_STATE_BLOCK_ADDRESS

Source
pub const QUERY_STATE_BLOCK_ADDRESS: &str = "WITH RECURSIVE chain AS (\n    -- Base case: start with the given block address\n    -- Note the key may be found here\n    SELECT\n        b.id AS block_id,\n        b.parent_block_id,\n        b.block_address,\n        b.number AS number,\n        m.value AS found_value,\n        0 AS depth\n    FROM\n        block b\n        LEFT JOIN block_solution_set bs ON bs.block_id = b.id\n        LEFT JOIN solution ON solution.solution_set_id = bs.solution_set_id AND solution.contract_addr = :contract_ca\n        LEFT JOIN mutation m ON m.solution_id = solution.id\n        AND m.key = :key\n    WHERE\n        b.block_address = :block_address\n    UNION\n    ALL -- Recursive case: follow parent pointers until we either find a value or hit a finalized block\n    SELECT\n        b.id AS block_id,\n        b.parent_block_id,\n        b.block_address,\n        b.number AS number,\n        m.value AS found_value,\n        c.depth + 1\n    FROM\n        chain c\n        JOIN block b ON b.id = c.parent_block_id\n        LEFT JOIN solution ON solution.solution_set_id = b.id AND solution.contract_addr = :contract_ca\n        LEFT JOIN mutation m ON m.solution_id = solution.id\n        AND m.key = :key\n    WHERE\n        c.found_value IS NULL -- Stop recursing if we found a value\n        AND NOT EXISTS (\n            -- Stop recursing if we hit a finalized block\n            SELECT\n                1\n            FROM\n                finalized_block fb\n            WHERE\n                fb.block_id = c.block_id\n        )\n)\nSELECT\n    (\n        -- The key was found in this block but we\n        -- have to find the latest version within this block\n        SELECT\n            value\n        FROM\n            block_solution_set bs\n            JOIN solution ON solution.solution_set_id = bs.solution_set_id AND solution.contract_addr = :contract_ca\n            JOIN mutation m ON m.solution_id = solution.id\n        WHERE\n            bs.block_id = chain.block_id\n            AND m.key = :key\n            AND (\n                :solution_set_index IS NULL\n                OR bs.solution_set_index <= :solution_set_index\n            )\n        ORDER BY\n            bs.solution_set_index DESC\n    ) AS found_value,\n    number\nFROM\n    chain\nWHERE\n    found_value IS NOT NULL\nORDER BY\n    depth ASC\nLIMIT\n    1;\n";
Expand description
WITH RECURSIVE chain AS (
    -- Base case: start with the given block address
    -- Note the key may be found here
    SELECT
        b.id AS block_id,
        b.parent_block_id,
        b.block_address,
        b.number AS number,
        m.value AS found_value,
        0 AS depth
    FROM
        block b
        LEFT JOIN block_solution_set bs ON bs.block_id = b.id
        LEFT JOIN solution ON solution.solution_set_id = bs.solution_set_id AND solution.contract_addr = :contract_ca
        LEFT JOIN mutation m ON m.solution_id = solution.id
        AND m.key = :key
    WHERE
        b.block_address = :block_address
    UNION
    ALL -- Recursive case: follow parent pointers until we either find a value or hit a finalized block
    SELECT
        b.id AS block_id,
        b.parent_block_id,
        b.block_address,
        b.number AS number,
        m.value AS found_value,
        c.depth + 1
    FROM
        chain c
        JOIN block b ON b.id = c.parent_block_id
        LEFT JOIN solution ON solution.solution_set_id = b.id AND solution.contract_addr = :contract_ca
        LEFT JOIN mutation m ON m.solution_id = solution.id
        AND m.key = :key
    WHERE
        c.found_value IS NULL -- Stop recursing if we found a value
        AND NOT EXISTS (
            -- Stop recursing if we hit a finalized block
            SELECT
                1
            FROM
                finalized_block fb
            WHERE
                fb.block_id = c.block_id
        )
)
SELECT
    (
        -- The key was found in this block but we
        -- have to find the latest version within this block
        SELECT
            value
        FROM
            block_solution_set bs
            JOIN solution ON solution.solution_set_id = bs.solution_set_id AND solution.contract_addr = :contract_ca
            JOIN mutation m ON m.solution_id = solution.id
        WHERE
            bs.block_id = chain.block_id
            AND m.key = :key
            AND (
                :solution_set_index IS NULL
                OR bs.solution_set_index <= :solution_set_index
            )
        ORDER BY
            bs.solution_set_index DESC
    ) AS found_value,
    number
FROM
    chain
WHERE
    found_value IS NOT NULL
ORDER BY
    depth ASC
LIMIT
    1;