Introducing a new way to review your PRs.

Read the announcement

luke-jr / bfgminer

Modular ASIC/FPGA miner written in C, featuring overclocking, monitoring, fan speed control and remote interface capabilities.

View on GitHub

13 documents · Updated Nov 16, 2026

docs / work-scheduling-pool-protocols

Work Scheduling and Pool Protocols

Overview

This area turns upstream mining work into local/proxied work, then routes submitted nonces back through the miner core. The owned protocol front-ends are driver-getwork.c for HTTP JSON-RPC getwork clients, driver-stratum.c for a local Stratum server, driver-proxy.c/driver-proxy.h for per-username virtual devices, and work2d.c/work2d.h for splitting one upstream Stratum/GBT job across downstream clients by embedding a small client id in extranonce space. The miner core entry points these files call are get_work() and submit_nonce() in miner.c, plus Stratum work builders gen_stratum_work2()/gen_stratum_work3() and struct stratum_work state declared in miner.h.

driver-proxy.c is the shared identity/device layer for proxy protocols. proxy_find_or_create_client() maps an authenticated username to a struct proxy_client, allocates a virtual cgpu_info using proxy_drv, creates cgpu threads via create_new_cgpus(), and reports the thread in before returning it. Each proxy client owns a work hash used by getwork and a desired share difficulty controlled by the diff device setting. A background pruner in driver-proxy.c walks all proxy clients every 60 seconds and frees cached getwork jobs older than opt_expiry.

For getwork, handle_getwork() in driver-getwork.c accepts an HTTP request through the embedded MicroHTTPD server. It parses an optional JSON body, requires method == "getwork", extracts a Basic Auth username, and resolves that username to a proxy client. A request without a submit parameter calls get_work(thr) on the proxy client's thread, sets work->nonce_diff from the client's desired difficulty or the mining algorithm's low difficulty, caps it at work->work_difficulty, and returns JSON containing target, 128 bytes of data, midstate, and hash1. The returned work is timestamped and inserted into client->work, keyed by the first 76 bytes of the block header so a later submission can be matched. A request with a submit parameter decodes the supplied getwork header, extracts the little-endian nonce at byte 76, looks up the cached work by the first 76 bytes, calls submit_nonce(thr, work, nonce), then reports either success, H-not-zero, stale, or unknown-work. The optional X-Hashes-Done header, or a fallback derived from nonce_diff, is passed to hashes_done2().

For downstream Stratum, driver-stratum.c runs a libevent listener and maintains a cache of downstream-visible jobs. stratumsrv_init_server() calls work2d_init(), builds an event base, installs a periodic timer and notifier, and starts stratumsrv_thread(). _stratumsrv_update_notify() uses current_pool() and stratumsrv_update_notify_str() to convert the current upstream pool->swork into a downstream mining.notify. That conversion first verifies pool_has_usable_swork(pool), generates dummy work with work2d_gen_dummy_work_for_stale_check() to avoid broadcasting stale upstream work, and skips near-identical work by comparing the header, coinbase slices outside nonce2, and difficulty bits against the last downstream job. It then builds a job id, splits the upstream coinbase at swork->nonce2_offset, pads the front coinbase fragment with work2d_pad_xnonce(), appends merkle links, version, nbits, and rolling ntime, copies the struct stratum_work into a struct stratumsrv_job, and stores that job in _ssm_jobs. Existing downstream connections receive mining.set_goal, mining.set_difficulty, and mining.notify according to their advertised capabilities.

Downstream Stratum clients are represented by struct stratumsrv_conn in driver-stratum.c. mining.capabilities toggles support for notify, set_difficulty, and set_goal. mining.authorize resolves the username through proxy_find_or_create_client() and links that proxy client to the connection. mining.subscribe reserves a unique little-endian xnonce1 with reserve_work2d_(), replies with that xnonce1 and the fixed downstream xnonce2 size, records per-client share difficulty in the current stratumsrv_job.job_pdiff[xnonce1], and sends the current notify. mining.submit resolves the submitting username, finds the cached job id in _ssm_jobs, parses extranonce2, ntime, and nonce, looks up the assigned share difficulty for the connection's xnonce1, and calls work2d_submit_nonce(). If the downstream client does not use the mining.hashes_done extension, the server accounts hashes by multiplying the share difficulty by the full 32-bit nonce range.

work2d.c is the adapter that reconstructs a valid internal struct work from a downstream Stratum share. work2d_init() derives the client-id extranonce width from WORK2D_MAX_DIVISIONS and fixes downstream xnonce2 at two bytes. reserve_work2d_() and release_work2d_() allocate and free client divisions. work2d_pad_xnonce_size() computes how many bytes remain in upstream swork->n2size after client id and downstream extranonce2; work2d_pad_xnonce() fills that gap with 0xbb bytes, or hex b characters when constructing a Stratum notify. work2d_gen_dummy_work() assembles work->nonce2 as padding + xnonce1 + downstream xnonce2, calls gen_stratum_work2(), and copies pool/restart timestamps. work2d_submit_nonce() then overwrites ntime in the generated header, sets nonce_diff, optionally checks staleness, and passes the nonce to submit_nonce().

libblkmaker/** and libbase58/** are owned by this doc but are empty directories in this tree snapshot, so there is no local implementation to describe here.

Coverage

Coupling

  • If struct stratum_work in miner.h changes its nonce2, coinbase, merkle, target, or locking fields, also update work2d.c and driver-stratum.c: they manually splice swork->coinbase, swork->nonce2_offset, swork->n2size, swork->merkle_bin, swork->diffbits, and swork->target instead of going through a higher-level serialization API.
  • If gen_stratum_work2() or gen_stratum_work3() in miner.c changes ownership or locking assumptions, verify work2d_gen_dummy_work() and work2d_gen_dummy_work_for_stale_check() in work2d.c: the former mutates swork->coinbase by installing work->nonce2, while the latter may pass a caller-supplied lock pointer for stale checks.
  • If the getwork response format in driver-getwork.c changes, keep the submission key in sync: handle_getwork() hashes returned jobs by the first 76 bytes of work->data and later matches submissions by the first 76 bytes of the submitted 80-byte header.
  • If the downstream Stratum extranonce layout changes, update both notification and submission paths together: driver-stratum.c advertises xnonce1 and work2d_xnonce2sz in mining.subscribe, injects padding into coinb1 in stratumsrv_update_notify_str(), and work2d.c reconstructs the same layout before calling submit_nonce().
  • If proxy share difficulty semantics change in driver-proxy.c, verify driver-stratum.c: proxy_set_diff() calls stratumsrv_client_changed_diff(), while Stratum connections compute the effective difficulty from all authorized users and store it per job in job_pdiff[xnonce1].

Risks

  • driver-stratum.c indexes stratumsrv_job.job_pdiff by conn->xnonce1_le, and work2d.c reserves ids up to WORK2D_MAX_DIVISIONS. If the reservation range or byte order changes without resizing job_pdiff and updating subscribe/submit encoding, downstream submits can read the wrong difficulty or run out of bounds.
  • work2d_pad_xnonce_size() returns a negative value when upstream swork->n2size is too small for padding + client id + downstream extranonce2. driver-stratum.c handles that by failing notify generation and possibly booting subscribed clients; a pool with insufficient extranonce space therefore makes the Stratum proxy unusable until upstream work changes.
  • driver-getwork.c stores every issued getwork item in proxy_client.work until driver-proxy.c prunes it by opt_expiry. If pruning stalls or tv_work_start is not set before insertion, repeated getwork polling can accumulate stale work records and later submissions may produce unknown-work/stale behavior that depends on cache lifetime rather than pool state.