diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 709fe1e..4395edb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -132,24 +132,8 @@ jobs: docker build -t dashnet-recipe-test -f internal/bootstrap/testdata/Dockerfile . docker run --rm --network none dashnet-recipe-test - core-contract: - if: github.event_name != 'workflow_dispatch' || inputs.proof_operation == 'none' - runs-on: ubuntu-latest - timeout-minutes: 25 - steps: - - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - with: - persist-credentials: false - - name: Real Core rendered by dashmate; wallet, registration recovery and join - run: | - set -euo pipefail - pin() { docker pull -q "$1" >/dev/null && docker image inspect "$1" --format '{{index .RepoDigests 0}}'; } - core=$(pin dashpay/dashd:23) - helper=$(pin dashpay/dashmate-helper:4.2.0-beta.3) - # The Tor image dashmate 4.2.0-beta.3 selects (digest-pinned by dashmate). - tor=$(pin osminogin/tor-simple:0.4.9.11@sha256:7aef2e7d957e2236d5fac7fca8e67c15fe65e6349eb90099da1cdc26bc4caa69) - sudo env CORE_IMAGE="$core" HELPER_IMAGE="$helper" TOR_IMAGE="$tor" DASHNET_DISPOSABLE_CI=1 python3 internal/node/testdata/core_integration.py - sudo env CORE_IMAGE="$core" HELPER_IMAGE="$helper" TOR_IMAGE="$tor" DASHNET_DISPOSABLE_CI=1 python3 internal/node/testdata/join_contract.py + # The real-Core contract (wallet, registration recovery, join) runs once per + # supported Core major in core-compat.yml. platform-contract: if: github.event_name != 'workflow_dispatch' || inputs.proof_operation == 'none' diff --git a/.github/workflows/core-compat.yml b/.github/workflows/core-compat.yml new file mode 100644 index 0000000..99ab680 --- /dev/null +++ b/.github/workflows/core-compat.yml @@ -0,0 +1,61 @@ +name: Core compatibility +run-name: "Core compatibility / ${{ inputs.image || github.sha }}" + +# The code that talks to Dash Core, against a real dashd of every supported +# Core major. Dispatch it by hand with any other dashpay/dashd tag (a release +# candidate, a patch release) to check that release before it is deployed. + +on: + pull_request: + push: + branches: [main] + workflow_dispatch: + inputs: + image: + description: One dashpay/dashd image to check instead of the supported majors, e.g. dashpay/dashd:24.1.0-rc.1 + type: string + required: false + +permissions: + contents: read + +concurrency: + group: core-compat-${{ github.ref }}-${{ inputs.image || 'supported' }} + cancel-in-progress: ${{ github.event_name != 'workflow_dispatch' }} + +jobs: + core: + name: ${{ matrix.image }} + runs-on: ubuntu-latest + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + image: ${{ fromJSON(inputs.image && format('["{0}"]', inputs.image) || '["dashpay/dashd:23", "dashpay/dashd:24"]') }} + env: + IMAGE: ${{ matrix.image }} + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - name: Pull the Core image under test + run: | + set -euo pipefail + [[ "$IMAGE" =~ ^(docker\.io/)?dashpay/dashd(:[A-Za-z0-9_][A-Za-z0-9_.-]*)?(@sha256:[0-9a-f]{64})?$ ]] || { echo "not a dashpay/dashd image: $IMAGE"; exit 1; } + docker pull -q "$IMAGE" + docker image inspect "$IMAGE" --format 'Core under test: {{index .RepoDigests 0}}' | tee -a "$GITHUB_STEP_SUMMARY" + - name: Lone regtest node; wallet, spork key, funding, registration and activation + working-directory: internal/node/testdata + run: CORE_COMPAT_IMAGE="$IMAGE" python3 -m unittest -v test_core_compat + # Runs even if the step above failed: the two answer different questions. + - name: Devnet Core rendered by dashmate; wallet, registration recovery and join + if: ${{ !cancelled() }} + run: | + set -euo pipefail + pin() { docker pull -q "$1" >/dev/null && docker image inspect "$1" --format '{{index .RepoDigests 0}}'; } + core=$(pin "$IMAGE") + helper=$(pin dashpay/dashmate-helper:4.2.0-beta.3) + # The Tor image dashmate 4.2.0-beta.3 selects (digest-pinned by dashmate). + tor=$(pin osminogin/tor-simple:0.4.9.11@sha256:7aef2e7d957e2236d5fac7fca8e67c15fe65e6349eb90099da1cdc26bc4caa69) + sudo env CORE_IMAGE="$core" HELPER_IMAGE="$helper" TOR_IMAGE="$tor" DASHNET_DISPOSABLE_CI=1 python3 internal/node/testdata/core_integration.py + sudo env CORE_IMAGE="$core" HELPER_IMAGE="$helper" TOR_IMAGE="$tor" DASHNET_DISPOSABLE_CI=1 python3 internal/node/testdata/join_contract.py diff --git a/README.md b/README.md index 42d43f7..c0d7723 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,12 @@ are not the shared journal; EC2 provisioning stores that separately in DynamoDB. This does not register a new validator or expand an enrolled fleet implicitly. - **Container contracts:** real Core wallet/registration/recovery and real Tenderdash/Envoy/DAPI configuration/TLS/gRPC; no AWS access or fleet-health claim. +- **Core compatibility:** the Core-facing code against a real `dashd` of each + supported Core major (23 and 24): a lone regtest node for the wallet, spork + key, funding, EvoNode registration and spork activation, then the devnet Core + that dashmate renders. Before deploying any other Core release, dispatch it + with that image: `image=dashpay/dashd:`. It proves the RPC surface this + tool uses, not quorums, ChainLocks or Platform on that Core. ## Development diff --git a/internal/node/testdata/core_integration.py b/internal/node/testdata/core_integration.py index f975428..e20517d 100644 --- a/internal/node/testdata/core_integration.py +++ b/internal/node/testdata/core_integration.py @@ -50,9 +50,6 @@ def call(action): call("core-finalize") # Reload after Core restart before funding (wallet load is explicit). call("wallet") - call("activate") - # Exercise the live update RPC as well as readback/idempotent resume. - call("activate") q["requiredBalance"] = 4001 # Premine at minimum difficulty in batches, as a real devnet does. q["context"]["premineHeight"] = 600 @@ -125,6 +122,15 @@ def call(action): assert ( len(w.rpc("listlockunspent", [], True)) == 2 ), "Second registration spent first collateral" + # Sporks last, as a deployment does (registrations, then activate). + # With InstantSend on, Core keeps a transaction that has no + # InstantSend lock out of blocks for ten minutes, and this lone + # node has no quorum to lock one. Core 23 skipped that rule on a + # node whose masternode sync is incomplete; Core 24 does not, so + # registering after activation cannot confirm here. + call("activate") + # Exercise the live update RPC as well as readback/idempotent resume. + call("activate") print( "Real Core: config, wallet, signed EvoNode registration, idempotent replay, stop/resume passed.", flush=True, diff --git a/internal/node/testdata/test_core_compat.py b/internal/node/testdata/test_core_compat.py new file mode 100644 index 0000000..f35a7ba --- /dev/null +++ b/internal/node/testdata/test_core_compat.py @@ -0,0 +1,377 @@ +"""The worker's Core-facing code against a real dashd, one image per run. + +Fixtures cannot show that a new Core version changed an RPC, a default or an +error code. This starts the image named by CORE_COMPAT_IMAGE as a lone regtest +node and runs the real worker against it: the wallet and spork identity, the +funding, the EvoNode registration and the spork activation of a deployment, +with nothing scripted. CI runs it once per supported Core major +(.github/workflows/core-compat.yml), and it can be dispatched by hand against +any other tag, such as a release candidate, before that release is deployed. + +Without CORE_COMPAT_IMAGE the whole module is skipped, so the ordinary test +run needs neither Docker nor a network. A lone regtest node has no masternode +of its own, no quorums and no ChainLocks: those reads are checked only as far +as "the RPC exists and fails with a code the worker expects". Core as dashmate +renders it, on a devnet, is core_integration.py's subject. +""" + +import base64 +import json +import os +from pathlib import Path +import re +import secrets +import subprocess +import tempfile +import time +import unittest +import urllib.error +import urllib.request + +from test_worker import worker, request + +IMAGE = os.environ.get("CORE_COMPAT_IMAGE", "") +IMAGE_OK = re.fullmatch( + r"(docker\.io/)?dashpay/dashd(:[A-Za-z0-9_][A-Za-z0-9_.-]{0,127})?(@sha256:[0-9a-f]{64})?", IMAGE +) +CONF = "/etc/dash/dash.conf" # where the image reads it +NODE = Path(__file__).parents[1] +# Regtest accepts ProTx transactions from height 432 (DIP3) in Core 23 and 24, +# and its block reward halves every 150 blocks: the wallet is funded early. +PREMINE = 600 +# Every Core has these. Core 23 also has sporks 3 and 9, which Core 24 removed. +SPORKS = [ + "SPORK_2_INSTANTSEND_ENABLED", + "SPORK_17_QUORUM_DKG_ENABLED", + "SPORK_19_CHAINLOCKS_ENABLED", + "SPORK_21_QUORUM_ALL_CONNECTED", +] +OLDER_SPORKS = ["SPORK_3_INSTANTSEND_BLOCK_FILTERING", "SPORK_9_SUPERBLOCKS_ENABLED"] + + +def sh(*args, timeout=180): + return subprocess.run(args, capture_output=True, text=True, timeout=timeout) + + +def rpcs_used(): + """Every Core RPC the node workers name, read from the scripts themselves so + a newly added call is covered without touching this test.""" + names = set() + for path in [NODE / "worker.py", NODE / "upgrade.py", NODE / "join.py", NODE / "observer.py"]: + names.update(re.findall(r"""\.rpc\(\s*["']([a-z]+)["']""", path.read_text())) + managed = (NODE.parent / "managed" / "worker.py").read_text() + names.update(re.findall(r"""\.rpc\(\s*selected,\s*["']([a-z]+)["']""", managed)) + return sorted(names) + + +class Node: + """A lone regtest dashd from the image under test.""" + + ports = iter(range(19898, 19998)) + + def __init__(self): + self.port = next(self.ports) + self.password = secrets.token_hex(16) + self.work = tempfile.TemporaryDirectory(prefix="dashnet-core-compat-") + # The node runs as the image's own user and reads this through the mount. + os.chmod(self.work.name, 0o755) + self.conf = Path(self.work.name) / "dash.conf" + self.configure([]) + self.conf.chmod(0o644) + self.name = "core-compat-" + secrets.token_hex(4) + started = sh( + "docker", "run", "-d", "--name", self.name, "-p", "127.0.0.1:%d:%d" % (self.port, self.port), + "-v", "%s:%s:ro" % (self.conf, CONF), IMAGE, "dashd", "-conf=" + CONF, + ) + if started.returncode: + raise AssertionError("could not start %s: %s" % (IMAGE, started.stderr.strip()[-500:])) + self.wait() + + def configure(self, extra): + # Rewritten in place: the container mounts this one file. + with self.conf.open("w") as stream: + stream.write("\n".join([ + "regtest=1", "server=1", "listen=0", "dnsseed=0", "printtoconsole=1", "fallbackfee=0.0001", + "txindex=1", "rpcuser=dashnet", "rpcpassword=" + self.password, "rpcallowip=0.0.0.0/0", + *extra, "[regtest]", "rpcbind=0.0.0.0", "rpcport=%d" % self.port, "", + ])) + + def wait(self): + deadline = time.time() + 120 + while sh("docker", "exec", self.name, "dash-cli", "-conf=" + CONF, "getblockchaininfo").returncode: + if time.time() > deadline: + logs = sh("docker", "logs", "--tail", "40", self.name) + raise AssertionError( + "%s did not answer RPC within two minutes:\n%s%s" % (IMAGE, logs.stdout[-3000:], logs.stderr[-3000:]) + ) + time.sleep(2) + + def restart(self, extra): + """As core-finalize does: the same data, a configuration with more in it.""" + self.configure(extra) + restarted = sh("docker", "restart", self.name) + if restarted.returncode: + raise AssertionError("could not restart %s: %s" % (IMAGE, restarted.stderr.strip()[-500:])) + self.wait() + + def cli(self, *args, wallet=None): + """dash-cli in the container. Returns (value, None) or (None, error text).""" + command = ["docker", "exec", self.name, "dash-cli", "-conf=" + CONF] + command += ["-rpcwallet=" + wallet] if wallet else [] + p = sh(*command, *[str(a) for a in args]) + if p.returncode: + return None, (p.stderr or p.stdout).strip()[-400:] + try: + return json.loads(p.stdout), None + except ValueError: + return p.stdout.strip(), None + + def message(self, method, params, wallet): + """Core's own error for a call. The worker's client keeps only the + code, which is all it acts on; whoever reads a failed run of this test + needs the reason.""" + url = "http://127.0.0.1:%d/" % self.port + ("wallet/dashnet" if wallet else "") + auth = base64.b64encode(("dashnet:" + self.password).encode()).decode() + body = json.dumps(dict(jsonrpc="1.0", id="why", method=method, params=params or [])).encode() + call = urllib.request.Request(url, data=body, headers={"Authorization": "Basic " + auth, "Content-Type": "application/json"}) + opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) + try: + try: + response = opener.open(call, timeout=90) + except urllib.error.HTTPError as error: + response = error + with response: + return json.dumps(json.loads(response.read(1024 * 1024)).get("error")) + except (OSError, ValueError) as error: + return "no answer (%s)" % type(error).__name__ + + def worker(self): + """The real worker, with this node as its host's Core.""" + q = request() + q["context"]["ports"]["coreRPC"] = self.port + root = Path(tempfile.mkdtemp(prefix="state-", dir=self.work.name)) + w = worker.Worker(q, root, root / "lock") + w.atomic("secrets.json", {"rpcPassword": self.password}) + return w + + def remove(self): + sh("docker", "rm", "-f", self.name) + self.work.cleanup() + + +@unittest.skipUnless(IMAGE, "set CORE_COMPAT_IMAGE to a dashpay/dashd image to run against a real node") +class CoreCompat(unittest.TestCase): + summary = {} + nodes = [] + + @classmethod + def setUpClass(cls): + if not IMAGE_OK: + raise AssertionError("CORE_COMPAT_IMAGE must be a dashpay/dashd image reference, got %r" % IMAGE) + cls.node = cls.start() + cls.w = cls.node.worker() + + @classmethod + def start(cls): + node = Node() + cls.nodes.append(node) + return node + + @classmethod + def tearDownClass(cls): + for node in cls.nodes: + node.remove() + if cls.summary: + print("\ncore-compat " + json.dumps(cls.summary, sort_keys=True), flush=True) + + def ok(self, node, *args, wallet=None): + value, error = node.cli(*args, wallet=wallet) + self.assertIsNone(error, "%s failed on %s: %s" % (" ".join(map(str, args)), IMAGE, error)) + return value + + def step(self, node, w, action): + """One worker action. When Core refuses a call, the same call is asked + once more for the message the worker's client drops.""" + calls, client = [], w.rpc + + def recording(method, params=None, wallet=False): + calls.append((method, params, wallet)) + return client(method, params, wallet) + + w.rpc = recording + try: + return action() + except worker.RPCFailure as error: + self.fail("%s on %s: Core answered %s" % (error, IMAGE, node.message(*calls[-1]))) + finally: + del w.rpc + + def needs_identity(self): + if not hasattr(self, "identity"): + self.skipTest("needs the wallet identity that test_3 did not produce") + + def code(self, w, method, params=None, wallet=False): + """Core's error code for a call, as the worker's own client reports it.""" + try: + w.rpc(method, params, wallet) + except worker.RPCFailure as error: + return error.code + return None + + def key_address(self, node, key): + """What Core itself says a private key's address is.""" + info = self.ok(node, "getdescriptorinfo", "pkh(%s)" % key) + return self.ok(node, "deriveaddresses", info["descriptor"])[0] + + def test_1_image_is_the_version_it_claims(self): + net = self.ok(self.node, "getnetworkinfo") + self.assertIsInstance(net.get("version"), int) + self.summary.update(image=IMAGE, version=net["version"], subversion=net.get("subversion")) + major = re.search(r"dashd:(\d+)(?:[.-]|$)", IMAGE) + if major: + self.assertEqual(net["version"] // 10000, int(major.group(1)), "the image tag and the running Core disagree") + self.assertEqual(self.w.rpc("getblockchaininfo")["chain"], "regtest") + + def test_2_every_rpc_the_workers_call_still_exists(self): + names = rpcs_used() + # A guard on the scan itself: an empty list would pass vacuously. + for expected in ["createwallet", "getaddressesbylabel", "getbestchainlock", "getblockchaininfo", "lockunspent", + "masternode", "mnsync", "protx", "quorum", "spork", "sporkupdate"]: + self.assertIn(expected, names) + missing = [n for n in names if str(self.ok(self.node, "help", n)).startswith("help: unknown command")] + self.assertEqual(missing, [], "RPCs the workers call that %s does not have" % IMAGE) + self.summary["rpcs"] = len(names) + # Reads every role makes, in the argument forms the workers pass. + self.assertIsInstance(self.w.rpc("mnsync", ["status"]).get("IsSynced"), bool) + self.assertIsInstance(self.w.rpc("mnsync", ["status"]).get("IsBlockchainSynced"), bool) + self.assertIsInstance(self.w.rpc("quorum", ["list"]), dict) + self.assertIsInstance(self.w.rpc("quorum", ["dkgstatus"]), dict) + self.assertIsInstance(self.w.rpc("getconnectioncount"), int) + self.assertIsInstance(self.w.rpc("getpeerinfo"), list) + self.assertIsInstance(self.w.rpc("getnetworkinfo").get("subversion"), str) + self.assertIsInstance(self.w.rpc("spork", ["active"]), dict) + # These need a ChainLock or a registered masternode. They must fail with + # a code the worker treats as "not yet", not as an unknown method. + self.assertIn(self.code(self.w, "getbestchainlock"), [-32603, -5, -8]) + self.assertIn(self.code(self.w, "masternode", ["status"]), [-32603, -1, -8]) + # Before submission, a signed registration is "not found" by this code. + self.assertEqual(self.code(self.w, "getrawtransaction", ["0" * 64, True]), -5) + + def test_3_wallet_and_spork_identity(self): + w, node = self.w, self.node + first = self.step(node, w, w.wallet) + info = self.ok(node, "getwalletinfo", wallet="dashnet") + # Core's own default: legacy up to Core 23, descriptor from Core 24. + self.summary["defaultWalletDescriptors"] = info.get("descriptors", False) + for address in [first["payoutAddress"], first["sporkAddress"]]: + self.assertTrue(self.ok(node, "validateaddress", address)["isvalid"], address) + self.assertNotEqual(first["payoutAddress"], first["sporkAddress"]) + key = w.read("secrets.json")["sporkKey"] + self.assertEqual(self.key_address(node, key), first["sporkAddress"], "the recorded spork key is not the spork address's key") + self.assertTrue(self.ok(node, "getaddressinfo", first["payoutAddress"], wallet="dashnet")["ismine"]) + # A retry, and a resume on a Core that restarted without the wallet loaded. + self.assertEqual(self.step(node, w, w.wallet), first) + self.ok(node, "unloadwallet", "dashnet") + self.assertEqual(self.step(node, w, w.wallet), first) + self.assertEqual(w.read("secrets.json")["sporkKey"], key) + # How the worker learns that the wallet does not have a label yet. + self.assertEqual(self.code(w, "getaddressesbylabel", ["dashnet:absent"], True), -11) + type(self).identity = first + + def test_4_core_restarts_with_the_spork_key(self): + # core-finalize: Core is configured with the spork address every node + # trusts and, on the wallet host, the key that signs for it. + self.needs_identity() + key = self.w.read("secrets.json")["sporkKey"] + self.node.restart(["sporkaddr=" + self.identity["sporkAddress"], "sporkkey=" + key]) + self.assertEqual(self.step(self.node, self.w, self.w.wallet), self.identity, "the wallet identity changed across a Core restart") + + def test_5_fund_and_register_an_evonode(self): + self.needs_identity() + w, node = self.w, self.node + w.c["premineHeight"] = PREMINE + w.q["requiredBalance"] = 4001 + self.assertGreaterEqual(self.step(node, w, w.fund)["balance"], 4001) + self.assertGreaterEqual(w.rpc("getblockcount"), PREMINE) + # The masternode list is defined from the first block on. + self.assertIsInstance(w.rpc("protx", ["list", "valid"]), list) + w.q["registration"] = dict( + name="validator-1", address="1.2.3.4", operatorPublicKey=w.rpc("bls", ["generate"])["public"], nodeId="a" * 40 + ) + w.q["requiredConfirmations"] = 1 + registration = self.step(node, w, w.register) + self.assertRegex(registration["proTxHash"], r"^[0-9a-f]{64}$") + self.assertGreaterEqual(registration["confirmations"], 1) + saved = (w.root / "transactions/validator-1.json").read_bytes() + balance = w.rpc("getbalance", [], True) + # A replay submits nothing and spends nothing. + self.assertEqual(self.step(node, w, w.register)["proTxHash"], registration["proTxHash"]) + self.assertEqual((w.root / "transactions/validator-1.json").read_bytes(), saved) + self.assertEqual(w.rpc("getbalance", [], True), balance) + self.assertEqual(len(w.rpc("listlockunspent", [], True)), 1, "the collateral is not locked") + # What fast-forward and the upgrade gate read. + listed = w.rpc("protx", ["list", "registered", True]) + self.assertEqual([m["proTxHash"] for m in listed], [registration["proTxHash"]]) + self.assertIsInstance(listed[0]["state"]["registeredHeight"], int) + # The lock is the wallet's own record: it survives the wallet's reload. + self.ok(node, "unloadwallet", "dashnet") + self.step(node, w, w.wallet) + self.assertEqual(len(w.rpc("listlockunspent", [], True)), 1, "the collateral lock was lost on reload") + + def test_6_spork_key_activates_the_network(self): + self.needs_identity() + w = self.w + before = w.rpc("spork", ["active"]) + self.summary["sporks"] = sorted(before) + wanted = SPORKS + [k for k in OLDER_SPORKS if k in before] + self.assertEqual([before.get(k) for k in wanted], [False] * len(wanted), "regtest sporks start off; nothing here would exercise the key") + # Core signs each update with the configured key and accepts it only + # if that key is the configured spork address's. + self.step(self.node, w, w.activate) + after = w.rpc("spork", ["active"]) + self.assertEqual([after.get(k) for k in wanted], [True] * len(wanted)) + # Only what a devnet needs was switched on, and a replay changes nothing. + self.assertEqual({k: v for k, v in after.items() if k not in wanted}, {k: v for k, v in before.items() if k not in wanted}) + self.step(self.node, w, w.activate) + self.assertEqual(w.rpc("spork", ["active"]), after) + + def test_7_deployment_made_with_an_exported_wallet_key(self): + """Deployments made by earlier versions of this tool, on Core 23, hold a + legacy wallet and the spork key they exported from it. Resuming one, on + the same Core or after its upgrade, must keep that spork identity.""" + node = self.start() + w = node.worker() + # descriptors=false, which Core 24 accepts only with load_on_startup. + _, error = node.cli("createwallet", "dashnet", "false", "false", "", "false", "false", "true") + if error is not None: + self.summary["legacyWallet"] = error[:200] + # Core 23 and 24 create legacy wallets. A later Core that does not + # can no longer be given this state: say so, loudly, and go on. + self.assertGreaterEqual(self.ok(node, "getnetworkinfo")["version"], 250000, "no legacy wallet on %s: %s" % (IMAGE, error)) + self.skipTest("%s no longer creates legacy wallets: %s" % (IMAGE, error[:200])) + self.assertIs(self.ok(node, "getwalletinfo", wallet="dashnet").get("descriptors", False), False) + payout = self.ok(node, "getnewaddress", "dashnet:payout", wallet="dashnet") + spork = self.ok(node, "getnewaddress", "dashnet:spork", wallet="dashnet") + key = self.ok(node, "dumpprivkey", spork, wallet="dashnet") + w.atomic("secrets.json", dict(w.read("secrets.json"), sporkKey=key)) + self.assertEqual(self.step(node, w, w.wallet), dict(payoutAddress=payout, sporkAddress=spork)) + self.assertEqual(w.read("secrets.json")["sporkKey"], key) + self.summary["legacyWallet"] = "ok" + + def test_8_deployment_interrupted_before_the_spork_key(self): + """A deployment that stopped where Core 24 first broke it: the wallet + and its labelled addresses exist, no spork key was recorded.""" + node = self.start() + w = node.worker() + self.ok(node, "createwallet", "dashnet") + payout = self.ok(node, "getnewaddress", "dashnet:payout", wallet="dashnet") + self.ok(node, "getnewaddress", "dashnet:spork", wallet="dashnet") + self.assertNotIn("sporkKey", w.read("secrets.json")) + resumed = self.step(node, w, w.wallet) + self.assertEqual(resumed["payoutAddress"], payout) + self.assertEqual(self.key_address(node, w.read("secrets.json")["sporkKey"]), resumed["sporkAddress"]) + self.assertEqual(self.step(node, w, w.wallet), resumed) + + +if __name__ == "__main__": + unittest.main() diff --git a/internal/node/testdata/test_worker.py b/internal/node/testdata/test_worker.py index a0411e5..56d7118 100644 --- a/internal/node/testdata/test_worker.py +++ b/internal/node/testdata/test_worker.py @@ -3,6 +3,7 @@ import hashlib import importlib.util import json +import re import io from pathlib import Path import tempfile @@ -215,6 +216,29 @@ def rpc(self, method, params=None, wallet=False): with self.assertRaisesRegex(worker.Failure, "unsupported-spork-profile"): w.activate() + class Core24(Sporks): + """Core 24 has no sporks 3 and 9 and refuses an update that names one.""" + active = {"SPORK_2_INSTANTSEND_ENABLED": False, + "SPORK_17_QUORUM_DKG_ENABLED": False, + "SPORK_19_CHAINLOCKS_ENABLED": False, + "SPORK_21_QUORUM_ALL_CONNECTED": False, + "SPORK_23_QUORUM_POSE": False} + + def rpc(self, method, params=None, wallet=False): + if method == "sporkupdate" and params[0] not in self.active: + raise worker.RPCFailure(method, -8) + return super().rpc(method, params, wallet) + + w = Core24(request()) + w.activate() + w.activate() + self.assertEqual(w.updates, 4) + self.assertEqual(w.active["SPORK_23_QUORUM_POSE"], False, "activates only what a devnet needs") + # Only the two sporks Core retired may be absent. + del w.active["SPORK_2_INSTANTSEND_ENABLED"] + with self.assertRaisesRegex(worker.Failure, "unsupported-spork-profile"): + w.activate() + def test_block_time_sets_core_spacing_and_miner_cadence(self): class Miner(worker.Worker): def __init__(self, q, root): @@ -333,6 +357,73 @@ def rpc(self, method, params=None, wallet=False): w.fund() self.assertEqual(w.calls, []) + def test_spork_key_is_made_here_and_never_exported_from_the_wallet(self): + class Core(worker.Worker): + """Core 24: a descriptor wallet, from which no key can be exported.""" + + def __init__(self, q, root): + super().__init__(q, root, root / "lock") + self.atomic("secrets.json", {"rpcPassword": "p" * 64}) + self.parsed, self.refuse, self.echo = [], False, False + + def rpc(self, method, params=None, wallet=False): + if method == "listwallets": + return ["dashnet"] + if method == "getaddressesbylabel": + assert params == ["dashnet:payout"] and wallet, "only the payout address is the wallet's" + return {"y" + "1" * 33: {}} + if method == "getdescriptorinfo": + assert not wallet and re.fullmatch(r"pkh\([1-9A-HJ-NP-Za-km-z]+\)", params[0]) + if self.refuse: + raise worker.RPCFailure(method, -5) + self.parsed.append(params[0][4:-1]) + public = params[0] if self.echo else "pkh(02" + hashlib.sha256(params[0].encode()).hexdigest() + ")#checksum" + return dict(descriptor=public, checksum="other", isrange=False, issolvable=True, hasprivatekeys=True) + if method == "deriveaddresses": + assert not wallet and params[0].startswith("pkh(02") + return ["y" + hashlib.sha256(params[0].encode()).hexdigest()[:33]] + raise AssertionError(method) # dumpprivkey above all + + def decode(text): + value = 0 + for c in text: + value = value * 58 + worker.BASE58.index(c) + return value.to_bytes(38, "big") + + with tempfile.TemporaryDirectory() as tmp: + w = Core(request(), Path(tmp)) + first = w.wallet() + key = w.read("secrets.json")["sporkKey"] + raw = decode(key) + # A compressed test-chain key in wallet import format, in range. + self.assertEqual((raw[0], raw[33]), (0xEF, 1)) + self.assertEqual(hashlib.sha256(hashlib.sha256(raw[:34]).digest()).digest()[:4], raw[34:]) + self.assertTrue(0 < int.from_bytes(raw[1:33], "big") < worker.SECP256K1_ORDER) + self.assertEqual(first["payoutAddress"], "y" + "1" * 33) + self.assertNotEqual(first["sporkAddress"], first["payoutAddress"]) + # A resume derives the same address from the same recorded key. + self.assertEqual(w.wallet(), first) + self.assertEqual(w.read("secrets.json")["sporkKey"], key) + self.assertEqual(set(w.parsed), {key}) + with tempfile.TemporaryDirectory() as tmp: + # A key exported by an earlier version of this tool is kept as it is. + w = Core(request(), Path(tmp)) + w.atomic("secrets.json", dict(w.read("secrets.json"), sporkKey="cExported")) + w.wallet() + self.assertEqual((w.parsed, w.read("secrets.json")["sporkKey"]), (["cExported"], "cExported")) + with tempfile.TemporaryDirectory() as tmp: + # Nothing is recorded for a key Core did not accept or did not + # turn into a public descriptor. + w = Core(request(), Path(tmp)) + w.refuse = True + with self.assertRaisesRegex(worker.Failure, "rpc-getdescriptorinfo:-5"): + w.wallet() + w.refuse, w.echo = False, True + with self.assertRaisesRegex(worker.Failure, "private-key-descriptor"): + w.wallet() + self.assertNotIn("sporkKey", w.read("secrets.json")) + self.assertEqual(len({worker.new_private_key() for _ in range(50)}), 50) + def test_lost_registration_response_resends_no_new_funding(self): with tempfile.TemporaryDirectory() as tmp: q = request() diff --git a/internal/node/worker.py b/internal/node/worker.py index 79b66ba..d86559f 100644 --- a/internal/node/worker.py +++ b/internal/node/worker.py @@ -153,6 +153,27 @@ def block_age(stamp, now=None): return max(0, int((now - when).total_seconds())) +# Order of the secp256k1 group: a private key is an integer in [1, order - 1]. +SECP256K1_ORDER = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 +BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" + + +def new_private_key(): + """A new private key in Core's wallet import format, for a compressed + public key on a test chain (devnet, testnet and regtest share version 239). + + Only the encoding is done here. Core checks the key and derives its + address: see Worker.key_address.""" + number = secrets.randbelow(SECP256K1_ORDER - 1) + 1 + raw = b"\xef" + number.to_bytes(32, "big") + b"\x01" + raw += hashlib.sha256(hashlib.sha256(raw).digest()).digest()[:4] + value, text = int.from_bytes(raw, "big"), "" + while value: + value, digit = divmod(value, 58) + text = BASE58[digit] + text + return text + + class Failure(Exception): pass @@ -1119,6 +1140,25 @@ def address(self, label): raise return self.rpc("getnewaddress", [label], True) + def key_address(self, key): + """The address of a private key, derived by Core without any wallet. + + Core parses the key (refusing one for another chain or a damaged one) + and returns the same descriptor with the public key in its place.""" + info = self.rpc("getdescriptorinfo", ["pkh(" + key + ")"]) + self.require( + info.get("hasprivatekeys") is True + and info.get("isrange") is False + and key not in info.get("descriptor", key), + "private-key-descriptor", + ) + found = self.rpc("deriveaddresses", [info["descriptor"]]) + self.require( + isinstance(found, list) and len(found) == 1 and isinstance(found[0], str) and found[0], + "private-key-address", + ) + return found[0] + def wallet(self): self.require(self.t["role"] == "wallet", "wallet-only") if "dashnet" not in self.rpc("listwallets"): @@ -1128,10 +1168,20 @@ def wallet(self): else: self.rpc("createwallet", ["dashnet"]) secret = self.secret() - payout, spork = self.address("dashnet:payout"), self.address("dashnet:spork") + payout = self.address("dashnet:payout") + # The spork key is this tool's, not the wallet's. Core 24 creates + # descriptor wallets, from which no single key can be exported + # (dumpprivkey is refused), and a legacy wallet is a type Core is + # retiring. So the key is made here and Core derives its address, + # identically on every Core version and wallet type. A deployment made + # before this kept the key it exported from its legacy wallet: that + # key derives the same address it always had. if "sporkKey" not in secret: - secret["sporkKey"] = self.rpc("dumpprivkey", [spork], True) + key = new_private_key() + self.key_address(key) # Core accepts it before it is recorded + secret["sporkKey"] = key self.atomic("secrets.json", secret) + spork = self.key_address(secret["sporkKey"]) # Core can restart between broadcast and the controller checkpoint. Re-lock # every locally recorded collateral before any subsequent wallet funding. for path in sorted((self.root / "transactions").glob("*.json")): @@ -1293,6 +1343,11 @@ def activate(self): "SPORK_21_QUORUM_ALL_CONNECTED", ] active = self.rpc("spork", ["active"]) + # Core 24 removed sporks 3 and 9: what they gated is always on there, + # and they are no longer listed or accepted. Any other absence is a + # Core this tool does not know how to activate. + retired = ["SPORK_3_INSTANTSEND_BLOCK_FILTERING", "SPORK_9_SUPERBLOCKS_ENABLED"] + required = [key for key in required if key in active or key not in retired] self.require(all(key in active for key in required), "unsupported-spork-profile") for key in required: if not active[key]: