From 11c57daa6801a1a9ea66fba876e43507d55a0a83 Mon Sep 17 00:00:00 2001 From: pharr117 Date: Tue, 29 Aug 2023 22:19:00 -0400 Subject: [PATCH 1/2] Add tools folder, add tool to validate epoch rewards are indexing correctly by counting epoch reward events per epoch height --- .../osmosis-rewards-validation/.env.template | 5 + tools/osmosis-rewards-validation/.gitignore | 3 + tools/osmosis-rewards-validation/main.py | 92 +++++++++++++++++++ .../requirements.txt | 1 + 4 files changed, 101 insertions(+) create mode 100644 tools/osmosis-rewards-validation/.env.template create mode 100644 tools/osmosis-rewards-validation/.gitignore create mode 100644 tools/osmosis-rewards-validation/main.py create mode 100644 tools/osmosis-rewards-validation/requirements.txt diff --git a/tools/osmosis-rewards-validation/.env.template b/tools/osmosis-rewards-validation/.env.template new file mode 100644 index 0000000..20e71a3 --- /dev/null +++ b/tools/osmosis-rewards-validation/.env.template @@ -0,0 +1,5 @@ +export DB_HOST="" +export DB_PORT="" +export DB_NAME="" +export DB_USER="" +export DB_PASSWORD="" \ No newline at end of file diff --git a/tools/osmosis-rewards-validation/.gitignore b/tools/osmosis-rewards-validation/.gitignore new file mode 100644 index 0000000..b265e99 --- /dev/null +++ b/tools/osmosis-rewards-validation/.gitignore @@ -0,0 +1,3 @@ +.env +venv +output/* \ No newline at end of file diff --git a/tools/osmosis-rewards-validation/main.py b/tools/osmosis-rewards-validation/main.py new file mode 100644 index 0000000..8fe818f --- /dev/null +++ b/tools/osmosis-rewards-validation/main.py @@ -0,0 +1,92 @@ +import psycopg2 +import os +import json +import traceback + +def get_env(): + ret = { + "host": os.environ.get("DB_HOST", ""), + "password": os.environ.get("DB_PASSWORD", ""), + "user": os.environ.get("DB_USER", ""), + "port": os.environ.get("DB_PORT", ""), + "db_name": os.environ.get("DB_NAME", "") + } + + if any([ret[x] == "" for x in ret]): + raise Exception("Must provide env vars") + + return ret + +EPOCHS_DAY_ALL_QUERY = "SELECT epoch_number, start_height FROM epoches WHERE identifier='day' ORDER BY start_height ASC;" +BLOCKS_BY_HEIGHT_QUERY = "SELECT id, height FROM blocks WHERE height=%s;" +EVENTS_BY_HEIGHT_AND_SOURCE_QUERY = "SELECT COUNT(*) FROM taxable_event WHERE block_id=%s AND source=0;" + +# An naive/unoptimized implementation to find counts for epoch reward events +# Does the following: +# 1. Finds all epochs +# 2. Finds the block ID for each epoch height +# 3. Counts the reward for each block id +# 4. Reassociates the block rewards back with its epoch +# 5. Outputs the counts for each epoch +if __name__ == "__main__": + env = get_env() + + conn = psycopg2.connect(f"dbname={env['db_name']} user={env['user']} host={env['host']} password={env['password']} port={env['port']}") + try: + epochs = [] + with conn.cursor() as cur: + cur.execute(EPOCHS_DAY_ALL_QUERY) + for record in cur.fetchall(): + epochs.append({ + "epoch_number": record[0], + "start_height": record[1] + }) + + json.dump(epochs, open("output/epochs_tracked.json", 'w'), indent=4) + blocks = [] + heights = [int(epoch["start_height"]) for epoch in epochs] + with conn.cursor() as cur: + for height in heights: + cur.execute(BLOCKS_BY_HEIGHT_QUERY, (height,)) + rec = cur.fetchone() + if rec is None: + print(height) + print(BLOCKS_BY_HEIGHT_QUERY % height) + continue + blocks.append({ + "id": rec[0], + "height": rec[1] + }) + + json.dump(blocks, open("output/blocks_tracked.json", 'w'), indent=4) + events_by_block_height_counts = {} + for block in blocks: + with conn.cursor() as cur: + cur.execute(EVENTS_BY_HEIGHT_AND_SOURCE_QUERY, (block["id"],)) + rec = cur.fetchone() + events_by_block_height_counts[block["height"]] = rec[0] + json.dump(events_by_block_height_counts, open("output/events_by_block_id_tracked.json", 'w'), indent=4) + + epoch_number_counts = [] + available_heights = events_by_block_height_counts.keys() + + for epoch in epochs: + if epoch["start_height"] in available_heights: + epoch_number_counts.append({ + "epoch_number": epoch["epoch_number"], + "count": events_by_block_height_counts[epoch["start_height"]] + }) + else: + epoch_number_counts.append({ + "epoch_number": epoch["epoch_number"], + "count": 0 + }) + + json.dump(epoch_number_counts, open("output/epoch_counts.json", 'w'), indent=4) + + + except Exception as err: + print(err) + traceback.print_exc(err) + finally: + conn.close() diff --git a/tools/osmosis-rewards-validation/requirements.txt b/tools/osmosis-rewards-validation/requirements.txt new file mode 100644 index 0000000..9edde47 --- /dev/null +++ b/tools/osmosis-rewards-validation/requirements.txt @@ -0,0 +1 @@ +psycopg2==2.9.7 From c5b9f9fe04f1426c1e9c4ee2c020d5451cce9a01 Mon Sep 17 00:00:00 2001 From: pharr117 Date: Tue, 29 Aug 2023 22:22:37 -0400 Subject: [PATCH 2/2] Precommit --- tools/osmosis-rewards-validation/.env.template | 2 +- tools/osmosis-rewards-validation/.gitignore | 2 +- tools/osmosis-rewards-validation/main.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/osmosis-rewards-validation/.env.template b/tools/osmosis-rewards-validation/.env.template index 20e71a3..958ef16 100644 --- a/tools/osmosis-rewards-validation/.env.template +++ b/tools/osmosis-rewards-validation/.env.template @@ -2,4 +2,4 @@ export DB_HOST="" export DB_PORT="" export DB_NAME="" export DB_USER="" -export DB_PASSWORD="" \ No newline at end of file +export DB_PASSWORD="" diff --git a/tools/osmosis-rewards-validation/.gitignore b/tools/osmosis-rewards-validation/.gitignore index b265e99..3f9a892 100644 --- a/tools/osmosis-rewards-validation/.gitignore +++ b/tools/osmosis-rewards-validation/.gitignore @@ -1,3 +1,3 @@ .env venv -output/* \ No newline at end of file +output/* diff --git a/tools/osmosis-rewards-validation/main.py b/tools/osmosis-rewards-validation/main.py index 8fe818f..9824911 100644 --- a/tools/osmosis-rewards-validation/main.py +++ b/tools/osmosis-rewards-validation/main.py @@ -83,7 +83,7 @@ def get_env(): }) json.dump(epoch_number_counts, open("output/epoch_counts.json", 'w'), indent=4) - + except Exception as err: print(err)