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.
pythondef score(self, batch: Batch, evaluate: Evaluator) -> Mapping[int, float]: ...
What a batch contains
| field | type | what it is |
|---|---|---|
| batch.round_index | int | 0-based round counter |
| batch.responses | tuple[Response] | this round's submissions |
| batch.history | tuple[tuple[Response]] | every previous round, oldest first |
| batch.registered_uids | frozenset[int] | everyone registered, including silent miners |
What a response contains
| field | type | what it is |
|---|---|---|
| response.uid | int | the submitting miner |
| response.payload | bytes | what they submitted |
| response.digest | str | sha256 of the payload — use this to deduplicate |
| response.latency_ms | float | how long they took |
| response.submitted_at_ns | int | when, for ordering and staleness |
| response.is_empty | bool | payload is whitespace |
A hardened example
Four defences, each aimed at a different class of attack. Nothing here is exotic — that is the point.
pythonclass Hardened(Mechanism):name = "hardened"SAMPLES = 3QUALITY_FLOOR = 0.30 # must clear the evaluator's noise banddef score(self, batch, evaluate):# 1. freshness — anything seen in a previous round is deadseen = {r.digest for prev in batch.history for r in prev}# 2. deduplicate — identical payloads are one unit of workgroups = {}for resp in batch.responses:groups.setdefault(resp.digest, []).append(resp)weights = {}for digest, group in groups.items():if digest in seen:continuegroup.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 samplessamples = sorted(evaluate(original) for _ in range(self.SAMPLES))quality = samples[len(samples) // 2]if quality < self.QUALITY_FLOOR:continueweights[original.uid] = qualityreturn weights