Skip to content

Encode scope in secondary keys for cross-scope isolation#47

Merged
heifner merged 3 commits into
masterfrom
feature/kv-idx-scope-isolation
Apr 7, 2026
Merged

Encode scope in secondary keys for cross-scope isolation#47
heifner merged 3 commits into
masterfrom
feature/kv-idx-scope-isolation

Conversation

@heifner
Copy link
Copy Markdown
Contributor

@heifner heifner commented Apr 4, 2026

Summary

  • Bug: kv_idx_find_secondary and kv_idx_lower_bound have no scope parameter, so secondary index iteration returned entries from ALL scopes within a contract, breaking multi_index scope isolation.
  • Fix: kv_multi_index now prepends [scope:8B BE] to secondary keys and stores pri_key as [pk:8B] (was [scope:8B][pk:8B]). Scope isolation is enforced at the key encoding level.
  • Fast-path encode_scoped_secondary overloads for uint64_t, uint128_t, and double
  • operator-- end-sentinel sizes max_sec buffer from sizeof(secondary_key_type) with static_assert guard
  • Updated intrinsics reference and storage guide docs

Depends on: Wire-Network/wire-sysio#284 (chain-side changes)

Secondary index iteration via kv_idx_* intrinsics was leaking entries
across scopes because sec_key had no scope discriminator. Fix by
prepending [scope:8B BE] to secondary keys and shrinking pri_key from
[scope:8B][pk:8B] to [pk:8B] (scope is now in sec_key).

Changes:
- encode_scoped_secondary: prepend scope to all secondary key types
- Fast-path overloads for uint64_t, uint128_t, and double
- operator-- end-sentinel: size max_sec buffer from sizeof(secondary_key_type)
- static_assert that sizeof matches pack_size for secondary types
- check_scope validates scope prefix on every iterator advance
- Update docs (intrinsics reference, storage guide)
- Add cross-scope isolation tests (CDT unit + integration)
heifner added 2 commits April 4, 2026 17:15
…_view

Make datastream<size_t> constexpr-friendly: add constexpr to constructor,
skip, write, tellp. Make primitive operator<< and pack_size constexpr,
using if-constexpr to avoid reinterpret_cast in the size-counting path.

Replace the pack_size-based static_assert with std::is_trivially_copyable
which is constexpr for all secondary key types including checksum256.
The mi_scope_tests contract's row struct lacked the [[sysio::table]]
attribute, so CDT's ABI generator emitted the scopetbl table entry
without the corresponding row type definition. This caused set_abi
to fail with "invalid_type_inside_abi: row" in integration tests.
@heifner heifner merged commit 653fc8d into master Apr 7, 2026
4 checks passed
@heifner heifner deleted the feature/kv-idx-scope-isolation branch April 7, 2026 22:47
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.

2 participants