generated from StabilityNexus/Template-Repo
-
-
Notifications
You must be signed in to change notification settings - Fork 8
MiniChain v0 prototype completion #48
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
arunabha003
wants to merge
52
commits into
StabilityNexus:main
Choose a base branch
from
arunabha003:v0-completion
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
52 commits
Select commit
Hold shift + click to select a range
91c9969
feat: issue #8 project scaffolding
arunabha003 1a26ae7
feat(crypto): add Ed25519 identity and signature helpers
arunabha003 fce3e7a
test(crypto): cover keypair, address, and signature validation
arunabha003 15c67bd
feat(serialization): add canonical transaction and header encoding
arunabha003 313ee5d
test(serialization): add deterministic encoding coverage
arunabha003 58125c2
chore: fix serialization lint formatting
arunabha003 dcc3d23
feat: implement signed transaction model and verification
arunabha003 ca2fd8e
test: add transaction tamper and identity mismatch coverage
arunabha003 92e96a4
feat: add merkle root computation using blake2b
arunabha003 761eb20
test: add merkle root determinism and edge-case coverage
arunabha003 0b5b764
feat: add deterministic transaction id hashing
arunabha003 bd229ab
feat: implement block header hashing and merkle validation
arunabha003 a7b4f4b
test: add block hash and merkle-root coverage
arunabha003 aa7ea05
feat: implement account state transitions and atomic block apply
arunabha003 af35a69
test: add state transfer, nonce, and rollback coverage
arunabha003 1fb521f
feat: add configurable genesis block and state initialization
arunabha003 b930f7e
test: add genesis block creation and application coverage
arunabha003 42cba00
feat: implement proof-of-work mining engine
arunabha003 0783e6d
test: add pow validation and mining interruption coverage
arunabha003 3d723fd
feat: implement bounded difficulty adjustment
arunabha003 8a4d38b
test: add difficulty retarget scenarios
arunabha003 748a959
feat: add coinbase transaction validation and state handling
arunabha003 8f05a01
test: cover coinbase acceptance and rejection paths
arunabha003 17e53fb
feat: implement mempool queueing and mining selection
arunabha003 d5e8f92
test: add mempool dedup ordering and eviction coverage
arunabha003 e29ada4
feat: implement chain manager with fork reorg handling
arunabha003 387adf3
test: add chain extension and reorg coverage
arunabha003 834cbb0
feat: add candidate block construction for mining
arunabha003 9a153ac
test: cover block template assembly and mining flow
arunabha003 420a747
feat: implement sqlite persistence for blocks state and metadata
arunabha003 6bdb5cd
test: add sqlite roundtrip restart and atomicity coverage
arunabha003 d02843c
feat: implement node lifecycle orchestration and persistence
arunabha003 d73589d
test: add node startup mining and restart coverage
arunabha003 9762f65
feat: implement multi-command cli interface
arunabha003 cd064d7
test: add cli command flow coverage
arunabha003 72b961a
feat: implement peer discovery networking service
arunabha003 6debee4
test: add peer discovery integration coverage
arunabha003 b5ed620
feat: add transaction gossip protocol with dedup forwarding
arunabha003 4aaa08e
test: add three-node transaction gossip propagation coverage
arunabha003 59bc33c
feat: add block propagation protocol with dedup forwarding
arunabha003 6f8c171
test: add three-node block propagation integration coverage
arunabha003 7f9a9e3
feat: add chain synchronization protocol with range sync
arunabha003 984cdda
test: add two-node chain catch-up sync coverage
arunabha003 1ee5462
test: add multi-node convergence and fork reorg integration coverage
arunabha003 3ba5320
test: add double-spend rejection propagation scenario
arunabha003 7c72a44
chore: update cli verification flow in readme
arunabha003 e3f1c5e
chore: add detailed component implementation reference
arunabha003 5b765f1
chore: restore readme from main
arunabha003 bf534c4
chore: update implementation and testing docs
arunabha003 ee2a1e5
feat: add daemon-aware cli networking and peer status
arunabha003 1d45b4c
test: add coverage for daemon submit and peer reconnect
arunabha003 2140219
docs update
arunabha003 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: "3.11" | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| python -m pip install -e .[dev] | ||
| - name: Lint | ||
| run: make lint | ||
|
|
||
| - name: Run tests | ||
| run: make test |
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,21 @@ | ||
| PYTHON ?= python3 | ||
|
|
||
| .PHONY: install dev-install test lint format start-node | ||
|
|
||
| install: | ||
| $(PYTHON) -m pip install . | ||
|
|
||
| dev-install: | ||
| $(PYTHON) -m pip install -e .[dev] | ||
|
|
||
| test: | ||
| $(PYTHON) -m pytest | ||
|
|
||
| lint: | ||
| $(PYTHON) -m ruff check src tests | ||
|
|
||
| format: | ||
| $(PYTHON) -m ruff format src tests | ||
|
|
||
| start-node: | ||
| PYTHONPATH=src $(PYTHON) -m minichain --host 127.0.0.1 --port 7000 |
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,105 @@ | ||
| # MiniChain Implementation Reference | ||
|
|
||
| This document summarizes the current MiniChain v0 implementation by subsystem. | ||
|
|
||
| ## 1) Crypto and Identity (`src/minichain/crypto.py`) | ||
|
|
||
| - Ed25519 key generation/sign/verify through PyNaCl. | ||
| - Address derivation from public key digest (20-byte lowercase hex address). | ||
| - Serialization helpers for private/public keys. | ||
|
|
||
| ## 2) Canonical Serialization (`src/minichain/serialization.py`) | ||
|
|
||
| - Deterministic JSON serialization for consensus-critical payloads. | ||
| - Strict field presence/order checks to avoid cross-node divergence. | ||
|
|
||
| ## 3) Transactions (`src/minichain/transaction.py`) | ||
|
|
||
| - Account-model transfer transaction with: sender, recipient, amount, nonce, fee, timestamp. | ||
| - Signature + public key included for validation. | ||
| - Coinbase transaction shape and validation helpers. | ||
| - Deterministic transaction id generation. | ||
|
|
||
| ## 4) Merkle and Blocks (`src/minichain/merkle.py`, `src/minichain/block.py`) | ||
|
|
||
| - Deterministic Merkle root construction for block transaction lists. | ||
| - Block header contains parent hash, height, timestamp, target, nonce, root. | ||
| - Block hash = canonical header hash. | ||
| - Header/body consistency checks for Merkle root. | ||
|
|
||
| ## 5) Consensus and Mining (`src/minichain/consensus.py`, `src/minichain/mining.py`) | ||
|
|
||
| - Proof-of-work target validation and nonce search. | ||
| - Difficulty bounds and retarget policy. | ||
| - Candidate block building from mempool with coinbase reward + fees. | ||
| - Interrupt-capable mining loop support. | ||
|
|
||
| ## 6) State and Chain Logic (`src/minichain/state.py`, `src/minichain/chain.py`, `src/minichain/genesis.py`) | ||
|
|
||
| - Account state tracks balance and nonce per address. | ||
| - Deterministic tx apply rules: nonce progression, amount+fee affordability, signer identity. | ||
| - Canonical chain selection by valid longest chain. | ||
| - Reorg support with state replay and validation. | ||
| - Configurable genesis creation and initial state. | ||
|
|
||
| ## 7) Mempool (`src/minichain/mempool.py`) | ||
|
|
||
| - Signature/identity checks before acceptance. | ||
| - Per-sender nonce queueing (ready vs waiting). | ||
| - Mining selection prioritizes fee while preserving sender nonce order. | ||
| - Eviction by age and capacity. | ||
|
|
||
| ## 8) Persistence (`src/minichain/storage.py`) | ||
|
|
||
| - SQLite storage for blocks, chain metadata, and state snapshots. | ||
| - Canonical head persistence and restart recovery. | ||
| - Replay + snapshot consistency checks on startup. | ||
|
|
||
| ## 9) Node Orchestration (`src/minichain/node.py`) | ||
|
|
||
| - `MiniChainNode` composes chain manager, mempool, and storage. | ||
| - APIs for transaction submission, block acceptance, and mining. | ||
| - Clean startup/shutdown lifecycle with config validation. | ||
|
|
||
| ## 10) Networking (`src/minichain/network.py`) | ||
|
|
||
| - TCP handshake (`hello`) and peer management. | ||
| - Peer discovery via bootstrap and mDNS/local discovery fallback. | ||
| - Gossip protocols: | ||
| - `/minichain/tx/1.0.0` | ||
| - `/minichain/block/1.0.0` | ||
| - Sync protocol (`/minichain/sync/1.0.0`) with range requests. | ||
| - Block/tx dedup caches. | ||
| - Automatic reconnect loop to bootstrap/known peers. | ||
| - Transaction gossip returns explicit `tx_result` ack (accepted/reason). | ||
|
|
||
| ## 11) CLI (`src/minichain/__main__.py`) | ||
|
|
||
| Primary command groups: | ||
|
|
||
| - `node start` | ||
| - `node run [--peer host:port] [--mine]` | ||
| - `node stop` | ||
| - `wallet generate-key|balance|details|list` | ||
| - `tx submit` | ||
| - `chain info|block|accounts` | ||
| - `mine` | ||
| - `shell` | ||
|
|
||
| Current daemon-aware behavior: | ||
|
|
||
| - If `node run` is active for the same `--data-dir`, `tx submit` routes over network to the running daemon. | ||
| - `chain info` includes `connected_peers`. | ||
| - Daemon status logging includes `height`, `tip`, `mempool_size`, `connected_peers`. | ||
|
|
||
| ## 12) Test Coverage (`tests/`) | ||
|
|
||
| - Unit tests for core primitives and validation logic. | ||
| - Integration tests for: | ||
| - peer discovery | ||
| - tx gossip | ||
| - block propagation | ||
| - chain sync/catch-up | ||
| - fork/reorg and convergence scenarios | ||
|
|
||
| MiniChain v0 currently provides a complete educational blockchain node with deterministic behavior, CLI-driven flows, and reproducible local/multi-node testing. |
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,207 @@ | ||
| # MiniChain End-to-End Testing | ||
|
|
||
| This document is the single source for testing MiniChain end-to-end: | ||
| - single-device flow with live logs | ||
| - two-node sync on one machine (multi-device simulation) | ||
| - true multi-device LAN sync | ||
|
|
||
| ## 1) Prerequisites | ||
|
|
||
| ```bash | ||
| source .venv/bin/activate | ||
| python -m pip install -e . | ||
| ``` | ||
|
|
||
| Optional clean start: | ||
|
|
||
| ```bash | ||
| rm -rf .demo-single .demo-a .demo-b | ||
| ``` | ||
|
|
||
|
|
||
| ## 3) Single-Device End-to-End | ||
|
|
||
| Use two terminals. | ||
|
|
||
| ### Terminal A: create keys and run node daemon | ||
|
|
||
| ```bash | ||
| MINER_OUT=$(minichain --data-dir .demo-single wallet generate-key) | ||
| RECIP_OUT=$(minichain --data-dir .demo-single wallet generate-key) | ||
|
|
||
| MINER_ADDR=$(echo "$MINER_OUT" | awk -F= '/address/{gsub(/ /,"",$2); print $2; exit}') | ||
| MINER_PK=$(echo "$MINER_OUT" | awk -F= '/private_key/{gsub(/ /,"",$2); print $2; exit}') | ||
| RECIP_ADDR=$(echo "$RECIP_OUT" | awk -F= '/address/{gsub(/ /,"",$2); print $2; exit}') | ||
|
|
||
| minichain --data-dir .demo-single --host 127.0.0.1 --port 7000 --miner-address "$MINER_ADDR" \ | ||
| node run --advertise-host 127.0.0.1 --mine --mine-interval-seconds 10 --status-interval-seconds 2 | ||
| ``` | ||
|
|
||
| ### Terminal B: verify chain, submit tx, verify balances | ||
|
|
||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-single chain info | ||
|
|
||
| minichain --data-dir .demo-single --host 127.0.0.1 --port 7000 tx submit \ | ||
| --private-key "$MINER_PK" \ | ||
| --recipient "$RECIP_ADDR" \ | ||
| --amount 5 \ | ||
| --fee 1 \ | ||
| --no-mine-now | ||
| ``` | ||
|
|
||
| Wait for the next mined block, then: | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-single wallet balance --address "$MINER_ADDR" | ||
| minichain --data-dir .demo-single wallet balance --address "$RECIP_ADDR" | ||
| minichain --data-dir .demo-single chain accounts --limit 20 | ||
| ``` | ||
|
|
||
| Stop daemon: | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-single node stop | ||
| ``` | ||
|
|
||
| ## 4) Two Nodes on One Machine (Simulate Multi-Device) | ||
|
|
||
| Use two terminals. | ||
|
|
||
| ### Terminal A (miner node) | ||
|
|
||
| ```bash | ||
| MINER_OUT=$(minichain --data-dir .demo-a wallet generate-key) | ||
| MINER_ADDR=$(echo "$MINER_OUT" | awk -F= '/address/{gsub(/ /,"",$2); print $2; exit}') | ||
| MINER_PK=$(echo "$MINER_OUT" | awk -F= '/private_key/{gsub(/ /,"",$2); print $2; exit}') | ||
|
|
||
| minichain --data-dir .demo-a --host 127.0.0.1 --port 7000 --miner-address "$MINER_ADDR" \ | ||
| node run --advertise-host 127.0.0.1 --mine --mine-interval-seconds 15 --status-interval-seconds 2 | ||
| ``` | ||
|
|
||
| ### Terminal B (observer node) | ||
|
|
||
| ```bash | ||
| RECIP_OUT=$(minichain --data-dir .demo-b wallet generate-key) | ||
| RECIP_ADDR=$(echo "$RECIP_OUT" | awk -F= '/address/{gsub(/ /,"",$2); print $2; exit}') | ||
|
|
||
| minichain --data-dir .demo-b --host 127.0.0.1 --port 7001 \ | ||
| node run --advertise-host 127.0.0.1 --peer 127.0.0.1:7000 --status-interval-seconds 2 | ||
| ``` | ||
|
|
||
| ### Terminal C (tx submit) | ||
|
|
||
|
|
||
|
|
||
| Submit tx to node A: | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-a --host 127.0.0.1 --port 7000 tx submit \ | ||
| --private-key "$MINER_PK" \ | ||
| --recipient "$RECIP_ADDR" \ | ||
| --amount 7 \ | ||
| --fee 1 \ | ||
| --no-mine-now | ||
| ``` | ||
|
|
||
| After next mined block on A, verify sync: | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-a chain info | ||
| minichain --data-dir .demo-b chain info | ||
| minichain --data-dir .demo-a wallet balance --address "$MINER_ADDR" | ||
| minichain --data-dir .demo-a wallet balance --address "$RECIP_ADDR" | ||
| minichain --data-dir .demo-b wallet balance --address "$MINER_ADDR" | ||
| minichain --data-dir .demo-b wallet balance --address "$RECIP_ADDR" | ||
| ``` | ||
|
|
||
| Stop: | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-a node stop | ||
| minichain --data-dir .demo-b node stop | ||
| ``` | ||
|
|
||
| ## 5) True Multi-Device LAN Test | ||
|
|
||
| Assume: | ||
| - Device A IP: `<A_IP>` | ||
| - Device B IP: `<B_IP>` | ||
|
|
||
| ### Device A (miner) | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-a --host 0.0.0.0 --port 7000 --miner-address "$MINER_ADDR" \ | ||
| node run --advertise-host <A_IP> --mine --mine-interval-seconds 15 --status-interval-seconds 2 | ||
| ``` | ||
|
|
||
| ### Device B (observer) | ||
|
|
||
| ```bash | ||
| minichain --data-dir .demo-b --host 0.0.0.0 --port 7001 \ | ||
| node run --advertise-host <B_IP> --peer <A_IP>:7000 --status-interval-seconds 2 | ||
| ``` | ||
|
|
||
| ## 6) Complete CLI Command Reference | ||
|
|
||
| Top-level: | ||
|
|
||
| ```bash | ||
| minichain [--host HOST] [--port PORT] [--data-dir DATA_DIR] [--miner-address MINER_ADDRESS] <command> | ||
| ``` | ||
|
|
||
| Commands: | ||
|
|
||
| ```bash | ||
| minichain node start | ||
| minichain node run [--peer HOST:PORT] [--advertise-host HOST] [--node-id ID] [--mdns] [--mine] [--mine-interval-seconds N] [--sync-batch-size N] [--status-interval-seconds N] | ||
| minichain node stop [--timeout-seconds N] [--force] | ||
|
|
||
| minichain wallet generate-key | ||
| minichain wallet balance --address <20-byte-lowercase-hex> | ||
| minichain wallet details --address <20-byte-lowercase-hex> | ||
| minichain wallet list [--limit N] | ||
|
|
||
| minichain tx submit --private-key <hex-ed25519-key> --recipient <20-byte-lowercase-hex> --amount N [--fee N] [--nonce N] [--mine-now|--no-mine-now] | ||
|
|
||
| minichain chain info | ||
| minichain chain block (--height N | --hash <block-hash-hex>) | ||
| minichain chain accounts [--limit N] | ||
|
|
||
| minichain mine [--count N] [--max-transactions N] | ||
| minichain shell | ||
| ``` | ||
|
|
||
| Useful help commands: | ||
|
|
||
| ```bash | ||
| minichain --help | ||
| minichain node --help | ||
| minichain node run --help | ||
| minichain node stop --help | ||
| minichain wallet --help | ||
| minichain wallet balance --help | ||
| minichain wallet details --help | ||
| minichain wallet list --help | ||
| minichain tx --help | ||
| minichain tx submit --help | ||
| minichain chain --help | ||
| minichain chain block --help | ||
| minichain chain accounts --help | ||
| minichain mine --help | ||
| ``` | ||
|
|
||
| Legacy aliases (still accepted and auto-remapped): | ||
|
|
||
| ```bash | ||
| start | ||
| generate-key | ||
| balance | ||
| submit-tx | ||
| chain-info | ||
| block | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove trailing space in section heading (Line 22).
🧹 Fix trailing space
📝 Committable suggestion
🧰 Tools
🪛 markdownlint-cli2 (0.21.0)
[warning] 22-22: Trailing spaces
Expected: 0 or 2; Actual: 1
(MD009, no-trailing-spaces)
🤖 Prompt for AI Agents