Skip to content

Phase 1: wallet state digest (ECMH over ristretto255), logged every 1000 blocks - #14

Merged
EnzinoBB merged 2 commits into
masterfrom
feat/state-digest
Oct 9, 2026
Merged

EnzinoBB merged 2 commits into
masterfrom
feat/state-digest

Conversation

@EnzinoBB

@EnzinoBB EnzinoBB commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Phase 1 of the state-root plan (agreed with BK)

An order-independent digest of the wallet state that every node computes and logs. It travels outside the block: no change to the protocol, the consensus rules or the storage format, and the node version is unchanged. Its purpose is to see balance divergence, like the one that left nodes stuck at block 176'776'210, instead of finding out when a node stops.

Commit 1: the primitive

  • cs::MultisetHash: ECMH over ristretto255 (libsodium, already linked through cscrypto). Each item is BLAKE2b-512 mapped to the group with crypto_core_ristretto255_from_hash, and the digest is the group sum. Adding or removing an item costs O(1), about 50 us regardless of state size, and the result does not depend on order.
  • cs::serializeWalletState(): canonical, versioned serialisation of key, balance, delegated amount and delegations in both directions. Delegations are sorted, because their order depends on apply/revert history. A wallet without state serialises to nothing and does not move the digest.

Commit 2: the integration

  • Updates: in MultiWallets::onWalletCacheUpdated(), the single place where records are inserted or replaced, each update removes the wallet's previous element and adds the new one. The full computation happens once, at start after the chain is loaded, so slow start does not pay a per-update cost.
  • Stored element: each wallet's element is kept in its record. The delegation maps are shared_ptrs that Staking updates in place, so re-serialising the old record would remove the wrong element.
  • Mirror type: WalletsCache_Serializer aliases the container through a mirror WalletData. The mirror gets the same fields, not serialised, so the checkpoint format is unchanged, and a static_assert on the sizes turns any future mismatch into a compile error.
  • Log: STATE DIGEST #<seq> <hex> wallets <n> at start and every 1'000 blocks.

How to use it

Healthy nodes at the same sequence print the same digest. A different digest points to a divergent node and, between two log lines, to the block range where the divergence started. It is also the data needed to choose the majority state before Phase 2.

Verification and limits

  • nodetests 132/132 (6 new tests); the node binary builds.
  • Not verified on mainnet data, since there is no DB here: the first log lines from operators will be the proof.
  • The full computation at start costs about 50 s per million wallets, once.
  • Pre-existing issue flagged, not touched: the mirror container has one more index than the real one.

Possible next steps

Expose the digest through the API and show it per node in BK's dashboard; add a digest of the contract state.

🤖 Generated with Claude Code

EnzinoBB and others added 2 commits October 9, 2026 08:22
…lisation

Phase 1 of the state-root plan agreed with BK: an order-independent digest of
the wallet state that every node can compute and compare, out of block, so
nothing changes in the protocol.

- cs::MultisetHash: elliptic-curve multiset hash over ristretto255
  (libsodium, already linked through cscrypto). An item is mapped to a group
  element via BLAKE2b-512 and crypto_core_ristretto255_from_hash. The digest
  is the group sum, starting from the identity (32 zero bytes). Adding or
  removing an item costs O(1), about 50 us, independent of state size, and
  the result does not depend on insertion order. ristretto255 is used instead
  of raw ed25519 to avoid cofactor issues.
- cs::serializeWalletState(): canonical, versioned serialisation of the
  balance-relevant state of a wallet: key, balance, delegated amount, and
  delegations in both directions. Counterparts are sorted by key (std::map);
  entries are sorted because their order depends on apply/revert history.
  Empty for a wallet without such state, so empty records never move the
  digest. Little-endian, fixed-width fields.

Tests (tests/csnode/statedigest_tests.cpp): order independence, add/remove
inverse, distinct sets, empty wallet, balance and key sensitivity, delegation
order independence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… blocks

Wire the ECMH digest into MultiWallets, the single place where wallet records
are inserted or replaced (onWalletCacheUpdated).

- The digest is computed in full on the first call to stateDigest(). Node
  calls it right after the blockchain is loaded (slow or quick start), before
  taking part in the network. From then on, each wallet update removes the
  wallet's previous element and adds the new one. Slow-start replay does not
  pay a per-update cost.
- The element of each wallet is stored in its record (stateElement_). The
  delegation maps are shared_ptrs that Staking updates in place before the
  record is replaced, so re-serialising the old record would remove the
  wrong element. The element is not persisted; it is rebuilt at start.
- WalletsCache_Serializer aliases the MultiWallets container through a
  mirror WalletData type, so the mirror gets the same two fields (not
  serialised: the checkpoint format is unchanged). A static_assert on the two
  sizes now catches any future mismatch at compile time.
- Node logs "STATE DIGEST #<seq> <hex> wallets <n>" at start and on every
  block whose sequence is a multiple of 1000. Healthy nodes print the same
  digest for the same sequence; a different one pinpoints a divergent node
  and, between two log points, the block range where it diverged.

No protocol, consensus or storage format change. nodetests: 132/132 pass;
the node binary builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EnzinoBB
EnzinoBB merged commit 60969da into master Oct 9, 2026
1 check passed
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.

1 participant