-
Notifications
You must be signed in to change notification settings - Fork 392
change(rc): Update Remote Config condition evaluation hashing #2760
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
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c9b42e2
move from farmhash to sha256
77a8707
lint
3d04abb
add more special char test cases
ae2fabe
another special char case
c3ba59f
further refine test cases
9cc3820
clean up tests
9a8570a
Merge branch 'master' into ssrc-hash
kjelko f8dd7f5
Trigger CIs
c6c1e6d
fix newline
83b438c
Merge branch 'master' into ssrc-hash
kjelko 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 |
---|---|---|
|
@@ -27,7 +27,8 @@ import { | |
CustomSignalCondition, | ||
CustomSignalOperator, | ||
} from './remote-config-api'; | ||
import * as farmhash from 'farmhash-modern'; | ||
import { createHash } from 'crypto'; | ||
|
||
|
||
/** | ||
* Encapsulates condition evaluation logic to simplify organization and | ||
|
@@ -151,9 +152,8 @@ export class ConditionEvaluator { | |
const seedPrefix = seed && seed.length > 0 ? `${seed}.` : ''; | ||
const stringToHash = `${seedPrefix}${context.randomizationId}`; | ||
|
||
const hash64 = ConditionEvaluator.hashSeededRandomizationId(stringToHash) | ||
|
||
const instanceMicroPercentile = hash64 % BigInt(100 * 1_000_000); | ||
const hash = ConditionEvaluator.hashSeededRandomizationId(stringToHash) | ||
const instanceMicroPercentile = hash % BigInt(100 * 1_000_000); | ||
|
||
switch (percentOperator) { | ||
case PercentConditionOperator.LESS_OR_EQUAL: | ||
|
@@ -173,18 +173,8 @@ export class ConditionEvaluator { | |
} | ||
|
||
static hashSeededRandomizationId(seededRandomizationId: string): bigint { | ||
// For consistency with the Remote Config fetch endpoint's percent condition behavior | ||
// we use Farmhash's fingerprint64 algorithm and interpret the resulting unsigned value | ||
// as a signed value. | ||
let hash64 = BigInt.asIntN(64, farmhash.fingerprint64(seededRandomizationId)); | ||
|
||
// Manually negate the hash if its value is less than 0, since Math.abs doesn't | ||
// support BigInt. | ||
if (hash64 < 0) { | ||
hash64 = -hash64; | ||
} | ||
|
||
return hash64; | ||
const hex = createHash('sha256').update(seededRandomizationId).digest('hex'); | ||
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. Thinking (no action req'd): we had a 64 bits hash before, so we don't need 256 bits, but sha256 is broadly available and there's no noticeable performance difference, so sha256 is fine. |
||
return BigInt(`0x${hex}`); | ||
} | ||
|
||
private evaluateCustomSignalCondition( | ||
|
@@ -332,4 +322,4 @@ function compareSemanticVersions( | |
if (version1[i] > version2[i]) return predicateFn(1); | ||
} | ||
return false; | ||
} | ||
} | ||
kjelko marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking (no action req'd): we're now directly instantiating the bigint with the hash, rather than coercing it to behave like a 64bit signed Java long, so it will always be positive.