Run an Avalon Worker
Start an MDK Worker for a Canaan Avalon device.
TL;DR
Run
mdk runfrom your project folder (holdingmdk.yaml). Runtime:mdk run, which writes the Kernel key to.mdk/kernel.key, or a host script, which writes it to~/.mdk/.kernel-keywherever you run it.
- Under
mdk run,mock: truestarts the Worker's own mock; a host script needsnpm run mock a1346 4028, run from the repository root - Check the device reports telemetry with
curlthrough the Gateway:READYproves the Worker registered, not that its device answers
Overview
This page details how to run the Canaan Avalon Worker. The package is a directory-loaded Worker Plugin
(mdk-contract.json plus handler files) that mdk run hosts from mdk.yaml, or that your own Node.js service
hosts on WorkerRuntimeV2. Select the development (mock) or real-device path.
Prerequisites
Review common deployment prerequisites before you start.
Deployment-specific requirements:
- An MDK project with
mdk.yaml(mdk onboard), or a Node.js service or script in your deployment that hosts the Worker.@tetherto/mdk-worker-avalonis not published to npm, so the package resolves only through the workspace of an MDK repository clone afternpm installat its root: keep the project or script inside that clone - A supported Avalon device reachable from the machine or container running the Worker
- The miner API reachable over the native CGMiner TCP API, typically port
4028 - A
passwordfor each device: reads need no credentials, and the Worker's own validation refuses to load a device without one (ERR_DEVICE_CONFIG_INVALID)
Development
Run against a mock
The Avalon Worker ships a mock A1346 that answers the CGMiner API with canned data. Declare the Worker under spec.workers
in the mdk.yaml mdk onboard wrote, replacing any entry onboarding seeded
for it, with mock: true, and mdk run starts one mock per device, on the port each device names:
spec:
workers:
- name: avalon
package: '@tetherto/mdk-worker-avalon'
config:
mock: true
devices:
- id: AV-DEV-001
opts: { address: 127.0.0.1, port: 14030, password: admin }mdk runExpected output includes a line for the Worker, with your own key:
✔ worker avalon ready 98edeb00a6142891A warning that the Worker may be missing device options can print with it: Worker troubleshooting covers
what it checks.
Check the stack from a second terminal in the project directory:
mdk statusExpected output ends with:
WORKER REGISTERED STATE HEALTH DEVICES
avalon yes READY HEALTHY 1
✔ Health: healthyREADY and HEALTHY prove the Worker registered, not that its device answers: what a healthy run shows
covers the difference. To see a reading, check the device reports telemetry.
To host the Worker yourself instead of with mdk run, run the mock in one terminal and a host script in a second, both from the repository root:
-
Start the mock on the CGMiner port:
npm run mock a1346 4028 -
Save this script as
avalon-dev.jsin the repository root. It is the boot call from Connect a miner, pointed at127.0.0.1:'use strict' const os = require('os') const path = require('path') const { getKernel } = require('@tetherto/mdk-core') const { WorkerRuntimeV2 } = require('@tetherto/mdk-worker') const main = async () => { const kernel = await getKernel({ handleSignals: true }) const runtime = new WorkerRuntimeV2(path.dirname(require.resolve('@tetherto/mdk-worker-avalon/package.json')), { workerId: 'avalon-dev', storeDir: path.join(os.tmpdir(), 'mdk-dev', 'avalon'), allowedKeys: [kernel.getClientPublicKey()], devices: [{ deviceId: 'AV-DEV-001', config: { address: '127.0.0.1', port: 4028, password: 'admin' } }] }) await runtime.start() const registration = await kernel.registerWorker(runtime.getPublicKey()) if (!registration.registered) throw new Error(`Worker not registered: ${registration.error}`) kernel._cleanup.push(() => runtime.stop()) // Ctrl+C stops the Worker before the Kernel console.log('Worker registered:', registration.workerId) console.log('Kernel HRPC key:', kernel.getPublicKey().toString('hex')) console.log('Ctrl+C to stop.') } main().catch((err) => { console.error(err); process.exit(1) }) -
Run the script in a second terminal:
node avalon-dev.js
Expected output, with your own key:
Worker registered: avalon-dev
Kernel HRPC key: a4303ca6404442264fda55e37a35db3400eeab1a4b4b68f9e1413fff99f9521e
Ctrl+C to stop.A Kernel that refuses the Worker stops the script with Error: Worker not registered: <error> and a stack trace instead:
Worker troubleshooting covers each error.
Ctrl+C stops the Worker, then the Kernel; stop the services cleanly covers the contract. Stop the mock with Ctrl+C in its own terminal when you are done; nothing else stops it.
Either way, the Worker serves live telemetry (hashrate, power, temperature) from the mock. In the host script, the telemetry check reads it. The mock's options are in USAGE.md.
The Kernel keeps its state in ~/.mdk, wherever you run a host script: its store, its trusted keys, and its key in ~/.mdk/.kernel-key.
To run more than one Kernel on a machine, keep each one's state next to its Worker's.
Keep the Kernel's state next to the Worker's
Pass the Kernel a root and key file under the same temp folder as the Worker's store:
const kernelRoot = path.join(os.tmpdir(), 'mdk-dev', 'avalon-kernel')
const kernel = await getKernel({
handleSignals: true,
root: kernelRoot,
keyFile: path.join(kernelRoot, '.kernel-key')
})A Gateway in another process needs this keyFile and trustedKeysDir: path.join(kernelRoot, '.trusted-keys'); one started in
the same process with kernel needs neither. The telemetry check in Worker troubleshooting spreads its own
root and keyFile: false into getKernel(), so use it or this change, not both.
Connect a miner
2.1 Confirm the model
Avalon ships one model family today, a1346 — confirm this against the USAGE.md as new models are added.
2.2 Declare your miner
Avalon devices use the native CGMiner TCP API on port 4028. Reads over the API need no credentials, but the Avalon Worker Plugin's
own validation refuses to load a device with no password (ERR_DEVICE_CONFIG_INVALID).
setupPools sends no device credentials, so the miner rejects the pool change, keeps its pools, and reboots. With a pools
parameter the command returns success: false; without one it still returns success: true.
Point the mdk.yaml entry at the real miner and drop mock:
spec:
workers:
- name: avalon
package: '@tetherto/mdk-worker-avalon'
config:
devices:
- id: AV-001
opts: { address: 192.168.1.30, port: 4028, password: "${AVALON_PASSWORD}" }The password is a ${AVALON_PASSWORD} placeholder, so it stays out of mdk.yaml. Add it to the project's .env, quoted as
writing a value in .env describes:
AVALON_PASSWORD='<your-miner-password>'Keep .env out of version control and readable by you alone (chmod 600 .env).
The CLI resolves the placeholder when it loads mdk.yaml, and stops with an error naming the variable when it
is unset.
Without mdk run, add this to the Node.js service or script that hosts the Worker. The snippet seeds one Avalon
device; replace the example IP address with your miner's. The snippet reads the password from AVALON_PASSWORD, and node
doesn't read .env on its own, so start the script with node --env-file=.env <script> or export the variable first. With
it unset, the Worker fails to start with an error that includes ERR_DEVICE_CONFIG_INVALID:
const path = require('path')
const { getKernel } = require('@tetherto/mdk-core')
const { WorkerRuntimeV2 } = require('@tetherto/mdk-worker')
const main = async () => {
const kernel = await getKernel()
const runtime = new WorkerRuntimeV2(path.dirname(require.resolve('@tetherto/mdk-worker-avalon/package.json')), {
workerId: 'avalon-rack-1',
storeDir: './store/avalon-rack-1',
allowedKeys: [kernel.getClientPublicKey()],
devices: [{ deviceId: 'AV-001', config: { address: '192.168.1.30', port: 4028, password: process.env.AVALON_PASSWORD } }]
})
await runtime.start()
const registration = await kernel.registerWorker(runtime.getPublicKey())
if (!registration.registered) throw new Error(`Worker not registered: ${registration.error}`)
}
main().catch((err) => { console.error(err); process.exit(1) })allowedKeys lists the RPC callers the Worker accepts. The Worker refuses to start without it
(ERR_WORKER_ALLOWED_KEYS_REQUIRED), so pass the Kernel's client key.
Make sure each miner's IP is reachable from the machine or container running the Worker before registering. Commands act on physical hardware — prioritize thermal safety.
The device list is fixed at construction — there is no registerThing/updateThing/forgetThings equivalent.
Adding, updating, or removing a miner means editing the device list and restarting the Worker.
The full device option reference and the mock createServer options are in the Worker's USAGE.md
and the shared install pattern.
Check the device reports telemetry
Under mdk run, add the telemetry route to an app once, sign in, then read the device from a second
terminal:
curl -sS -H "Authorization: Bearer $TOKEN" http://localhost:3847/apps/fleet/api/fleet/devices/AV-DEV-001/telemetryExpected output against the mock, with your own values, shortened:
{"deviceId":"AV-DEV-001","metrics":{"hashrate_avg":299.93,"power":3100,"temperature":36,"fan_speed":0,"status":"mining","uptime":11,"efficiency":10.34,"power_mode":"normal",...For a real miner, put its device ID, AV-001, in the path.
In a script that boots your Kernel, add the telemetry check instead. In avalon-dev.js, call it after the
line that prints the Kernel key:
const { checkKernelOpts, checkTelemetry } = require('./check-telemetry')
const kernel = await getKernel({ handleSignals: true, ...checkKernelOpts })
// ...
console.log('Kernel HRPC key:', kernel.getPublicKey().toString('hex'))
await checkTelemetry(kernel, 'AV-DEV-001')
console.log('Ctrl+C to stop.')For a real miner, make the same change to the script from Connect a miner and pass its device ID, AV-001.
Expected output against the mock, with your own key and values:
Worker registered: avalon-dev
Kernel HRPC key: 10a8ff609ec4954cabb359fa04e09cc7041558aa269a33970e5e416ac583ed82
Telemetry from AV-DEV-001:
hashrate_avg: 294.48
power: 3100
temperature: 36
fan_speed: 0
status: mining
uptime: 12
efficiency: 10.53
power_mode: normal
Ctrl+C to stop.An error instead of a value? Read a failed check.
Troubleshooting
An unreachable miner doesn't fail at load time: the Worker reports the device online and the failure surfaces as
ERR_MINER_UNREACHABLE inside the telemetry channel or command response that touched the network. If the Worker
doesn't come up, or a mock port is already in use, follow Worker troubleshooting.
Next steps
- Decide how to run the Worker service — Deployment topologies
- Review telemetry units, command shapes, and error codes —
mdk-contract.json