') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); Add deterministic random bytes in `override_random_bytes` by moisesPompilio · Pull Request #3647 · lightningdevkit/rust-lightning · GitHub
Skip to content

Add deterministic random bytes in override_random_bytes - #3647

Merged
TheBlueMatt merged 3 commits into
lightningdevkit:mainfrom
moisesPompilio:2827_issue
Mar 25, 2025
Merged

Add deterministic random bytes in override_random_bytes#3647
TheBlueMatt merged 3 commits into
lightningdevkit:mainfrom
moisesPompilio:2827_issue

Conversation

@moisesPompilio

@moisesPompiliomoisesPompilio commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

Fixes#2827

To ensure deterministic override_random_bytes for each node, I first reverted to the state before commit a3b416a and used keys_manager.get_secure_random_bytes() to retrieve the required values. Then, I restored the current state of the code and assigned the collected values to their respective nodes.

This is my first PR for LDK, so I’d appreciate any feedback on my approach and possible improvements.

@ldk-reviews-bot

ldk-reviews-bot commented Mar 5, 2025

Copy link
Copy Markdown

👋 I see @valentinewallace was un-assigned.
If you'd like another reviewer assignemnt, please click here.

@codecov

codecovBot commented Mar 5, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.23%. Comparing base (ebdbee0) to head (f8d1126).
Report is 17 commits behind head on main.

Files with missing linesPatch %Lines
lightning/src/ln/functional_test_utils.rs87.50%0 Missing and 1 partial ⚠️
Additional details and impacted files
@@ Coverage Diff @@## main #3647 +/- ##
==========================================
- Coverage 89.25% 89.23% -0.03% 
==========================================
Files 155 155 Lines 119954 119952 -2 Branches 119954 119952 -2 ==========================================
- Hits 107064 107034 -30 - Misses 10277 10292 +15 - Partials 2613 2626 +13 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

I think post-#3302 we should focus on making the test robust in that it doesn't rely on the output of the RNG, rather than trying to fix the RNG output (which #3302 largely already did).

@moisesPompilio

Copy link
Copy Markdown
ContributorAuthor

Ok, thanks for the feedback! Quick question—would it make more sense to reduce the RNG dependency just enough so override_random_bytes doesn’t break the test so easily, or should I focus on keeping override_random_bytes and minimizing the test’s reliance on RNG? Sorry if I’m being redundant with my question, still getting familiar with the project.

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

In an ideal world I think we can drop the override_random_bytes and also the set_counter lines above. I think that would be the goal to close the issue.

@moisesPompilio

Copy link
Copy Markdown
ContributorAuthor

Got it! I'll work on these removals and see if I can make it work.

@moisesPompilio

Copy link
Copy Markdown
ContributorAuthor

Hello @TheBlueMatt,

I updated the code as suggested. I removed the set_counter call from do_test_restored_packages_retry and changed serialized_monitor to match the node under test. To do this, I reverted to the code state before commit a3b416a, adjusted the override_random_bytes to reflect the current implementation, and used get_monitor to obtain the updated serialized_monitor value.

Could you please confirm if this reasoning is correct or if there's anything else needed?

@TheBlueMatt

Copy link
Copy Markdown
Collaborator

Doh! When I did #3302 and commented above I had not actually bothered to look into why the test needed specific RNG output. Having to have the RNG output match the hard-coded ChannelMonitor is really the annoying part here. Ideally we'd have a way to override the keys selected themselves so that we're not relying on the RNG at all. I think that would mean having a variant to create_chanmon_cfgs which allows us to specify the node's seed and then hooking TestKeysInterface::generate_channel_keys_id to let us specify the next keys-id that will be used so that it matches the one we want.

@moisesPompilio

Copy link
Copy Markdown
ContributorAuthor

Thanks a lot! I saw your comment—I was trying to tackle the RNG issue in a different way, but I realized I was way off and getting stuck. I had already gone back and modified serialized_monitor, but I started questioning if I was just overcomplicating things. I'll start making the suggested changes now. Appreciate the guidance!

Add `override_next_keys_id` to `TestKeysInterface`, enabling predictable `generate_channel_keys_id` output when se
Similar to `create_chanmon_cfgs`, but allows using `override_next_keys_id` in `TestKeysInterface` to ensure predictable key ID generation
Removes `set_counter` (which resets RNG) and adds control over channel key ID generation
@moisesPompilio

Copy link
Copy Markdown
ContributorAuthor

Hi @TheBlueMatt

I've implemented the requested changes. I removed set_account and introduced override_next_keys_id, allowing generate_channel_keys_id to return a predefined value if set. Otherwise, it follows the normal flow. The override resets to None after use to prevent duplicate IDs.

I also added create_chanmon_cfgs_with_keys, which works like create_chanmon_cfgs but allows predefined keys_id values to be set in chan_mon_cfgs, ensuring predictable key generation when needed. Additionally, I used set_counter to track generate_channel_keys_id outputs, preserving the original keys_id values and avoiding changes to serialized_monitor.

I believe the test now relies less on RNG manipulation.

@TheBlueMattTheBlueMatt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hell yea, thanks! Just gonna land this since its test-only and straightforward.

@TheBlueMatt
TheBlueMatt merged commit 05dec16 into lightningdevkit:mainMar 25, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_restored_packages_retry is brittle

3 participants

@moisesPompilio@ldk-reviews-bot@TheBlueMatt