forked from hyphanet/fred
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor(support-math): Decouple support math from node APIs #1158
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
Merged
Changes from all commits
Commits
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
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
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
90 changes: 90 additions & 0 deletions
90
src/main/java/network/crypta/support/math/KeyspaceMath.java
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package network.crypta.support.math; | ||
|
|
||
| /** | ||
| * Performs wrap-aware arithmetic on Cryptad's normalized unit keyspace. | ||
| * | ||
| * <p>This helper centralizes the circular math shared by support-side averages and node-side | ||
| * location utilities such as {@code network.crypta.node.Location}. Callers use it when they already | ||
| * enforce their own validity checks and need the historical wrap behavior at the {@code 0.0}/{@code | ||
| * 1.0} boundary. The methods are pure, allocate no state, and keep the long-standing convention | ||
| * that diametrically opposite points produce a positive half-turn. | ||
| * | ||
| * <p>The class is intentionally narrow: | ||
| * | ||
| * <ul> | ||
| * <li>{@link #normalize(double)} maps arbitrary doubles into the canonical storage range. | ||
| * <li>{@link #change(double, double)} returns the signed shortest path across the ring. | ||
| * <li>{@link #distance(double, double)} derives the absolute arc length from the same rule set. | ||
| * </ul> | ||
| * | ||
| * <p>By keeping these semantics in one place, support-layer code such as {@code | ||
| * DecayingKeyspaceAverage} can use the same behavior as node-layer location code without depending | ||
| * on node-specific APIs. | ||
| */ | ||
| public final class KeyspaceMath { | ||
|
|
||
| /** Prevents instantiation of this stateless helper class. */ | ||
| private KeyspaceMath() {} | ||
|
|
||
| /** | ||
| * Returns the shortest signed delta from one normalized position to another. | ||
| * | ||
| * <p>Use this when a caller needs to move from a current keyspace position toward a target, and | ||
| * the shortest route may cross the wrap boundary. The result matches Cryptad's historical | ||
| * location semantics: values greater than {@code 0.5} wrap backward by {@code 1.0}, values at or | ||
| * below {@code -0.5} wrap forward by {@code 1.0}, and exactly opposite points produce {@code | ||
| * +0.5}. The method does not validate inputs, so callers should normalize or validate first when | ||
| * range guarantees matter. | ||
| * | ||
| * @param from normalized starting position supplied by the caller. | ||
| * @param to normalized destination position supplied by the caller. | ||
| * @return a signed shortest-path delta on the unit keyspace ring. | ||
| */ | ||
| public static double change(double from, double to) { | ||
| double change = to - from; | ||
| if (change > 0.5) { | ||
| return change - 1.0; | ||
| } | ||
| if (change <= -0.5) { | ||
| return change + 1.0; | ||
| } | ||
| return change; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the shortest absolute arc length between two normalized positions. | ||
| * | ||
| * <p>This is a convenience wrapper around {@link #change(double, double)} for callers that need | ||
| * only the scale of the shortest route. The computation remains symmetric for any ordered pair | ||
| * because it derives from the same signed wrap rules. Like the other helpers in this class, it | ||
| * assumes the caller has already decided whether the inputs are valid keyspace coordinates. | ||
| * | ||
| * @param a first normalized keyspace position to compare. | ||
| * @param b second normalized keyspace position to compare. | ||
| * @return shortest absolute distance measured on the unit ring. | ||
| */ | ||
| public static double distance(double a, double b) { | ||
| return Math.abs(change(a, b)); | ||
| } | ||
|
|
||
| /** | ||
| * Maps an arbitrary double into Cryptad's canonical keyspace range. | ||
| * | ||
| * <p>The returned value is always in {@code [0.0, 1.0)}, which makes it suitable for storage, | ||
| * comparison, and further calls to {@link #change(double, double)} or {@link #distance(double, | ||
| * double)}. Multiples of {@code 1.0} collapse onto the same position, so {@code 1.0} becomes | ||
| * {@code 0.0}. The implementation intentionally follows Java remainder rules and then adjusts | ||
| * negative results, preserving the behavior historically exposed through the node-side {@code | ||
| * Location.normalize(double)} helper. | ||
| * | ||
| * @param rough arbitrary keyspace value that may need wrapping. | ||
| * @return equivalent normalized position in the canonical unit range. | ||
| */ | ||
| public static double normalize(double rough) { | ||
| double normal = rough % 1.0; | ||
| if (normal < 0) { | ||
| return 1.0 + normal; | ||
| } | ||
| return normal; | ||
| } | ||
| } |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/network/crypta/support/math/TimeSkewAlertCallback.java
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package network.crypta.support.math; | ||
|
|
||
| /** | ||
| * Receives notifications when support-side time checks detect a local clock skew. | ||
| * | ||
| * <p>This interface defines the narrow contract that support-layer components use when they need to | ||
| * surface a user-visible time-skew condition without depending on node-specific alerting code. In | ||
| * practice, callers such as {@code TimeDecayingRunningAverage} invoke it after detecting wall-clock | ||
| * or uptime regressions while continuing their monotonic-time calculations. That keeps timing math | ||
| * isolated from presentation concerns while still giving higher layers a clear place to register | ||
| * operator-facing alerts. | ||
| * | ||
| * <p>Implementations may map the notification to a UI banner, a persistent user alert, or another | ||
| * operator-facing mechanism. Callers may invoke the callback from non-UI threads and may notify | ||
| * more than once if skew is detected repeatedly, so implementations should return quickly and | ||
| * perform any deduplication or thread marshaling they require. | ||
| */ | ||
| public interface TimeSkewAlertCallback { | ||
|
|
||
| /** | ||
| * Requests an operator-visible alert for a detected local time-skew condition. | ||
| * | ||
| * <p>Callers use this after they have already determined that the local wall clock moved backward | ||
| * or otherwise became inconsistent with elapsed monotonic time. The callback is | ||
| * notification-only: it does not influence the caller's timing calculations, and it does not | ||
| * imply that the condition has cleared. Implementations may schedule UI work or alert | ||
| * registration, but they should keep the call itself lightweight so reporting paths remain | ||
| * responsive. | ||
| */ | ||
| void setTimeSkewDetectedUserAlert(); | ||
| } |
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
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.