Skip to content

Commit

Permalink
Fixes discovered during schemathesis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdlrt committed Sep 18, 2023
1 parent bc34d76 commit da96b9b
Show file tree
Hide file tree
Showing 18 changed files with 903 additions and 924 deletions.
8 changes: 3 additions & 5 deletions files/grest/rpc/account/account_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ BEGIN
txo.stake_address_id,
txo.id
FROM tx_out AS txo
WHERE
txo.stake_address_id = ANY(sa_id_list)
AND tx_out.consumed_by_tx_in_id IS NULL
WHERE txo.stake_address_id = ANY(sa_id_list)
AND txo.consumed_by_tx_in_id IS NULL
) AS x
)

Expand All @@ -55,8 +54,7 @@ BEGIN
txo.stake_address_id,
txo.id
FROM tx_out AS txo
WHERE
txo.stake_address_id = ANY(sa_id_list)
WHERE txo.stake_address_id = ANY(sa_id_list)
LIMIT (CASE WHEN _first_only IS TRUE THEN 1 ELSE NULL END)
) AS x
)
Expand Down
12 changes: 5 additions & 7 deletions files/grest/rpc/account/account_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ BEGIN
--
UNION
--
SELECT tx_in_id AS tx_id
SELECT consumed_by_tx_in_id AS tx_id
FROM tx_out
LEFT JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id
AND tx_out.index = tx_in.tx_out_index
WHERE
tx_in.tx_in_id IS NOT NULL
tx_out.consumed_by_tx_in_id IS NULL
AND tx_out.stake_address_id = ANY(SELECT id FROM stake_address WHERE view = _stake_address)
AND tx_in.tx_in_id >= _tx_id_min
AND tx_out.consumed_by_tx_in_id >= _tx_id_min
) AS tmp;

RETURN QUERY
Expand All @@ -45,8 +43,8 @@ BEGIN
FROM public.tx
INNER JOIN public.block AS b ON b.id = tx.block_id
WHERE tx.id = ANY(_tx_id_list)
AND block.block_no >= _after_block_height
ORDER BY block.block_no DESC;
AND b.block_no >= _after_block_height
ORDER BY b.block_no DESC;
END;
$$;

Expand Down
10 changes: 5 additions & 5 deletions files/grest/rpc/address/address_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ BEGIN
--
UNION
--
SELECT tx_in_id AS tx_id
SELECT consumed_by_tx_in_id AS tx_id
FROM tx_out
LEFT JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id
AND tx_out.index = tx_in.tx_out_index
WHERE tx_in.tx_in_id IS NOT NULL
WHERE tx_out.consumed_by_tx_in_id IS NOT NULL
AND tx_out.address = ANY(_addresses)
AND tx_in.tx_in_id >= _tx_id_min
AND tx_out.consumed_by_tx_in_id >= _tx_id_min
) AS tmp;

RETURN QUERY
Expand All @@ -44,8 +44,8 @@ BEGIN
FROM public.tx
INNER JOIN public.block AS b ON b.id = tx.block_id
WHERE tx.id = ANY(_tx_id_list)
AND block.block_no >= _after_block_height
ORDER BY block.block_no DESC;
AND b.block_no >= _after_block_height
ORDER BY b.block_no DESC;
END;
$$;

Expand Down
12 changes: 5 additions & 7 deletions files/grest/rpc/address/credential_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ BEGIN
--
UNION
--
SELECT tx_in_id AS tx_id
SELECT consumed_by_tx_in_id AS tx_id
FROM tx_out
LEFT JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id
AND tx_out.index = tx_in.tx_out_index
WHERE tx_in.tx_in_id IS NOT NULL
WHERE tx_out.consumed_by_tx_in_id IS NOT NULL
AND tx_out.payment_cred = ANY(_payment_cred_bytea)
AND tx_in.tx_in_id >= _tx_id_min
AND tx_out.consumed_by_tx_in_id >= _tx_id_min
) AS tmp;

RETURN QUERY
Expand All @@ -52,8 +50,8 @@ BEGIN
FROM public.tx
INNER JOIN public.block AS b ON b.id = tx.block_id
WHERE tx.id = ANY(_tx_id_list)
AND block.block_no >= _after_block_height
ORDER BY block.block_no DESC;
AND b.block_no >= _after_block_height
ORDER BY b.block_no DESC;
END;
$$;

Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/assets/asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BEGIN
FROM ma_tx_out AS mto
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
WHERE mto.ident = _asset_id
AND tx_out.consumed_by_tx_in_id IS NULL
AND txo.consumed_by_tx_in_id IS NULL
) AS x
GROUP BY x.address;
END;
Expand Down
5 changes: 2 additions & 3 deletions files/grest/rpc/assets/asset_txs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ BEGIN
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
INNER JOIN tx ON tx.id = txo.tx_id
INNER JOIN block ON block.id = tx.block_id
LEFT JOIN tx_in AS txi ON txo.tx_id = txi.tx_out_id
AND txo.index::smallint = txi.tx_out_index::smallint
WHERE
mto.ident = _asset_id
AND block.block_no >= _after_block_height
AND (_history = TRUE OR txi.id IS NULL)
AND (_history = TRUE OR txo.consumed_by_tx_in_id IS NULL)
GROUP BY
ident,
tx.hash,
txo.index::smallint,
block.epoch_no,
block.block_no,
block.time
Expand Down
5 changes: 2 additions & 3 deletions files/grest/rpc/assets/asset_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
INNER JOIN _assets ON tx_out.id = _assets.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN script ON script.tx_id = tx_out.reference_script_id
LEFT JOIN _assets ON tx_out.id = _assets.id
WHERE mto.ident = ANY(_asset_id_list)
AND tx_out.consumed_by_tx_in_id IS NULL
WHERE tx_out.consumed_by_tx_in_id IS NULL
;
END;
$$;
Expand Down
38 changes: 10 additions & 28 deletions files/grest/rpc/assets/policy_asset_addresses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,20 @@ RETURNS TABLE (
)
LANGUAGE plpgsql
AS $$
DECLARE
_asset_policy_decoded bytea;
BEGIN
SELECT DECODE(_asset_policy, 'hex') INTO _asset_policy_decoded;
RETURN QUERY
WITH
_all_assets AS (
SELECT
id,
ENCODE(name, 'hex') AS asset_name
FROM multi_asset AS ma
WHERE ma.policy = _asset_policy_decoded
)

SELECT
x.asset_name,
x.address,
SUM(x.quantity)::text
FROM
(
SELECT
aa.asset_name,
txo.address,
mto.quantity
FROM _all_assets AS aa
INNER JOIN ma_tx_out AS mto ON mto.ident = aa.id
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
WHERE tx_out.consumed_by_tx_in_id IS NULL
) AS x
ENCODE(ma.name, 'hex') AS asset_name,
txo.address,
SUM(mto.quantity)::text
FROM multi_asset AS ma
INNER JOIN ma_tx_out AS mto ON mto.ident = ma.id
INNER JOIN tx_out AS txo ON txo.id = mto.tx_out_id
WHERE ma.policy = DECODE(_asset_policy, 'hex')
AND txo.consumed_by_tx_in_id IS NULL
GROUP BY
x.asset_name,
x.address;
ma.name,
txo.address;
END;
$$;

Expand Down
6 changes: 1 addition & 5 deletions files/grest/rpc/assets/policy_asset_info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ RETURNS TABLE (
)
LANGUAGE plpgsql
AS $$
DECLARE
_asset_policy_decoded bytea;
_policy_asset_ids bigint[];
BEGIN
SELECT DECODE(_asset_policy, 'hex') INTO _asset_policy_decoded;
RETURN QUERY
SELECT
ENCODE(ma.name, 'hex') AS asset_name,
Expand Down Expand Up @@ -73,7 +69,7 @@ BEGIN
FROM tx_metadata AS tm
WHERE tm.tx_id = tx.id
) AS metadata ON TRUE
WHERE ma.policy = _asset_policy_decoded;
WHERE ma.policy = DECODE(_asset_policy, 'hex');
END;
$$;

Expand Down
6 changes: 3 additions & 3 deletions files/grest/rpc/pool/pool_metadata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ BEGIN
pic.meta_hash,
pod.json,
pic.pool_status
FROM grest.pool_hash AS ph
LEFT JOIN grest.pool_infor_cache AS pic ON ph.view = pic.pool_id_bech32
FROM public.pool_hash AS ph
LEFT JOIN grest.pool_info_cache AS pic ON ph.view = pic.pool_id_bech32
LEFT JOIN public.pool_offline_data AS pod ON pod.pmr_id = pic.meta_id
WHERE
CASE
WHEN _pool_bech32_ids IS NULL THEN TRUE
WHEN _pool_bech32_ids IS NOT NULL THEN pic.pool_id_bech32 = ANY(SELECT UNNEST(_pool_bech32_ids))
END
ORDER BY
ph.view,
pic.pool_id_bech32,
pic.tx_id DESC;
END;
$$;
Expand Down
5 changes: 2 additions & 3 deletions files/grest/rpc/script/script_utxos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ BEGIN
END) AS is_spent
FROM tx_out
INNER JOIN tx ON tx_out.tx_id = tx.id
INNER JOIN script ON script.tx_id = tx.id
LEFT JOIN stake_address AS sa ON tx_out.stake_address_id = sa.id
LEFT JOIN block AS b ON b.id = tx.block_id
LEFT JOIN ma_tx_out AS mto ON mto.tx_out_id = tx_out.id
LEFT JOIN multi_asset AS ma ON ma.id = mto.ident
LEFT JOIN grest.asset_info_cache AS aic ON aic.asset_id = ma.id
LEFT JOIN datum ON datum.id = tx_out.inline_datum_id
LEFT JOIN script ON script.tx_id = tx.id
WHERE
script.hash = DECODE(_script_hash,'hex')
WHERE script.hash = DECODE(_script_hash,'hex')
;
END;
$$;
Expand Down
Loading

0 comments on commit da96b9b

Please sign in to comment.