8000 feat: expose client extended and standard mining channels by pavlenex · Pull Request #10 · stratum-mining/sv2-ffi · GitHub
[go: up one dir, main page]

Skip to content

feat: expose client extended and standard mining channels - #10

Merged
plebhash merged 4 commits into
stratum-mining:mainfrom
pavlenex:client-channels
Dec 1, 2026
Merged

plebhash merged 4 commits into
stratum-mining:mainfrom
pavlenex:client-channels

Conversation

@pavlenex
@pavlenex pavlenex commented Nov 30, 2026 •
Copy link
Copy Markdown
Contributor

Closes #9

While adding native Stratum V2 support to Canaan's open-source Avalon Nano 3S firmware (Canaan-Creative/canaan_sdk_for_linux#6), I could use sv2-ffi for the Noise initiator handshake, framing, and message records, but had to write a custom C channel state machine because src/channels/ only wrapped channels_sv2::server. This PR adds C bindings for channels_sv2::client so C miners can delegate client channel state and share validation to SRI directly.

What changed

  • Added src/channels/client/ (mod.rs, extended.rs, standard.rs, group.rs) wrapping channels_sv2::client's ExtendedChannel, StandardChannel, GroupChannel, and client ShareAccounting behind opaque Sv2FfiClientExtendedChannel, Sv2FfiClientStandardChannel, and Sv2FfiClientGroupChannel handles.
  • Kept existing Sv2FfiExtendedChannel / Sv2FfiStandardChannel names and all existing error codes (-20..-39) unchanged so the committed header stays backwards-compatible.
  • Client channels open from the wire parameters of Open{Extended,Standard}MiningChannel.Success (ExtranoncePrefix::from_wire). Allocator-backed sub-allocation is left out of this PR: ExtranonceAllocator::from_upstream_prefix exists in channels_sv2, and sv2-ffi does not wrap it yet.
  • active_job() and group future_job() stay within the existing caller-owned buffer rule (AGENTS.md untouched): Sv2FfiClientStandardJob holds extranonce_prefix inline (fixed [u8; 32] plus extranonce_prefix_len), sv2_ffi_client_extended_channel_active_job() copies variable-length fields into a caller buffer with capacity and a *needed output (supporting NULL, 0, &needed size queries), and sv2_ffi_client_group_channel_{active,future}_job() uses the same caller-buffer pattern to populate Sv2FfiNewExtendedMiningJob directly so it can be passed straight to member channels.
  • on_share_rejection() maps non-UTF-8 upstream bytes to UNKNOWN_ERROR_CODE so rejections are still counted under "unknown", while a NULL pointer with non-zero length returns SV2_FFI_ERR_INVALID.
  • Regenerated include/sv2_ffi.h with cbindgen 0.29.4 and updated README.md.

Testing

  • Added Rust and C tests (tests/capi_test.c) pairing client and server extended and standard channels end-to-end plus Sv2FfiClientGroupChannel membership and job lifecycle tests, covering error paths (NULL args, zero target, oversized/mismatched extranonce, bad UTF-8, SetExtranoncePrefix before SetNewPrevHash, SetTarget after activation, SetNewPrevHash miss, active_job() / future_job() / channel_ids() size queries and short buffers, duplicate/stale shares, and group jobs on standard channels).
  • Added a dedicated client_channel_share fuzz target in fuzz/ (covering extended, standard, and group channels) and wired it into .github/workflows/ci.yml so channel_share keeps its full server fuzzing budget.
  • Ran cargo fmt --check (crate and fuzz), cargo clippy --release --all-targets --locked -- -D warnings, cargo test --release --locked, ./test.sh (including UBSan), and cargo +nightly clippy --manifest-path fuzz/Cargo.toml --bin client_channel_share --locked -- -D warnings.

Comment thread src/channels/client/mod.rs Resolved
Comment thread src/channels/client/extended.rs Resolved
Comment thread src/channels/client/extended.rs Resolved
Comment thread tests/capi_test.c Resolved
Comment thread src/channels/client/extended.rs Resolved
Comment thread src/channels/client/mod.rs Resolved
@plebhash
Copy link
Copy Markdown
Member

Could you split the fuzz target and CI change into a second commit, and reword the allocator sentence? from_upstream_prefix exists in the crate; it's sv2-ffi that doesn't wrap it yet.

@plebhash
plebhash commented Nov 30, 2026 •
Copy link
Copy Markdown
Member

To make the comments above easier to act on:

  • The one thing I'd like settled before merge is how active_job() hands data out (the comment on Sv2FfiClientExtendedJob). Everything else is non-blocking.
  • The comments on sync_active_merkle_path() and on the fuzz target only apply if the borrowed views stay. With a caller buffer the cache and its sync logic go away, and so do those two comments.

@plebhash
Copy link
Copy Markdown
Member

On the question in #9: yes, please include GroupChannel (channels_sv2::client::group) in this PR.

Its job getter should hand data out the same way active_job() on the extended channel ends up doing, so it is probably easiest to add once that question is settled.

Expose channels_sv2's client-side ExtendedChannel and StandardChannel
through the C API so C mining clients and firmware can track upstream
jobs, validate candidate shares locally and maintain client share
accounting without reimplementing protocol state.

Each channel supports opening from Open*MiningChannel.Success values,
reading and rotating target and extranonce prefix, handling future and
active jobs (including group-channel job conversion on standard
channels), SetNewPrevHash activation, active job and chain tip queries,
local share validation, and upstream share acknowledgement and rejection
accounting.

Active job reads stay within the existing caller-owned buffer rule:
Sv2FfiClientStandardJob inlines its extranonce_prefix (up to 32 bytes),
and sv2_ffi_client_extended_channel_active_job() writes variable-length
fields into a caller-provided buffer with a required-size output.

Closes stratum-mining#9
Add a cargo-fuzz target that drives both client extended and standard
channels through target and extranonce updates, future/active/group
jobs, SetNewPrevHash, candidate share validation, upstream share
acknowledgements and rejections, and active_job reads (including short
buffer checks and reading every byte of each returned view so
AddressSanitizer verifies the full range).

Wire client_channel_share into the CI fuzz matrix.

Ref stratum-mining#9
Expose channels_sv2::client::group::GroupChannel as
Sv2FfiClientGroupChannel so C mining clients can track group channel
membership, enforce a matching full extranonce size across member
channels, and hold the group's future and active NewExtendedMiningJobs.

sv2_ffi_client_group_channel_active_job() and _future_job() use the
same caller-buffer pattern as sv2_ffi_client_extended_channel_active_job()
and populate Sv2FfiNewExtendedMiningJob directly so the result can be
passed straight into member standard or extended channels.

Includes regenerated sv2_ffi.h, Rust and C unit tests, README updates,
and group-channel coverage in client_channel_share.

Ref stratum-mining#9
@pavlenex
Copy link
Copy Markdown
Contributor Author

Split the fuzz target and CI update into 8957b0c and reworded the ExtranonceAllocator::from_upstream_prefix note in the PR description.

Added channels_sv2::client::group::GroupChannel (Sv2FfiClientGroupChannel) in 4aa8c88. sv2_ffi_client_group_channel_active_job() and _future_job() use the same caller-buffer + needed pattern as the extended channel and fill Sv2FfiNewExtendedMiningJob directly so callers can pass the result straight into member standard or extended channels.

/// point at `cap` writable bytes (or be NULL when `cap` is 0), and `needed`
/// must be NULL or writable.
#[no_mangle]
pub unsafe extern "C" fn sv2_ffi_client_group_channel_future_job(
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.

Non-blocking. future_job() needs a job_id, and only the count is exposed, so a caller that wants to hand the group's queued jobs to a newly added member has to track the IDs itself. Is that the intended use? If so, a _future_job_ids(out, cap, needed) like _channel_ids would cover it. Fine as a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added sv2_ffi_client_group_channel_future_job_ids() with the same contract as _channel_ids. e4e821f

/// Copy a `NewExtendedMiningJobOwned` into `*out`, placing its variable
/// length byte fields (`merkle_path`, `coinbase_tx_prefix`,
/// `coinbase_tx_suffix`) into caller buffer `buf`.
fn copy_group_job(
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.

Nit, non-blocking. This is nearly the same code as the body of sv2_ffi_client_extended_channel_active_job() (size, check, copy merkle path / prefix / suffix). One shared helper in client/mod.rs that lays out a list of slices into buf would leave a single place to get the bounds right.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: lay_out_job() in client/mod.rs is now the single place for the bounds, used by both getters.

/// # Safety
/// `ch` must be a live group channel.
#[no_mangle]
pub unsafe extern "C" fn sv2_ffi_client_group_channel_full_extranonce_size(ch: *const Sv2FfiClientGroupChannel) -> i32 {
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.

Nit, non-blocking. Before any member is added this returns SV2_FFI_ERR_CH_EXTRANONCE_SIZE, the same code add_channel_id uses for a mismatch. It's documented, so OK to keep, but it's a normal state reported as an error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Kept as documented. 0 is a valid full extranonce size, so a sentinel would be ambiguous, and an out-parameter seemed heavier than the documented code

Comment thread src/lib.rs
//! extranonce allocator, and extended and standard channels that take
//! their jobs as the bytes the server sends, validate the shares and keep
//! the share accounting.
//! - `channels`: server-side and client-side mining channels through
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.

Nit, non-blocking. The channels bullet still says "extended and standard channels"; it should mention the client group channel too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done e4e821f

@plebhash
plebhash merged commit 3ac5f2a into stratum-mining:main Dec 1, 2026
3 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.

Client mining channels (channels_sv2::client)

2 participants

0