Skip to content

Commit

Permalink
refactor: streamline searching through event history to improve startup
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Jul 14, 2023
1 parent 8326ce4 commit 1b8dd84
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
40 changes: 38 additions & 2 deletions apepay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ def contract(self) -> ContractInstance:
except Exception:
return self.project_manager.StreamManager.at(self.address)

def _get_contract_receipt(self, start_block=0, stop_block=None) -> ReceiptAPI:
if stop_block is None:
stop_block = self.chain_manager.blocks.head.number

mid_block = (stop_block - start_block) // 2 + start_block
# NOTE: biased towards mid_block == start_block

if start_block == mid_block:
for tx in self.chain_manager.blocks[mid_block].transactions:
if tx.receipt.contract_address == self.address:
return self.chain_manager.get_receipt(tx.receipt.txn_hash)

raise # cannot find receipt

# HACK: until https://github.com/ApeWorX/ape/issues/1540
# elif self.provider.get_code(self.address, block_id=mid_block)
elif self.provider.web3.eth.get_code(self.address, block_identifier=mid_block):
return self._get_contract_receipt(
start_block=start_block, stop_block=mid_block
)

else:
return self._get_contract_receipt(
start_block=mid_block + 1, stop_block=stop_block
)

@cached_property
def creation_block(self) -> int:
# TODO: Get this into Ape core instead
if not self.contract.receipt:
self.contract.receipt = self._get_contract_receipt()

return self.contract.receipt.block_number

def __repr__(self) -> str:
return f"<apepay_sdk.StreamManager address={self.address}>"

Expand Down Expand Up @@ -233,7 +267,9 @@ def streams_by_creator(self, creator: AddressType) -> Iterator["Stream"]:
yield Stream(self, creator, stream_id)

def all_streams(self) -> Iterator["Stream"]:
for stream_created_event in self.contract.StreamCreated:
for stream_created_event in self.contract.StreamCreated.range(
self.creation_block, self.chain_manager.blocks.head.number
):
yield Stream.from_event(
manager=self,
event=stream_created_event,
Expand Down Expand Up @@ -333,7 +369,7 @@ def reason(self) -> Union[HexBytes, str, dict]:
try:
return json.loads(reason_str)

except Exception:
except (Exception, json.JSONDecodeError):
return reason_str

@property
Expand Down
3 changes: 2 additions & 1 deletion apepay/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ async def app_started(state):
# Start watching all active streams and claim any completed but unclaimed streams
*(
create_task_by_status(stream)
for stream in itertools.chain(SM.active_streams(), SM.unclaimed_streams())
for stream in SM.all_streams()
if stream.is_active or stream.amount_unlocked > 0
)
)

Expand Down

0 comments on commit 1b8dd84

Please sign in to comment.