refactor(policy): migrate policies[id] slot to Solidity packed struct (bit-identical)#77
Draft
amiecorso wants to merge 1 commit into
Draft
refactor(policy): migrate policies[id] slot to Solidity packed struct (bit-identical)#77amiecorso wants to merge 1 commit into
amiecorso wants to merge 1 commit into
Conversation
1840648 to
b56d6c1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Continues the structural-slot-packing work from #75. The lane-based
slots (
transferPolicyIds,mintPolicyIds,redeemPolicyIds) nowuse Solidity packed structs; this PR does the same for the
PolicyRegistry'spolicies[id]slot — bit-identical to theexisting layout (no Rust coordination required).
Per Conner's Slack thread.
Layout is frozen — zero Rust change required
Unlike the lane slots (where Solidity's natural LSB-first packing
matched the hand-rolled convention), the policy slot has its
existsflag at bit 255 with a 95-bit gap above admin. A clean
struct { address admin; bool exists; }would have Solidity placeexistsat bit 160 (right above admin), changing the layout.To preserve the bit-255 location, the struct uses Conner's first
sketch shape:
This is bit-identical to the prior hand-rolled
uint256layout —verified by the existing layout-pin tests in
PolicyRegistryFullLayout.t.solpassing unchanged against themigrated storage. Rust side stays as-is.
The footgun and how it's hidden
The
existsBytefield is a single byte but only its high bit(= slot bit 255) is the existence signal. Writing
existsByte = 1puts the bit at slot position 248 instead of 255 — wrong location.
Hidden behind two library helpers, which are the only sanctioned
API for the existence flag:
newPolicy(address admin)— always writesexistsByte = 0x80(= slot bit 255). All construction goes through this.
existsSet(PolicyPacked memory packed)— returnspacked.existsByte != 0. Tolerant of any non-zero byte value soa buggy writer that lands the bit at the wrong position still
triggers existence semantics; the layout-pin tests catch the
wrong bit position separately.
Consumer code never touches
existsBytedirectly:_encodeand_decodeAdminhelpers inMockPolicyRegistryremoved—
newPolicy(a)andpacked.admincover both paths.Intentional non-goal: "polymorphic" policy struct
Conner's Slack message also raised future UNION/INTERSECT composite
policy types and asked whether the struct should be "somewhat
polymorphic". Documented in the storage library NatSpec as an
explicit non-goal:
Tests
465 passed / 0 failed. Tests stay valid because the binary layout is
bit-identical to the hand-rolled version. The codec helpers
(
packPolicy,policyAdminFromPacked,policyExistsFromPacked)retained — they operate on raw `uint256` slot reads from `vm.load`
and now match what Solidity emits for the struct.