You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR modifies the account size retrieval logic to use real-time timestamps instead of start-of-day calculations, and adds a validation check to prevent withdrawals when miners have open positions. The changes affect both the core business logic and related tests.
✅ Strengths
Improved timestamp precision: The change from day-based to timestamp-based lookups (line 101 in miner_account_manager.py) provides more granular control and better reflects real-time account states
Critical business rule enforcement: Added validation preventing withdrawals with open positions (lines 486-490 in validator_contract_manager.py) protects against potential inconsistent states
Test alignment: Tests were updated to match the new behavior, removing unnecessary day-offset calculations
Cleaner code: Removed unnecessary date manipulation logic, making the code simpler and easier to reason about
⚠️ Concerns
Critical: Missing Migration Strategy
The change in get_account_size() logic fundamentally alters how historical records are interpreted:
Old logic: Matched records using valid_date_timestamp (start of day)
New logic: Matches using update_time_ms (exact timestamp)
Problem: Existing persisted CollateralRecord objects may have update_time_ms values that don't align with the new lookup expectations. This could cause:
Historical account sizes to be retrieved incorrectly
Edge cases where no matching record is found when one should exist
Financial calculation inconsistencies for existing miners
Recommendation: Need a data migration script or backward-compatibility check.
Potential Race Condition (Minor)
In validator_contract_manager.py lines 486-490, the check for open positions and subsequent withdrawal logic are not atomic. Between checking positions and processing withdrawal, new positions could theoretically be opened (depending on your concurrency model).
Recommendation: Consider if this needs transaction-level protection or locking.
Logging Inconsistency
Lines 460 and 548 in miner_account_manager.py now log update_time_ms (a numeric timestamp) instead of valid_date_str (human-readable). This degrades log readability.
# Before: "Updated account size for miner123: $10,000.00 (valid from 2024-01-15)"# After: "Updated account size for miner123: $10,000.00 (valid from 1705276800000)"
💡 Suggestions
1. Restore Human-Readable Logging
fromdatetimeimportdatetime, timezone# In logging statements (lines 460, 548):readable_time=datetime.fromtimestamp(collateral_record.update_time_ms/1000, tz=timezone.utc).isoformat()
bt.logging.info(
f"Updated account size for {hotkey}: ${account_size:,.2f} (valid from {readable_time})")
2. Add Defensive Check for Edge Cases
In get_account_size() method:
# After line 103ifrecord.update_time_ms<=timestamp_ms:
# Add validationifrecord.account_size_theta<0:
bt.logging.warning(f"Invalid negative account size for {self.hotkey}, skipping record")
continuetheta=min(record.account_size_theta, ValiConfig.MAX_COLLATERAL_BALANCE_THETA)
returnmax(theta*ValiConfig.COST_PER_THETA, ValiConfig.MIN_CAPITAL)
3. Enhanced Test Coverage
The current tests don't cover some important scenarios:
deftest_account_size_with_future_timestamp(self):
"""Test querying account size with timestamp before any records exist"""current_time=int(time.time() *1000)
very_old_time=current_time- (365*self.DAY_MS) # 1 year agoself.miner_account_client.set_miner_account_size(self.MINER_1, 0.001, current_time)
# Should return None or MIN_CAPITAL for timestamps before first recordsize=self.miner_account_client.get_miner_account_size(self.MINER_1, very_old_time)
# Add assertion based on expected behaviordeftest_withdrawal_blocked_with_open_positions(self):
"""Test that withdrawal fails when positions are open"""# Create open position for miner# Attempt withdrawal# Assert failure with correct error message
4. Documentation Update Needed
The behavioral change in timestamp handling should be documented:
Update docstring for get_account_size() method to clarify it now uses exact timestamp matching
Document the withdrawal restriction in relevant API/user documentation
Add inline comment explaining the timestamp comparison logic
5. Consider Adding Index/Caching
If collateral_records grows large, the reversed iteration (line 101) could become inefficient. Consider:
# Add to MinerAccount classdef_build_timestamp_index(self):
"""Build sorted index for faster lookups"""self._timestamp_index=sorted(
[(r.update_time_ms, i) fori, rinenumerate(self.collateral_records)]
)
defget_account_size(self, timestamp_ms=None):
# Use binary search on _timestamp_indeximportbisect# ... implementation
🔒 Security Notes
Medium: Withdrawal Validation Bypass Risk
The new position check (line 486) is good, but consider:
Timing attack: Could a miner close positions, trigger withdrawal, then immediately reopen before withdrawal completes?
Partial state: What happens if withdrawal succeeds but position closure verification fails?
Recommendation: Implement withdrawal as a two-phase commit or use optimistic locking with version checks.
Low: Integer Overflow in Timestamp Calculations
Using int(time.time() * 1000) for millisecond timestamps is generally safe in Python 3, but be aware:
Ensure all timestamp comparisons use consistent units (milliseconds throughout)
Consider adding validation: assert 0 < timestamp_ms < 2**53 (JavaScript MAX_SAFE_INTEGER if this interops with JS)
Low: Missing Input Validation
The withdrawal check doesn't validate miner_hotkey format. Consider adding:
⚠️ Clarify migration strategy for existing CollateralRecord data
⚠️ Fix logging to display human-readable timestamps
⚠️ Add test case for withdrawal blocking with open positions
Should Consider:
4. Document the behavioral change in method docstrings
5. Add edge case tests (timestamps before first record, etc.)
6. Review concurrency implications of the withdrawal check
Nice to Have:
7. Performance optimization for large collateral_records lists
8. Enhanced input validation
Verdict
CONDITIONAL APPROVE - The core logic improvement is sound, but needs clarification on data migration and logging fixes before merge. The withdrawal validation is a valuable addition. Address the "Must Address" items, and this will be good to go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Taoshi Pull Request
Description
Use account equity to calculate portfolio returns and drawdown.
Related Issues (JIRA)
[Reference any related issues or tasks that this pull request addresses or closes.]
Checklist
Reviewer Instructions
[Provide any specific instructions or areas you would like the reviewer to focus on.]
Definition of Done
Checklist (for the reviewer)
Optional: Deploy Notes
[Any instructions or notes related to deployment, if applicable.]
/cc @mention_reviewer