Skip to content

Comments

refactor: flatten directory structure to single minichain package#19

Open
SIDDHANTCOOKIE wants to merge 2 commits intoStabilityNexus:mainfrom
SIDDHANTCOOKIE:feature/flatten-structure
Open

refactor: flatten directory structure to single minichain package#19
SIDDHANTCOOKIE wants to merge 2 commits intoStabilityNexus:mainfrom
SIDDHANTCOOKIE:feature/flatten-structure

Conversation

@SIDDHANTCOOKIE
Copy link

@SIDDHANTCOOKIE SIDDHANTCOOKIE commented Feb 21, 2026

Description

Hey @Zahnentferner
This PR is a direct follow-up to the recent discussions on discord and addresses the feedback regarding the project's folder structure:
To improve minimalism and make the codebase easier for new developers to explore, this PR flattens the architecture by consolidating the single-file packages into a single cohesive minichain directory.

Changes Made:

  • Consolidated Folders: Moved the contents of consensus/, core/, [network/] and [node/] into a single minichain/ directory.
  • Removed Boilerplate: Deleted the empty [init.py]) files that were required by the previous nested structure.
  • Updated Imports: Refactored internal module imports and updated [main.py] and the tests/ directory to import directly from the unified minichain package.

Addressed Issues:

Resolves maintainer request on discord to flatten the directory structure.

Screenshots/Recordings:

image

Additional Notes:

  • Simpler navigation: Instead of opening 4 different directories to find 4 files, everything core to the blockchain logic lives in one place.
  • Testing: Verified that all unit tests still pass successfully and [main.py] executes without errors.

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions.
  • If applicable, I have made corresponding changes or additions to the documentation.
  • If applicable, I have made corresponding changes or additions to tests.
  • My changes generate no new warnings or errors.
  • I have joined the Stability Nexus's Discord server and I will share a link to this PR with the project maintainers there.
  • I have read the Contribution Guidelines.
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.

AI Usage Disclosure

Check one of the checkboxes below:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: N/A

Summary by CodeRabbit

  • Refactor

    • Reorganized package exports and consolidated public APIs.
    • Switched many imports to package-relative paths.
    • Updated core constructor and runtime call signatures.
    • Simplified network initialization and shutdown behavior.
  • Tests

    • Updated test imports to reference the reorganized package surface.
  • Chores

    • Added Python build-artifact patterns to .gitignore.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 21, 2026

Walkthrough

Consolidates public API surface into the minichain package: re-exports for PoW, P2P, and mempool now live in minichain; several packages remove their prior re-exports. Multiple modules switch absolute imports to relative imports and test/import sites updated accordingly. Minor .gitignore additions.

Changes

Cohort / File(s) Summary
Build Configuration
/.gitignore
Added Python ignore patterns: __pycache__/, *.py[cod], *$py.class, *.so.
API re-exports moved to minichain
consensus/__init__.py, minichain/__init__.py, network/__init__.py, node/__init__.py
Removed re-exports from consensus, network, and node; added re-exports in minichain for mine_block, calculate_hash, MiningExceededError, P2PNetwork, and Mempool.
Import path refactors (relative imports)
minichain/block.py, minichain/chain.py, minichain/mempool.py, minichain/state.py
Replaced several absolute imports (core.*, consensus.pow) with relative imports (.transaction, .block, .state, .pow, .contract) to use package-local modules.
Main script adjustments
main.py
Switched to minichain import; changed Blockchain(state)Blockchain(); P2PNetwork(None)P2PNetwork(lambda x: None); updated _run_node call signature and removed network.stop() in finally block.
Tests import updates
tests/test_contract.py, tests/test_core.py
Updated tests to import State, Transaction, and Blockchain from minichain instead of core.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Python Lang

Suggested reviewers

  • Zahnentferner

Poem

🐇 I hopped through modules, neat and spry,
Gathering exports beneath one sky.
From scattered paths to minichain's door,
I tidy namespaces — then leap once more! ✨

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main objective of the PR: restructuring the codebase to consolidate multiple packages (consensus, core, network, node) into a single minichain package.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@SIDDHANTCOOKIE SIDDHANTCOOKIE force-pushed the feature/flatten-structure branch from 592e7a2 to 59b0093 Compare February 21, 2026 11:55
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
main.py (1)

80-92: ⚠️ Potential issue | 🟠 Major

state and chain.state are now two different, permanently diverged objects — claim_nonce reads stale data.

Blockchain() (line 81) creates its own internal State() instance. The standalone state = State() on line 80 is never mutated by any transaction or block addition; state.get_account(address) will always return a freshly-initialized account with nonce = 0.

claim_nonce only produces correct nonces by coincidence: max(account_nonce=0, local_nonce) reduces to pending_nonce_map[address], which is kept in sync with chain.state via sync_nonce. If pending_nonce_map has no entry for an address (first call before any sync_nonce for that address), the function returns 0 regardless of what chain.state actually holds for that address's nonce.

The AI summary confirms this was previously Blockchain(state) — the two objects were the same. That sharing was lost in this PR.

The fix is to remove state and close over chain instead:

🐛 Proposed fix
 async def node_loop():
     logger.info("Starting MiniChain Node with Smart Contracts")
 
-    state = State()
     chain = Blockchain()
     mempool = Mempool()
 
     pending_nonce_map = {}
 
     def claim_nonce(address):
-        account = state.get_account(address)
+        account = chain.state.get_account(address)
         account_nonce = account.get("nonce", 0) if account else 0
         local_nonce = pending_nonce_map.get(address, account_nonce)
         next_nonce = max(account_nonce, local_nonce)
         pending_nonce_map[address] = next_nonce + 1
         return next_nonce
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@main.py` around lines 80 - 92, The standalone State() instance is stale and
claim_nonce reads from it; remove the unused state variable and have claim_nonce
consult the blockchain's canonical state (chain.state) instead of state; update
any uses of state.get_account(...) inside claim_nonce to use
chain.state.get_account(...), ensure pending_nonce_map and sync_nonce continue
to coordinate with chain.state, and delete the now-unused State() instantiation
to avoid diverged state objects.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.gitignore:
- Around line 328-332: Update the Python ignore block in .gitignore to cover
common artifacts missing from the current entries (__pycache__/, *.py[cod],
*$py.class, *.so): add virtualenv folders (e.g., .venv/, venv/, env/),
build/dist metadata (build/, dist/, *.egg-info/, pip-wheel-metadata/), test and
cache outputs (.pytest_cache/, .mypy_cache/, .cache/, .eggs/), coverage and
report files (.coverage, htmlcov/), and packaging helpers (eggs, wheel caches);
modify the Python block around the existing patterns so these extra patterns are
included together for clearer maintenance.

In `@main.py`:
- Line 94: Replace the unused lambda parameter name to follow convention: change
the anonymous callback passed to P2PNetwork from "lambda x: None" to "lambda _:
None" so the unused argument is deliberately named as a discard and silences
ARG005; update the instantiation at the P2PNetwork call site accordingly.

---

Outside diff comments:
In `@main.py`:
- Around line 80-92: The standalone State() instance is stale and claim_nonce
reads from it; remove the unused state variable and have claim_nonce consult the
blockchain's canonical state (chain.state) instead of state; update any uses of
state.get_account(...) inside claim_nonce to use chain.state.get_account(...),
ensure pending_nonce_map and sync_nonce continue to coordinate with chain.state,
and delete the now-unused State() instantiation to avoid diverged state objects.

Comment on lines +328 to +332
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Consider expanding the Python ignore block with common missing patterns.

The four patterns added (__pycache__/, *.py[cod], *$py.class, *.so) are correct and standard. However, for a Python project of this structure (with setup.py, tests/, and likely a virtual environment), several commonly generated artifacts are not yet covered.

♻️ Proposed additions to the Python ignore block
 # Python
 __pycache__/
 *.py[cod]
 *$py.class
 *.so
+
+# Distribution / packaging
+dist/
+build/
+*.egg-info/
+*.egg
+
+# Virtual environments
+venv/
+.venv/
+env/
+.env
+
+# Testing & coverage
+.pytest_cache/
+.coverage
+htmlcov/
📝 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
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
# Distribution / packaging
dist/
build/
*.egg-info/
*.egg
# Virtual environments
venv/
.venv/
env/
.env
# Testing & coverage
.pytest_cache/
.coverage
htmlcov/
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.gitignore around lines 328 - 332, Update the Python ignore block in
.gitignore to cover common artifacts missing from the current entries
(__pycache__/, *.py[cod], *$py.class, *.so): add virtualenv folders (e.g.,
.venv/, venv/, env/), build/dist metadata (build/, dist/, *.egg-info/,
pip-wheel-metadata/), test and cache outputs (.pytest_cache/, .mypy_cache/,
.cache/, .eggs/), coverage and report files (.coverage, htmlcov/), and packaging
helpers (eggs, wheel caches); modify the Python block around the existing
patterns so these extra patterns are included together for clearer maintenance.

return next_nonce

network = P2PNetwork(None)
network = P2PNetwork(lambda x: None)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Unused lambda argument — use _ per convention.

Ruff (ARG005) flags x as unused. Replace with the conventional discard name to silence the warning and signal intent.

🔧 Proposed fix
-    network = P2PNetwork(lambda x: None)
+    network = P2PNetwork(lambda _: None)
📝 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
network = P2PNetwork(lambda x: None)
network = P2PNetwork(lambda _: None)
🧰 Tools
🪛 Ruff (0.15.1)

[warning] 94-94: Unused lambda argument: x

(ARG005)

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

In `@main.py` at line 94, Replace the unused lambda parameter name to follow
convention: change the anonymous callback passed to P2PNetwork from "lambda x:
None" to "lambda _: None" so the unused argument is deliberately named as a
discard and silences ARG005; update the instantiation at the P2PNetwork call
site accordingly.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In @.gitignore:
- Around line 327-332: The Python ignore block currently includes __pycache__/,
*.py[cod], *$py.class, and *.so but should be expanded to cover common
virtualenvs, build artifacts and test/coverage outputs; update the .gitignore by
adding patterns for virtual environments (.venv/, venv/, env/, ENV/), build/dist
and packaging metadata (build/, dist/, *.egg-info/, pip-wheel-metadata/),
testing and coverage caches (.pytest_cache/, .coverage, coverage.xml, htmlcov/),
and tool caches (mypy cache: .mypy_cache/, pip cache if needed) so files
produced by functions/tools referenced in the repo are not accidentally
committed. Ensure the new entries are placed near the existing Python block for
clarity and do not remove the current patterns (__pycache__/, *.py[cod],
*$py.class, *.so).

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 participant