Skip to content

Emit DatabaseChanges deterministically: sorted fields and ordered composite keys #14

Open

Description

@DenisCarriere

Tables::to_database_changes (5.0.0, and develop today) emits the same rows as different bytes from one WASM build to the next:

  • Fields. Row.columns is a std::collections::HashMap (src/tables.rs L386), and its iteration order becomes the order of TableChange.fields.
  • Composite keys. PrimaryKey::Composite is already a BTreeMap, but to_database_changes collects it into the generated CompositePrimaryKey.keys (L201). That is a buffa HashMap, whose foldhash state is seeded from stack and heap addresses on wasm32-unknown-unknown.

Table changes themselves are sorted by ordinal, so only the order inside a change moves. Sinks read columns and keys by name, so stored rows are unaffected. But a module's output digest changes whenever its WASM changes, for example after a toolchain bump.

We hit this in pinax-network/substreams-evm-extended (evm-balances db_out): a Rust 1.88 build and a 1.99 build give identical rows but different bytes. Comparing builds then needs a canonicalizing decoder instead of a digest.

Proposal

  1. Emit each change's fields in name order: make Row.columns a BTreeMap, or sort change.fields by name before pushing the change.
  2. Generate CompositePrimaryKey.keys as a BTreeMap, which encodes in key order.

(1) is a small change to tables.rs; we can send it as a PR. (2) changes the type of a public generated field (HashMap to BTreeMap), so it likely needs a major version.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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