From c4f9c6c00c1b25228d82b0e55aabf5544cdbc818 Mon Sep 17 00:00:00 2001 From: Haris Angelidakis <64154020+harisang@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:09:55 +0300 Subject: [PATCH] Remove consistency rewards calculations from sql (#404) This PR removes the sql code that computes consistency rewards, as it is not used anymore. Together with the merged PR #395, this concludes the removal of consistency rewards from the script. --- queries/orderbook/barn_batch_rewards.sql | 61 +++--------------------- queries/orderbook/prod_batch_rewards.sql | 61 +++--------------------- tests/queries/batch_rewards_test_db.sql | 41 ---------------- tests/queries/test_batch_rewards.py | 15 ------ 4 files changed, 14 insertions(+), 164 deletions(-) diff --git a/queries/orderbook/barn_batch_rewards.sql b/queries/orderbook/barn_batch_rewards.sql index f9bc96b3..a586de53 100644 --- a/queries/orderbook/barn_batch_rewards.sql +++ b/queries/orderbook/barn_batch_rewards.sql @@ -17,19 +17,6 @@ WITH observed_settlements AS ( ss.block_deadline >= {{start_block}} AND ss.block_deadline <= {{end_block}} ), -auction_participation AS ( - SELECT - ss.auction_id, - array_agg(participant) AS participating_solvers - FROM - auction_participants - JOIN settlement_scores ss ON auction_participants.auction_id = ss.auction_id - WHERE - block_deadline >= {{start_block}} - AND block_deadline <= {{end_block}} - GROUP BY - ss.auction_id -), -- order data order_data AS ( SELECT @@ -185,7 +172,7 @@ reward_data AS ( ss.auction_id, -- TODO - Assuming that `solver == winner` when both not null -- We will need to monitor that `solver == winner`! - COALESCE(os.solver, winner) AS solver, + ss.winner AS solver, block_number AS settlement_block, block_deadline, COALESCE(execution_cost, 0) AS execution_cost, @@ -198,8 +185,6 @@ reward_data AS ( ELSE 0 END AS observed_score, reference_score, - -- auction_participation - participating_solvers, -- protocol_fees COALESCE(CAST(protocol_fee AS NUMERIC(78, 0)), 0) AS protocol_fee, COALESCE( @@ -208,13 +193,13 @@ reward_data AS ( ) AS network_fee FROM settlement_scores ss - -- If there are reported scores, - -- there will always be a record of auction participants - JOIN auction_participation ap ON ss.auction_id = ap.auction_id -- outer joins made in order to capture non-existent settlements. LEFT OUTER JOIN observed_settlements os ON os.auction_id = ss.auction_id LEFT OUTER JOIN batch_protocol_fees bpf ON bpf.tx_hash = os.tx_hash LEFT OUTER JOIN batch_network_fees bnf ON bnf.tx_hash = os.tx_hash + WHERE + ss.block_deadline >= {{start_block}} + AND ss.block_deadline <= {{end_block}} ), reward_per_auction AS ( SELECT @@ -237,39 +222,10 @@ reward_per_auction AS ( {{EPSILON_UPPER}} ) AS capped_payment, winning_score, - reference_score, - participating_solvers AS participating_solvers + reference_score FROM reward_data ), -participation_data AS ( - SELECT - tx_hash, - block_deadline, - unnest(participating_solvers) AS participant - FROM - reward_per_auction -), -participation_data_intermediate AS ( - SELECT - tx_hash, - CASE - WHEN block_deadline <= 20365510 THEN 1 -- final block deadline of accounting week of July 16 - July 23, 2024 - ELSE 0 - END AS count_participation, - participant - FROM - participation_data -), -participation_counts AS ( - SELECT - participant AS solver, - sum(count_participation) AS num_participating_batches - FROM - participation_data_intermediate - GROUP BY - participant -), primary_rewards AS ( SELECT rpt.solver, @@ -301,16 +257,13 @@ aggregate_partner_fees_per_solver AS ( ), aggregate_results AS ( SELECT - CONCAT('0x', encode(pc.solver, 'hex')) AS solver, + CONCAT('0x', encode(pr.solver, 'hex')) AS solver, COALESCE(payment, 0) AS primary_reward_eth, - num_participating_batches, COALESCE(protocol_fee, 0) AS protocol_fee_eth, COALESCE(network_fee, 0) AS network_fee_eth, partner_list, partner_fee AS partner_fee_eth - FROM - participation_counts pc - LEFT OUTER JOIN primary_rewards pr ON pr.solver = pc.solver + FROM primary_rewards pr LEFT OUTER JOIN aggregate_partner_fees_per_solver aif ON pr.solver = aif.solver ) -- SELECT diff --git a/queries/orderbook/prod_batch_rewards.sql b/queries/orderbook/prod_batch_rewards.sql index f9bc96b3..a586de53 100644 --- a/queries/orderbook/prod_batch_rewards.sql +++ b/queries/orderbook/prod_batch_rewards.sql @@ -17,19 +17,6 @@ WITH observed_settlements AS ( ss.block_deadline >= {{start_block}} AND ss.block_deadline <= {{end_block}} ), -auction_participation AS ( - SELECT - ss.auction_id, - array_agg(participant) AS participating_solvers - FROM - auction_participants - JOIN settlement_scores ss ON auction_participants.auction_id = ss.auction_id - WHERE - block_deadline >= {{start_block}} - AND block_deadline <= {{end_block}} - GROUP BY - ss.auction_id -), -- order data order_data AS ( SELECT @@ -185,7 +172,7 @@ reward_data AS ( ss.auction_id, -- TODO - Assuming that `solver == winner` when both not null -- We will need to monitor that `solver == winner`! - COALESCE(os.solver, winner) AS solver, + ss.winner AS solver, block_number AS settlement_block, block_deadline, COALESCE(execution_cost, 0) AS execution_cost, @@ -198,8 +185,6 @@ reward_data AS ( ELSE 0 END AS observed_score, reference_score, - -- auction_participation - participating_solvers, -- protocol_fees COALESCE(CAST(protocol_fee AS NUMERIC(78, 0)), 0) AS protocol_fee, COALESCE( @@ -208,13 +193,13 @@ reward_data AS ( ) AS network_fee FROM settlement_scores ss - -- If there are reported scores, - -- there will always be a record of auction participants - JOIN auction_participation ap ON ss.auction_id = ap.auction_id -- outer joins made in order to capture non-existent settlements. LEFT OUTER JOIN observed_settlements os ON os.auction_id = ss.auction_id LEFT OUTER JOIN batch_protocol_fees bpf ON bpf.tx_hash = os.tx_hash LEFT OUTER JOIN batch_network_fees bnf ON bnf.tx_hash = os.tx_hash + WHERE + ss.block_deadline >= {{start_block}} + AND ss.block_deadline <= {{end_block}} ), reward_per_auction AS ( SELECT @@ -237,39 +222,10 @@ reward_per_auction AS ( {{EPSILON_UPPER}} ) AS capped_payment, winning_score, - reference_score, - participating_solvers AS participating_solvers + reference_score FROM reward_data ), -participation_data AS ( - SELECT - tx_hash, - block_deadline, - unnest(participating_solvers) AS participant - FROM - reward_per_auction -), -participation_data_intermediate AS ( - SELECT - tx_hash, - CASE - WHEN block_deadline <= 20365510 THEN 1 -- final block deadline of accounting week of July 16 - July 23, 2024 - ELSE 0 - END AS count_participation, - participant - FROM - participation_data -), -participation_counts AS ( - SELECT - participant AS solver, - sum(count_participation) AS num_participating_batches - FROM - participation_data_intermediate - GROUP BY - participant -), primary_rewards AS ( SELECT rpt.solver, @@ -301,16 +257,13 @@ aggregate_partner_fees_per_solver AS ( ), aggregate_results AS ( SELECT - CONCAT('0x', encode(pc.solver, 'hex')) AS solver, + CONCAT('0x', encode(pr.solver, 'hex')) AS solver, COALESCE(payment, 0) AS primary_reward_eth, - num_participating_batches, COALESCE(protocol_fee, 0) AS protocol_fee_eth, COALESCE(network_fee, 0) AS network_fee_eth, partner_list, partner_fee AS partner_fee_eth - FROM - participation_counts pc - LEFT OUTER JOIN primary_rewards pr ON pr.solver = pc.solver + FROM primary_rewards pr LEFT OUTER JOIN aggregate_partner_fees_per_solver aif ON pr.solver = aif.solver ) -- SELECT diff --git a/tests/queries/batch_rewards_test_db.sql b/tests/queries/batch_rewards_test_db.sql index 05e81ff3..6cd9168e 100644 --- a/tests/queries/batch_rewards_test_db.sql +++ b/tests/queries/batch_rewards_test_db.sql @@ -1,5 +1,4 @@ DROP TABLE IF EXISTS settlements; -DROP TABLE IF EXISTS auction_participants; DROP TABLE IF EXISTS settlement_scores; DROP TABLE IF EXISTS settlement_observations; DROP TABLE IF EXISTS auction_prices; @@ -27,12 +26,6 @@ CREATE TABLE IF NOT EXISTS settlements CREATE INDEX settlements_tx_from_tx_nonce ON settlements (tx_from, tx_nonce); -CREATE TABLE IF NOT EXISTS auction_participants -( - auction_id bigint, - participant bytea -); - CREATE TABLE IF NOT EXISTS settlement_scores ( auction_id bigint PRIMARY KEY, @@ -136,7 +129,6 @@ CREATE TABLE app_data ( TRUNCATE settlements; -TRUNCATE auction_participants; TRUNCATE settlement_scores; TRUNCATE settlement_observations; TRUNCATE auction_prices; @@ -166,39 +158,6 @@ VALUES (1, 10, '\x5111111111111111111111111111111111111111'::bytea, '\x7111'::by (59, 10, '\x03'::bytea, '\x09'::bytea, '\x03'::bytea, 2, 59), (60, 10, '\x03'::bytea, '\x0a'::bytea, '\x03'::bytea, 3, 60); -INSERT INTO auction_participants (auction_id, participant) -VALUES (1, '\x5222222222222222222222222222222222222222'::bytea), - (1, '\x5333333333333333333333333333333333333333'::bytea), - (1, '\x5111111111111111111111111111111111111111'::bytea), - (2, '\x5444444444444444444444444444444444444444'::bytea), - (2, '\x5333333333333333333333333333333333333333'::bytea), - (2, '\x5222222222222222222222222222222222222222'::bytea), - (3, '\x5444444444444444444444444444444444444444'::bytea), - (3, '\x5333333333333333333333333333333333333333'::bytea), - (3, '\x5111111111111111111111111111111111111111'::bytea), - (5, '\x5444444444444444444444444444444444444444'::bytea), - (5, '\x5333333333333333333333333333333333333333'::bytea), - (5, '\x5111111111111111111111111111111111111111'::bytea), - (6, '\x5444444444444444444444444444444444444444'::bytea), - (6, '\x5333333333333333333333333333333333333333'::bytea), - (6, '\x5111111111111111111111111111111111111111'::bytea), - (7, '\x5111111111111111111111111111111111111111'::bytea), - (8, '\x5111111111111111111111111111111111111111'::bytea), - (9, '\x5444444444444444444444444444444444444444'::bytea), - (9, '\x5333333333333333333333333333333333333333'::bytea), - (9, '\x5111111111111111111111111111111111111111'::bytea), - (10, '\x5444444444444444444444444444444444444444'::bytea), - (10, '\x5333333333333333333333333333333333333333'::bytea), - (51, '\x01'::bytea), - (52, '\x02'::bytea), - (53, '\x01'::bytea), - (54, '\x02'::bytea), - (55, '\x01'::bytea), - (56, '\x02'::bytea), - (57, '\x03'::bytea), - (58, '\x03'::bytea), - (59, '\x03'::bytea), - (60, '\x03'::bytea); INSERT INTO settlement_scores (auction_id, winning_score, reference_score, winner, block_deadline, simulation_block) VALUES (1, 6000000000000000000, 4000000000000000000, '\x5111111111111111111111111111111111111111'::bytea, 10, 0), diff --git a/tests/queries/test_batch_rewards.py b/tests/queries/test_batch_rewards.py index 678ad84c..19d1f9ed 100644 --- a/tests/queries/test_batch_rewards.py +++ b/tests/queries/test_batch_rewards.py @@ -27,7 +27,6 @@ def test_get_batch_rewards(self): "0x5111111111111111111111111111111111111111", "0x5222222222222222222222222222222222222222", "0x5333333333333333333333333333333333333333", - "0x5444444444444444444444444444444444444444", ], "primary_reward_eth": [ 2000000000000000.0, @@ -36,16 +35,6 @@ def test_get_batch_rewards(self): 28000000000000000.0, 12000000000000000.0, -10000000000000000.0, - 0.0, - ], - "num_participating_batches": [ - 3, - 3, - 4, - 7, - 2, - 7, - 6, ], "protocol_fee_eth": [ 1250000000000000.0, # 0.5 / (1 - 0.5) * 1e18 * 5e14 / 1e18 + 0.0045 / (1 - 0.0045) * 95e18 * 5e14 / 1e18 @@ -54,7 +43,6 @@ def test_get_batch_rewards(self): 0.0, 0.0, 0.0, - 0.0, ], "network_fee_eth": [ 4792548202188630.0, # around 2 * 6_000_000 * 5e26 / 1e18 - 1250000000000000.0 @@ -63,7 +51,6 @@ def test_get_batch_rewards(self): 0.0, # zero due to missing surplus fee data 0.0, 0.0, - 0.0, ], "partner_list": [ ["0x90a48d5cf7343b08da12e067680b4c6dbfe551be"], @@ -72,7 +59,6 @@ def test_get_batch_rewards(self): None, None, None, - None, ], "partner_fee_eth": [ [250000000000000.0], @@ -81,7 +67,6 @@ def test_get_batch_rewards(self): None, None, None, - None, ], } )