Skip to content

Commit

Permalink
Merge pull request #323 from lidofinance/chore/batches-progressbar
Browse files Browse the repository at this point in the history
Slight improvement in visibility and fetching events with batches
  • Loading branch information
TheDZhon authored Nov 29, 2024
2 parents ff929d4 + cd757f0 commit 0aa3f1a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions tests/regression/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
import os

from tqdm import tqdm
from web3 import Web3
from brownie import interface, convert, web3
from brownie.network.event import _decode_logs
Expand Down Expand Up @@ -489,19 +490,30 @@ def active_aragon_roles(protocol_permissions):
w3 = Web3(Web3.HTTPProvider(f'https://mainnet.infura.io/v3/{os.getenv("WEB3_INFURA_PROJECT_ID")}'))

event_signature_hash = w3.keccak(text="SetPermission(address,address,bytes32,bool)").hex()
events_before_voting = w3.eth.filter(
{"address": contracts.acl.address, "fromBlock": ACL_DEPLOY_BLOCK_NUMBER, "topics": [event_signature_hash]}
).get_all_entries()

def fetch_events_in_batches(start_block, end_block, step=100000):
"""Fetch events in batches of `step` blocks with a progress bar."""
events = []
total_batches = (end_block - start_block) // step + 1
with tqdm(total=total_batches, desc="Fetching Events") as pbar:
for batch_start in range(start_block, end_block, step):
batch_end = min(batch_start + step - 1, end_block)
batch_events = w3.eth.filter(
{"address": contracts.acl.address, "fromBlock": batch_start, "toBlock": batch_end, "topics": [event_signature_hash]}
).get_all_entries()
events.extend(batch_events)
pbar.update(1)
return events

events_before_voting = fetch_events_in_batches(ACL_DEPLOY_BLOCK_NUMBER, w3.eth.block_number)

permission_events = _decode_logs(events_before_voting)["SetPermission"]._ordered

history = TxHistory()
if len(history) > 0:
vote_block = history[0].block_number

events_after_voting = web3.eth.filter(
{"address": contracts.acl.address, "fromBlock": vote_block, "topics": [event_signature_hash]}
).get_all_entries()
events_after_voting = fetch_events_in_batches(vote_block, w3.eth.block_number)

try:
permission_events_after_voting = _decode_logs(events_after_voting)["SetPermission"]._ordered
Expand Down

0 comments on commit 0aa3f1a

Please sign in to comment.