Skip to content

Commit

Permalink
Chunkify withdrawals records
Browse files Browse the repository at this point in the history
Signed-off-by: cyc60 <avsysoev60@gmail.com>
  • Loading branch information
cyc60 committed Aug 23, 2023
1 parent 4f48ba5 commit 3a85ba0
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions oracle/oracle/rewards/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,33 @@ async def calculate_withdrawal_rewards(
)
execution_client = get_web3_client()

chunk_size = 50000
for block_number in range(0, to_block - from_block, chunk_size):
withdrawals_amount += self.fetch_withdrawal_chunk(
validator_indexes=validator_indexes,
from_block=block_number,
to_block=min(block_number + chunk_size, to_block),
execution_client=execution_client,
)

withdrawals_amount = Web3.toWei(withdrawals_amount, "gwei")
if NETWORK == GNOSIS_CHAIN:
# apply mGNO <-> GNO exchange rate
withdrawals_amount = Wei(int(withdrawals_amount * WAD // MGNO_RATE))
return withdrawals_amount

async def fetch_withdrawal_chunk(
self,
validator_indexes: set[int],
from_block: int,
to_block: int,
execution_client,
) -> int:
logger.info(
f"Retrieving pool validator withdrawals chunk "
f"from block: {from_block} to block: {to_block}"
)
withdrawals_amount = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
futures = [
executor.submit(get_withdrawals, execution_client, block_number)
Expand All @@ -231,11 +258,6 @@ async def calculate_withdrawal_rewards(
for withdrawal in withdrawals:
if withdrawal["validator_index"] in validator_indexes:
withdrawals_amount += withdrawal["amount"]

withdrawals_amount = Web3.toWei(withdrawals_amount, "gwei")
if NETWORK == GNOSIS_CHAIN:
# apply mGNO <-> GNO exchange rate
withdrawals_amount = Wei(int(withdrawals_amount * WAD // MGNO_RATE))
return withdrawals_amount

async def get_withdrawals_from_block(self, current_slot: int) -> BlockNumber | None:
Expand Down

0 comments on commit 3a85ba0

Please sign in to comment.