|
| 1 | +# WASM Adaptations Guide |
| 2 | + |
| 3 | +**Branch**: [`wasm-adapt`](https://github.com/utxostack/rust-lightning/branches?query=wasm-adapt) |
| 4 | + |
| 5 | +## Core Modifications |
| 6 | + |
| 7 | +The essence of Wasm adaptation, in this context, is to resolve the underlying implementation of the `now()` function for different environments. |
| 8 | + |
| 9 | +``` |
| 10 | +Get Time (`.now()`) |
| 11 | +│ |
| 12 | +│ |
| 13 | +├─► Test Environment (`#[cfg(test)]`) |
| 14 | +│ │ |
| 15 | +│ └─ Instant::now() ───── [Mock Clock] |
| 16 | +│ └─ Behavior: Returns a virtual time point controlled manually by test code. (Provided by LDK) |
| 17 | +│ |
| 18 | +│ |
| 19 | +└─► Production Environment (`#[cfg(not(test))]`) |
| 20 | + │ |
| 21 | + │ |
| 22 | + ├─► No-Standard-Library (`#[cfg(not(feature = "std"))]`) |
| 23 | + │ │ |
| 24 | + │ └─ SystemTime Equivalent ── [Internal Estimation] |
| 25 | + │ └─ Behavior: Uses a known timestamp as a fallback when no OS clock is available. (Provided by LDK) |
| 26 | + │ |
| 27 | + │ |
| 28 | + └─► With-Standard-Library (`#[cfg(feature = "std")]`) |
| 29 | + │ |
| 30 | + │ |
| 31 | + ├─► Native Environment (`#[cfg(not(target_arch = "wasm32"))]`) |
| 32 | + │ │ |
| 33 | + │ ├─ SystemTime::now() ── [std::time] |
| 34 | + │ │ └─ Underlying Mechanism: Direct OS API call (for wall-clock time). (Provided by LDK) |
| 35 | + │ │ |
| 36 | + │ └─ Instant::now() ───── [std::time] |
| 37 | + │ └─ Underlying Mechanism: Direct OS API call (for monotonic time). (Provided by LDK) |
| 38 | + │ |
| 39 | + │ |
| 40 | + └─► Browser Environment (`#[cfg(target_arch = "wasm32")]`) |
| 41 | + │ |
| 42 | + ├─ SystemTime::now() ── [web_time Crate] |
| 43 | + │ └─ Underlying Mechanism: Calls JavaScript `Date.now()`. (Requires `web_time` dependency) |
| 44 | + │ |
| 45 | + └─ Instant::now() ───── [web_time Crate] |
| 46 | + └─ Underlying Mechanism: Calls JavaScript `performance.now()`. (Requires `web_time` dependency) |
| 47 | +``` |
| 48 | + |
| 49 | +```diff |
| 50 | +- use std::time::{SystemTime, UNIX_EPOCH, Instant, Duration}; |
| 51 | ++ use lightning_common::{SystemTime, UNIX_EPOCH, Instant, Duration}; |
| 52 | +``` |
| 53 | + |
| 54 | +## Scope |
| 55 | + |
| 56 | +- Applies to all source files **except** test modules |
| 57 | +- Centralized in [`lightning-common`](./lightning-common/src/lib.rs) crate |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## Maintenance Workflow |
| 62 | + |
| 63 | +This workflow ensures that the Wasm adaptations are cleanly applied to new upstream versions of LDK, resulting in a single, consolidated patch. This is superior to git rebase when histories have diverged. |
| 64 | + |
| 65 | +Let's assume you are upgrading to a new upstream version, v0.1.4. |
| 66 | + |
| 67 | +### 1. Create a New Branch from Upstream |
| 68 | + |
| 69 | + Start from a clean slate by checking out the official upstream version tag. This ensures you're only building on top of the official LDK code. |
| 70 | + |
| 71 | + ```bash |
| 72 | + # Fetch the latest tags and branches from the official LDK repository |
| 73 | + git fetch upstream --tags |
| 74 | + |
| 75 | + # Create a new, versioned branch based on the target upstream tag (e.g., v0.1.4) |
| 76 | + git checkout -b wasm-adapt-v0.1.4 upstream/v0.1.4 |
| 77 | + ``` |
| 78 | + |
| 79 | +### 2. Apply the Core Adaptation Patch |
| 80 | + |
| 81 | + Use cherry-pick to apply your single, consolidated Wasm adaptation commit from your previous branch onto the new one. |
| 82 | + |
| 83 | + ```bash |
| 84 | + # First, find the commit hash of your Wasm adaptation patch from your old branch |
| 85 | + git log wasm-adapt |
| 86 | + |
| 87 | + # Cherry-pick that single commit. This will apply your core changes. |
| 88 | + git cherry-pick <your-single-wasm-adaptation-commit-hash> |
| 89 | + ``` |
| 90 | + |
| 91 | + Note: You may encounter merge conflicts during this step if upstream files have changed significantly. Resolve these conflicts as needed before proceeding. |
| 92 | + |
| 93 | +### 3. Audit and Update for New Code |
| 94 | + |
| 95 | + The cherry-pick only applies previous changes. You must now manually audit the codebase for any new usages of platform-dependent time APIs introduced in the upstream version. |
| 96 | + |
| 97 | + The primary targets for replacement are `SystemTime` and `Instant`. |
| 98 | + |
| 99 | + Rules for Replacement |
| 100 | + Review each search result and apply the following rules to determine if a replacement is necessary: |
| 101 | + |
| 102 | + ✅ YES, replace these types when found in production code: |
| 103 | + |
| 104 | + - `std::time::SystemTime` → `lightning_common::SystemTime` |
| 105 | + - Note: Any std::time::UNIX_EPOCH used in the same expression should be updated as a consequence. |
| 106 | + - `std::time::Instant` → `lightning_common::Instant` |
| 107 | + |
| 108 | + ❌ NO, ignore these cases: |
| 109 | + |
| 110 | + - `std::time::Duration`: This type is platform-agnostic (it comes from `core::time::Duration`) and should not be replaced. |
| 111 | + - Code within test modules: Any findings inside a `#[cfg(test)]` block or in files under a `tests/` directory should be ignored. The adaptation applies to production code only. |
| 112 | + |
| 113 | +### 4. Consolidate All Changes into One Commit |
| 114 | + |
| 115 | + This is the key step. After making your manual edits, "amend" the cherry-picked commit to fold your new changes into it. This keeps the history clean with a single, comprehensive adaptation commit. |
| 116 | + |
| 117 | + ```bash |
| 118 | + # Stage all the manual fixes you just made |
| 119 | + git add . |
| 120 | + |
| 121 | + # Amend the previous commit, adding your changes to it without changing the commit message |
| 122 | + git commit --amend --no-edit |
| 123 | + ``` |
| 124 | + |
| 125 | +### 5. Apply Documentation Changes |
| 126 | + |
| 127 | + Cherry-pick the commit containing this guide to ensure the documentation on your new branch is also up-to-date. This keeps the explanation of the process alongside the code it describes. |
| 128 | + |
| 129 | + ```bash |
| 130 | + # Find the commit hash for this documentation file on your previous branch |
| 131 | + git log wasm-adapt-v0.0.125 -- 'WASM-ADAPTATIONS.md' |
| 132 | + |
| 133 | + # Cherry-pick the documentation commit. It will appear as a new commit. |
| 134 | + git cherry-pick <documentation-commit-hash> |
| 135 | + ``` |
| 136 | + |
| 137 | +### 6. Tag the Final Version |
| 138 | + |
| 139 | + Once all code and documentation changes are finalized and committed, tag the branch's HEAD to mark a stable, distributable release point. |
| 140 | + |
| 141 | + ```bash |
| 142 | + # Tag the final commit with a corresponding wasm version |
| 143 | + git tag wasm-v0.1.4 |
| 144 | + ``` |
0 commit comments