Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,59 +140,5 @@ def test_unsigned_transaction_fails_verification(alice, bob):
assert not tx.verify(), "An unsigned transaction must not verify."


# ------------------------------------------------------------------
# 4. Replay protection
# ------------------------------------------------------------------

def test_replay_attack_same_nonce_rejected(alice, bob, funded_state):
"""Replaying the same transaction must be rejected the second time."""
alice_sk, alice_pk = alice
_, bob_pk = bob

tx = Transaction(alice_pk, bob_pk, 10, nonce=0)
tx.sign(alice_sk)

assert funded_state.apply_transaction(tx), "First submission must succeed."
assert not funded_state.apply_transaction(tx), "Replayed transaction must be rejected."
# Ensure the rejected replay did not mutate the ledger
assert funded_state.get_account(alice_pk)["balance"] == 90, \
"Alice's balance must not change after a rejected replay."
assert funded_state.get_account(alice_pk)["nonce"] == 1, \
"Alice's nonce must not advance after a rejected replay."


def test_out_of_order_nonce_rejected(alice, bob, funded_state):
"""A transaction with a skipped nonce must be rejected."""
alice_sk, alice_pk = alice
_, bob_pk = bob

tx = Transaction(alice_pk, bob_pk, 10, nonce=5)
tx.sign(alice_sk)

assert not funded_state.apply_transaction(tx), "A transaction with a skipped nonce must be rejected."
# Ensure the rejected transaction did not mutate the ledger
assert funded_state.get_account(alice_pk)["balance"] == 100, \
"Alice's balance must remain unchanged after a rejected transaction."
assert funded_state.get_account(alice_pk)["nonce"] == 0, \
"Alice's nonce must remain unchanged after a rejected transaction."


def test_sequential_nonces_accepted(alice, bob, funded_state):
"""Two transactions with consecutive nonces must both succeed."""
alice_sk, alice_pk = alice
_, bob_pk = bob

tx0 = Transaction(alice_pk, bob_pk, 10, nonce=0)
tx0.sign(alice_sk)
assert funded_state.apply_transaction(tx0)

tx1 = Transaction(alice_pk, bob_pk, 10, nonce=1)
tx1.sign(alice_sk)
assert funded_state.apply_transaction(tx1)

assert funded_state.get_account(alice_pk)["nonce"] == 2, \
"Alice's nonce should advance to 2 after two accepted transactions."
assert funded_state.get_account(alice_pk)["balance"] == 80, \
"Alice's balance should be 80 after two 10-coin transfers."
assert funded_state.get_account(bob_pk)["balance"] == 20, \
"Bob's balance should be 20 after receiving two transfers."
"Bob's balance should be 20 after receiving two transfers."
Comment on lines 143 to +144
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Re-add replay/nonce regression tests removed in this deletion area.

This change drops coverage for duplicate nonce rejection, skipped nonce rejection, and sequential nonce progression. Those behaviors are enforced in minichain/state.py (Line 37-43 and Line 79-126), so removing these tests meaningfully weakens replay-protection safety.

🧰 Tools
🪛 Ruff (0.15.6)

[warning] 144-144: Unexpected indentation

(invalid-syntax)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_transaction_signing_karun.py` around lines 143 - 144, Re-add the
replay/nonce regression tests you removed in
tests/test_transaction_signing_karun.py to cover duplicate-nonce rejection,
skipped-nonce rejection, and valid sequential-nonce progression against the
enforcement in minichain.state.State (the nonce/validation logic around lines
referenced). Concretely: add three test cases that (1) submit the same nonce
twice and assert the second submit is rejected/raises, (2) submit a tx with a
nonce that skips the current account nonce and assert it is rejected/raises, and
(3) submit txs with increasing sequential nonces and assert they are accepted
and update the account nonce accordingly; use the same transaction
construction/helpers present in the test file and assert against
State.apply_transaction / State.process_transaction results or exceptions to
verify the behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

fd 'test_transaction_signing_karun\.py$' | while read -r f; do
  echo "== $f =="
  nl -ba "$f" | sed -n '132,160p'
done

Repository: StabilityNexus/MiniChain

Length of output: 173


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find the file and display lines around 144 using cat
fd 'test_transaction_signing_karun\.py$' -type f | while read -r f; do
  echo "== $f =="
  echo "=== File size ==="
  wc -l "$f"
  echo "=== Lines 130-160 with line numbers ==="
  cat -n "$f" | sed -n '130,160p'
done

Repository: StabilityNexus/MiniChain

Length of output: 238


🏁 Script executed:

#!/bin/bash
# Verify Python syntax by attempting to compile the file
fd 'test_transaction_signing_karun\.py$' -type f | while read -r f; do
  echo "== Syntax check for $f =="
  python3 -m py_compile "$f" 2>&1 || echo "Syntax error detected"
done

Repository: StabilityNexus/MiniChain

Length of output: 238


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find the file with correct fd syntax
fd 'test_transaction_signing_karun\.py$' | while read -r f; do
  echo "== $f =="
  echo "=== File line count ==="
  wc -l "$f"
  echo "=== Lines 130-160 ==="
  cat -n "$f" | sed -n '130,160p'
done

Repository: StabilityNexus/MiniChain

Length of output: 709


🏁 Script executed:

#!/bin/bash
# Verify Python syntax
python3 -c "
import sys
import py_compile

try:
    py_compile.compile('tests/test_transaction_signing_karun.py', doraise=True)
    print('✓ File syntax is valid')
except py_compile.PyCompileError as e:
    print(f'✗ Syntax error detected:')
    print(e)
    sys.exit(1)
"

Repository: StabilityNexus/MiniChain

Length of output: 181


Remove the orphaned indented string at EOF — it causes a Python IndentationError and prevents pytest collection.

Line 144 is a standalone string literal with unexpected indentation at the end of the file. Python cannot parse it, causing an IndentationError: unexpected indent. This blocks all test execution.

Fix
-        "Bob's balance should be 20 after receiving two transfers."
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"Bob's balance should be 20 after receiving two transfers."
🧰 Tools
🪛 Ruff (0.15.6)

[warning] 144-144: Unexpected indentation

(invalid-syntax)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/test_transaction_signing_karun.py` at line 144, Remove the orphaned
indented string literal "Bob's balance should be 20 after receiving two
transfers." at the EOF of tests/test_transaction_signing_karun.py (the
standalone indented string on line 144) which causes the IndentationError;
either delete that line or convert it into a proper comment or docstring placed
at the top of the test function, ensuring no stray indented string remains so
pytest can collect tests.

Loading