Skip to content

Add vardiff test framework#1729

Merged
GitGab19 merged 8 commits into
stratum-mining:mainfrom
Shourya742:2025-06-03-add-vardiff-test-framework
Jun 11, 2025
Merged

Add vardiff test framework#1729
GitGab19 merged 8 commits into
stratum-mining:mainfrom
Shourya742:2025-06-03-add-vardiff-test-framework

Conversation

@Shourya742

Copy link
Copy Markdown
Member

Partially solves: #1725

@codecov

codecov Bot commented Jun 3, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 23.18%. Comparing base (3ff64b0) to head (fca6472).
Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1729      +/-   ##
==========================================
+ Coverage   22.96%   23.18%   +0.21%     
==========================================
  Files         146      148       +2     
  Lines       11302    11402     +100     
==========================================
+ Hits         2596     2643      +47     
- Misses       8706     8759      +53     
Flag Coverage Δ
binary_codec_sv2-coverage 0.00% <ø> (ø)
binary_sv2-coverage 7.74% <ø> (ø)
bip32_derivation-coverage 0.00% <ø> (ø)
buffer_sv2-coverage 37.68% <ø> (ø)
codec_sv2-coverage 0.02% <ø> (ø)
common_messages_sv2-coverage 0.19% <ø> (ø)
error_handling-coverage 0.00% <ø> (ø)
framing_sv2-coverage 0.41% <ø> (ø)
jd_client-coverage 0.38% <ø> (ø)
jd_server-coverage 13.28% <ø> (ø)
job_declaration_sv2-coverage 0.00% <ø> (ø)
key-utils-coverage 3.61% <ø> (ø)
mining-coverage 3.87% <ø> (ø)
mining_device-coverage 0.00% <ø> (ø)
mining_proxy_sv2-coverage 1.17% <ø> (ø)
noise_sv2-coverage 6.55% <ø> (ø)
pool_sv2-coverage 5.67% <ø> (ø)
protocols 34.00% <100.00%> (+0.25%) ⬆️
roles 10.70% <ø> (ø)
roles_logic_sv2-coverage 22.42% <100.00%> (+0.52%) ⬆️
sv2_ffi-coverage 0.00% <ø> (ø)
template_distribution_sv2-coverage 0.00% <ø> (ø)
translator_sv2-coverage 8.28% <ø> (ø)
utils 36.39% <ø> (ø)
v1-coverage 3.63% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

Comment thread protocols/v2/roles-logic-sv2/src/vardiff/test/mod.rs Outdated
Comment thread protocols/v2/roles-logic-sv2/src/vardiff/test/mod.rs Outdated
Comment on lines +10 to +11
pub const TEST_INITIAL_HASHRATE: f32 = 1000.0;
pub const TEST_SHARES_PER_MINUTE: f32 = 10.0;
pub const TEST_MIN_ALLOWED_HASHRATE: f32 = 10.0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I did cargo t on this branch and I feel the test duration is a bit prohibitive.

waiting 70s or even 250s for a test to finish its execution feels a bit too much.

but also, I don't see any fundamental reason that forces us to do this

I mean, sure, expected share rate is set on a per-minute basis for UX reasons, but that doesn't mean that we must test scenarios for multiple minutes to assert the sanity of the APIs

if we remember that all math done here is linear, we can simply tweak these constants in order to allow us to perform our simulations under smaller execution timespans

@plebhash plebhash Jun 4, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for example, if TEST_SHARES_PER_MINUTE is 60_000, we expect 1_000 shares per second

if in 1 second we only get 10 shares, that should be a trigger to lower the difficulty

if in 1 second we get 5_000 shares, that should be a trigger to raise the difficulty

if we want to emulate scenarios of "long" timespans, we can do ~10s

overall, we have plenty of room to play around with magnitudes, while trying to optimize for test execution time

@plebhash plebhash Jun 9, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ok I see the minimum window we have is 15s

if delta_time <= 15 {
return Ok(None);
}

@GitGab19 I assume this is important for the correct behavior of the algorithm, right?

if so, then we can still make test durations much smaller, sticking to 15s as the default execution time and making artithmetic adaptations around TEST_SHARES_PER_MINUTE

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I’ve added a test configuration for this, which will also allow delta_time <= 0 in testing scenarios.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Although the linearity seems tempting to exploit for testing, there’s a fundamental reason we need to wait for this duration our logic is designed to trigger at specific time intervals. Anyway, I’ve updated the tests to specifically target those wait points, and they’re now more intuitive to understand. The downside is that we still need to wait at least 60 seconds for those points to be reached. I initially considered adding some test-specific logic to bypass this, but that felt like heavy lifting given the current setup.

Comment thread protocols/v2/roles-logic-sv2/src/vardiff/test/mod.rs Outdated
);
assert_eq!(vardiff.shares_since_last_update(), 0);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see tests to assert target increase and decrease

but I don't see anything asserting that the vardiff converges to a correct hashrate estimation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I understand your point about the convergence test. I’m planning to add that in a separate PR since it will require more work. This PR focuses on adding the basic skeleton for the testing framework for the vardiff module and strictly handling edge cases in the vardiff calculation. We already have a convergence mechanism in the difficulty management of tproxy, but it's somewhat unreliable. Also, simulating the entire hashing process is influenced by variable processing speeds, which makes it tricky. Designing such tests to be deterministic across the system would need a more careful and thorough review.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

when I wrote this comment originally, I wasn't necessarily thinking about dynamically testing with real hashrate from a CPU miner

my mindset was more around statically running the tests with specific values, such that we can assert that the vardiff algo converges into the right direction on its hashrate estimation

@Shourya742 Shourya742 force-pushed the 2025-06-03-add-vardiff-test-framework branch 2 times, most recently from 318321b to cddb61f Compare June 10, 2025 10:27
@Shourya742 Shourya742 force-pushed the 2025-06-03-add-vardiff-test-framework branch 2 times, most recently from bbd09cf to 79aad22 Compare June 10, 2025 11:16
@Shourya742 Shourya742 force-pushed the 2025-06-03-add-vardiff-test-framework branch from 79aad22 to a8532cb Compare June 10, 2025 11:23
@Shourya742 Shourya742 requested a review from plebhash June 10, 2025 11:32
@GitGab19 GitGab19 merged commit 24f3eeb into stratum-mining:main Jun 11, 2025
16 checks passed
Sign up for free to 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.

3 participants