Skip to content

Commit

Permalink
[IND-399] Fix line numbers in SQL function stack traces (#821)
Browse files Browse the repository at this point in the history
This makes so that the PG_EXCEPTION_CONTEXT contains the correct line numbers giving a stack like:
PL/pgSQL function dydx_market_modify_handler(jsonb) line 17 at RAISE
PL/pgSQL function dydx_block_processor(jsonb) line 85 at assignment
  • Loading branch information
lcwik authored Nov 29, 2023
1 parent d6fa5fd commit 87e6119
Show file tree
Hide file tree
Showing 46 changed files with 195 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE OR REPLACE FUNCTION dydx_asset_create_handler(event_data jsonb) RETURNS jsonb AS $$
/**
Parameters:
- event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-proto/blob/8d35c86/dydxprotocol/indexer/indexer_manager/event.proto#L25)
converted to JSON format. Conversion to JSON is expected to be done by JSON.stringify.
Returns: JSON object containing fields:
- asset: The created asset in asset-model format (https://github.com/dydxprotocol/indexer/blob/cc70982/packages/postgres/src/models/asset-model.ts).
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_asset_create_handler(event_data jsonb) RETURNS jsonb AS $$
DECLARE
market_record_id integer;
asset_record assets%ROWTYPE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE OR REPLACE FUNCTION dydx_clob_pair_status_to_market_status(status jsonb)
RETURNS text AS $$
/**
Returns the market status (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/types/perpetual-market-types.ts#L60)
from the clob pair status (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L157).
The conversion is equivalent to https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/lib/protocol-translations.ts#L351.
Parameters:
- status: the ClobPairStatus (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L157)
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_clob_pair_status_to_market_status(status jsonb)
RETURNS text AS $$
BEGIN
CASE status
WHEN '1'::jsonb THEN RETURN 'ACTIVE'; /** CLOB_PAIR_STATUS_ACTIVE */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE OR REPLACE FUNCTION dydx_create_initial_rows_for_tendermint_block(
block_height text, block_time text, tx_hashes text[], events jsonb[]) RETURNS void AS $$
/**
Parameters:
- block_height: the height of the block being processed.
- block_time: the time of the block being processed.
- tx_hashes: Array of transaction hashes from the IndexerTendermintBlock.
- events: Array of IndexerTendermintEvent objects.
Returns: void.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_create_initial_rows_for_tendermint_block(
block_height text, block_time text, tx_hashes text[], events jsonb[]) RETURNS void AS $$
BEGIN
-- Create block.
INSERT INTO blocks ("blockHeight", "time") VALUES (block_height::bigint, block_time::timestamp);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
CREATE OR REPLACE FUNCTION dydx_create_tendermint_event(
event jsonb, block_height text
) RETURNS jsonb AS $$
/**
Parameters:
- event: The IndexerTendermintEvent object.
- block_height: the height of the block being processed.
Returns: The inserted event.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_create_tendermint_event(
event jsonb, block_height text
) RETURNS jsonb AS $$
DECLARE
transaction_idx int;
event_id bytea;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE OR REPLACE FUNCTION dydx_create_transaction(
transaction_hash text, block_height text, transaction_index int
) RETURNS jsonb AS $$
/**
Parameters:
- transaction_hash: the hash of the transaction being processed.
- block_height: the height of the block being processed.
- transaction_index: the index of the transaction in the block.
Returns: The inserted transaction.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_create_transaction(
transaction_hash text, block_height text, transaction_index int
) RETURNS jsonb AS $$
DECLARE
inserted_transaction jsonb;
BEGIN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CREATE OR REPLACE FUNCTION dydx_deleveraging_handler(
block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int,
transaction_hash text) RETURNS jsonb AS $$
/**
Parameters:
- block_height: the height of the block being processing.
Expand All @@ -14,10 +17,9 @@
- perpetual_market: The perpetual market for the deleveraging in perpetual-market-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-market-model.ts).
- liquidated_perpetual_position: The updated liquidated perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-position-model.ts).
- offsetting_perpetual_position: The updated offsetting perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-position-model.ts).
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_deleveraging_handler(
block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int,
transaction_hash text) RETURNS jsonb AS $$
DECLARE
QUOTE_CURRENCY_ATOMIC_RESOLUTION constant numeric = -6;
FEE constant numeric = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CREATE OR REPLACE FUNCTION dydx_event_id_from_parts(block_height int, transaction_index int, event_index int) RETURNS bytea AS $$
/**
Returns an event id from parts.
Expand All @@ -6,8 +7,9 @@
- transaction_index: The transaction_index of the IndexerTendermintEvent after the conversion that takes into
account the block_event (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/services/ender/src/lib/helper.ts#L41)
- event_index: The 'event_index' of the IndexerTendermintEvent.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_event_id_from_parts(block_height int, transaction_index int, event_index int) RETURNS bytea AS $$
BEGIN
/*
int4send converts to network order (which is also big endian order).
Expand Down
6 changes: 4 additions & 2 deletions indexer/services/ender/src/scripts/dydx_from_jsonlib_long.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CREATE OR REPLACE FUNCTION dydx_from_jsonlib_long(long_value jsonb) RETURNS numeric AS $$
/**
Converts JSON objects of the form (https://www.npmjs.com/package/long):
{
Expand All @@ -7,8 +8,9 @@
}
and converts it to a numeric. Note that this is the format used to convert Long types when converted using
JSON.stringify.
*/
CREATE OR REPLACE FUNCTION dydx_from_jsonlib_long(long_value jsonb) RETURNS numeric AS $$
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
DECLARE
POWER_2_32 constant numeric = power(2::numeric, 32::numeric);
BEGIN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/**
Converts the 'Side' enum from the IndexerOrder protobuf (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L56)
to the 'OrderSide' enum in postgres.
*/
CREATE OR REPLACE FUNCTION dydx_from_protocol_order_side(order_side jsonb) RETURNS text AS $$
/**
Converts the 'Side' enum from the IndexerOrder protobuf (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L56)
to the 'OrderSide' enum in postgres.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
BEGIN
CASE order_side
WHEN '1'::jsonb THEN RETURN 'BUY'; /** SIDE_BUY */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
CREATE OR REPLACE FUNCTION dydx_from_protocol_time_in_force(tif jsonb) RETURNS text AS $$
/**
Converts the TimeInForce field from an IndexerOrder proto (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L94)
to a TimeInForce enum in postgres.
Converts the TimeInForce field from an IndexerOrder proto (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/protocol/v1/clob.proto#L94)
to a TimeInForce enum in postgres.
Raise an exception if the input TimeInForce enum is not in the known enum values for TimeInForce.
*/
CREATE OR REPLACE FUNCTION dydx_from_protocol_time_in_force(tif jsonb) RETURNS text AS $$
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
BEGIN
CASE tif
WHEN '-1'::jsonb THEN RETURN 'GTT'; /** Default behavior with UNRECOGNIZED = GTT (Good-Til-Time) */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
CREATE OR REPLACE FUNCTION dydx_from_serializable_int(serializable_int jsonb) RETURNS numeric AS $$
/**
Converts a JSON.stringify byte array representing a SerializableInt
(https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/protocol/dtypes/serializable_int.go#L84) to a numeric.
Note that the underlying SerializableInt encoding format uses the big.Int GobEncoding
(https://github.com/golang/go/blob/886fba5/src/math/big/intmarsh.go#L18)
which is represented as [versionAndSignByte bigEndianByte0 bigEndianByte1 ... bigEndianByte2]
byte array.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_from_serializable_int(serializable_int jsonb) RETURNS numeric AS $$
DECLARE
rval numeric = 0;
version_and_sign int;
Expand Down
6 changes: 4 additions & 2 deletions indexer/services/ender/src/scripts/dydx_funding_handler.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE OR REPLACE FUNCTION dydx_funding_handler(
block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int) RETURNS jsonb AS $$
/**
Parameters:
- block_height: the height of the block being processing.
Expand All @@ -10,9 +12,9 @@
Returns: JSON object containing fields:
- perpetual_markets: A mapping from perpetual market id to the associated perpetual market in perpetual-market-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-market-model.ts).
- errors: An array containing an error string (or NULL if no error occurred) for each FundingEventUpdate.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_funding_handler(
block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int) RETURNS jsonb AS $$
DECLARE
PPM_EXPONENT constant numeric = -6;
FUNDING_RATE_FROM_PROTOCOL_IN_HOURS constant numeric = 8;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
CREATE OR REPLACE FUNCTION dydx_get_fee(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
/**
Returns the fee given the liquidity side.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_get_fee(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
BEGIN
IF fill_liquidity = 'TAKER' THEN
RETURN dydx_from_jsonlib_long(event_data->'takerFee');
Expand Down
6 changes: 4 additions & 2 deletions indexer/services/ender/src/scripts/dydx_get_order_status.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE OR REPLACE FUNCTION dydx_get_order_status(total_filled numeric, size numeric, order_canceled_status text, order_flags bigint, time_in_force text)
RETURNS text AS $$
/**
Computes the order status given a set of order parameters.
Expand All @@ -21,9 +23,9 @@
- order_canceled_status - The status of the order.
- order_flags - The flags of the order.
Returns the order status.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_get_order_status(total_filled numeric, size numeric, order_canceled_status text, order_flags bigint, time_in_force text)
RETURNS text AS $$
BEGIN
IF total_filled >= size THEN
RETURN 'FILLED';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE OR REPLACE FUNCTION dydx_get_perpetual_market_for_clob_pair(
clob_pair_id bigint
) RETURNS perpetual_markets AS $$
/**
Returns the perpetual market record for the provided clob pair.
Parameters:
- clob_pair_id: The clob pair id.
Returns: the only perpetual market for the clob pair. Throws an exception if not exactly one row is found.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_get_perpetual_market_for_clob_pair(
clob_pair_id bigint
) RETURNS perpetual_markets AS $$
DECLARE
perpetual_market_record perpetual_markets%ROWTYPE;
BEGIN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
CREATE OR REPLACE FUNCTION dydx_get_total_filled(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
/**
Returns the order total filled amount given the liquidity side.
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_get_total_filled(fill_liquidity text, event_data jsonb) RETURNS numeric AS $$
BEGIN
IF fill_liquidity = 'TAKER' THEN
RETURN dydx_from_jsonlib_long(event_data->'totalFilledTaker');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CREATE OR REPLACE FUNCTION dydx_get_weighted_average(first_price numeric, first_weight numeric, second_price numeric, second_weight numeric) RETURNS numeric AS $$
/**
Returns the weighted average between two prices.
Expand All @@ -9,8 +10,9 @@
- first_weight: The weight of the first price.
- second_price: The second price. Defaults to 0 if null.
- second_weight: The weight of the second price.
*/
CREATE OR REPLACE FUNCTION dydx_get_weighted_average(first_price numeric, first_weight numeric, second_price numeric, second_weight numeric) RETURNS numeric AS $$
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
BEGIN
RETURN dydx_trim_scale((coalesce(first_price, 0::numeric) * first_weight +
coalesce(second_price, 0::numeric) * second_weight)::numeric(256, 20)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
/**
Parameters:
- field: the field storing the order to process.
- block_height: the height of the block being processing.
- block_time: the time of the block being processed.
- event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/indexer_manager/event.proto#L25)
converted to JSON format. Conversion to JSON is expected to be done by JSON.stringify.
- event_index: The 'event_index' of the IndexerTendermintEvent.
- transaction_index: The transaction_index of the IndexerTendermintEvent after the conversion that takes into
account the block_event (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/services/ender/src/lib/helper.ts#L41)
- transaction_hash: The transaction hash corresponding to this event from the IndexerTendermintBlock 'tx_hashes'.
- fill_liquidity: The liquidity for the fill record.
- fill_type: The type for the fill record.
- usdc_asset_id: The USDC asset id.
Returns: JSON object containing fields:
- order: The updated order in order-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/order-model.ts).
Only returned if field == 'makerOrder'.
- fill: The updated fill in fill-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/fill-model.ts).
- perpetual_market: The perpetual market for the order in perpetual-market-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-market-model.ts).
- perpetual_position: The updated perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-position-model.ts).
*/
CREATE OR REPLACE FUNCTION dydx_liquidation_fill_handler_per_order(
field text, block_height int, block_time timestamp, event_data jsonb, event_index int, transaction_index int,
transaction_hash text, fill_liquidity text, fill_type text, usdc_asset_id text) RETURNS jsonb AS $$
Expand All @@ -42,6 +21,29 @@ DECLARE
total_filled numeric;
maker_price numeric;
event_id bytea;
/**
Parameters:
- field: the field storing the order to process.
- block_height: the height of the block being processing.
- block_time: the time of the block being processed.
- event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/indexer_manager/event.proto#L25)
converted to JSON format. Conversion to JSON is expected to be done by JSON.stringify.
- event_index: The 'event_index' of the IndexerTendermintEvent.
- transaction_index: The transaction_index of the IndexerTendermintEvent after the conversion that takes into
account the block_event (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/services/ender/src/lib/helper.ts#L41)
- transaction_hash: The transaction hash corresponding to this event from the IndexerTendermintBlock 'tx_hashes'.
- fill_liquidity: The liquidity for the fill record.
- fill_type: The type for the fill record.
- usdc_asset_id: The USDC asset id.
Returns: JSON object containing fields:
- order: The updated order in order-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/order-model.ts).
Only returned if field == 'makerOrder'.
- fill: The updated fill in fill-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/fill-model.ts).
- perpetual_market: The perpetual market for the order in perpetual-market-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-market-model.ts).
- perpetual_position: The updated perpetual position in perpetual-position-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/perpetual-position-model.ts).
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
BEGIN
order_ = event_data->field;
maker_order = event_data->'makerOrder';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE OR REPLACE FUNCTION dydx_liquidity_tier_handler(event_data jsonb) RETURNS jsonb AS $$
/**
Parameters:
- event_data: The 'data' field of the IndexerTendermintEvent (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/proto/dydxprotocol/indexer/indexer_manager/event.proto#L25)
converted to JSON format. Conversion to JSON is expected to be done by JSON.stringify.
Returns: JSON object containing fields:
- liquidy_tier: The upserted liquidity tier in liquidity-tiers-model format (https://github.com/dydxprotocol/v4-chain/blob/9ed26bd/indexer/packages/postgres/src/models/liquidity-tiers-model.ts).
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
CREATE OR REPLACE FUNCTION dydx_liquidity_tier_handler(event_data jsonb) RETURNS jsonb AS $$
DECLARE
liquidity_tier_record liquidity_tiers%ROWTYPE;
BEGIN
Expand Down
Loading

0 comments on commit 87e6119

Please sign in to comment.