Skip to content

Rewrite index entries earlier versions laid out otherwise, then drop the stored-layout reads (PV15+) #5196

Open

Description

@QuantumExplorer

Basic explanation

#5173 (protocol version 14) makes two things depend on an index's own values only: the shape of a unique index's entry, and whether a nullSearchable: false index writes one. Entries written before version 14 can be in another shape, left by two earlier writers:

  • the index-level insert walkers carried the null flags from one sibling sub-level into the next;
  • the replace walker (update 0) laid out a unique index with some values missing as the bare reference, and never skipped a nullSearchable: false entry.

#5173 lets the delete walker and the replace walker (update 1) read how such an entry is stored and act on that shape. Readers that find an entry by its values, such as the uniqueness check and document queries through the index, still expect the shape the rule gives.

This issue is the cleanup for a later protocol version (15 or later):

  1. rewrite every such entry once, in the first block of that version, to the shape the rule gives;
  2. then drop the reads fix(drive)!: index null flags follow each index's own path (PV14) #5173 added, in a new generation of the walkers.

What to rewrite

In each pair, the left side is what an earlier version stored and the right side is the entry after the rewrite. [0] at the document type is primary storage, <id> the document, <owner> its owner, and "" the null key a missing value takes.

A unique index whose own values are all present, stored as a [0] tree (listing { "slug": "my-bike" } with byOwnerCategory [$ownerId, category] beside the unique byOwnerSlug [$ownerId, slug]):

stored before protocol version 14               after the rewrite

listing/                                        listing/
└── $ownerId                                    └── $ownerId
    └── <owner>                                     └── <owner>
        ├── category                                    ├── category
        │   └── "" (null)                               │   └── "" (null)
        │       └── [0]                                 │       └── [0]
        │           └── <id>: reference                 │           └── <id>: reference
        └── slug                                        └── slug
            └── "my-bike"                                   └── "my-bike"
                └── [0]              ◄── a tree                 └── [0]: reference
                    └── <id>: reference

A unique index with a value missing, stored as the bare reference (badge { "code": "a" } after a replace dropped variant, unique byOwnerCodeVariant [$ownerId, code, variant]):

stored before protocol version 14               after the rewrite

badge/                                          badge/
└── $ownerId                                    └── $ownerId
    └── <owner>                                     └── <owner>
        └── code                                        └── code
            └── "a"                                         └── "a"
                └── variant                                     └── variant
                    └── "" (null)                                   └── "" (null)
                        └── [0]: reference  ◄── bare                    └── [0]
                                                                            └── <id>: reference

A nullSearchable: false index holding an entry for a document missing all of its values (profile { "name": "ada" } with byCityName [city, name] beside byCityZip [city, zip], nullSearchable: false):

stored before protocol version 14               after the rewrite

profile/                                        profile/
└── city                                        └── city
    └── "" (null)                                   └── "" (null)
        ├── name                                        ├── name
        │   └── "ada"                                   │   └── "ada"
        │       └── [0]                                 │       └── [0]
        │           └── <id>: reference                 │           └── <id>: reference
        └── zip                                         └── zip
            └── "" (null)                                   └── "" (null)   ◄── no entry
                └── [0]      ◄── an entry
                    └── <id>: reference

How

  • Where: a transition_to_version_N step in perform_events_on_first_block_of_protocol_change, which already runs the one-time migrations of versions 4 to 14.
  • Finding candidates: a static check of every contract's document types, then a walk of the matching indexes only:
    • a unique index sharing its path with a sibling index whose property sorts earlier and can be missing (an optional property, or a system timestamp the type does not require);
    • a unique index with an optional property, on a type whose documents could be replaced before version 14;
    • a nullSearchable: false index, on a type with a sibling index under the same path or whose documents could be replaced before version 14.
  • Per entry: rewrite the element to the rule's shape as in the diagrams, keeping the same reference and its storage flags.
  • To get right:
    • count and sum aggregates above a rewritten entry come out unchanged;
    • a later delete still refunds the right owner and epoch;
    • who gets the bytes a rewrite frees (the owner, or the system);
    • the work stays deterministic and bounded within one block.
  • Before writing it: rerun the census of these shapes on each network. When fix(drive)!: index null flags follow each index's own path (PV14) #5173 was written, no mainnet contract had one.

After the rewrite

Every network that reaches the version after the rewrite has run it. A new generation of the index-level delete walker and of update, selected from then on, can drop:

  • the stored-layout reads (drive::document::stored_index_entry);
  • the legacy_any_fields_null parameter of the delete walker;
  • update's old_sibling_value_missing scan.

Related

The value trees above a skipped nullSearchable: false entry are written on insert and, with no entry below them, not pruned on delete. Fixing that changes what inserts write, so it needs its own change. The same rewrite could prune the empty trees already written.

Activity

  1. added this to the v4.3.0 milestone on Sep 29, 2026
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

    consensus changeenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions