Skip to content

Commit

Permalink
add support for epochs in the cache table
Browse files Browse the repository at this point in the history
  • Loading branch information
nibty committed Sep 19, 2024
1 parent bc24e79 commit 5f6904d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
55 changes: 35 additions & 20 deletions make_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,62 @@ def recreate_cache_table():
account TEXT PRIMARY KEY,
total_blocks INTEGER,
hashes_per_second REAL,
super_blocks INTEGER
super_blocks INTEGER,
rank INTEGER DEFAULT 0,
xnm BIGINT DEFAULT 0
)""")
cache_conn.commit()

try:
cache_cursor.execute("""
ALTER TABLE cache_table ADD COLUMN rank INTEGER DEFAULT 0
""")
cache_cursor.execute("""
ALTER TABLE cache_table ADD COLUMN xnm BIGINT DEFAULT 0
""")
cache_conn.commit()
except sqlite3.OperationalError:
# Ignore error, if the column already exists.
pass

# Fetch data from the original database and populate the cache table
original_cursor.execute("""
WITH RankedBlocks AS (
SELECT
b.account,
COUNT(b.block_id) AS total_blocks,
COALESCE(sb.super_block_count, 0) AS super_blocks
FROM blocks b
LEFT JOIN super_blocks sb ON b.account = sb.account
GROUP BY 1
)
SELECT
LOWER(account) AS account,
SUM(total_blocks) AS total_blocks,
100000 AS hashes_per_second,
SUM(super_blocks) AS super_blocks,
ROW_NUMBER() OVER (ORDER BY total_blocks DESC, super_blocks DESC, account DESC) AS rank -- Use super_blocks as secondary sort
FROM RankedBlocks
GROUP BY 1
ORDER BY rank
WITH grouped_blocks AS (
SELECT
LOWER(b.account) AS account,
1 + CAST(
(strftime('%Y', b.created_at) - 2023) +
CASE
WHEN strftime('%m-%d %H:%M:%S', b.created_at) >= '09-16 21:00:00'
THEN 0 ELSE -1
END AS INTEGER
) AS epoch,
COUNT(b.block_id) AS blocks_per_epoch,
COALESCE(sb.super_block_count, 0) AS super_blocks
FROM blocks b
LEFT JOIN super_blocks sb ON b.account = sb.account
GROUP BY LOWER(b.account), epoch
)
SELECT
account,
ROW_NUMBER() OVER (ORDER BY SUM(blocks_per_epoch) DESC, SUM(super_blocks) DESC, account DESC) AS rank,
SUM(blocks_per_epoch) AS total_blocks,
SUM(super_blocks) AS total_super_blocks,
SUM(blocks_per_epoch * POWER(10, 18) / POWER(2, epoch - 1)) AS xnm,
100000 AS hashes_per_second
FROM grouped_blocks
GROUP BY account
ORDER BY rank
""")

rows = original_cursor.fetchall()
num_rows_fetched = len(rows) # Get the number of rows fetched
logging.info(f"Fetched {num_rows_fetched} rows from the original database.")

# Insert fetched rows into the cache table
cache_cursor.executemany("""
INSERT OR REPLACE INTO cache_table (account, total_blocks, hashes_per_second, super_blocks, rank) VALUES (?, ?, ?, ?, ?)
INSERT OR REPLACE INTO cache_table (account, rank, total_blocks, super_blocks, xnm, hashes_per_second) VALUES (?, ?, ?, ?, ?, ?)
""", rows)
cache_conn.commit()

Expand Down
21 changes: 19 additions & 2 deletions rpc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def handle_eth_call(data):
balance = get_xuni_account_count('0x' + address_queried) * 1000000000000000000
elif target_address == "0x999999cf1046e68e36e1aa2e0e07105eddd00001":
balance = get_xblk_account_count('0x' + address_queried) * 1000000000000000000
print ("SUPERBLOCK BALANCE for: ", address_queried, balance)
else:
# Handle unknown contract address or give a default balance
balance = 0
Expand Down Expand Up @@ -328,14 +329,30 @@ def index():
if not data:
abort(400, description="No data provided")

if data['jsonrpc'] != '2.0':
response = {'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Invalid Request'}, 'id': None}

# Check if data is a dictionary
if isinstance(data, dict):
if data.get('jsonrpc') != '2.0':
response = {'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Invalid Request'}, 'id': None}
print("Sending response:", response) # Print response to the screen
return jsonify(response), 400
else:
# data is not a dictionary, handle error
response = {'jsonrpc': '2.0', 'error': {'code': -32700, 'message': 'Parse error'}, 'id': None}
print("Sending response:", response) # Print response to the screen
return jsonify(response), 400


# Initialize the result variable
result = None

if data.get("method") == "web3_clientVersion":
return jsonify({
"id": data.get("id"),
"jsonrpc": "2.0",
"result": "XenBlocks/1.0" # Replace this with your actual client version
})

if data['method'] == 'eth_blockNumber':
current_block_number += 1 # Increment block number
result = hex(current_block_number)
Expand Down

0 comments on commit 5f6904d

Please sign in to comment.