Add PolicyRegistry implementation#7
Draft
eric-ships wants to merge 11 commits into
Draft
Conversation
Implements IPolicyRegistry with a single-slot storage layout optimized for lookup efficiency. Every policy lookup (simple or compound) resolves in at most 2 SLOADs. Also adds a fourth slot to CompoundPolicyData for redeem policy specification, with matching updates to the interface signatures and a new isAuthorizedRedeemer query.
…rename _loadCustom
PolicyNotSimple -> ConstituentIsCompound, _createSimple -> _createPolicy, _requireSimpleConstituent -> _requireConstituent. Updates interface NatSpec and comments throughout to use WHITELIST/BLACKLIST/COMPOUND directly.
…tdoc, simplify _checkRole
policyData() now returns the correct type for built-in IDs rather than the misleading WHITELIST placeholder.
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.
Motivation
This adds an implementation of
PolicyRegistry.soland the small interface updateneeded to support it, covering policy creation, administration, and authorization for
the WHITELIST, BLACKLIST, and COMPOUND types (TIP-403 and TIP-1015 parity, plus a
fourth compound slot for redeem policy specification).
What changed
IPolicyRegistry: addedredeemerPolicyIdas a fourth slot onCompoundPolicyData,updated
createCompoundPolicyandcompoundPolicyDatasignatures to match, addedthe
isAuthorizedRedeemerquery, and updatedCompoundPolicyCreatedevent.PolicyRegistry.sol: implementation covering policy creation, administration,and authorization queries for WHITELIST, BLACKLIST, and COMPOUND policy types.
What was tried / considered
Single-slot storage layout. The central design constraint was: a compound policy
lookup should cost at most 2 SLOADs, regardless of which role is being checked. To hit
that, all four constituent IDs and the type discriminator need to fit in one slot.
The naive layout would use two separate mappings (one for simple policies, one for
compound) and require reading the type first then branching to the right mapping, which
is either 1 or 2 SLOADs depending on whether you get lucky. Instead, both policy types
are packed into a single
mapping(uint64 => uint256), with the low 8 bits alwaysholding the type tag and the remaining bits varying by type:
[type(8b) | admin(160b)], fits with 88 bits to spare[type(8b) | sender(62b) | recipient(62b) | mintRecipient(62b) | redeemer(62b)],exactly 256 bits
Using 62-bit rather than 64-bit child policy IDs is what makes compound fit. Four 64-bit
IDs plus the 8-bit type tag would be 264 bits (8 over budget), so 2 bits are trimmed
from each ID. The interface still uses
uint64throughout; the truncation only happensin packed storage, and 2^62 policies is not a realistic ceiling.
Unified policy ID namespace. All policy IDs, whether simple or constituent slots
inside a compound policy, are drawn from the same counter. A 62-bit ID supports the
same range regardless of where it appears.
No recursion cap needed. Compound constituents are validated at creation time to be
non-COMPOUND, so the evaluation graph is at most one level deep by construction. No
cycle detection or depth counter is needed at query time.
isAuthorizedsemantics unchanged. The existingisAuthorized(policyId, user)remains
isAuthorizedSender && isAuthorizedRecipient. Redeem checks use the newisAuthorizedRedeemerexplicitly; there is no combined "all roles" helper.