Repository navigation
feat: expose client extended and standard mining channels - #10
Conversation
d45dec1 to
639b2e5
Compare
|
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. |
|
To make the comments above easier to act on:
|
|
On the question in #9: yes, please include Its job getter should hand data out the same way |
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
639b2e5 to
8957b0c
Compare
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
|
Split the fuzz target and CI update into 8957b0c and reworded the Added |
| /// 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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| //! 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 |
There was a problem hiding this comment.
Nit, non-blocking. The channels bullet still says "extended and standard channels"; it should mention the client group channel too.
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-ffifor the Noise initiator handshake, framing, and message records, but had to write a custom C channel state machine becausesrc/channels/only wrappedchannels_sv2::server. This PR adds C bindings forchannels_sv2::clientso C miners can delegate client channel state and share validation to SRI directly.What changed
src/channels/client/(mod.rs,extended.rs,standard.rs,group.rs) wrappingchannels_sv2::client'sExtendedChannel,StandardChannel,GroupChannel, and clientShareAccountingbehind opaqueSv2FfiClientExtendedChannel,Sv2FfiClientStandardChannel, andSv2FfiClientGroupChannelhandles.Sv2FfiExtendedChannel/Sv2FfiStandardChannelnames and all existing error codes (-20..-39) unchanged so the committed header stays backwards-compatible.Open{Extended,Standard}MiningChannel.Success(ExtranoncePrefix::from_wire). Allocator-backed sub-allocation is left out of this PR:ExtranonceAllocator::from_upstream_prefixexists inchannels_sv2, andsv2-ffidoes not wrap it yet.active_job()and groupfuture_job()stay within the existing caller-owned buffer rule (AGENTS.mduntouched):Sv2FfiClientStandardJobholdsextranonce_prefixinline (fixed[u8; 32]plusextranonce_prefix_len),sv2_ffi_client_extended_channel_active_job()copies variable-length fields into a caller buffer with capacity and a*neededoutput (supportingNULL, 0, &neededsize queries), andsv2_ffi_client_group_channel_{active,future}_job()uses the same caller-buffer pattern to populateSv2FfiNewExtendedMiningJobdirectly so it can be passed straight to member channels.on_share_rejection()maps non-UTF-8 upstream bytes toUNKNOWN_ERROR_CODEso rejections are still counted under"unknown", while aNULLpointer with non-zero length returnsSV2_FFI_ERR_INVALID.include/sv2_ffi.hwithcbindgen 0.29.4and updatedREADME.md.Testing
tests/capi_test.c) pairing client and server extended and standard channels end-to-end plusSv2FfiClientGroupChannelmembership and job lifecycle tests, covering error paths (NULL args, zero target, oversized/mismatched extranonce, bad UTF-8,SetExtranoncePrefixbeforeSetNewPrevHash,SetTargetafter activation,SetNewPrevHashmiss,active_job()/future_job()/channel_ids()size queries and short buffers, duplicate/stale shares, and group jobs on standard channels).client_channel_sharefuzz target infuzz/(covering extended, standard, and group channels) and wired it into.github/workflows/ci.ymlsochannel_sharekeeps its full server fuzzing budget.cargo fmt --check(crate and fuzz),cargo clippy --release --all-targets --locked -- -D warnings,cargo test --release --locked,./test.sh(including UBSan), andcargo +nightly clippy --manifest-path fuzz/Cargo.toml --bin client_channel_share --locked -- -D warnings.