Skip to content

feat(security): audit cross-contract calls and implement reentrancy guard (closes #354) - #387

Open
shobhamerabacha-star wants to merge 2 commits into
ChainLearnOfficial:mainfrom
shobhamerabacha-star:feat/reentrancy-protection-audit
Open

shobhamerabacha-star wants to merge 2 commits into
ChainLearnOfficial:mainfrom
shobhamerabacha-star:feat/reentrancy-protection-audit

Conversation

@shobhamerabacha-star

Copy link
Copy Markdown

Summary

Closes #354

Delivers a comprehensive cross-contract security audit and robust reentrancy protection architecture across the ChainLearn smart contract ecosystem (learn-token, credential-nft, and progress-tracker).

Key Changes

  • Cross-Contract Audit Report (docs/reentrancy-audit.md):
    • Full inventory of external invocations (invoke_contract calls) in learn-token (claim_reward, batch_claim_reward, preview_claim_reward) and credential-nft (mint_credential).
    • Threat modeling and vulnerability identification (same-function reentrancy during score verification, cross-function reentrancy into token operations, and credential minting recursion).
    • Remediation architecture and formal verification notes.
  • RAII Reentrancy Guard Architecture:
    • contracts/learn-token/src/storage.rs: Added TokenDataKey::ReentrancyGuard in instance storage with is_reentrancy_locked and set_reentrancy_locked.
    • contracts/learn-token/src/lib.rs: Introduced ReentrancyGuard RAII struct enforcing zero reentrant entries across claim_reward, batch_claim_reward, mint, transfer, transfer_from, burn, burn_from, and claim_vested.
    • contracts/credential-nft/src/metadata.rs & contracts/credential-nft/src/lib.rs: Added CredentialDataKey::ReentrancyGuard and protected mint_credential during external progress-tracker verification.
  • Unit & Integration Attack Suite (tests/integration/security_reentrancy_tests.rs):
    • test_reentrancy_during_claim_reward_blocked_by_guard: Adversarial progress tracker attempts recursive claim_reward during quiz score resolution; halted with panic, 0 tokens minted.
    • test_cross_function_reentrancy_blocked_by_guard: Adversarial tracker attempts mint during score resolution; halted, total supply and balances remain uncorrupted.
    • test_credential_nft_reentrancy_blocked_by_guard: Adversarial tracker attempts nested mint_credential during eligibility verification; blocked, credential counter remains 0.
    • test_reentrancy_prevented_state_consistent_and_no_funds_lost: Invariance verification under failed reentrancy attacks.
    • test_reentrancy_during_transfer: Transfer level reentrancy prevention.
  • Contract Tests:
    • cargo test -p learn-token: 78/78 tests passed.
    • cargo test -p credential-nft: 37/37 tests passed.
    • cargo test --test security_reentrancy_tests: 5/5 tests passed.

Verification

  • cargo test -p learn-token: 78 passed, 0 failed.
  • cargo test -p credential-nft: 37 passed, 0 failed.
  • cargo test --test security_reentrancy_tests: 5 passed, 0 failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1. Add reentrancy protection audit

1 participant