Compare commits

..
Author SHA1 Message Date
claudeandClaude Opus 5.5 15470d5130 MOBILE_MINING.md: record the wallet keep-alive trade-off (review on #78)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:51:57 -05:00
claudeandClaude Opus 5.5 8694239d7c MOBILE_MINING.md: emulator mined accepted testnet block #92
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:37:35 -05:00
claudeandClaude Opus 5.5 2e4836f5e8 Android app can mine: native RandomX engine, WikiDeckMiner plugin, mobile panel
- scripts/build-randomx-android.sh: NDK cross-build of the pinned native/ engine
  (light mode) for arm64-v8a and x86_64, packaged as jniLibs/<abi>/libwikideck_randomx.so
- MinerPlugin.java (Capacitor "WikiDeckMiner"): runs engine processes over the
  line protocol, on-device self-test, and halts on background, battery saver,
  thermal, hot or low battery. Never receives wallet keys.
- src/mobile-miner.ts: template -> mine -> sign -> submit loop in the WebView,
  byte-identical to consensus miningBlob/mintId (tests/mobile-mining-miner.test.ts)
- src/MobileMiningPanel.tsx: consent, intensity (1 thread default; Max = 4 only
  while charging), live hashrate/status; testnet-only (disabled on mainnet builds)
- docs/night2/MOBILE_MINING.md: how it works, safeguards, measured hashrate,
  store policies (sideload APK only), options for Eric

No protocol, consensus or server changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:32:51 -05:00
19 changed files with 854 additions and 615 deletions
+4
View File
@@ -28,3 +28,7 @@ dist-ssr
*.njsproj
*.sln
*.sw?
# Native RandomX engine for the app (scripts/build-randomx-android.sh)
native/build-android-*/
android/app/src/main/jniLibs/
+7
View File
@@ -16,6 +16,13 @@ android {
ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
}
}
packagingOptions {
jniLibs {
// The RandomX engine (jniLibs/<abi>/libwikideck_randomx.so) is an executable, not a JNI library.
// Android may exec it only from the extracted nativeLibraryDir, so keep legacy extraction.
useLegacyPackaging true
}
}
buildTypes {
release {
minifyEnabled false
@@ -1,5 +1,14 @@
package dev.eom.wikideck;
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
public class MainActivity extends BridgeActivity {}
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
// Local plugins must be registered before the bridge starts.
registerPlugin(MinerPlugin.class);
super.onCreate(savedInstanceState);
}
}
@@ -0,0 +1,264 @@
package dev.eom.wikideck;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
* Native RandomX engine for the WikiDeck app (sideload build only, see docs/night2/MOBILE_MINING.md).
*
* The plugin only runs the pinned `wikideck-randomx` executable (shipped as jniLibs/<abi>/libwikideck_randomx.so)
* in LIGHT mode and speaks its line protocol (docs/randomx-native.md). It never sees wallet keys: the WebView builds
* the work blob, signs the block claim with the web wallet, and submits it to the node. This class also enforces the
* device safeguards: engines are killed when the app goes to the background, on battery saver, when the device is
* hot, or when the battery is low and not charging. Each stop is reported to the WebView as a `halted` event.
*/
@CapacitorPlugin(name = "WikiDeckMiner")
public class MinerPlugin extends Plugin {
private static final String BINARY = "libwikideck_randomx.so";
private static final int MAX_THREADS = 4;
/** At or above Android's THERMAL_STATUS_MODERATE we stop. */
private static final int THERMAL_LIMIT = 2;
/** Battery temperature limit in tenths of a degree Celsius. */
private static final int BATTERY_TEMP_LIMIT = 400;
private static final int LOW_BATTERY_PERCENT = 20;
private static final Pattern HEX = Pattern.compile("^[0-9a-f]+$");
private final List<Engine> engines = new ArrayList<>();
private BroadcastReceiver receiver;
private Object thermalListener;
/** One long-lived engine process with exactly one outstanding command (it is strictly request/response). */
private static final class Engine {
final Process process;
final OutputStream in;
final BufferedReader out;
final ExecutorService worker = Executors.newSingleThreadExecutor();
Engine(Process process) {
this.process = process;
this.in = process.getOutputStream();
this.out = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.US_ASCII));
}
String command(String line) throws Exception {
in.write((line + "\n").getBytes(StandardCharsets.US_ASCII));
in.flush();
String reply = out.readLine();
if (reply == null) throw new Exception("RandomX engine exited");
return reply;
}
void kill() {
worker.shutdownNow();
try { in.close(); } catch (Exception ignored) { }
process.destroy();
try { if (!process.waitFor(1, TimeUnit.SECONDS)) process.destroyForcibly(); } catch (InterruptedException ignored) { }
}
}
private File binary() {
return new File(getContext().getApplicationInfo().nativeLibraryDir, BINARY);
}
@Override
public void load() {
receiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) { checkAndHalt(); }
};
IntentFilter filter = new IntentFilter();
filter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
getContext().registerReceiver(receiver, filter);
if (Build.VERSION.SDK_INT >= 29) {
PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
PowerManager.OnThermalStatusChangedListener l = status -> checkAndHalt();
pm.addThermalStatusListener(l);
thermalListener = l;
}
}
@Override
protected void handleOnPause() {
halt("background");
super.handleOnPause();
}
@Override
protected void handleOnDestroy() {
halt("closed");
try { getContext().unregisterReceiver(receiver); } catch (Exception ignored) { }
if (Build.VERSION.SDK_INT >= 29 && thermalListener != null) {
PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
pm.removeThermalStatusListener((PowerManager.OnThermalStatusChangedListener) thermalListener);
}
super.handleOnDestroy();
}
/** Current device state and, if mining must not run, the reason. */
private JSObject conditionsObject() {
PowerManager pm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
Intent battery = getContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int temp = battery == null ? -1 : battery.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
int level = battery == null ? -1 : battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery == null ? -1 : battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int plugged = battery == null ? 0 : battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
int percent = level >= 0 && scale > 0 ? Math.round(100f * level / scale) : -1;
int thermal = Build.VERSION.SDK_INT >= 29 ? pm.getCurrentThermalStatus() : 0;
boolean powerSave = pm.isPowerSaveMode();
String block = null;
if (powerSave) block = "battery-saver";
else if (thermal >= THERMAL_LIMIT) block = "thermal";
else if (temp >= BATTERY_TEMP_LIMIT) block = "battery-hot";
else if (plugged == 0 && percent >= 0 && percent < LOW_BATTERY_PERCENT) block = "battery-low";
JSObject o = new JSObject();
o.put("powerSave", powerSave);
o.put("thermalStatus", thermal);
o.put("batteryTempC", temp < 0 ? null : temp / 10.0);
o.put("batteryPercent", percent);
o.put("charging", plugged != 0);
o.put("cores", Runtime.getRuntime().availableProcessors());
o.put("abi", Build.SUPPORTED_ABIS.length > 0 ? Build.SUPPORTED_ABIS[0] : "unknown");
o.put("engineInstalled", binary().canExecute());
o.put("blocked", block);
return o;
}
private void checkAndHalt() {
String block = conditionsObject().getString("blocked");
if (block != null) halt(block);
}
private synchronized void halt(String reason) {
if (engines.isEmpty()) return;
for (Engine e : engines) e.kill();
engines.clear();
JSObject event = new JSObject();
event.put("reason", reason);
notifyListeners("halted", event);
}
@PluginMethod
public void conditions(PluginCall call) {
call.resolve(conditionsObject());
}
/** Runs upstream's known-vector test (Hash test 1a/2a) on this device. */
@PluginMethod
public void selfTest(PluginCall call) {
new Thread(() -> {
try {
Process p = new ProcessBuilder(binary().getAbsolutePath(), "--self-test").redirectErrorStream(true).start();
p.getOutputStream().close();
StringBuilder text = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
for (String line; (line = r.readLine()) != null && text.length() < 2000; ) text.append(line).append('\n');
boolean done = p.waitFor(120, TimeUnit.SECONDS);
if (!done) p.destroyForcibly();
JSObject o = new JSObject();
o.put("ok", done && p.exitValue() == 0);
o.put("output", text.toString().trim());
call.resolve(o);
} catch (Exception e) {
call.reject("Self-test failed to run: " + e.getMessage());
}
}).start();
}
/** Starts 1..4 light-mode engines (more than 2 only while charging) (each needs about 256 MiB). Refuses if a safeguard is active. */
@PluginMethod
public synchronized void start(PluginCall call) {
int threads = call.getInt("threads", 1);
if (threads < 1 || threads > MAX_THREADS || threads > Runtime.getRuntime().availableProcessors()) { call.reject("threads must be 1 to " + MAX_THREADS + " and at most the core count"); return; }
String block = conditionsObject().getString("blocked");
if (block == null && threads > 2 && !conditionsObject().getBool("charging")) block = "max-needs-charging";
if (block != null) { call.reject("Mining is paused: " + block, block); return; }
if (!engines.isEmpty()) { for (Engine e : engines) e.kill(); engines.clear(); }
try {
for (int i = 0; i < threads; i++) {
// No arguments = LIGHT mode. `--fast` (about 2 GiB) is never used on phones.
Process p = new ProcessBuilder(binary().getAbsolutePath()).redirectError(ProcessBuilder.Redirect.to(new File("/dev/null"))).start();
engines.add(new Engine(p));
}
} catch (Exception e) {
for (Engine en : engines) en.kill();
engines.clear();
call.reject("Could not start the RandomX engine: " + e.getMessage());
return;
}
JSObject o = new JSObject();
o.put("threads", threads);
call.resolve(o);
}
@PluginMethod
public synchronized void stop(PluginCall call) {
for (Engine e : engines) e.kill();
engines.clear();
call.resolve();
}
/**
* One bounded batch on engine `lane`: `mine <key> <blob> <target> <nonceOffset> <maxAttempts>`.
* Resolves {found:false} on exhaustion or {found:true, hash, nonce} (nonce as a decimal string).
*/
@PluginMethod
public void mine(PluginCall call) {
String key = call.getString("key", ""), blob = call.getString("blob", ""), target = call.getString("target", "");
int lane = call.getInt("lane", 0), offset = call.getInt("nonceOffset", -1), attempts = call.getInt("maxAttempts", 0);
if (key.length() != 64 || target.length() != 64 || blob.length() < 2 || blob.length() > 16384 || blob.length() % 2 != 0
|| !HEX.matcher(key).matches() || !HEX.matcher(target).matches() || !HEX.matcher(blob).matches()
|| offset < 0 || offset + 8 > blob.length() / 2 || attempts < 1 || attempts > 10000) {
call.reject("Invalid mining request");
return;
}
final Engine engine;
synchronized (this) {
if (lane < 0 || lane >= engines.size()) { call.reject("Miner is not running", "stopped"); return; }
engine = engines.get(lane);
}
String line = "mine " + key + " " + blob + " " + target + " " + offset + " " + attempts;
try {
engine.worker.execute(() -> {
try {
String reply = engine.command(line);
JSObject o = new JSObject();
if (reply.equals("exhausted")) {
o.put("found", false);
} else {
String[] parts = reply.split(" ");
if (parts.length != 3 || !parts[0].equals("found") || parts[1].length() != 64 || !HEX.matcher(parts[1]).matches()
|| !parts[2].matches("^[0-9]{1,20}$")) throw new Exception("Malformed engine reply");
o.put("found", true);
o.put("hash", parts[1]);
o.put("nonce", parts[2]);
}
call.resolve(o);
} catch (Exception e) {
call.reject("Miner stopped: " + e.getMessage(), "stopped");
}
});
} catch (Exception e) {
call.reject("Miner is not running", "stopped");
}
}
}
+5 -1
View File
@@ -1,11 +1,15 @@
#!/bin/sh
# Build the WikiDeck debug APK against the private Night-1 testnet.
# Needs ANDROID_HOME and a JDK 21 (JAVA_HOME). Output: android/app/build/outputs/apk/debug/app-debug.apk
# Needs ANDROID_HOME (with an NDK and CMake for the RandomX engine) and a JDK 21 (JAVA_HOME). Output: android/app/build/outputs/apk/debug/app-debug.apk
set -eu
cd "$(dirname "$0")/.."
export VITE_WIKIDECK_NETWORK="${VITE_WIKIDECK_NETWORK:-testnet}"
export VITE_WIKIDECK_GENESIS_TIME="${VITE_WIKIDECK_GENESIS_TIME:-1792411200}"
export VITE_NODE_URL="${VITE_NODE_URL:-http://192.168.1.32:3101}"
# Native RandomX engine (jniLibs/<abi>/libwikideck_randomx.so); built once, reused after (scripts/build-randomx-android.sh).
for abi in ${ABIS:-arm64-v8a x86_64}; do
[ -x "android/app/src/main/jniLibs/$abi/libwikideck_randomx.so" ] || ABIS="$abi" sh scripts/build-randomx-android.sh
done
npm run build
npx cap sync android
cd android && ./gradlew --no-daemon -q assembleDebug
+78
View File
@@ -0,0 +1,78 @@
# Mobile mining (Android, sideload build)
**Status (Night 2, 2026-11-17):** it works. The emulator mined and got testnet block #92 accepted with the in-app native RandomX engine (see "Results tonight"). Issue: claude/wikideck#59.
BRIEF decision 4: the mobile app must be able to mine, because otherwise mobile players can never earn the 40 owned cards a deck needs. Night 1's `isNativeApp()` hid the mining panel and the app had no miner at all.
## App stores: why this is a sideloaded APK
**Google Play and the Apple App Store both prohibit on-device cryptocurrency mining**, so the mining build cannot ship through either store. It is distributed as a sideloaded APK only.
- **Google Play**, Developer Program Policy, *Device and Network Abuse*: "We don't allow apps that mine cryptocurrency on devices." Apps that *remotely manage* mining that happens elsewhere are allowed.
- **Apple**, App Review Guidelines **3.1.5(b)(ii) Cryptocurrencies, Mining**: "Apps may not mine for cryptocurrencies unless the processing is performed off device (e.g. cloud-based mining)." Guideline 2.4.2 also bans unrelated background processes such as mining.
A WikiDeck block is a RandomX proof-of-work that mints an NFT on WikiDeck's own chain. That is cryptocurrency mining under both policies, whatever the reward looks like. Re-check the policy wording before any store submission; these quotes are from memory, not verified tonight (tests and agents make no web calls).
## How it works
```
WebView (React) Native (Java, Capacitor plugin "WikiDeckMiner")
src/MobileMiningPanel.tsx consent, intensity, stats
src/mobile-miner.ts same loop as server/testnet/miner.ts
POST /api/mining/template {owner, publicKey} ──► node (picks the article and stats; the phone never calls Wikipedia)
build blob = sha256(headerMessage) || nonceLE8
WikiDeckMiner.mine(lane, key, blob, target, 32, 16) ──► MinerPlugin.java ──stdin/stdout──► libwikideck_randomx.so
(the pinned native/ engine, LIGHT mode, line protocol)
found → hash, tokenId (mintId), sign(blockClaimMessage) with the web wallet
POST /api/blocks ──► node validates as usual
```
- **Same engine as desktop.** `native/` (RandomX v1.2.1, pinned SHA `102f8acf…`) is cross-compiled with the Android NDK by `scripts/build-randomx-android.sh` for `arm64-v8a` and `x86_64` (the emulator). It is packaged as `jniLibs/<abi>/libwikideck_randomx.so`: an executable named like a library, so Android extracts it to `nativeLibraryDir`, the one place an app may `exec` from (`useLegacyPackaging true` in `android/app/build.gradle`). The binaries are build outputs, so they're gitignored, and `android/build-debug.sh` builds them when they're missing.
- **Light mode only** (256 MiB cache per engine process). Fast mode (about 2 GiB) is never started on a phone: the plugin runs the binary with no arguments.
- **Known-vector check on the device.** Before the first start in each session, the app runs `libwikideck_randomx.so --self-test` (upstream Hash test 1a/2a). If the hash doesn't match, mining is disabled. A wrong hash would get every block rejected.
- **Keys never leave the web wallet.** The plugin only receives the public work (key, blob, target). The WebView builds and signs the block with the existing wallet code (`src/wallet.ts` `sign`). The private key and backup password are never passed to native code or to the node.
- **No protocol, consensus or server changes.** `src/mobile-miner.ts` rebuilds `miningBlob` and `mintId` with Web Crypto. `tests/mobile-mining-miner.test.ts` checks that they're byte-identical to `server/testnet/consensus.ts` and that the consensus validator accepts the assembled block.
- **Testnet only tonight.** In a mainnet build (`IS_MAINNET`), the panel only says mainnet mining isn't enabled. The debug build points at the private testnet `http://192.168.1.32:3101`.
## Safeguards
| Safeguard | How |
|---|---|
| Explicit consent | An unchecked consent box ("uses this phone's CPU and battery and can make it warm"). Start stays disabled until it's ticked. The choice is stored locally (`wikideck.mobileMining.consent.v1`). |
| Intensity | Low (1 thread, rests 400 ms between 16-hash batches), **Normal (1 thread, default)**, High (2 threads, about 512 MiB, offered only on 4+ cores). |
| Foreground only | `MinerPlugin.handleOnPause` kills the engines as soon as the activity pauses, and the WebView also stops on `visibilitychange`. There is no foreground service, no wake lock and no background mining. The web wallet also locks when the app is hidden. |
| Battery saver | `PowerManager.isPowerSaveMode()`, checked before start, every 2 s while mining, and on `ACTION_POWER_SAVE_MODE_CHANGED`. |
| Heat | `PowerManager.getCurrentThermalStatus() >= THERMAL_STATUS_MODERATE` (API 29+, with a listener), or a battery temperature of 40 °C or more (`ACTION_BATTERY_CHANGED`). |
| Low battery | Below 20 % while not charging. |
| Visible activity | Live hashrate, hashes tried, the block height being mined, accepted blocks, battery % and °C, and the self-test result. |
| Wallet change | If the wallet locks or changes, mining stops, so it never keeps mining to an old address. |
Each automatic stop shows its reason ("Paused: battery saver is on"). Mining never restarts by itself: the player taps Start again.
**Wallet idle lock:** a found block must be signed by the unlocked wallet, and the wallet locks after 5 minutes without taps. While mining is active (which means foreground and consented), each status update calls `touchWallet()` to keep the session alive. The 4-hour absolute limit and lock-on-hide still apply. night-review accepted this trade-off on PR #78. The session can't stay unlocked in the background: hiding the app locks the wallet, and `handleOnPause` halts mining, so the keep-alive applies only to consented foreground mining. Eric may prefer a stricter rule, such as re-prompting for the password when a block is found.
## Measured hashrate (emulator)
Measured on inspiron-3670 (6 cores, shared with ~7 agents), using the AVD `wikideck` (Android 15, x86_64 google_apis, KVM, `-cores 4`, 2 GB RAM):
| Measurement | Result |
|---|---|
| Known vector (`--self-test`), x86_64 | passed, 1.45 s including the 256 MiB cache init |
| Known vector, arm64-v8a (through the emulator's ARM translation) | passed |
| Raw engine, 1 process, 2000 hashes | about 53 s → **~37 H/s per thread** |
| In the app, Max (4 threads) | **~125 H/s** shown in the panel, with 4 engine processes at ~260 MB RSS each |
Light mode is about 10× slower per thread than desktop fast mode. At tonight's testnet target (`00002514…`, about 452 K expected hashes), 125 H/s means **one block per hour on average**, with high variance. On a real phone, expect roughly 20–60 H/s per big core (not measured tonight: no device).
## Results tonight
Verified in the emulator with the debug APK (`~/worker/jobs/night/mobile-mining/apk/wikideck-mining-testnet-debug.apk` on inspiron):
- The APK contains `lib/{arm64-v8a,x86_64}/libwikideck_randomx.so`. The app runs the self-test before its first start ("known vector OK"), then mines on the testnet (:3101) with live hashrate and block height.
- **Every safeguard halts the engines** (the engine process count drops to 0, and the panel shows the reason):
- thermal: `cmd thermalservice override-status 3` → "Paused: the device is warm"
- battery saver (unplugged, `settings put global low_power 1`) → "Paused: battery saver is on"
- low battery (`dumpsys battery set level 15`, unplugged) → halted (Android's low-battery dialog paused the activity first)
- background (HOME) → "Paused: the app went to the background"; the web wallet locks too, so restarting needs an unlock
- Max intensity is refused while unplugged
- **Accepted block:** at 22:37 EST the emulator (Max, ~136 H/s, 4 light threads) mined **testnet block #92** ("International FJ"; block `edaf8ac3…a8cb0`, NFT `acf778b1…7ee58`) to the in-app wallet `3b2362c7…5e054`. All three nodes (:3101–:3103) accepted it as their tip. That one was lucky: about 41 K hashes against ~452 K expected.
![Emulator panel after the accepted block](mobile-mining-accepted.jpg)
## Options for Eric
1. **Keep it sideload-only** (what exists now). Publish a signed release APK from a WikiDeck download page with clear battery and heat copy. This needs a release keystore (not in git) and an "unknown sources" install guide. F-Droid is a possible channel, but its anti-features policy would flag it, and its build server needs a reproducible NDK build.
2. **Store builds without mining.** Ship the store APK/AAB and the iOS app without the engine (a build flag that drops jniLibs and the panel), with the panel replaced by "Mine on desktop, or with the sideload app", plus a remote-management view of a desktop miner (allowed by Google's policy).
3. **Make mobile mining worthwhile.** Phones in light mode are roughly 10× slower per core than desktop fast mode. Options that need **consensus changes** (out of scope tonight): a lower-difficulty mobile lane, or pooled/shared mining where a phone's partial shares earn a card.
4. **Later engineering:** a JNI wrapper instead of exec (needed if a future Android blocks exec from `nativeLibraryDir`), an arm64 device test on real hardware, and battery-drain measurements.
-67
View File
@@ -1,67 +0,0 @@
# Night 2 private testnet: how to test (testnet-ops, #63)
**Testnet only, LAN only, on poweredge-r350.** It is never mainnet and never the live `wikideck` namespace.
## Nodes
| node | URL | game modes (env from Night 1 #22) |
|---|---|---|
| node-a | http://192.168.1.32:3101 | classic, trending |
| node-b | http://192.168.1.32:3102 | draft |
| node-c | http://192.168.1.32:3103 | classic, draft, trending |
- **Autodeploy:** the nodes follow `night/2026-11-17`. A loop on r350 (`deploy/night/autodeploy.sh`, in a dedicated detached worktree) polls the branch every 3 min.
- On a new head it rebuilds the image and restarts node-a, then b, then c, each after the previous one passes `/healthz`.
- While any match or lobby is live it defers, for up to 15 min.
- Lobbies are in memory, so a deploy that does go through ends games in progress. After night-lead merges, wait about 5 min, or check that all three nodes report the new head, before you start an end-to-end run.
- Ledgers persist in the Docker volumes across deploys.
## Test wallets
Four disposable wallets: **alice**, **bob**, **carol** and **dave**.
| wallet | address | cards (confirmed) |
|---|---|---|
| alice | `ff9273368c225698d8ae34430800cfe30b10a1c3d8c21091d42960493db556df` | see #63 |
| bob | `80f17f10ca782efd16783d10d71ce43b2993aadccda36dbd0364dee1921cbc22` | see #63 |
| carol | `f2d908633c4313bd77e86650c13d11ac3e7e656223ebe881cc34ffba3f3e0524` | leftovers |
| dave | `02bb97f202e1616ab97b45e8938e384dcbf622a0b44a7f4c1589f44236d3ccef` | leftovers |
- Confirmed at height 91: **alice 41, bob 40**, carol 5, dave 5. Block #92 came from the Android emulator (#59).
- alice and bob were consolidated to 40 cards each with on-chain NFT transfers from carol and dave, so **production `deckSize` 40 works on the testnet** and no lower-deckSize test mode is needed.
- **The encrypted browser backups are in a comment on #63**, not in git.
- Backup password: `wikideck-night-testnet`. It is **public and testnet-only**: never reuse it, and never put a real secret next to it.
- Anyone who can read this repo's issues can open these wallets. Keep them on the LAN-only testnet, and never send them anything of value.
- Re-check counts at any time: `GET http://192.168.1.32:3101/api/decks/<address>?offset=0&limit=24` (pages of 24, follow `next`).
### Load a wallet in the browser
**First make the node origins a secure context** (found by night-review on #63).
- `http://192.168.1.32:310x` is plain HTTP on a LAN address, so the browser leaves `crypto.subtle` undefined. Open wallet then fails with "Cannot read properties of undefined (reading 'digest')" or "Wallet storage unavailable".
- **Chrome/Chromium:** open `chrome://flags/#unsafely-treat-insecure-origin-as-secure`, add `http://192.168.1.32:3101,http://192.168.1.32:3102,http://192.168.1.32:3103`, then relaunch.
- **Command line:** `--unsafely-treat-insecure-origin-as-secure=http://192.168.1.32:3101,http://192.168.1.32:3102,http://192.168.1.32:3103 --user-data-dir=<fresh profile dir>`.
- **Playwright:** use the full `chromium` channel. The headless shell ignores this flag.
- **SSH tunnel to `localhost`:** this also works for the wallet, but it breaks the Dex callback URLs, so you can't do the login/link step that way.
- **Firefox** has no per-origin equivalent.
Use a separate browser profile per player, because a browser holds one wallet per origin.
1. Open a node URL, then **Open wallet**.
2. **Recover from a local backup**: choose the backup JSON file (save it from the #63 comment as `alice.backup.json`), and enter the backup password above.
3. Set a new local encryption password (at least 12 characters, anything testnet-only), then confirm it.
4. The wallet dialog should show the address above and its confirmed card count.
### eom.dev login (Dex)
- There is no new Dex config tonight. The existing `wikideck` client already allows the 3101–3103 callbacks.
- Log in with **Log in with eom.dev**, then unlock the wallet and press **Link wallet …** to link it to the account.
- PvP seats are keyed to the account, and ownership is checked on the server against the account's linked wallets. So for a two-player run you need **two eom.dev accounts**, one with alice linked and one with bob. Which accounts to use is night-lead's call (see #53/#63). Never use a real person's account without their OK.
## Mining on the testnet
- Mining isn't needed for tests. Only run a miner if night-lead asks for more blocks, or to confirm transfers.
- `deploy/night/testnet.sh mine N [workers]` runs the native multi-worker miner in a container named `wikideck-night-miner` (at most 4 workers on r350).
- Every template costs Wikipedia requests on the node, so it is paced by `MIN_GAP`.
- **testnet-ops stops any miner by 01:55** (`docker rm -f wikideck-night-miner`). The nodes stay up.
- **Difficulty (for mobile mining, #59):** at block ~90, an accepted block took between about 2 K and 130 K hashes. That is up to ~8 min at the 4-worker r350 rate of ~250 H/s. One light-mode thread on the emulator is far slower, so expect a long wait per block, with high variance.
## Card identity pre-bake (engineer WS E)
- `server/game/identity.ts` is a persistent per-token store (`node:sqlite`). It freezes each card's off-chain identity at first sight and never rewrites it. The identity covers P31/P279 classes, description, categories, countries, years, coordinates and outlink pageIds. Its fields match duel-cards' `CardProfile` (#70), and `toProfile()` returns that view.
- `scripts/night2/prebake-identity.ts --node <url> [--store <file>] [--cards <cached.json> ...]` reads the canonical chain from one node and freezes what the cached card files know. The store defaults to `$WIKIDECK_DATA_DIR/card-identity.sqlite` (`identityStoreFile()`), and hosts should open it at the same path.
- It **never calls Wikimedia itself**.
- Live facts arrive only through the wikimedia team's client on its `background` lane (#62), once that client has merged.
- Cards that are still unknown stay unfrozen and are retried on a later run.
Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

+35
View File
@@ -0,0 +1,35 @@
#!/bin/sh
# Cross-compile the pinned native RandomX engine (native/, RandomX v1.2.1) for the Android app.
# Output: android/app/src/main/jniLibs/<abi>/libwikideck_randomx.so, an executable named like a
# library so Android extracts it to nativeLibraryDir, where an app may exec it (docs/night2/MOBILE_MINING.md).
#
# Needs ANDROID_HOME with an NDK (ndk;<version>) and CMake 3.24+ (cmake;<version>, or cmake on PATH).
# ABIS="x86_64 arm64-v8a" JOBS=2 sh scripts/build-randomx-android.sh
set -eu
cd "$(dirname "$0")/.."
: "${ANDROID_HOME:?set ANDROID_HOME}"
ABIS="${ABIS:-arm64-v8a x86_64}"
JOBS="${JOBS:-2}"
API="${ANDROID_API:-24}"
NDK="${ANDROID_NDK_HOME:-$(ls -d "$ANDROID_HOME"/ndk/* 2>/dev/null | sort -V | tail -n1)}"
[ -f "$NDK/build/cmake/android.toolchain.cmake" ] || { echo "No NDK under $ANDROID_HOME/ndk" >&2; exit 1; }
CMAKE_DIR="$(ls -d "$ANDROID_HOME"/cmake/* 2>/dev/null | sort -V | tail -n1 || true)"
[ -n "$CMAKE_DIR" ] && PATH="$CMAKE_DIR/bin:$PATH"
command -v cmake >/dev/null || { echo "cmake not found" >&2; exit 1; }
GEN=""
command -v ninja >/dev/null && GEN="-G Ninja"
for abi in $ABIS; do
build="native/build-android-$abi"
# shellcheck disable=SC2086
cmake -S native -B "$build" $GEN \
-DCMAKE_TOOLCHAIN_FILE="$NDK/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI="$abi" -DANDROID_PLATFORM="android-$API" \
-DCMAKE_BUILD_TYPE=Release -DWIKIDECK_RANDOMX_TESTS=OFF
cmake --build "$build" --config Release --target wikideck-randomx --parallel "$JOBS"
out="android/app/src/main/jniLibs/$abi"
mkdir -p "$out"
cp "$build/wikideck-randomx" "$out/libwikideck_randomx.so"
"$NDK"/toolchains/llvm/prebuilt/*/bin/llvm-strip "$out/libwikideck_randomx.so"
ls -l "$out/libwikideck_randomx.so"
done
-56
View File
@@ -1,56 +0,0 @@
// testnet-ops (engineer WS E): pre-bake frozen card identities for every card on a chain.
//
// node --experimental-strip-types scripts/night2/prebake-identity.ts --node http://192.168.1.32:3101 \
// --store $WIKIDECK_DATA_DIR/card-identity.sqlite [--cards docs/game/card-sample.json] [--batch 50]
//
// - Reads the canonical chain from ONE node (GET /api/summary, then /api/canonical pages), so every
// card is built from its mint block, never from client JSON.
// - Facts come ONLY from cached card files (`--cards`, repeatable). This script never calls
// Wikipedia or Wikidata itself. Live facts must go through the wikimedia team's process-wide client
// on its `background` lane (server/wikimedia, #62); that IdentitySource plugs into `prebake()` in
// server/game/identity.ts once it has merged. Until then, unknown cards are left unfrozen and are
// picked up by a later run.
// - Idempotent and freeze-at-first-sight: already frozen tokens are skipped and never rewritten.
import { readFileSync } from 'node:fs';
import { parseArgs } from 'node:util';
import { IdentityStore, cachedSource, identityStoreFile, prebake, type ChainArticle } from '../../server/game/identity.ts';
const { values } = parseArgs({ options: {
node: { type: 'string', default: 'http://192.168.1.32:3101' },
store: { type: 'string', default: identityStoreFile() },
cards: { type: 'string', multiple: true, default: ['docs/game/card-sample.json'] },
batch: { type: 'string', default: '50' },
} });
const nodeUrl = new URL(values.node!);
if (!['http:', 'https:'].includes(nodeUrl.protocol)) throw new Error('--node must be an http(s) WikiDeck node URL');
if (/wikipedia\.org|wikidata\.org|wikimedia\.org/i.test(nodeUrl.hostname)) throw new Error('--node must be a WikiDeck node, not Wikimedia');
async function get<T>(path: string): Promise<T> {
const r = await fetch(new URL(path, nodeUrl), { signal: AbortSignal.timeout(30000), redirect: 'error' });
if (!r.ok) throw new Error(`${path}: HTTP ${r.status}`);
return await r.json() as T;
}
type Block = { tokenId: string; article: { pageId: number; title: string; revisionId?: number } };
async function chainArticles(): Promise<ChainArticle[]> {
const { tip } = await get<{ tip: string }>('/api/summary');
const out: ChainArticle[] = [];
for (let offset: number | null = 0; offset !== null;) {
const page: { blocks: Block[]; next: number | null } = await get(`/api/canonical?tip=${tip}&offset=${offset}&limit=16`);
for (const b of page.blocks) if (b.article && b.tokenId)
out.push({ tokenId: b.tokenId, pageId: b.article.pageId, title: b.article.title, revisionId: b.article.revisionId ?? null });
offset = page.next;
}
return out;
}
const cards = values.cards!.flatMap(f => {
const d = JSON.parse(readFileSync(f, 'utf8'));
return Array.isArray(d) ? d : d.cards ?? [];
});
const articles = await chainArticles();
const store = new IdentityStore(values.store!);
try {
const report = await prebake(store, cachedSource(cards), articles, { batchSize: Number(values.batch) });
console.log(JSON.stringify({ node: nodeUrl.origin, chainCards: articles.length, cachedCards: cards.length, ...report, stored: store.count() }));
} finally { store.close(); }
-113
View File
@@ -1,113 +0,0 @@
// Live IdentitySource for server/game/identity.ts (engineer WS E, data §3).
//
// Every request goes through the process-wide polite Wikimedia client on the **background** lane:
// - the page enrichment (description, wikidataId, categories) via `enrichment.getBackground()`,
// which also feeds the disk cache card faces use;
// - one `wbgetentities&props=claims` per batch of at most 50 items;
// - one `wbgetentities&props=labels` per 50 class ids still missing a label.
// That is about 2 Wikidata requests per 50 new cards, plus one cached enrichment per card.
// Outlinks (prop=links, data §4) are not fetched tonight: they cost about 1.2 requests per card,
// and Connected falls back to its other tiers without them.
//
// The profile itself is built by duel-cards' pure `profileFromSources()` (shared/duel/profile.ts, #70),
// so the frozen identity and what derive() reads cannot drift apart. That module, and the client
// (server/wikimedia, #73), are injected structurally, so this file has no import from unmerged PRs.
// Wiring, once both have merged:
// wikimediaIdentitySource({ client: wikimediaClient, enrichment: wikipediaEnrichment,
// profile: await import('../../shared/duel/profile.ts') })
// Any transport failure leaves the affected cards unknown (undefined), so they are retried later,
// never frozen half-empty.
import type { PageEnrichment } from '../../shared/enrichment.ts';
import type { ChainArticle, IdentityFacts, IdentitySource, WikidataClass } from './identity.ts';
/** Structural slices of server/wikimedia/client.ts (#73) and shared/duel/profile.ts (#70). */
export type BackgroundRequest = { lane: 'background'; timeoutMs: number; cache?: { ttlMs: number; maxBytes?: number } };
export type WikidataEntity = { id?: string; claims?: Record<string, readonly {
rank?: string; mainsnak?: { snaktype?: string; datavalue?: { type?: string; value?: unknown } } }[]> };
export type ProfileLike = { wikidataId: string | null; description: string | null; categories: readonly string[];
instanceOf: readonly WikidataClass[]; subclassOf?: readonly WikidataClass[]; countries?: readonly string[];
years?: readonly number[]; died?: boolean; links?: readonly number[]; cites?: readonly string[]; fetchedRevisionId?: number };
export type ProfileBuilder = {
wikidataEntityParams(qids: readonly string[]): Record<string, string>;
wikidataLabelParams(qids: readonly string[]): Record<string, string>;
classIdsOf(entity: WikidataEntity | null | undefined): string[];
profileFromSources(src: { enrichment: PageEnrichment; entity?: WikidataEntity | null; labels?: Readonly<Record<string, string>> }): ProfileLike;
};
export type IdentitySourceDeps = {
enrichment: { getBackground(pageId: number): Promise<PageEnrichment> };
client: { request(url: URL, options: BackgroundRequest): Promise<{ response: Response }> };
profile: ProfileBuilder;
};
const WIKIDATA_API = 'https://www.wikidata.org/w/api.php';
const MAX_BYTES = 4 * 1024 * 1024;
const CACHE = { ttlMs: 7 * 24 * 60 * 60 * 1000, maxBytes: MAX_BYTES };
async function wikidata(client: IdentitySourceDeps['client'], params: Record<string, string>): Promise<Record<string, unknown>> {
const url = new URL(WIKIDATA_API);
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
const { response } = await client.request(url, { lane: 'background', timeoutMs: 15000, cache: CACHE });
if (!response.ok) { await response.body?.cancel(); throw new Error(`Wikidata HTTP ${response.status}`); }
const text = await response.text();
if (text.length > MAX_BYTES) throw new Error('Wikidata response too large');
const body = JSON.parse(text) as { entities?: Record<string, unknown> | unknown[] };
const entities = body.entities ?? {};
// formatversion=2 returns an object keyed by id; tolerate an array too.
return Array.isArray(entities)
? Object.fromEntries(entities.filter(e => e && typeof (e as { id?: unknown }).id === 'string').map(e => [(e as { id: string }).id, e]))
: entities as Record<string, unknown>;
}
const chunks = <T>(xs: T[], n: number) => Array.from({ length: Math.ceil(xs.length / n) }, (_, i) => xs.slice(i * n, i * n + n));
export function wikimediaIdentitySource(deps: IdentitySourceDeps): IdentitySource {
const { classIdsOf, profileFromSources, wikidataEntityParams, wikidataLabelParams } = deps.profile;
return {
kind: 'wikimedia',
async lookup(articles: ChainArticle[]) {
const out = new Map<number, IdentityFacts | undefined>(articles.map(a => [a.pageId, undefined]));
// 1. Enrichment, one page at a time (the client paces it); failures stay unknown.
const enriched = new Map<number, PageEnrichment>();
for (const a of articles) {
try { enriched.set(a.pageId, await deps.enrichment.getBackground(a.pageId)); } catch { /* retry later */ }
}
// 2. Claims for every item we know, in batches of ≤ 50.
const qids = [...new Set([...enriched.values()].map(e => e.wikidataId).filter((q): q is string => !!q && /^Q\d+$/.test(q)))];
const entities = new Map<string, WikidataEntity>();
let claimsFailed = false;
for (const batch of chunks(qids, 50)) {
try { for (const [id, e] of Object.entries(await wikidata(deps.client, wikidataEntityParams(batch)))) entities.set(id, e as WikidataEntity); }
catch { claimsFailed = true; }
}
// 3. English labels for the P31/P279 classes.
const classIds = [...new Set([...entities.values()].flatMap(e => classIdsOf(e)))];
const labels: Record<string, string> = {};
let labelsFailed = false;
for (const batch of chunks(classIds, 50)) {
try {
for (const [id, e] of Object.entries(await wikidata(deps.client, wikidataLabelParams(batch)))) {
const v = (e as { labels?: { en?: { value?: unknown } } }).labels?.en?.value;
if (typeof v === 'string') labels[id] = v;
}
} catch { labelsFailed = true; }
}
// 4. Freeze only complete profiles: a card whose item couldn't be read stays unknown.
for (const a of articles) {
const e = enriched.get(a.pageId);
if (!e) continue;
const qid = e.wikidataId && /^Q\d+$/.test(e.wikidataId) ? e.wikidataId : null;
const entity = qid ? entities.get(qid) : null;
if (qid && !entity && claimsFailed) continue; // item not read: retry later
if (entity && labelsFailed) continue; // class labels not read: retry later
const p = profileFromSources({ enrichment: e, entity: entity ?? null, labels });
out.set(a.pageId, {
fetchedRevisionId: p.fetchedRevisionId ?? null, wikidataId: p.wikidataId, description: p.description,
categories: [...p.categories], instanceOf: [...p.instanceOf], subclassOf: [...(p.subclassOf ?? [])],
countries: [...(p.countries ?? [])], years: [...(p.years ?? [])], died: p.died === true,
links: [...(p.links ?? [])], cites: [...(p.cites ?? [])],
});
}
return out;
},
};
}
-204
View File
@@ -1,204 +0,0 @@
// Card identity (engineer WS E, data review §3): the off-chain, mutable facts about a card's article
// (P31/P279 classes, a few Wikidata claims, categories, outlinks) frozen ONCE per tokenId, the first time
// the card is seen, so that a later Wikipedia/Wikidata edit can never retype a card someone owns.
//
// - On-chain data (article, statsSnapshot) stays the source of truth for everything it covers.
// - Identities are never fetched at match time. They are pre-baked in the background
// (scripts/night2/prebake-identity.ts), from cached data or through an injected IdentitySource that
// must use the process-wide Wikimedia client on its `background` lane. This file performs no I/O
// besides the sqlite store and never calls fetch itself.
// - First write wins: `freeze` never overwrites an existing record (data §3 "freeze at first sight").
import { DatabaseSync } from 'node:sqlite';
import { createHash } from 'node:crypto';
import { join, resolve } from 'node:path';
export const IDENTITY_VERSION = 1;
/** Where hosts keep the store: next to the ledger, `$WIKIDECK_DATA_DIR/card-identity.sqlite` (default ./data). */
export function identityStoreFile(env: NodeJS.ProcessEnv = process.env): string {
return join(env.WIKIDECK_DATA_DIR ?? resolve('data'), 'card-identity.sqlite');
}
/** Same shape as shared/duel/classify.ts `WikidataClass` (duel-cards, #70). */
export type WikidataClass = { id: string; label: string };
/**
* A frozen card identity. Its profile fields are structurally the `CardProfile` that duel-cards'
* `derive()` reads (shared/duel/classify.ts), so `toProfile(identity)` can be passed straight in.
*/
export type CardIdentity = {
version: typeof IDENTITY_VERSION;
tokenId: string;
pageId: number;
title: string;
/** article.revisionId from the mint block. */
mintRevisionId: number | null;
/** The live revision the off-chain facts were read from (data §3; may differ from the mint revision). */
fetchedRevisionId: number | null;
wikidataId: string | null;
wikidataRev: number | null;
/** Wikipedia short description. */
description: string | null;
/** Non-hidden category titles without the "Category:" prefix, sorted. */
categories: string[];
/** P31 (instance of). */
instanceOf: WikidataClass[];
/** P279 of the item, or its P31 classes' superclasses, nearest first. */
subclassOf: WikidataClass[];
/** P17 / P27 country Q-ids, sorted. */
countries: string[];
/** Years from P569/P570/P571/P580/P585 (negative = BCE), sorted. */
years: number[];
/** P570 (date of death) present. */
died: boolean;
/** P625 or prop=coordinates, [lat, lon]. */
coords: [number, number] | null;
/** ns0 outlinks as pageIds, sorted, deduplicated (Connected, Linked tier). Empty when unknown. */
links: number[];
/** Item Q-ids cited by the frozen claims (P17, P27, P131, P361, P106, ...). */
cites: string[];
/** sha256 of `links`, so match records can log a short fingerprint. */
linksHash: string;
/** Where the facts came from: an offline cache (e.g. card-sample.json) or a live background fetch. */
source: 'cache' | 'wikimedia';
frozenAt: string;
};
/** What a source knows about an article; any field may be missing. */
export type IdentityFacts = Partial<Pick<CardIdentity, 'fetchedRevisionId' | 'wikidataId' | 'wikidataRev' | 'description'
| 'categories' | 'instanceOf' | 'subclassOf' | 'countries' | 'years' | 'died' | 'coords' | 'links' | 'cites'>>;
/** The minimum the chain tells us about a card (built from its mint block, never from client JSON). */
export type ChainArticle = { tokenId: string; pageId: number; title: string; revisionId?: number | null };
/**
* A source of identity facts. A live implementation must route every request through the shared
* Wikimedia client on the background lane (never the mint or interactive lanes) and batch where it
* can. Returning `undefined` for a page means "unknown for now": nothing is frozen and it is retried
* on a later run.
*/
export interface IdentitySource {
readonly kind: CardIdentity['source'];
lookup(articles: ChainArticle[]): Promise<Map<number, IdentityFacts | undefined>>;
}
export function linksHash(links: readonly number[]): string {
return createHash('sha256').update(JSON.stringify(links)).digest('hex');
}
const QID = /^Q\d+$/;
const uniqStrings = (xs: readonly unknown[] | undefined) => [...new Set((xs ?? []).filter((x): x is string => typeof x === 'string'))].sort();
const uniqInts = (xs: readonly unknown[] | undefined) =>
[...new Set((xs ?? []).filter((x): x is number => Number.isSafeInteger(x)))].sort((a, b) => a - b);
const classes = (xs: readonly WikidataClass[] | undefined) =>
(xs ?? []).filter(c => c && QID.test(c.id)).map(c => ({ id: c.id, label: typeof c.label === 'string' ? c.label : '' }));
/** Pure: build a frozen identity from chain data plus whatever facts a source returned. */
export function buildIdentity(article: ChainArticle, facts: IdentityFacts, source: CardIdentity['source'], now: Date): CardIdentity {
const links = uniqInts(facts.links);
const coords = facts.coords && facts.coords.length === 2 && facts.coords.every(Number.isFinite) ? [facts.coords[0], facts.coords[1]] as [number, number] : null;
return {
version: IDENTITY_VERSION,
tokenId: article.tokenId,
pageId: article.pageId,
title: article.title,
mintRevisionId: article.revisionId ?? null,
fetchedRevisionId: facts.fetchedRevisionId ?? null,
wikidataId: facts.wikidataId && QID.test(facts.wikidataId) ? facts.wikidataId : null,
wikidataRev: facts.wikidataRev ?? null,
description: typeof facts.description === 'string' ? facts.description : null,
categories: uniqStrings(facts.categories),
instanceOf: classes(facts.instanceOf),
subclassOf: classes(facts.subclassOf),
countries: uniqStrings(facts.countries),
years: uniqInts(facts.years),
died: facts.died === true,
coords,
links,
cites: uniqStrings(facts.cites).filter(q => QID.test(q)),
linksHash: linksHash(links),
source,
frozenAt: now.toISOString(),
};
}
/** The duel-cards `CardProfile` view of an identity (structural; no import, so no stacked PR). */
export function toProfile(i: CardIdentity) {
return { wikidataId: i.wikidataId, description: i.description, categories: i.categories, instanceOf: i.instanceOf,
subclassOf: i.subclassOf, countries: i.countries, years: i.years, died: i.died, links: i.links, cites: i.cites,
...(i.fetchedRevisionId != null ? { fetchedRevisionId: i.fetchedRevisionId } : {}) };
}
/** Persistent per-token identity store. Records are immutable once written. */
export class IdentityStore {
readonly #db: DatabaseSync;
constructor(file: string) {
this.#db = new DatabaseSync(file);
this.#db.exec(`CREATE TABLE IF NOT EXISTS card_identity (
token_id TEXT PRIMARY KEY, page_id INTEGER NOT NULL, version INTEGER NOT NULL, body TEXT NOT NULL)`);
}
get(tokenId: string): CardIdentity | undefined {
const row = this.#db.prepare('SELECT body FROM card_identity WHERE token_id = ?').get(tokenId) as { body: string } | undefined;
return row ? JSON.parse(row.body) as CardIdentity : undefined;
}
has(tokenId: string): boolean { return this.get(tokenId) !== undefined; }
/** Freeze at first sight: returns the stored record, which is the earlier one if it already existed. */
freeze(identity: CardIdentity): CardIdentity {
this.#db.prepare('INSERT OR IGNORE INTO card_identity (token_id, page_id, version, body) VALUES (?, ?, ?, ?)')
.run(identity.tokenId, identity.pageId, identity.version, JSON.stringify(identity));
return this.get(identity.tokenId)!;
}
/** Freeze several in one transaction (one fsync per batch, not per card). */
freezeMany(identities: CardIdentity[]): void {
this.#db.exec('BEGIN');
try { for (const i of identities) this.freeze(i); this.#db.exec('COMMIT'); }
catch (e) { this.#db.exec('ROLLBACK'); throw e; }
}
count(): number { return (this.#db.prepare('SELECT COUNT(*) AS n FROM card_identity').get() as { n: number }).n; }
all(): CardIdentity[] {
return (this.#db.prepare('SELECT body FROM card_identity ORDER BY token_id').all() as { body: string }[]).map(r => JSON.parse(r.body));
}
close() { this.#db.close(); }
}
export type PrebakeReport = { seen: number; alreadyFrozen: number; frozen: number; unknown: number };
/**
* Freeze identities for every card not yet in the store, asking the source in batches. The caller
* controls pacing (batch size, and the source's client controls request rate).
*/
export async function prebake(store: IdentityStore, source: IdentitySource, articles: ChainArticle[],
opts: { batchSize?: number; now?: () => Date } = {}): Promise<PrebakeReport> {
const batchSize = Math.max(1, Math.min(50, opts.batchSize ?? 50)), now = opts.now ?? (() => new Date());
const seenTokens = new Set<string>(), todo: ChainArticle[] = [];
let alreadyFrozen = 0;
for (const a of articles) {
if (seenTokens.has(a.tokenId)) continue;
seenTokens.add(a.tokenId);
if (store.has(a.tokenId)) alreadyFrozen++; else todo.push(a);
}
let frozen = 0, unknown = 0;
for (let i = 0; i < todo.length; i += batchSize) {
const batch = todo.slice(i, i + batchSize), facts = await source.lookup(batch), ready = [];
for (const a of batch) {
const f = facts.get(a.pageId);
if (!f) { unknown++; continue; }
ready.push(buildIdentity(a, f, source.kind, now()));
}
store.freezeMany(ready); frozen += ready.length;
}
return { seen: seenTokens.size, alreadyFrozen, frozen, unknown };
}
/** Offline source: facts from an already-cached card file such as docs/game/card-sample.json. */
export function cachedSource(cards: readonly { pageId: number; wikidataId?: string | null; revisionId?: number | null;
description?: string | null; instanceOf?: WikidataClass[]; categories?: string[] }[]): IdentitySource {
const byPage = new Map(cards.map(c => [c.pageId, c]));
return {
kind: 'cache',
async lookup(articles) {
return new Map(articles.map(a => {
const c = byPage.get(a.pageId);
return [a.pageId, c ? { fetchedRevisionId: c.revisionId ?? null, wikidataId: c.wikidataId ?? null,
description: c.description ?? null, instanceOf: c.instanceOf ?? [], categories: c.categories ?? [] } : undefined];
}));
},
};
}
+3 -1
View File
@@ -16,6 +16,7 @@ import { IS_MAINNET, NETWORK_GENESIS_TIME, NETWORK_LABEL } from '../shared/netwo
import { StateClient } from '../shared/rpc-client.ts';
import { PlaySection } from './game/PlaySection.tsx';
import { isNativeApp } from './native.ts';
import { MobileMiningPanel } from './MobileMiningPanel.tsx';
import { Landing } from './Landing.tsx';
import './App.css';
import './testnet.css';
@@ -190,7 +191,8 @@ export default function App() {
<div><span className="metric-icon"><WalletIcon size={19} /></span><div><span>Confirmed ownership</span><strong>{wallet && snapshot ? owned.length : '—'}<small>in your wallet</small></strong></div></div>
<div><span className="metric-icon"><Cpu size={19} /></span><div><span>Proof of work</span><strong className="algorithm">{snapshot?.algorithm ?? 'RandomX'}<small>native CPU mining</small></strong></div></div>
</section>
<section className="mining-panel native-mining" ref={minerPanel} hidden={isNativeApp()}>
{isNativeApp() && <MobileMiningPanel ref={minerPanel} wallet={wallet} onAccepted={() => { void refresh().catch(() => {}); }} />}
<section className="mining-panel native-mining" ref={isNativeApp() ? undefined : minerPanel} hidden={isNativeApp()}>
<div className="mining-description"><div className="section-eyebrow"><span className="idle-dot" /> RANDOMX · DESIGNED FOR GENERAL-PURPOSE CPUS</div><h2>Mining should be for more people.</h2><p>Wikideck and Monero use RandomX, an algorithm designed to resist specialized ASICs and keep general-purpose CPUs competitive. That can make mining more accessible—not make every machine equally powerful or promise rewards. Each accepted Wikideck block earns its miner a distinct illustrated Wikipedia card. Wikideck is its own independent network.</p><div className="mining-meta"><span><Cpu size={13} /> Mine with a CPU</span><span><Shield size={13} /> Independently validating peers</span></div><details className="mining-details"><summary>Hardware, rewards &amp; network details</summary><div className="native-memory"><strong>Light · default</strong><span>256 MiB cache plus scratchpad and process overhead.</span><strong>Fast · --fast</strong><span>About 2 GiB dataset plus cache; budget over 2.3 GiB per process. Mining uses CPU, memory, electricity and time. ASIC resistance is a design goal, not a guarantee; results depend on hardware and network difficulty. Wikideck does not share Monero’s network, hashpower or security.</span></div></details></div>
<div className="miner-controls"><div className="miner-display"><div><span>NEXT WIKIPEDIA ARTICLE</span><strong>{snapshot ? 'Chosen when the miner requests work' : `Waiting for ${NETWORK_LABEL} RPC…`}</strong></div></div><p className="muted">The miner selects a random illustrated English Wikipedia article. The same article can appear again; each card keeps its own mint-time statistics.</p><details className="mining-details"><summary>Want to run a miner? Setup &amp; safety details</summary><ol className="native-steps"><li>Build <code>wikideck-randomx</code> using the repository’s native build instructions.</li><li>Save your encrypted backup and keep its password separate.</li><li>Replace <code>&lt;backup.json&gt;</code> with your local file path (quote paths with spaces), then run:</li></ol><pre className="native-command"><code>{nativeCommand}</code></pre><p className="miner-footnote">Node: {window.location.origin}. The command connects to this node; use HTTPS for remote nodes. Password entry stays local—never put it in the command. It defaults to one worker and at most one block. Ctrl+C stops the native miner; closing this page does not.</p></details><button className="button dark full" onClick={openWallet}><Download size={16} /> Download wallet backup</button><button className="text-button" onClick={() => setDialog('guide')}>Read the {NETWORK_LABEL.toLowerCase()} field guide <ArrowRight size={14} /></button></div>
</section>
+144
View File
@@ -0,0 +1,144 @@
// Android (sideload build) mining panel. Rendered only inside the native app; see docs/night2/MOBILE_MINING.md.
import { forwardRef, useEffect, useRef, useState } from 'react';
import { registerPlugin, type PluginListenerHandle } from '@capacitor/core';
import { Cpu, Pickaxe, Square } from 'lucide-react';
import { IS_MAINNET, NETWORK_LABEL } from '../shared/network.ts';
import { expectedChainIdentity } from '../shared/chain-identity.ts';
import { sign, touchWallet, type Wallet } from './wallet.ts';
import { nodeHref } from './native.ts';
import { MobileMiner, type MinerEngine, type MinerStatus, type MineResult } from './mobile-miner.ts';
import './mobile-mining.css';
type Conditions = { powerSave: boolean; thermalStatus: number; batteryTempC: number | null; batteryPercent: number;
charging: boolean; cores: number; abi: string; engineInstalled: boolean; blocked: string | null };
interface MinerPlugin {
conditions(): Promise<Conditions>;
selfTest(): Promise<{ ok: boolean; output: string }>;
start(o: { threads: number }): Promise<{ threads: number }>;
stop(): Promise<void>;
mine(o: { lane: number; key: string; blob: string; target: string; nonceOffset: number; maxAttempts: number }): Promise<MineResult>;
addListener(event: 'halted', fn: (e: { reason: string }) => void): Promise<PluginListenerHandle>;
}
const Native = registerPlugin<MinerPlugin>('WikiDeckMiner');
const engine: MinerEngine = {
start: async threads => { await Native.start({ threads }); },
stop: () => Native.stop(),
mine: (lane, key, blob, target, nonceOffset, maxAttempts) => Native.mine({ lane, key, blob, target, nonceOffset, maxAttempts }),
};
const CONSENT_KEY = 'wikideck.mobileMining.consent.v1';
const INTENSITY_KEY = 'wikideck.mobileMining.intensity';
const INTENSITIES = {
low: { label: 'Low: 1 thread, rests between batches', threads: 1, idleMs: 400 },
normal: { label: 'Normal: 1 thread', threads: 1, idleMs: 0 },
high: { label: 'High: 2 threads (about 512 MiB)', threads: 2, idleMs: 0 },
max: { label: 'Max: 4 threads (about 1 GiB), only while charging', threads: 4, idleMs: 0 },
} as const;
type Intensity = keyof typeof INTENSITIES;
const REASONS: Record<string, string> = {
background: 'the app went to the background', 'battery-saver': 'battery saver is on', thermal: 'the device is warm',
'battery-hot': 'the battery is hot', 'battery-low': 'the battery is below 20% and not charging', 'max-needs-charging': 'Max intensity needs the charger', closed: 'the app closed',
};
const explain = (reason: string) => REASONS[reason] ?? reason;
async function api(path: string, body?: unknown): Promise<unknown> {
const url = path.startsWith('/p2p/') ? nodeHref(path) : path; // /api/... is routed to the node by native.ts
const res = await fetch(url, { method: body ? 'POST' : 'GET', cache: 'no-store', signal: AbortSignal.timeout(30000),
headers: body ? { 'Content-Type': 'application/json' } : {}, body: body ? JSON.stringify(body) : undefined });
const text = await res.text();
if (text.length > 262144) throw new Error('Node response too large');
const data = text ? JSON.parse(text) : {};
if (!res.ok) throw new Error(data.error ?? `Node rejected request (${res.status})`);
return data;
}
export const MobileMiningPanel = forwardRef<HTMLElement, { wallet?: Wallet; onAccepted?: () => void }>(function MobileMiningPanel({ wallet, onAccepted }, ref) {
const [consent, setConsent] = useState(() => localStorage.getItem(CONSENT_KEY) === 'yes');
const [intensity, setIntensity] = useState<Intensity>(() => (localStorage.getItem(INTENSITY_KEY) as Intensity) in INTENSITIES ? localStorage.getItem(INTENSITY_KEY) as Intensity : 'normal');
const [conditions, setConditions] = useState<Conditions>();
const [status, setStatus] = useState<MinerStatus>();
const [check, setCheck] = useState<{ ok: boolean; output: string }>();
const [error, setError] = useState('');
const miner = useRef<MobileMiner | undefined>(undefined);
const accepted = useRef(0);
const active = !!status && ['starting', 'requesting', 'mining', 'submitting'].includes(status.phase);
useEffect(() => {
let live = true;
const poll = () => Native.conditions().then(c => { if (live) setConditions(c); }).catch(() => {});
void poll();
const timer = setInterval(poll, 5000);
const halted = Native.addListener('halted', e => { void miner.current?.stop(`Paused: ${explain(e.reason)}`); });
const hide = () => { if (document.visibilityState !== 'visible') void miner.current?.stop(`Paused: ${explain('background')}`); };
document.addEventListener('visibilitychange', hide);
return () => { live = false; clearInterval(timer); void halted.then(h => h.remove()); document.removeEventListener('visibilitychange', hide); void miner.current?.stop(); };
}, []);
// A different (or locked) wallet must never keep mining to the previous address.
useEffect(() => { if (miner.current?.active) void miner.current.stop('Stopped: the wallet changed or locked.'); }, [wallet]);
const start = async () => {
setError('');
if (!wallet) { setError('Unlock your wallet first: cards are mined to its address.'); return; }
try {
let result = check;
if (!result?.ok) { result = await Native.selfTest(); setCheck(result); }
if (!result.ok) { setError('The RandomX engine failed its known-vector self-test on this device, so mining is disabled.'); return; }
const { chainId } = await expectedChainIdentity();
const preset = INTENSITIES[intensity];
const current = new MobileMiner({
engine, api, chainId, idleMs: preset.idleMs,
sign: message => sign(wallet, message),
blocked: async () => {
const c = await Native.conditions(); setConditions(c);
if (c.blocked) return explain(c.blocked);
if (preset.threads > 2 && !c.charging) return explain('max-needs-charging');
return document.visibilityState !== 'visible' ? explain('background') : undefined;
},
onStatus: s => {
// Mining runs only in the foreground with consent; keep the unlocked session alive so a found block can be signed.
touchWallet();
setStatus(s);
if (s.accepted > accepted.current) { accepted.current = s.accepted; onAccepted?.(); }
},
}, wallet);
miner.current = current;
void current.start(preset.threads);
} catch (e) { setError((e as Error).message); }
};
const stop = () => { void miner.current?.stop('Stopped.'); };
if (IS_MAINNET) return <section className="mining-panel mobile-mining" ref={ref}><h2>Mining</h2>
<p className="muted">This build mines on the private testnet only. Mainnet mining from the app is not enabled.</p></section>;
const blockedNow = conditions?.blocked;
return <section className="mining-panel mobile-mining" ref={ref} aria-label="Mine on this phone">
<div className="section-eyebrow"><Cpu size={14} /> RANDOMX LIGHT · {NETWORK_LABEL.toUpperCase()} · SIDELOAD BUILD</div>
<h2>Mine cards on this phone</h2>
<p>Each accepted block mints one Wikipedia card to your wallet. The phone runs the same pinned RandomX engine as the desktop miner, in light mode (about 256 MiB per thread). Mining runs only while this screen is open, and it stops by itself on battery saver, when the phone is warm, or below 20% battery when not charging.</p>
<label className="mobile-mining-consent"><input type="checkbox" checked={consent} disabled={active}
onChange={e => { setConsent(e.target.checked); localStorage.setItem(CONSENT_KEY, e.target.checked ? 'yes' : 'no'); }} />
<span>I understand that mining uses this phone's CPU and battery and can make it warm, and I want to mine to my wallet.</span></label>
<label className="mobile-mining-intensity">Intensity
<select value={intensity} disabled={active} onChange={e => { setIntensity(e.target.value as Intensity); localStorage.setItem(INTENSITY_KEY, e.target.value); }}>
{Object.entries(INTENSITIES).map(([k, v]) => <option key={k} value={k} disabled={(k === 'high' && (conditions?.cores ?? 0) < 4) || (k === 'max' && ((conditions?.cores ?? 0) < 4 || !conditions?.charging))}>{v.label}</option>)}
</select></label>
<div className="mobile-mining-actions">
{active ? <button className="button outline" onClick={stop}><Square size={15} /> Stop mining</button>
: <button className="button" onClick={() => void start()} disabled={!consent || !wallet || !!blockedNow || conditions?.engineInstalled === false}><Pickaxe size={15} /> Start mining</button>}
</div>
{!wallet && <p className="muted">Unlock or create your wallet to mine.</p>}
{conditions?.engineInstalled === false && <p className="error-text">This APK has no RandomX engine for {conditions.abi}.</p>}
{blockedNow && !active && <p className="muted">Paused: {explain(blockedNow)}.</p>}
{error && <p className="error-text" role="alert">{error}</p>}
<dl className="mobile-mining-stats" aria-live="polite">
<div><dt>Hashrate</dt><dd>{status ? `${status.hashrate.toFixed(1)} H/s` : '—'}</dd></div>
<div><dt>Status</dt><dd>{status?.phase ?? 'idle'}{status?.height ? ` · block #${status.height}` : ''}</dd></div>
<div><dt>Hashes</dt><dd>{status?.attempts.toLocaleString() ?? 0}</dd></div>
<div><dt>Accepted blocks</dt><dd>{status?.accepted ?? 0}</dd></div>
<div><dt>Battery</dt><dd>{conditions ? `${conditions.batteryPercent}%${conditions.charging ? ' charging' : ''}${conditions.batteryTempC !== null ? ` · ${conditions.batteryTempC.toFixed(1)} °C` : ''}` : '—'}</dd></div>
<div><dt>Engine check</dt><dd>{check ? check.ok ? 'known vector OK' : 'FAILED' : 'runs before first start'}</dd></div>
</dl>
{status?.article && active && <p className="muted">Mining for: {status.article}</p>}
{status?.message && <p className="muted">{status.message}</p>}
</section>;
});
+173
View File
@@ -0,0 +1,173 @@
// Mobile (Android sideload) mining: the same loop as server/testnet/miner.ts, run in the WebView.
// The native WikiDeckMiner plugin (android/.../MinerPlugin.java) only runs the pinned RandomX engine in
// LIGHT mode; this module builds work from the node's template, signs the block claim with the local web
// wallet and submits it. The plugin never sees a private key or backup password.
// Browser-safe: Web Crypto only, no node: imports. See docs/night2/MOBILE_MINING.md.
import { CONSENSUS, NETWORK, blockClaimMessage, headerMessage, statsSnapshotTuple,
type BlockHeader, type MiningTemplate, type TestnetBlock } from '../shared/testnet.ts';
export type MineResult = { found: false } | { found: true; hash: string; nonce: string };
/** The RandomX engine as seen from the WebView (the Capacitor plugin, or a fake in tests). */
export interface MinerEngine {
start(threads: number): Promise<void>;
stop(): Promise<void>;
mine(lane: number, key: string, blobHex: string, target: string, nonceOffset: number, maxAttempts: number): Promise<MineResult>;
}
export interface MinerIdentity { address: string; publicKey: string }
export interface MinerDeps {
engine: MinerEngine;
/** POST/GET JSON against the node (relative paths such as /api/mining/template). */
api: (path: string, body?: unknown) => Promise<unknown>;
/** Sign a message with the unlocked web wallet. */
sign: (message: string) => Promise<string>;
chainId: string;
/** Checked before every batch; return a reason to stop (battery saver, thermal, background...). */
blocked?: () => string | undefined | Promise<string | undefined>;
onStatus?: (status: MinerStatus) => void;
now?: () => number;
/** Pause between batches per lane (ms): intensity control. */
idleMs?: number;
random?: () => number;
}
export type MinerPhase = 'idle' | 'starting' | 'requesting' | 'mining' | 'submitting' | 'stopped' | 'error';
export interface MinerStatus {
phase: MinerPhase; hashrate: number; attempts: number; height?: number; article?: string;
accepted: number; rejected: number; lastBlock?: { height: number; tokenId: string; title: string };
message?: string;
}
export const BATCH = 16;
const hex = (bytes: Uint8Array) => Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
export function hexToBytes(value: string): Uint8Array {
if (!/^([0-9a-f]{2})*$/i.test(value)) throw new Error('Invalid hex');
const out = new Uint8Array(value.length / 2);
for (let i = 0; i < out.length; i++) out[i] = parseInt(value.slice(2 * i, 2 * i + 2), 16);
return out;
}
export async function sha256Hex(data: string | Uint8Array): Promise<string> {
const bytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
return hex(new Uint8Array(await crypto.subtle.digest('SHA-256', bytes as BufferSource)));
}
/** Same bytes as consensus `miningBlob`: sha256(headerMessage) || nonce as uint64 little-endian. */
export async function miningBlobHex(header: BlockHeader): Promise<string> {
const nonce = new Uint8Array(8);
new DataView(nonce.buffer).setBigUint64(0, BigInt(header.nonce), true);
return await sha256Hex(headerMessage(header)) + hex(nonce);
}
/** Same value as consensus `mintId`, with the chain id taken from the (verified) header. */
export function mintIdOf(h: BlockHeader): Promise<string> {
return sha256Hex(JSON.stringify([NETWORK, h.chainId, 'mint', h.previousHash, h.height, h.owner,
h.article.pageId, statsSnapshotTuple(h.statsSnapshot)]));
}
export function meetsTarget(hash: string, target: string): boolean {
return BigInt(`0x${hash}`) <= BigInt(`0x${target}`);
}
/** Check a node template before spending CPU on it. */
export async function checkTemplate(t: MiningTemplate, chainId: string, me: MinerIdentity): Promise<void> {
const h = t?.header;
if (!h || h.network !== NETWORK || h.chainId !== chainId || t.nonceOffset !== 32) throw new Error('Node returned work for another network');
if (h.owner !== me.address || h.publicKey !== me.publicKey) throw new Error('Node returned work for another wallet');
if (!/^[0-9a-f]{64}$/.test(h.key) || !/^[0-9a-f]{64}$/.test(h.target)) throw new Error('Node returned malformed work');
if (typeof t.blob === 'string' && t.blob.slice(0, 64) !== (await miningBlobHex({ ...h, nonce: 0 })).slice(0, 64)) {
throw new Error('Node work blob does not match its header');
}
}
/** Assemble the block exactly as server/testnet/miner.ts does. */
export async function buildBlock(t: MiningTemplate, nonce: number, powHash: string, sign: (m: string) => Promise<string>): Promise<TestnetBlock> {
const header: BlockHeader = { ...t.header, nonce };
const blob = await miningBlobHex(header);
const hash = await sha256Hex(hexToBytes(blob));
const signature = await sign(blockClaimMessage(hash));
return { ...header, hash, tokenId: await mintIdOf(header), powHash, signature, transactions: t.transactions };
}
/** Runs until stop() or a safeguard trips. One instance per app. */
export class MobileMiner {
private stopping = false;
private running?: Promise<void>;
status: MinerStatus = { phase: 'idle', hashrate: 0, attempts: 0, accepted: 0, rejected: 0 };
private readonly deps: MinerDeps;
private readonly me: MinerIdentity;
constructor(deps: MinerDeps, me: MinerIdentity) { this.deps = deps; this.me = me; }
private set(patch: Partial<MinerStatus>) { this.status = { ...this.status, ...patch }; this.deps.onStatus?.(this.status); }
get active() { return !!this.running; }
start(threads = 1, maxBlocks = Infinity): Promise<void> {
if (this.running) return this.running;
this.stopping = false;
this.running = this.loop(Math.max(1, Math.min(4, Math.floor(threads))), maxBlocks)
.finally(() => { this.running = undefined; });
return this.running;
}
async stop(reason?: string): Promise<void> {
this.stopping = true;
if (reason) this.set({ message: reason });
await this.deps.engine.stop().catch(() => {});
await this.running?.catch(() => {});
}
private async loop(threads: number, maxBlocks: number) {
const { engine, api } = this.deps;
const now = this.deps.now ?? Date.now, random = this.deps.random ?? Math.random;
this.set({ phase: 'starting', message: undefined, hashrate: 0 });
try {
const reason = await this.deps.blocked?.();
if (reason) throw new Error(`Mining is paused: ${reason}`);
await engine.start(threads);
let accepted = 0;
while (!this.stopping && accepted < maxBlocks) {
this.set({ phase: 'requesting' });
const template = await api('/api/mining/template', { owner: this.me.address, publicKey: this.me.publicKey }) as MiningTemplate;
await checkTemplate(template, this.deps.chainId, this.me);
const h = template.header;
this.set({ phase: 'mining', height: h.height, article: h.article.title });
const started = now();
let attempts = 0, lastCheck = now(), stale = false;
let solution: { nonce: number; hash: string } | undefined;
const lane = async (index: number) => {
let nonce = Math.floor(random() * 2 ** 32);
while (!this.stopping && !solution && !stale) {
const blob = await miningBlobHex({ ...h, nonce });
const result = await engine.mine(index, h.key, blob, h.target, 32, BATCH);
attempts += BATCH;
if (result.found) {
const n = Number(result.nonce);
if (!Number.isSafeInteger(n) || !meetsTarget(result.hash, h.target)) throw new Error('Engine returned an invalid solution');
solution ??= { nonce: n, hash: result.hash };
return;
}
nonce += BATCH;
const elapsed = (now() - started) / 1000;
this.set({ attempts: this.status.attempts + BATCH, hashrate: elapsed > 0 ? attempts / elapsed : 0 });
if (index === 0 && now() - lastCheck >= 2000) {
lastCheck = now();
const reason = await this.deps.blocked?.();
if (reason) { this.stopping = true; this.set({ message: `Paused: ${reason}` }); return; }
const status = await api('/p2p/status') as { tip?: string };
if (status.tip !== h.previousHash || now() / 1000 - h.createdAt > CONSENSUS.targetSeconds * 2) stale = true;
}
if (this.deps.idleMs) await new Promise(r => setTimeout(r, this.deps.idleMs));
}
};
await Promise.all(Array.from({ length: threads }, (_, i) => lane(i)));
if (this.stopping || !solution) continue;
this.set({ phase: 'submitting' });
const block = await buildBlock(template, solution.nonce, solution.hash, this.deps.sign);
try {
const result = await api('/api/blocks', block) as { accepted?: boolean; reason?: string };
if (result.accepted === false) throw new Error(result.reason ?? 'Block not accepted');
accepted++;
this.set({ accepted: this.status.accepted + 1, lastBlock: { height: block.height, tokenId: block.tokenId, title: block.article.title },
message: `Accepted block #${block.height}: ${block.article.title}` });
} catch (error) {
this.set({ rejected: this.status.rejected + 1, message: (error as Error).message });
}
}
this.set({ phase: 'stopped' });
} catch (error) {
this.set({ phase: this.stopping ? 'stopped' : 'error', message: this.stopping ? this.status.message : (error as Error).message });
} finally {
await engine.stop().catch(() => {});
}
}
}
+10
View File
@@ -0,0 +1,10 @@
.mobile-mining { display: grid; gap: 12px; }
.mobile-mining h2 { margin: 0; }
.mobile-mining-consent { display: flex; gap: 10px; align-items: flex-start; }
.mobile-mining-consent input { margin-top: 4px; min-width: 20px; min-height: 20px; }
.mobile-mining-intensity { display: grid; gap: 6px; }
.mobile-mining-intensity select { min-height: 44px; }
.mobile-mining-actions .button { min-height: 44px; display: inline-flex; gap: 8px; align-items: center; }
.mobile-mining-stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 16px; margin: 0; }
.mobile-mining-stats dt { font-size: 0.75rem; opacity: 0.7; }
.mobile-mining-stats dd { margin: 0; font-variant-numeric: tabular-nums; }
+121
View File
@@ -0,0 +1,121 @@
// mobile-mining: the WebView mining loop must build blocks byte-identical to server/testnet/miner.ts.
// No network calls: the node API and the RandomX engine are in-process fakes.
import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash, generateKeyPairSync, sign } from 'node:crypto';
import { CHAIN_ID, MAX_TARGET, address, makeTemplate, miningBlob, mintId, sha256, validateBlock } from '../server/testnet/consensus.ts';
import { type MiningTemplate, type TestnetBlock } from '../shared/testnet.ts';
import { deriveStats } from '../shared/enrichment.ts';
import type { PowEngine } from '../server/testnet/randomx.ts';
import { MobileMiner, buildBlock, checkTemplate, meetsTarget, miningBlobHex, mintIdOf, type MinerEngine, type MineResult } from '../src/mobile-miner.ts';
const keys = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });
const publicKey = keys.publicKey.export({ format: 'der', type: 'spki' }).toString('base64');
const me = { publicKey, address: address(publicKey) };
const signer = async (message: string) => sign('sha256', Buffer.from(message), { key: keys.privateKey, dsaEncoding: 'ieee-p1363' }).toString('base64');
const fakeHash = (key: string, blob: Uint8Array) => createHash('sha256').update(Buffer.concat([Buffer.from(key, 'hex'), Buffer.from(blob)])).digest('hex');
// Test-only stand-in for RandomX on both sides (production has no fake PoW).
const pow: PowEngine = { hash: async (key, blob) => fakeHash(key, blob), close() {} };
function template(target = MAX_TARGET): MiningTemplate {
const pageBytes = 2048, languageCount = 3;
const stats = { capturedAt: '2026-11-17T00:00:00.000Z', revisionId: 7, pageBytes, languageCount, languagesTruncated: false,
pageviews: { status: 'unavailable' as const, months: [], latestMonth: '2026-10', total: null, dailyAverage: null, trendPercent: null },
stats: deriveStats(pageBytes, languageCount, null) };
const article = { pageId: 4242, title: 'Tetrated dodecahedron', extract: 'A deterministic test article.', image: 'https://upload.wikimedia.org/wikipedia/commons/test.jpg',
url: 'https://en.wikipedia.org/?curid=4242', revisionId: 7, source: 'live' as const, category: 'Science' };
const t = makeTemplate([], {}, me.address, me.publicKey, [], article, stats, Math.floor(Date.now() / 1000));
t.header.target = target;
t.blob = miningBlob(t.header).toString('hex');
return t;
}
/** Fake engine that speaks the plugin's mine contract with the fake hash. */
class FakeEngine implements MinerEngine {
started = 0; stopped = 0; calls = 0;
async start() { this.started++; }
async stop() { this.stopped++; }
async mine(_lane: number, key: string, blobHex: string, target: string, offset: number, max: number): Promise<MineResult> {
this.calls++;
const blob = Buffer.from(blobHex, 'hex');
let nonce = blob.readBigUInt64LE(offset);
for (let i = 0; i < max; i++, nonce++) {
blob.writeBigUInt64LE(nonce, offset);
const hash = fakeHash(key, blob);
if (meetsTarget(hash, target)) return { found: true, hash, nonce: nonce.toString() };
}
return { found: false };
}
}
test('browser blob and mint id match consensus', async () => {
const t = template();
for (const nonce of [0, 1, 2 ** 32 + 5, Number.MAX_SAFE_INTEGER]) {
const header = { ...t.header, nonce };
assert.equal(await miningBlobHex(header), miningBlob(header).toString('hex'));
}
assert.equal(await mintIdOf(t.header), mintId(t.header));
assert.equal(t.header.chainId, CHAIN_ID);
});
test('buildBlock produces a block the consensus validator accepts', async () => {
const t = template();
const header = { ...t.header, nonce: 0 };
while (!meetsTarget(fakeHash(header.key, miningBlob(header)), header.target)) header.nonce++;
const powHash = fakeHash(header.key, miningBlob(header));
const block = await buildBlock(t, header.nonce, powHash, signer);
assert.equal(block.hash, sha256(miningBlob(header)));
assert.equal(block.signature.length > 0, true);
const { block: valid } = await validateBlock(structuredClone(block), [], {}, pow);
assert.equal(valid.tokenId, mintId(header));
});
test('checkTemplate rejects work for another network or wallet', async () => {
const t = template();
await checkTemplate(t, CHAIN_ID, me);
await assert.rejects(checkTemplate(t, 'f'.repeat(64), me), /another network/);
await assert.rejects(checkTemplate(t, CHAIN_ID, { ...me, address: '0'.repeat(64) }), /another wallet/);
await assert.rejects(checkTemplate({ ...t, blob: '00'.repeat(40) }, CHAIN_ID, me), /does not match/);
});
test('MobileMiner mines, signs and submits one accepted block, then stops the engine', async () => {
const engine = new FakeEngine();
const submitted: TestnetBlock[] = [];
const api = async (path: string, body?: unknown) => {
if (path === '/api/mining/template') { assert.deepEqual(body, { owner: me.address, publicKey: me.publicKey }); return template('0' + 'f'.repeat(63)); }
if (path === '/api/blocks') { submitted.push(body as TestnetBlock); await validateBlock(structuredClone(body), [], {}, pow); return { accepted: true }; }
if (path === '/p2p/status') return { tip: 'x' };
throw new Error(`unexpected ${path}`);
};
const miner = new MobileMiner({ engine, api, sign: signer, chainId: CHAIN_ID }, me);
await miner.start(1, 1);
assert.equal(miner.status.phase, 'stopped', miner.status.message);
assert.equal(miner.status.accepted, 1);
assert.equal(submitted.length, 1);
assert.equal(submitted[0].owner, me.address);
assert.ok(engine.stopped >= 1);
});
test('a tripped safeguard stops mining before the engine starts', async () => {
const engine = new FakeEngine();
const miner = new MobileMiner({ engine, api: async () => { throw new Error('no network'); }, sign: signer, chainId: CHAIN_ID,
blocked: () => 'battery saver is on' }, me);
await miner.start(1, 1);
assert.equal(miner.status.phase, 'error');
assert.match(miner.status.message ?? '', /battery saver/);
assert.equal(engine.started, 0);
assert.equal(engine.calls, 0);
});
test('stop() halts an in-progress search', async () => {
const engine = new FakeEngine();
const api = async (path: string) => path === '/api/mining/template' ? template('0'.repeat(64)) : { tip: template().header.previousHash };
const miner = new MobileMiner({ engine, api, sign: signer, chainId: CHAIN_ID }, me);
const run = miner.start(1);
for (let i = 0; engine.calls < 3 && i < 2000; i++) await new Promise(r => setTimeout(r, 1));
assert.ok(engine.calls >= 3, miner.status.message);
await miner.stop('Stopped.');
await run;
assert.equal(miner.status.phase, 'stopped');
assert.equal(miner.status.accepted, 0);
});
-82
View File
@@ -1,82 +0,0 @@
// testnet-ops: live identity source over a FAKE Wikimedia client. Zero network calls.
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { wikimediaIdentitySource } from '../server/game/identity-source.ts';
import { IdentityStore, prebake, toProfile } from '../server/game/identity.ts';
import type { BackgroundRequest, ProfileBuilder } from '../server/game/identity-source.ts';
// A minimal stand-in for shared/duel/profile.ts (#70), so this test needs no unmerged module.
const ids = (e: any, p: string): string[] => (e?.claims?.[p] ?? []).map((s: any) => s.mainsnak?.datavalue?.value?.id).filter(Boolean);
const yrs = (e: any, p: string): number[] => (e?.claims?.[p] ?? []).map((s: any) => Number(/^[+-]0*(\d+)-/.exec(s.mainsnak?.datavalue?.value?.time ?? '')?.[1])).filter(Boolean);
const profile: ProfileBuilder = {
wikidataEntityParams: q => ({ action: 'wbgetentities', ids: [...q].sort().join('|'), props: 'claims', format: 'json', formatversion: '2' }),
wikidataLabelParams: q => ({ action: 'wbgetentities', ids: [...q].sort().join('|'), props: 'labels', languages: 'en', format: 'json', formatversion: '2' }),
classIdsOf: e => [...new Set([...ids(e, 'P31'), ...ids(e, 'P279')])].sort(),
profileFromSources: ({ enrichment, entity, labels = {} }) => ({
wikidataId: enrichment.wikidataId, description: enrichment.description, categories: enrichment.categories,
instanceOf: ids(entity, 'P31').map(id => ({ id, label: labels[id] ?? id })), countries: ids(entity, 'P27'),
years: [...yrs(entity, 'P569'), ...yrs(entity, 'P570')], died: ids(entity, 'P570').length > 0 || yrs(entity, 'P570').length > 0,
fetchedRevisionId: enrichment.revisionId }),
};
const enrichmentFor = (pageId: number, wikidataId: string | null) => ({
pageId, title: `Page ${pageId}`, description: `desc ${pageId}`, wikidataId, revisionId: 1000 + pageId, pageBytes: 1,
languageCount: 1, languagesTruncated: false, categories: ['Living people', '1948 births'], categoriesTruncated: false,
fetchedAt: '2026-11-17T00:00:00Z',
pageviews: { status: 'unavailable', months: [], latestMonth: '2026-10', total: null, dailyAverage: null, trendPercent: null },
}) as any;
const claim = (id: string) => ({ rank: 'normal', mainsnak: { snaktype: 'value', datavalue: { type: 'wikibase-entityid', value: { id } } } });
const time = (t: string) => ({ rank: 'normal', mainsnak: { snaktype: 'value', datavalue: { type: 'time', value: { time: t } } } });
function fakeClient(opts: { failClaims?: boolean } = {}) {
const calls: { url: URL; options: BackgroundRequest }[] = [];
return { calls, async request(url: URL, options: BackgroundRequest) {
calls.push({ url, options });
const props = url.searchParams.get('props'), ids = url.searchParams.get('ids')!.split('|');
if (props === 'claims' && opts.failClaims) return { response: new Response('busy', { status: 503 }), signal: new AbortController().signal, fromCache: false };
const entities = Object.fromEntries(ids.map(id => [id, props === 'claims'
? { id, claims: { P31: [claim('Q5')], P27: [claim('Q30')], P569: [time('+1948-01-02T00:00:00Z')], P570: [time('+2001-01-01T00:00:00Z')] } }
: { id, labels: { en: { value: id === 'Q5' ? 'human' : `label ${id}` } } }]));
return { response: Response.json({ entities }), signal: new AbortController().signal, fromCache: false };
} };
}
test('every request is on the background lane, batched, and the frozen identity is derive-ready', async () => {
const realFetch = globalThis.fetch;
globalThis.fetch = (() => { throw new Error('network call in test'); }) as typeof fetch;
const dir = mkdtempSync(join(tmpdir(), 'identity-src-'));
try {
const client = fakeClient();
const source = wikimediaIdentitySource({ client, profile, enrichment: { async getBackground(id) { return enrichmentFor(id, `Q${id}`); } } });
const store = new IdentityStore(join(dir, 'id.sqlite'));
const articles = [1, 2, 3].map(i => ({ tokenId: String(i).repeat(64), pageId: i, title: `Page ${i}`, revisionId: i }));
const r = await prebake(store, source, articles);
assert.deepEqual(r, { seen: 3, alreadyFrozen: 0, frozen: 3, unknown: 0 });
assert.ok(client.calls.every(c => c.options.lane === 'background'));
assert.ok(client.calls.every(c => c.url.hostname === 'www.wikidata.org'));
assert.deepEqual(client.calls.map(c => c.url.searchParams.get('props')), ['claims', 'labels']);
assert.equal(client.calls[0].url.searchParams.get('ids'), 'Q1|Q2|Q3');
const one = store.get('1'.repeat(64))!;
assert.equal(one.source, 'wikimedia');
assert.deepEqual(one.instanceOf, [{ id: 'Q5', label: 'human' }]);
assert.deepEqual(one.countries, ['Q30']);
assert.deepEqual(one.years, [1948, 2001]);
assert.equal(one.died, true);
assert.equal(one.fetchedRevisionId, 1001);
assert.equal(toProfile(one).description, 'desc 1');
store.close();
} finally { globalThis.fetch = realFetch; rmSync(dir, { recursive: true, force: true }); }
});
test('a failed claims request leaves cards unknown so they are retried, never frozen half-empty', async () => {
const source = wikimediaIdentitySource({ client: fakeClient({ failClaims: true }), profile,
enrichment: { async getBackground(id) { if (id === 9) throw new Error('throttled'); return enrichmentFor(id, id === 8 ? null : `Q${id}`); } } });
const got = await source.lookup([7, 8, 9].map(i => ({ tokenId: String(i).repeat(64), pageId: i, title: `P${i}` })));
assert.equal(got.get(7), undefined); // has an item, claims failed → retry
assert.equal(got.get(9), undefined); // enrichment failed → retry
assert.equal(got.get(8)?.wikidataId, null); // no Wikidata item at all → enrichment-only profile is final
assert.deepEqual(got.get(8)?.instanceOf, []);
});
-90
View File
@@ -1,90 +0,0 @@
// testnet-ops: card identity freeze-at-first-sight and pre-bake. Zero network calls.
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { identityStoreFile, IdentityStore, buildIdentity, cachedSource, linksHash, prebake, toProfile,
type ChainArticle, type IdentitySource } from '../server/game/identity.ts';
const sample = JSON.parse(readFileSync(new URL('../docs/game/card-sample.json', import.meta.url), 'utf8')).cards as any[];
const now = () => new Date('2026-11-17T22:00:00Z');
function tmpStore() {
const dir = mkdtempSync(join(tmpdir(), 'identity-'));
return { store: new IdentityStore(join(dir, 'identity.sqlite')), done: () => rmSync(dir, { recursive: true, force: true }) };
}
const art = (c: any, i: number): ChainArticle => ({ tokenId: i.toString(16).padStart(64, '0'), pageId: c.pageId, title: c.title, revisionId: c.revisionId });
test('no fetch: pre-bake from the cached sample freezes every sample card', async () => {
const realFetch = globalThis.fetch;
globalThis.fetch = (() => { throw new Error('network call in test'); }) as typeof fetch;
const { store, done } = tmpStore();
try {
const articles = sample.map(art);
const r = await prebake(store, cachedSource(sample), articles, { now });
assert.deepEqual(r, { seen: sample.length, alreadyFrozen: 0, frozen: sample.length, unknown: 0 });
const salah = store.get(articles[sample.findIndex(c => c.title === 'Salahuddin of Aceh')].tokenId)!;
assert.equal(salah.wikidataId, 'Q4264920');
assert.deepEqual(salah.instanceOf, [{ id: 'Q5', label: 'human' }]);
assert.equal(salah.source, 'cache');
assert.ok(salah.categories.includes('Sultans of Aceh'));
assert.equal(salah.description, 'Sultan of Aceh (1530–1537/9)');
const profile = toProfile(salah);
assert.deepEqual(profile.instanceOf, [{ id: 'Q5', label: 'human' }]);
assert.equal(profile.fetchedRevisionId, 1358732737);
} finally { globalThis.fetch = realFetch; store.close(); done(); }
});
test('freeze at first sight: a later, different profile never overwrites the stored one', async () => {
const { store, done } = tmpStore();
try {
const a = art(sample[0], 1);
const first = store.freeze(buildIdentity(a, { instanceOf: [{ id: 'Q5', label: 'human' }] }, 'cache', now()));
const second = store.freeze(buildIdentity(a, { instanceOf: [{ id: 'Q515', label: 'city' }] }, 'wikimedia', new Date()));
assert.deepEqual(second, first);
assert.equal(store.get(a.tokenId)!.instanceOf[0].id, 'Q5');
const again = await prebake(store, cachedSource(sample), [a, a], { now });
assert.deepEqual(again, { seen: 1, alreadyFrozen: 1, frozen: 0, unknown: 0 });
} finally { store.close(); done(); }
});
test('unknown pages are not frozen, so a later run can retry them', async () => {
const { store, done } = tmpStore();
try {
const stranger: ChainArticle = { tokenId: 'f'.repeat(64), pageId: 999999999, title: 'Not in sample', revisionId: 1 };
const r = await prebake(store, cachedSource(sample), [stranger], { now });
assert.deepEqual(r, { seen: 1, alreadyFrozen: 0, frozen: 0, unknown: 1 });
assert.equal(store.has(stranger.tokenId), false);
} finally { store.close(); done(); }
});
test('sources are asked in bounded batches and identities persist across reopen', async () => {
const dir = mkdtempSync(join(tmpdir(), 'identity-'));
try {
const calls: number[] = [];
const source: IdentitySource = { kind: 'wikimedia', async lookup(batch) {
calls.push(batch.length);
return new Map(batch.map(a => [a.pageId, { links: [7, 3, 3, 1.5], coords: [1, 2] as [number, number], cites: ['Q30', 'bad'], years: [1948, -44], died: true }]));
} };
let store = new IdentityStore(join(dir, 'id.sqlite'));
await prebake(store, source, sample.slice(0, 12).map(art), { batchSize: 5, now });
assert.deepEqual(calls, [5, 5, 2]);
store.close();
store = new IdentityStore(join(dir, 'id.sqlite'));
const one = store.get(art(sample[3], 3).tokenId)!;
assert.deepEqual(one.links, [3, 7]);
assert.equal(one.linksHash, linksHash([3, 7]));
assert.deepEqual(one.cites, ['Q30']);
assert.deepEqual(one.years, [-44, 1948]);
assert.equal(one.died, true);
assert.deepEqual(one.coords, [1, 2]);
assert.equal(one.mintRevisionId, sample[3].revisionId);
assert.equal(store.count(), 12);
store.close();
} finally { rmSync(dir, { recursive: true, force: true }); }
});
test('the store file lives under WIKIDECK_DATA_DIR, next to the ledger', () => {
assert.equal(identityStoreFile({ WIKIDECK_DATA_DIR: '/srv/wd' }), join('/srv/wd', 'card-identity.sqlite'));
assert.ok(identityStoreFile({}).endsWith(join('data', 'card-identity.sqlite')));
});