-
Notifications
You must be signed in to change notification settings - Fork 826
Simplify predicate handling #4175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StephenButtolph
merged 6 commits into
uplift-evm-predicate
from
simplify-predicate-handling
Aug 15, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,78 +11,86 @@ import ( | |
| "github.com/ava-labs/libevm/core/types" | ||
| ) | ||
|
|
||
| // delimiter is used as a delimiter for the bytes packed into a precompile predicate. | ||
| // Precompile predicates are encoded in the Access List of transactions in the access tuples | ||
| // which means that its length must be a multiple of 32 (common.HashLength). | ||
| // While the delimiter is always included, for messages that are not a multiple of 32, | ||
| // this delimiter is used to append/remove padding. | ||
| // delimiter separates the actual predicate bytes from the padded zero bytes. | ||
| // | ||
| // Predicates are encoded in the Access List of transactions by using in the | ||
| // access tuples. This means that the length must be a multiple of | ||
| // [common.HashLength]. | ||
| // | ||
| // Even if the original predicate bytes is a multiple of [common.HashLength], | ||
| // the delimiter must be appended to support decoding. | ||
| const delimiter = 0xff | ||
|
|
||
| var ( | ||
| errEmptyPredicate = errors.New("predicate specified empty predicate") | ||
| errAllZeroBytes = errors.New("predicate specified all zero bytes") | ||
| errExcessPadding = errors.New("predicate specified excess padding") | ||
| errWrongEndDelimiter = errors.New("wrong end delimiter") | ||
| errMissingDelimiter = errors.New("no delimiter found") | ||
| errExcessPadding = errors.New("predicate included excess padding") | ||
| errWrongDelimiter = errors.New("wrong delimiter") | ||
| ) | ||
|
|
||
| type Predicates interface { | ||
| HasPredicate(address common.Address) bool | ||
| } | ||
| // Predicate is a message padded with the delimiter and zeros and chunked into | ||
| // 32-byte chunks. | ||
| type Predicate []common.Hash | ||
|
|
||
| // FromAccessList extracts predicates from a transaction's access list. | ||
| // If an address is specified multiple times in the access list, each set of storage | ||
| // keys for that address is appended to a slice. Each entry represents a predicate | ||
| // encoded as a slice of common.Hash values (32-byte chunks) exactly as stored in | ||
| // the access list. | ||
| func FromAccessList(rules Predicates, list types.AccessList) map[common.Address][][]common.Hash { | ||
| predicateStorageSlots := make(map[common.Address][][]common.Hash) | ||
| for _, el := range list { | ||
| if !rules.HasPredicate(el.Address) { | ||
| continue | ||
| } | ||
| // Append a copy of the storage keys to avoid aliasing | ||
| keys := make([]common.Hash, len(el.StorageKeys)) | ||
| copy(keys, el.StorageKeys) | ||
| predicateStorageSlots[el.Address] = append(predicateStorageSlots[el.Address], keys) | ||
| // New constructs a predicate from raw predicate bytes. | ||
| // | ||
| // It chunks the predicate by appending [predicate.Delimiter] and zero-padding | ||
| // to a multiple of 32 bytes. | ||
| func New(b []byte) Predicate { | ||
| numUnpaddedChunks := len(b) / common.HashLength | ||
| chunks := make([]common.Hash, numUnpaddedChunks+1) | ||
| // Copy over chunks that don't require padding. | ||
| for i := range chunks[:numUnpaddedChunks] { | ||
| chunks[i] = common.Hash(b[common.HashLength*i:]) | ||
| } | ||
|
|
||
| return predicateStorageSlots | ||
| } | ||
|
|
||
| func RoundUpTo32(x int) int { | ||
| return (x + 31) / 32 * 32 | ||
| } | ||
|
|
||
| // pack returns a 32-byte aligned predicate from b by appending a delimiter and zero-padding | ||
| // until the length is a multiple of 32. 32-byte aligned predicates should not be exposed | ||
| // outside of this package. | ||
| func pack(b []byte) []byte { | ||
| bytes := make([]byte, len(b)+1) | ||
| copy(bytes, b) | ||
| bytes[len(b)] = delimiter | ||
|
|
||
| return common.RightPadBytes(bytes, RoundUpTo32(len(bytes))) | ||
| // Add the delimiter and required padding to the last chunk. | ||
| copy(chunks[numUnpaddedChunks][:], b[common.HashLength*numUnpaddedChunks:]) | ||
| chunks[numUnpaddedChunks][len(b)%common.HashLength] = delimiter | ||
| return chunks | ||
| } | ||
|
|
||
| // Unpack unpacks a predicate by stripping right padded zeroes, checking for the delimiter, | ||
| // ensuring there is not excess padding, and returning the original message. | ||
| // Bytes converts the chunked predicate into the original message. | ||
| // | ||
| // Returns an error if it finds an incorrect encoding. | ||
| func Unpack(p []byte) ([]byte, error) { | ||
StephenButtolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if len(p) == 0 { | ||
| return nil, errEmptyPredicate | ||
| func (p Predicate) Bytes() ([]byte, error) { | ||
| padded := make([]byte, common.HashLength*len(p)) | ||
StephenButtolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for i, chunk := range p { | ||
| copy(padded[common.HashLength*i:], chunk[:]) | ||
| } | ||
| trim := common.TrimRightZeroes(p) | ||
| if len(trim) == 0 { | ||
| return nil, fmt.Errorf("%w: length (%d)", errAllZeroBytes, len(p)) | ||
| trimmed := common.TrimRightZeroes(padded) | ||
| if len(trimmed) == 0 { | ||
| return nil, fmt.Errorf("%w: length (%d)", errMissingDelimiter, len(p)) | ||
| } | ||
|
|
||
| if paddedLength := RoundUpTo32(len(trim)); paddedLength != len(p) { | ||
| return nil, fmt.Errorf("%w: got length (%d), expected length (%d)", errExcessPadding, len(p), paddedLength) | ||
| expectedLen := (len(trimmed) + common.HashLength - 1) / common.HashLength | ||
| if expectedLen != len(p) { | ||
| return nil, fmt.Errorf("%w: got length (%d), expected length (%d)", errExcessPadding, len(p), expectedLen) | ||
| } | ||
|
|
||
| if trim[len(trim)-1] != delimiter { | ||
| return nil, errWrongEndDelimiter | ||
| delimiterIndex := len(trimmed) - 1 | ||
| if trimmed[delimiterIndex] != delimiter { | ||
| return nil, errWrongDelimiter | ||
| } | ||
|
|
||
| return trimmed[:delimiterIndex], nil | ||
| } | ||
|
|
||
| type Predicates interface { | ||
| HasPredicate(address common.Address) bool | ||
| } | ||
|
|
||
| // FromAccessList extracts predicates from a transaction's access list. | ||
| // | ||
| // If an address is specified multiple times in the access list, each set of | ||
| // storage keys for that address is considered an individual predicate. | ||
| func FromAccessList(rules Predicates, list types.AccessList) map[common.Address][]Predicate { | ||
| predicates := make(map[common.Address][]Predicate) | ||
| for _, el := range list { | ||
| if !rules.HasPredicate(el.Address) { | ||
| continue | ||
| } | ||
| predicates[el.Address] = append(predicates[el.Address], el.StorageKeys) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the copy here... If we want to keep the copy to avoid mutating the predicates if |
||
| } | ||
|
|
||
| return trim[:len(trim)-1], nil | ||
| return predicates | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.