multiformats / multiformats/py-multihash

No spec compliance test validating hash codes against `multicodec/table.csv`

Open
#52 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
17
Forks
17
Avg merge
1h 5m
Merged PRs (30d)
3

Description

There is no test that validates the hash function codes in constants.py and funcs.py against the authoritative upstream multicodec table.csv. go-multihash has a TestSpec that does this validation.

Problem

The HASH_CODES dict in constants.py and the Func IntEnum in funcs.py are manually maintained. There is no automated check that:

  • Every multihash-tagged entry in the CSV is present
  • The codes match the CSV
  • The names match the CSV
  • No extra entries exist

go-multihash validates against the spec:

func TestSpec(t *testing.T) {
    file, _ := os.Open("spec/multicodec/table.csv")
    // ... reads CSV, filters for tag == "multihash"
    for code, name := range multihash.Codes {
        expectedName, ok := expectedFunctions[code]
        if !ok {
            t.Errorf("multihash %q (%x) not defined in the spec", name, code)
        }
    }
}
Proposed Solution
  1. Add the multicodec spec as a git submodule:

    git submodule add https://github.com/multiformats/multicodec spec/multicodec
    
  2. Create tests/test_spec.py:

    import csv
    from multihash.constants import HASH_CODES, CODE_HASHES
    
    def test_spec_table_completeness():
        """Every multihash entry in table.csv should be in HASH_CODES."""
        with open("spec/multicodec/table.csv") as f:
            reader = csv.DictReader(f, skipinitialspace=True)
            for row in reader:
                if row["tag"].strip() != "multihash":
                    continue
                name = row["name"].strip()
                code = int(row["code"].strip(), 16)
                assert name in HASH_CODES, f"Missing hash: {name} (0x{code:x})"
                assert HASH_CODES[name] == code, f"Code mismatch for {name}"
    
Related

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read constants.py and funcs.py, then inspect the proposed tests/test_spec.py approach and the multicodec table at spec/multicodec/table.csv. Run the existing test suite first; done means the test compares every multihash-tagged CSV entry with HASH_CODES and verifies names, codes, missing entries, and extras.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.