Writing an adapter

The mechanism ABI, the objects it receives, and the one rule the interface enforces.

The contract

One method. It takes a batch and an evaluator, and returns weights keyed by uid. Values outside [0, 1] are clamped, uids that never submitted are ignored, and missing uids get zero.

python
def score(self, batch: Batch, evaluate: Evaluator) -> Mapping[int, float]: ...
Note
The harness knows which miners are honest and which are colluding. Your adapter does not — it gets responses and a noisy score, exactly as a real validator does.

What a batch contains

fieldtypewhat it is
batch.round_indexint0-based round counter
batch.responsestuple[Response]this round's submissions
batch.historytuple[tuple[Response]]every previous round, oldest first
batch.registered_uidsfrozenset[int]everyone registered, including silent miners

What a response contains

fieldtypewhat it is
response.uidintthe submitting miner
response.payloadbyteswhat they submitted
response.digeststrsha256 of the payload — use this to deduplicate
response.latency_msfloathow long they took
response.submitted_at_nsintwhen, for ordering and staleness
response.is_emptyboolpayload is whitespace

A hardened example

Four defences, each aimed at a different class of attack. Nothing here is exotic — that is the point.

python
class Hardened(Mechanism):
name = "hardened"
​
SAMPLES = 3
QUALITY_FLOOR = 0.30 # must clear the evaluator's noise band
​
def score(self, batch, evaluate):
# 1. freshness — anything seen in a previous round is dead
seen = {r.digest for prev in batch.history for r in prev}
​
# 2. deduplicate — identical payloads are one unit of work
groups = {}
for resp in batch.responses:
groups.setdefault(resp.digest, []).append(resp)
​
weights = {}
for digest, group in groups.items():
if digest in seen:
continue
group.sort(key=lambda r: (r.submitted_at_ns, r.uid))
original = group[0]
if original.is_empty:
continue
​
# 3. quality floor + 4. median of k samples
samples = sorted(evaluate(original) for _ in range(self.SAMPLES))
quality = samples[len(samples) // 2]
if quality < self.QUALITY_FLOOR:
continue
weights[original.uid] = quality
​
return weights

Previous

Installation

Next

Command reference