You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Emit DatabaseChanges deterministically: sorted fields and ordered composite keys #14
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-balancesdb_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
Emit each change's fields in name order: make Row.columns a BTreeMap, or sort change.fields by name before pushing the change.
Generate CompositePrimaryKey.keys as a BTreeMap, which encodes in key order.
buffa supports this through buffa_build's map_type_in(MapRepr::BTreeMap, ...).
Until it does, the alternative is generating this crate with buffa_build.
(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