generated from StabilityNexus/Template-Repo
-
-
Notifications
You must be signed in to change notification settings - Fork 14
[OPTIMIZE] Implement lazy hashing cache for Transaction IDs #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sanaica
wants to merge
16
commits into
StabilityNexus:main
Choose a base branch
from
sanaica:optimize-tx-hashing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6faa9de
optimize: implement lazy hashing cache for Transaction IDs
sanaica 8912e16
test: refactor diagnostic prints to assertions and fix linting
sanaica 43507e7
refactor: implement guarded __setattr__ for robust cache invalidation
sanaica 7c7d51c
refactor: use guarded __setattr__ for automated cache invalidation
sanaica 07f11c6
fix: frozenset for _TX_FIELDS and is not None timestamp check
sanaica 76e691c
chore: trigger CodeRabbit re-review
sanaica 655d49d
fix: use try/except/else in verify() for TRY300
sanaica cf4a0ba
chore: trigger CodeRabbit re-review
sanaica 7d3dc7f
fix: normalize timestamps and seal tx fields after signing
sanaica b18d6af
feat: add state sealing and robust cache invalidation
sanaica 8988a33
feat: add mock-based verification for hashing efficiency
sanaica 19fd40a
test: implement mock-based call counting to verify hashing efficiency
sanaica 9b20f49
test: implement mock-based call counting to verify hashing efficiency
sanaica 5a5d751
chore: trigger CodeRabbit re-review
sanaica 3e93e01
test: add parameterized loop to verify comprehensive cache invalidation
sanaica 445fcf5
chore: trigger CodeRabbit re-review
sanaica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import pytest | ||
| from unittest.mock import patch | ||
| from minichain.transaction import Transaction | ||
| from nacl.signing import SigningKey | ||
| from nacl.encoding import HexEncoder | ||
|
|
||
| def test_tx_caching_efficiency(): | ||
| """ | ||
| Verifies that the expensive hashing math is only performed once | ||
| and skipped on subsequent accesses (Memoization proof). | ||
| """ | ||
| sk = SigningKey.generate() | ||
| sender_hex = sk.verify_key.encode(encoder=HexEncoder).decode() | ||
| tx = Transaction(sender=sender_hex, receiver="addr", amount=100, nonce=1) | ||
|
|
||
| # We 'patch' the hashing function to count how many times it's called | ||
| with patch('minichain.transaction.canonical_json_hash') as mock_hash: | ||
| mock_hash.return_value = "mocked_hash_value" | ||
|
|
||
| # 1. First Access: Should trigger the hash calculation | ||
| res1 = tx.tx_id | ||
| assert res1 == "mocked_hash_value" | ||
| assert mock_hash.call_count == 1 | ||
|
|
||
| # 2. Second Access: Should return the cached value (count remains 1) | ||
| res2 = tx.tx_id | ||
| assert res2 == "mocked_hash_value" | ||
| assert mock_hash.call_count == 1 # <--- THIS proves the cache worked! | ||
|
|
||
| # 3. Comprehensive Invalidation: Changing ANY field must clear the cache | ||
| mutations = { | ||
| "sender": "new_sender_hex", | ||
| "receiver": "new_receiver", | ||
| "amount": 200, | ||
| "nonce": 2, | ||
| "data": "new_data", | ||
| "timestamp": 1234567890, | ||
| "signature": "fake_signature_hex" | ||
| } | ||
|
|
||
| expected_calls = 1 | ||
| for field, new_value in mutations.items(): | ||
| # Mutate the field dynamically | ||
| setattr(tx, field, new_value) | ||
|
|
||
| # Prove the cache was instantly killed | ||
| assert tx._cached_tx_id is None, f"Cache failed to clear when mutating {field}" | ||
|
|
||
| # Access ID again, which forces a re-calculation | ||
| _ = tx.tx_id | ||
|
|
||
| # Prove the hashing math ran exactly one more time | ||
| expected_calls += 1 | ||
| assert mock_hash.call_count == expected_calls, f"Hash did not recalculate for {field}" | ||
|
|
||
| def test_signed_tx_is_sealed(): | ||
| """Verifies that a signed transaction clears cache, changes ID, and cannot be modified.""" | ||
| sk = SigningKey.generate() | ||
| sender_hex = sk.verify_key.encode(encoder=HexEncoder).decode() | ||
| tx = Transaction(sender=sender_hex, receiver="bob", amount=100, nonce=1) | ||
|
|
||
| # 1. Grab the ID before signing | ||
| unsigned_id = tx.tx_id | ||
| assert tx._cached_tx_id == unsigned_id | ||
|
|
||
| # 2. Sign it | ||
| tx.sign(sk) | ||
|
|
||
| # 3. Prove signing killed the old cache | ||
| assert tx._cached_tx_id is None | ||
|
|
||
| # 4. Prove the new ID is totally different | ||
| signed_id = tx.tx_id | ||
| assert signed_id != unsigned_id | ||
|
|
||
| # 5. Prove it's locked down (Sealed) | ||
| with pytest.raises(AttributeError, match="Transaction is sealed"): | ||
| tx.amount = 500 | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.