[BUG]: P2P sync downloads full 500-block chunks without headers-first validation

未关闭
#178 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@anshulchikhale30-p 已经在做这个了。

开始于 2026年11月8日。

  • #179 来自 @anshulchikhale30-p —— 已关闭,未合并
  • #180 来自 @anshulchikhale30-p —— 未关闭

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

Start with main.py handlers request_chain, chain_request, and chain_response, then read minichain/p2p.py and minichain/chain.py to understand current sync and validation. Run the existing test suite and inspect its sync coverage before planning the new tests named in the issue. Done means header-first sync, bounded and rate-limited body requests, peer handling for invalid data, and the stated acceptance criteria are implemented and documented in docs/sync.md.

由索引模型根据 Issue 内容生成。

描述

bug documentation enhancement

Bug Description

MiniChain's P2P sync downloads up to 500 full blocks per request and has no headers-first phase or block locator. When a peer's tip is ahead, main.py calls request_chain(network, chain.last_block.index + 1, 500); on a fork it requests up to 50 blocks backward. This works for small local chains but is inefficient and unsafe at scale.

Current behavior:

  • request_chain broadcasts a chain_request with start_index and limit=500.
  • chain_request serves chain.chain[start_index:start_index+limit] and sends every full block (header + transactions + receipts).
  • chain_response parses all received blocks into memory and applies them one by one.
  • There is no getheaders/headers phase and no block locator, so an invalid chain is only discovered after downloading entire bodies.
  • chain_request accepts start_index and limit without bounds; a peer can request a very large range or send a very large response.
  • There is no request ID/response correlation, no maximum response size, and no per-peer sync rate limit.
  • p2p.py coverage is 37% (README), so these paths are under-tested.

Impact:

  • Slow sync and high memory use on long chains.
  • Wasted bandwidth and CPU on invalid or adversarial chains.
  • DoS vector via oversized requests/responses or request floods.
  • Repeated 50-block backward scans on forks.
  • Multi-node research at 10k+ blocks is impractical.

Expected behavior:

  • Sync headers first with a locator, validate the header chain (PoW, target, timestamp, parent linkage, cumulative work), then download bodies in bounded batches.
  • Bound request/response sizes, add request IDs, unicast to one peer, and rate-limit per peer.
  • Disconnect or ban peers that send invalid or oversized data.

Proposed fix:

  1. Add getheaders with a locator (recent block hashes), stop_hash, and max (e.g. 2000), plus a headers response.
  2. Validate headers before bodies: PoW, target, timestamp, parent linkage, cumulative work.
  3. Add getblock by hash or bounded range; fetch bodies in small batches (e.g. 64 blocks) with a maximum number of in-flight requests.
  4. Verify transaction root, receipt root, and state root after executing the bodies.
  5. Add request IDs, unicast requests to one peer, and deduplicate responses.
  6. Cap start_index, limit, and response bytes; add a per-peer token bucket.
  7. Feed invalid or oversized responses into the existing validation counters and ban repeat offenders.
  8. Add tests for 10k-block sync, 100-block reorg, invalid headers, invalid bodies, oversized requests/responses, restart mid-sync, and rate limiting.
  9. Document the protocol and limits in docs/sync.md.

Acceptance criteria:

  • 10k-block sync completes within an agreed target and bounded memory.
  • Headers are validated before bodies are downloaded.
  • Fork detection uses a locator, not repeated 50-block scans.
  • Requests and responses are bounded and rate-limited.
  • Invalid or oversized peers are disconnected or banned.
  • No unbounded queues or response buffers.
  • p2p.py coverage is at least 65%.
  • Sync protocol and limits are documented.

Relevant files:

  • main.py (request_chain, chain_request, chain_response)
  • minichain/p2p.py
  • minichain/chain.py
  • tests/test_sync.py (new)
  • tests/test_p2p_limits.py (new)

References:

Steps to Reproduce

  1. Start two nodes on different ports with separate data directories.
  2. Let one node mine more than 500 blocks.
  3. Connect the second node to the first node.
  4. Observe the hello handler request chain_request with limit=500, followed by full-block chain_response payloads.
  5. For the adversarial case, send a chain_request with a very large limit, or send a chain_response containing a very large list of blocks, and observe memory/CPU use.

Logs and Screenshots

Relevant log lines include:

?? Peer ... is ahead (...) . Initiating chunked sync...
?? Peer requested blocks from ... (limit 500).

No screenshots available.

Environment Details

  • OS: Windows 11 / Linux
  • Python 3.10+
  • MiniChain main branch as of 2026-11-08
  • p2p.py coverage: 37% (README coverage report)

Impact

High - Major feature is broken (sync works for small chains, but is not safe or efficient at scale).

Code of Conduct

  • I have joined the Discord server and will post updates there
  • I have searched existing issues to avoid duplicates
主要语言
Python
星标
12
派生
27
平均合并
2 分钟
30 天内合并 PR
1

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

StabilityNexus/MiniChain 的其他 Issue

查看 StabilityNexus/MiniChain 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。