Skip to content

refactor(l1,l2): remove duplicate bytes-to-nibbles conversion#6156

Open
viktorking7 wants to merge 3 commits into
lambdaclass:mainfrom
viktorking7:patch-1
Open

refactor(l1,l2): remove duplicate bytes-to-nibbles conversion#6156
viktorking7 wants to merge 3 commits into
lambdaclass:mainfrom
viktorking7:patch-1

Conversation

@viktorking7

Copy link
Copy Markdown

The compact_to_hex function in nibbles.rs contained a duplicate implementation of the bytes-to-nibbles conversion logic that already exists in Nibbles::from_raw. This duplication increases maintenance burden and risk of divergence.

@viktorking7 viktorking7 requested a review from a team as a code owner February 8, 2026 08:17
@greptile-apps

greptile-apps Bot commented Feb 8, 2026

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

This PR eliminates code duplication by removing the standalone keybytes_to_hex function and replacing it with a call to the existing Nibbles::from_raw method. The two implementations are mathematically equivalent:

  • Changed in compact_to_hex function (nibbles.rs:305-307)
    • Replaced keybytes_to_hex(compact) with Nibbles::from_raw(compact, true).into_vec()
    • Both split bytes into nibbles using equivalent operations: b / 16byte >> 4 & 0x0F and b % 16byte & 0x0F
    • Both append terminator nibble 16 at the end
  • Removed 13 lines of duplicate code (nibbles.rs:317-329)

The refactoring is safe and maintains identical behavior while improving maintainability.

Confidence Score: 5/5

  • This PR is safe to merge with no risk
  • The refactoring replaces duplicate logic with an existing, well-tested method. The mathematical operations are provably equivalent: integer division and modulo operations match bitwise shift and mask operations for nibble extraction. The change reduces code duplication without altering behavior, and the affected function is used internally in the well-established decode_compact method.
  • No files require special attention

Important Files Changed

Filename Overview
crates/common/trie/nibbles.rs Removed duplicate keybytes_to_hex function, replaced with Nibbles::from_raw call in compact_to_hex

Sequence Diagram

sequenceDiagram
    participant Caller
    participant decode_compact
    participant compact_to_hex
    participant from_raw
    participant from_hex

    Caller->>decode_compact: Nibbles::decode_compact(compact)
    decode_compact->>compact_to_hex: compact_to_hex(compact)
    
    alt compact is empty
        compact_to_hex-->>decode_compact: return vec![]
    else compact has data
        compact_to_hex->>from_raw: Nibbles::from_raw(compact, true)
        Note over from_raw: Split bytes into nibbles:<br/>(byte >> 4 & 0x0F), byte & 0x0F<br/>Add terminator nibble 16
        from_raw-->>compact_to_hex: Nibbles with data vec
        compact_to_hex->>compact_to_hex: into_vec() to extract data
        
        alt base[0] < 2 (extension node)
            compact_to_hex->>compact_to_hex: Remove terminator (16)
        end
        
        compact_to_hex->>compact_to_hex: Calculate chop = 2 - (base[0] & 1)
        Note over compact_to_hex: Remove prefix based on odd/even flag
        compact_to_hex-->>decode_compact: return base[chop..]
    end
    
    decode_compact->>from_hex: Nibbles::from_hex(result)
    from_hex-->>Caller: Nibbles instance
Loading

@viktorking7

Copy link
Copy Markdown
Author

@ElFantasma lint fixed

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.

3 participants