Skip to content

Read the total difficulty the importer actually wrote #5

Closed

Description

@SDL-A1

Migrated from SergioDemianLerner/rustock#5 (pull request by @SergioDemianLerner, 2026-09-12).
Merged: yes. The commits are already in this repository's history.
Original: SergioDemianLerner/rustock#5

Two encodings were being written to the total_difficulty column family. put_total_difficulty and store_headers_batch RLP-encode; the import's fast batch path wrote raw 32-byte big-endian values. The reader only understood RLP, and alloy_rlp rejects a leading zero byte, so every block the importer wrote had a total difficulty that would not parse.

This was not cosmetic. handle_headers_response reads the head's total difficulty with ?, so the failure propagated straight out of header processing:

ERROR Failed to process headers chunk: leading zero
INFO  Starting sync: our head #0, peer best #9233606

Sync could not proceed at all after an import until the head moved off an imported block. The visible symptom that prompted this -- every historical block reading as null from eth_getBlockByNumber -- was the smaller half of it.

Confirmed against the live mainnet database: imported blocks store 32 bytes, blocks the node wrote store 13, the boundary is exactly where the import ended, and the decoded values run continuously across it. The importer's numbers were always right; only their encoding was wrong.

Both forms are now read. Rewriting 9.2M rows would be a migration for no benefit, and the two cannot be confused: RLP never begins with a zero byte, a real total difficulty is nowhere near 2^248 so its raw form always starts with zeros, and the decoder additionally requires RLP to consume its whole buffer. Encoding now goes through one function that all three write sites share, which is what stopped them agreeing in the first place.

Separately, a block that exists no longer disappears because an auxiliary field will not parse: ok()? on the total difficulty turned a decode error into "no such block", hiding the header and transactions behind a bare null with nothing to explain it. An unknown total difficulty is now reported as zero and the block is still returned.

Tests cover both encodings through the reader, the RPC path with an importer-written value, and a block with a corrupt total difficulty. The existing eth_getBlockByNumber tests all passed throughout, because every one of them wrote the total difficulty through the RLP path -- nothing round-tripped what the importer produced.

Adds examples/block_info.rs, which prints stored block metadata read-only alongside a running node; it is how the diagnosis above was confirmed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions