-
Notifications
You must be signed in to change notification settings - Fork 7
unification of protocols #78
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| const { parentPort, workerData } = require('worker_threads'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| /** | ||
| * Worker thread for running offerbook sync operations. | ||
|
|
@@ -10,14 +12,10 @@ const { parentPort, workerData } = require('worker_threads'); | |
| const coinswapNapi = require('coinswap-napi'); | ||
| const { config } = workerData; | ||
|
|
||
| const protocol = config.protocol || 'v1'; | ||
| const TakerClass = | ||
| protocol === 'v2' ? coinswapNapi.TaprootTaker : coinswapNapi.Taker; | ||
| const TakerClass = coinswapNapi.Taker; | ||
|
|
||
| if (!TakerClass) { | ||
| throw new Error( | ||
| `${protocol === 'v2' ? 'TaprootTaker' : 'Taker'} class not found. Please rebuild coinswap-napi.` | ||
| ); | ||
| throw new Error('Taker class not found. Please rebuild coinswap-napi.'); | ||
| } | ||
|
|
||
| const taker = new TakerClass( | ||
|
|
@@ -30,9 +28,62 @@ const { parentPort, workerData } = require('worker_threads'); | |
| config.password || '' | ||
| ); | ||
|
|
||
| const offerbookPath = path.join(config.dataDir, 'offerbook.json'); | ||
| const initialMtime = fs.existsSync(offerbookPath) | ||
| ? fs.statSync(offerbookPath).mtimeMs | ||
| : 0; | ||
|
Comment on lines
+31
to
+34
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. Validate Line 31 builds Based on learnings: Sanitize file paths for wallet operations. 🤖 Prompt for AI Agents |
||
|
|
||
| taker.syncOfferbookAndWait(); | ||
|
|
||
| parentPort.postMessage({ type: 'completed' }); | ||
| // Keep the worker alive until the offerbook file has had a chance to be | ||
| // refreshed on disk. The unified backend can continue processing Nostr | ||
| // announcements briefly after syncOfferbookAndWait() returns. | ||
| const timeoutAt = Date.now() + 12000; | ||
| let sawUpdatedOfferbook = false; | ||
|
|
||
| while (Date.now() < timeoutAt) { | ||
| await new Promise((resolve) => setTimeout(resolve, 500)); | ||
|
|
||
| let stat; | ||
|
|
||
| try { | ||
| stat = fs.statSync(offerbookPath); | ||
| } catch (error) { | ||
| if (error.code === 'ENOENT') { | ||
| continue; | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
|
|
||
| if (!stat) { | ||
| continue; | ||
| } | ||
|
|
||
| if (stat.mtimeMs <= initialMtime) { | ||
| continue; | ||
| } | ||
|
|
||
| sawUpdatedOfferbook = true; | ||
|
|
||
| try { | ||
| const offerbook = JSON.parse(fs.readFileSync(offerbookPath, 'utf8')); | ||
| const makers = Array.isArray(offerbook.makers) ? offerbook.makers : []; | ||
|
|
||
| // Once the file is rewritten and we have maker entries, let the main | ||
| // process consume the refreshed offerbook immediately. | ||
| if (makers.length > 0) { | ||
| break; | ||
| } | ||
| } catch (error) { | ||
| // File may be mid-write; keep polling briefly. | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| parentPort.postMessage({ | ||
| type: 'completed', | ||
| offerbookUpdated: sawUpdatedOfferbook, | ||
| }); | ||
| } catch (err) { | ||
| parentPort.postMessage({ type: 'error', error: err.message }); | ||
| } | ||
|
|
||
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.
Put a deadline around the extra offerbook sync step.
api1.jsalready waits for a synced offerbook before spawning this worker, but this secondsyncOfferbookAndWait()has no timeout or watchdog. If Tor/Nostr stalls here, the worker never emitsprepared/error, so the swap stays stuck in startup indefinitely. Based on learnings: Timeout handling in swap protocols; Handle network failures during swaps.🤖 Prompt for AI Agents