From bd621728f7fb679cc7ca815c50e57cdec524d156 Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 14 Nov 2026 17:59:21 +0000 Subject: [PATCH 1/2] p2p_core%feat!: type the ChainLock and InstantSend messages Bound `isdlock` inputs at the SDK maximum count. --- pkgs/p2p_core/contrib/locks.py | 96 +++++++++++++++++++++++++ pkgs/p2p_core/corpus/chainlock.json5 | 82 +++++++++++++++++++++ pkgs/p2p_core/corpus/instantsend.json5 | 98 ++++++++++++++++++++++++++ pkgs/p2p_core/src/msg/chainlock.rs | 51 ++++++++++++++ pkgs/p2p_core/src/msg/instantsend.rs | 69 ++++++++++++++++++ pkgs/p2p_core/src/msg/mod.rs | 20 ++++-- 6 files changed, 412 insertions(+), 4 deletions(-) create mode 100644 pkgs/p2p_core/contrib/locks.py create mode 100644 pkgs/p2p_core/corpus/chainlock.json5 create mode 100644 pkgs/p2p_core/corpus/instantsend.json5 create mode 100644 pkgs/p2p_core/src/msg/chainlock.rs create mode 100644 pkgs/p2p_core/src/msg/instantsend.rs diff --git a/pkgs/p2p_core/contrib/locks.py b/pkgs/p2p_core/contrib/locks.py new file mode 100644 index 00000000..9d0674b0 --- /dev/null +++ b/pkgs/p2p_core/contrib/locks.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +# coding: latin-1 + +# +# Copyright (c) 2026-present, The Dash Core developers +# SPDX-License-Identifier: MIT +# See the accompanying file LICENSE or https://opensource.org/license/MIT +# + +"""Print live `clsig` and `isdlock` literals for the corpus programs.""" + +from __future__ import annotations + +import argparse +from pathlib import Path + +from util.rpc import Node, RpcError + +CHAINS = {"main": "Mainnet", "test": "Testnet"} +MAX_LOCK_QUERY = 100 + + +def render(node: Node) -> str: + """Return literals for the live locks held by *node*.""" + info = node.call("getblockchaininfo") + title = CHAINS.get(info.get("chain")) if isinstance(info, dict) else None + if title is None: + raise RpcError("the node is on neither mainnet nor testnet") + chainlock = node.call("getbestchainlock") + if not isinstance(chainlock, dict) or not isinstance( + chainlock.get("hex"), str + ): + raise RpcError("getbestchainlock did not return a hex payload") + block_hash = chainlock.get("blockhash") + height = chainlock.get("height") + if not isinstance(block_hash, str) or not isinstance(height, int): + raise RpcError("getbestchainlock did not return a block and height") + comment = ( + f" // {title} height {height:,}, block hash {block_hash}, " + "from getbestchainlock." + ) + lines = [ + comment, + " constexpr char CLSIG_LIVE[] =", + f' "{chainlock["hex"]}";', + ] + txids = node.call("getrawmempool") + if not isinstance(txids, list) or not all( + isinstance(txid, str) for txid in txids + ): + raise RpcError("getrawmempool did not return transaction ids") + found = [] + for start in range(0, len(txids), MAX_LOCK_QUERY): + locks = node.call("getislocks", txids[start:start + MAX_LOCK_QUERY]) + if not isinstance(locks, list): + raise RpcError("getislocks did not return an array") + for lock in locks: + if not isinstance(lock, dict): + continue + txid = lock.get("txid") + inputs = lock.get("inputs") + if not isinstance(txid, str) or not isinstance(inputs, list): + raise RpcError("getislocks returned an incomplete lock") + if isinstance(lock.get("hex"), str): + found.append(lock) + one = next((lock for lock in found if len(lock["inputs"]) == 1), None) + many = max(found, key=lambda lock: len(lock["inputs"]), default=None) + if one is None or many is None or len(many["inputs"]) < 2: + raise RpcError("the mempool holds no locks with one and with more inputs") + for label, lock in (("ONE", one), ("MANY", many)): + txid = lock["txid"] + inputs = lock["inputs"] + count = "one input" if len(inputs) == 1 else f"{len(inputs)} inputs" + lines.extend([ + f" // {title} txid {txid}, {count}, from getislocks.", + f" constexpr char ISDLOCK_LIVE_{label}[] =", + f' "{lock["hex"]}";', + ]) + return "\n".join(lines) + "\n" + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--node", nargs=2, required=True, metavar=("HOST:PORT", "COOKIE") + ) + return parser.parse_args() + + +def main() -> None: + args = parse_args() + print(render(Node(args.node[0], Path(args.node[1]).expanduser())), end="") + + +if __name__ == "__main__": + main() diff --git a/pkgs/p2p_core/corpus/chainlock.json5 b/pkgs/p2p_core/corpus/chainlock.json5 new file mode 100644 index 00000000..95d32c30 --- /dev/null +++ b/pkgs/p2p_core/corpus/chainlock.json5 @@ -0,0 +1,82 @@ +// +// ChainLock vectors. Generated with the reference implementation +// (dashpay/dash v24.0.0, 938fba5028). +// +// Each section comes with the program used to generate it. The literals of +// `clsig_payloads` were read from testnet with +// `python3 pkgs/p2p_core/contrib/locks.py --node HOST:PORT COOKIE`, +// which prints them as the program takes them. +// +// To run a program, paste it at the end of the `net_tests` suite in +// `src/test/net_tests.cpp` at the specified revision, include `bls/bls.h` and +// `chainlock/chainlock.h` there, build it with `make -C src test/test_dash` +// and run `./src/test/test_dash --run_test=net_tests/`. The output +// should match the section. +// + +{ +/* +BOOST_AUTO_TEST_CASE(clsig_payloads) +{ + // Testnet height 1,591,597, block hash 00000033c02bc0c9db6c29eace7eebaca13d42048964ba3dd3054d97f8765d96, from getbestchainlock. + constexpr char CLSIG_LIVE[] = + "2d491800965d76f8974d05d33dba648904423da1aceb7eceea296cdbc9c02bc033000000acdf465f86507649eb3937ff932dd94a6b98a8295eac689b6fb2be65ac9f379489f8c2257f7d028cbe66e03e9ac9c56f021638327790564052d827b4455848f2d600f79c459f6d419c7026501436a6cee091bdd6d5543f2710abd25e27cbbe32"; + const bool legacy{bls::bls_legacy_scheme.exchange(false)}; + std::printf(" \"clsig\": {\n"); + const auto print = [](const char* label, const CSerializedNetMsg& message) { + DataStream fields{message.data}; + chainlock::ChainLockSig clsig; + fields >> clsig; + DataStream sig{}; + sig << clsig.getSig(); + std::printf(" \"%s\": {\n \"raw\": \"%s\",\n \"details\": {\n \"height\": %d,\n" + " \"block_hash\": \"%s\",\n \"sig\": \"%s\"\n }\n },\n", + label, HexStr(message.data).c_str(), clsig.getHeight(), clsig.getBlockHash().ToString().c_str(), + HexStr(sig).c_str()); + }; + const CNetMsgMaker maker{PROTOCOL_VERSION}; + CBLSSecretKey sk; + BOOST_REQUIRE(sk.SetHexStr("0101010101010101010101010101010101010101010101010101010101010101", false)); + print("null", maker.Make(NetMsgType::CLSIG, chainlock::ChainLockSig{-1, uint256{}, CBLSSignature{}})); + print("max_height", maker.Make(NetMsgType::CLSIG, + chainlock::ChainLockSig{std::numeric_limits::max(), + uint256S("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"), + sk.Sign(uint256::TWO, false)})); + DataStream live(ParseHex(CLSIG_LIVE)); + chainlock::ChainLockSig clsig; + live >> clsig; + print("testnet_one", maker.Make(NetMsgType::CLSIG, clsig)); + std::printf(" },\n"); + bls::bls_legacy_scheme.store(legacy); +} +*/ + "clsig": { + // Null height, block hash and signature. + "null": { + "raw": "ffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "details": { + "height": -1, + "block_hash": "0000000000000000000000000000000000000000000000000000000000000000", + "sig": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + } + }, + // Maximum height with a nonzero block hash and signature. + "max_height": { + "raw": "ffffff7fffeeddccbbaa99887766554433221100ffeeddccbbaa9988776655443322110085d868bf084909a51630db09eb41c970b11e6a27dd1bbb2af1e511a67e6e68ca93364b6ed861decab01285ed556d76361627217469c4587176b80e92177d5a4fbe1e290db4cd7e9d68274bf5654f61624a8b7aae21920a6fd7cf5e87cc6e9b62", + "details": { + "height": 2147483647, + "block_hash": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", + "sig": "85d868bf084909a51630db09eb41c970b11e6a27dd1bbb2af1e511a67e6e68ca93364b6ed861decab01285ed556d76361627217469c4587176b80e92177d5a4fbe1e290db4cd7e9d68274bf5654f61624a8b7aae21920a6fd7cf5e87cc6e9b62" + } + }, + // Testnet height 1,591,597, block hash 00000033c02bc0c9db6c29eace7eebaca13d42048964ba3dd3054d97f8765d96, from getbestchainlock. + "testnet_one": { + "raw": "2d491800965d76f8974d05d33dba648904423da1aceb7eceea296cdbc9c02bc033000000acdf465f86507649eb3937ff932dd94a6b98a8295eac689b6fb2be65ac9f379489f8c2257f7d028cbe66e03e9ac9c56f021638327790564052d827b4455848f2d600f79c459f6d419c7026501436a6cee091bdd6d5543f2710abd25e27cbbe32", + "details": { + "height": 1591597, + "block_hash": "00000033c02bc0c9db6c29eace7eebaca13d42048964ba3dd3054d97f8765d96", + "sig": "acdf465f86507649eb3937ff932dd94a6b98a8295eac689b6fb2be65ac9f379489f8c2257f7d028cbe66e03e9ac9c56f021638327790564052d827b4455848f2d600f79c459f6d419c7026501436a6cee091bdd6d5543f2710abd25e27cbbe32" + } + }, + }, +} diff --git a/pkgs/p2p_core/corpus/instantsend.json5 b/pkgs/p2p_core/corpus/instantsend.json5 new file mode 100644 index 00000000..dc34e74d --- /dev/null +++ b/pkgs/p2p_core/corpus/instantsend.json5 @@ -0,0 +1,98 @@ +// +// InstantSend lock vectors. Generated with the reference implementation +// (dashpay/dash v24.0.0, 938fba5028). +// +// Each section comes with the program used to generate it. The literals of +// `isdlock_payloads` were read from testnet with +// `python3 pkgs/p2p_core/contrib/locks.py --node HOST:PORT COOKIE`, +// which prints them as the program takes them. +// +// To run a program, paste it at the end of the `net_tests` suite in +// `src/test/net_tests.cpp` at the specified revision, include `bls/bls.h` and +// `instantsend/lock.h` there, build it with `make -C src test/test_dash` and +// run `./src/test/test_dash --run_test=net_tests/`. The output should +// match the section. +// + +{ +/* +BOOST_AUTO_TEST_CASE(isdlock_payloads) +{ + // Testnet txid a42dba55ba7e5561be87da57f597bb8532eb3f2f9b939310bc8c2c892aa10748, one input, from getislocks. + constexpr char ISDLOCK_LIVE_ONE[] = + "0101daea6ae2db993a4dfa121b3b61672f31fdbc68fbfaa407bb009cbe7c9e50c276050000004807a12a892c8cbc1093939b2f3feb3285bb97f557da87be61557eba55ba2da4ff4275241baa5b5aca774ed0f628824202f75264b9caa781a33b364732000000809824e08d6b62fe58f0a5798b72d76d24416118de1763886a2f44070c8ecd8926b61e4eb32530f590fd657bfa1a79330afc4cd7603866eeb3432351914ddbc10901e744b3db26d595d7f45d86e0d47fcd7919279f4ee632ac4f78e33961684f"; + // Testnet txid c4fa855ceefd0afe02391591962f316eafd9a9c8537b0340762eff9706ca8bfd, 16 inputs, from getislocks. + constexpr char ISDLOCK_LIVE_MANY[] = + "0110944cfe7fa01c85688d2eac9cc7b0bb669752f11f8e8947075a78961bcb13c5a80000000058cf43d2709f5b32a61789b5a63574efdf7f5e0102e0c1283214021212b7c4c30d00000058cf43d2709f5b32a61789b5a63574efdf7f5e0102e0c1283214021212b7c4c301000000daea6ae2db993a4dfa121b3b61672f31fdbc68fbfaa407bb009cbe7c9e50c2760100000093b752e0334f64cc703408fa80e4c91d77dd531bcb65184f663edb9aaaeb2c5f0000000016d0baaa94cdede1e8abbdca7045d550f172ec0249b42d478c68763eda336e5a0500000095a776f9b3df28faff0b0d263fe6176cf3679ad46b8675c0322bfc2ad70e723e080000003d6e919149b17f7a4fcedc8aa0544895c088309da6e72199023cde6a16b8e007fc0000003d6e919149b17f7a4fcedc8aa0544895c088309da6e72199023cde6a16b8e0078e0000000cde0ef2ff24ef0c3ed19e30ee7adf02c365029c5abed57223533c28f0d7ec020900000082ed817753de30b05b828db0d182030a9abb73a84be948ea735b5af3b6632026d10000004d013187a0b34140b2597d4d24b06f1605317a9c9f0288c0dceea31ee30b2dee00000000aa5243b23890e6ed6b0d16e5dfede2de0240d7e06099d744bc2c1adc0851aeb50d000000e0d7476f34582e42da381e77111c5d6e11b46a18c9d497033940ed5d3ee1ebec00000000ff99d959f6d22101ad9307184e2ef7ef818fd477e8d3b63124f4027ce4a7e83b03000000f1787de7d9fadb3d2b37d79e8f0504923853ed2ffbdb0788e9ccf1c27a304f8b0e000000fd8bca0697ff2e7640037b53c8a9d9af6e312f9691153902fe0afdee5c85fac4ff4275241baa5b5aca774ed0f628824202f75264b9caa781a33b364732000000b684ad62d186a7f40f5a2262288b2a79a4a3f2b4453f87f123835ddc08d993a2659b66f5aa8434f44130e6f6db91325602a91d5f529e694b7aeeb91ac3f36d0c8c0943ea5a8130427100df2f1eb8074cecf8e685dfa86ee36a3db3a49282e262"; + const bool legacy{bls::bls_legacy_scheme.exchange(false)}; + std::printf(" \"isdlock\": {\n"); + const auto print = [](const char* label, const CSerializedNetMsg& message) { + DataStream fields{message.data}; + instantsend::InstantSendLock lock; + fields >> lock; + DataStream sig{}; + sig << lock.sig; + std::printf(" \"%s\": {\n \"raw\": \"%s\",\n \"details\": {\n \"version\": %u,\n" + " \"inputs\": [", + label, HexStr(message.data).c_str(), lock.nVersion); + for (size_t i = 0; i < lock.inputs.size(); ++i) { + std::printf("%s{ \"hash\": \"%s\", \"index\": %u }", i ? ", " : "", + lock.inputs[i].hash.ToString().c_str(), lock.inputs[i].n); + } + std::printf("],\n \"txid\": \"%s\",\n \"cycle_hash\": \"%s\",\n \"sig\": \"%s\"\n" + " }\n },\n", + lock.txid.ToString().c_str(), lock.cycleHash.ToString().c_str(), HexStr(sig).c_str()); + }; + const auto read = [](const char* raw) { + DataStream input(ParseHex(raw)); + instantsend::InstantSendLock lock; + input >> lock; + return lock; + }; + const CNetMsgMaker maker{PROTOCOL_VERSION}; + instantsend::InstantSendLock empty; + empty.nVersion = 255; + empty.sig.Set(CBLSSignature{}, false); + print("version_255_no_inputs", maker.Make(NetMsgType::ISDLOCK, empty)); + print("testnet_one_input", maker.Make(NetMsgType::ISDLOCK, read(ISDLOCK_LIVE_ONE))); + print("testnet_many_inputs", maker.Make(NetMsgType::ISDLOCK, read(ISDLOCK_LIVE_MANY))); + std::printf(" },\n"); + bls::bls_legacy_scheme.store(legacy); +} +*/ + "isdlock": { + // Version 255 with no inputs. + "version_255_no_inputs": { + "raw": "ff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "details": { + "version": 255, + "inputs": [], + "txid": "0000000000000000000000000000000000000000000000000000000000000000", + "cycle_hash": "0000000000000000000000000000000000000000000000000000000000000000", + "sig": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + } + }, + // Testnet txid a42dba55ba7e5561be87da57f597bb8532eb3f2f9b939310bc8c2c892aa10748, one input, from getislocks. + "testnet_one_input": { + "raw": "0101daea6ae2db993a4dfa121b3b61672f31fdbc68fbfaa407bb009cbe7c9e50c276050000004807a12a892c8cbc1093939b2f3feb3285bb97f557da87be61557eba55ba2da4ff4275241baa5b5aca774ed0f628824202f75264b9caa781a33b364732000000809824e08d6b62fe58f0a5798b72d76d24416118de1763886a2f44070c8ecd8926b61e4eb32530f590fd657bfa1a79330afc4cd7603866eeb3432351914ddbc10901e744b3db26d595d7f45d86e0d47fcd7919279f4ee632ac4f78e33961684f", + "details": { + "version": 1, + "inputs": [{ "hash": "76c2509e7cbe9c00bb07a4fafb68bcfd312f67613b1b12fa4d3a99dbe26aeada", "index": 5 }], + "txid": "a42dba55ba7e5561be87da57f597bb8532eb3f2f9b939310bc8c2c892aa10748", + "cycle_hash": "0000003247363ba381a7cab96452f702428228f6d04e77ca5a5baa1b247542ff", + "sig": "809824e08d6b62fe58f0a5798b72d76d24416118de1763886a2f44070c8ecd8926b61e4eb32530f590fd657bfa1a79330afc4cd7603866eeb3432351914ddbc10901e744b3db26d595d7f45d86e0d47fcd7919279f4ee632ac4f78e33961684f" + } + }, + // Testnet txid c4fa855ceefd0afe02391591962f316eafd9a9c8537b0340762eff9706ca8bfd, 16 inputs, from getislocks. + "testnet_many_inputs": { + "raw": "0110944cfe7fa01c85688d2eac9cc7b0bb669752f11f8e8947075a78961bcb13c5a80000000058cf43d2709f5b32a61789b5a63574efdf7f5e0102e0c1283214021212b7c4c30d00000058cf43d2709f5b32a61789b5a63574efdf7f5e0102e0c1283214021212b7c4c301000000daea6ae2db993a4dfa121b3b61672f31fdbc68fbfaa407bb009cbe7c9e50c2760100000093b752e0334f64cc703408fa80e4c91d77dd531bcb65184f663edb9aaaeb2c5f0000000016d0baaa94cdede1e8abbdca7045d550f172ec0249b42d478c68763eda336e5a0500000095a776f9b3df28faff0b0d263fe6176cf3679ad46b8675c0322bfc2ad70e723e080000003d6e919149b17f7a4fcedc8aa0544895c088309da6e72199023cde6a16b8e007fc0000003d6e919149b17f7a4fcedc8aa0544895c088309da6e72199023cde6a16b8e0078e0000000cde0ef2ff24ef0c3ed19e30ee7adf02c365029c5abed57223533c28f0d7ec020900000082ed817753de30b05b828db0d182030a9abb73a84be948ea735b5af3b6632026d10000004d013187a0b34140b2597d4d24b06f1605317a9c9f0288c0dceea31ee30b2dee00000000aa5243b23890e6ed6b0d16e5dfede2de0240d7e06099d744bc2c1adc0851aeb50d000000e0d7476f34582e42da381e77111c5d6e11b46a18c9d497033940ed5d3ee1ebec00000000ff99d959f6d22101ad9307184e2ef7ef818fd477e8d3b63124f4027ce4a7e83b03000000f1787de7d9fadb3d2b37d79e8f0504923853ed2ffbdb0788e9ccf1c27a304f8b0e000000fd8bca0697ff2e7640037b53c8a9d9af6e312f9691153902fe0afdee5c85fac4ff4275241baa5b5aca774ed0f628824202f75264b9caa781a33b364732000000b684ad62d186a7f40f5a2262288b2a79a4a3f2b4453f87f123835ddc08d993a2659b66f5aa8434f44130e6f6db91325602a91d5f529e694b7aeeb91ac3f36d0c8c0943ea5a8130427100df2f1eb8074cecf8e685dfa86ee36a3db3a49282e262", + "details": { + "version": 1, + "inputs": [{ "hash": "a8c513cb1b96785a0747898e1ff1529766bbb0c79cac2e8d68851ca07ffe4c94", "index": 0 }, { "hash": "c3c4b7121202143228c1e002015e7fdfef7435a6b58917a6325b9f70d243cf58", "index": 13 }, { "hash": "c3c4b7121202143228c1e002015e7fdfef7435a6b58917a6325b9f70d243cf58", "index": 1 }, { "hash": "76c2509e7cbe9c00bb07a4fafb68bcfd312f67613b1b12fa4d3a99dbe26aeada", "index": 1 }, { "hash": "5f2cebaa9adb3e664f1865cb1b53dd771dc9e480fa083470cc644f33e052b793", "index": 0 }, { "hash": "5a6e33da3e76688c472db44902ec72f150d54570cabdabe8e1edcd94aabad016", "index": 5 }, { "hash": "3e720ed72afc2b32c075866bd49a67f36c17e63f260d0bfffa28dfb3f976a795", "index": 8 }, { "hash": "07e0b8166ade3c029921e7a69d3088c0954854a08adcce4f7a7fb14991916e3d", "index": 252 }, { "hash": "07e0b8166ade3c029921e7a69d3088c0954854a08adcce4f7a7fb14991916e3d", "index": 142 }, { "hash": "02ecd7f0283c532372d5be5a9c0265c302df7aee309ed13e0cef24fff20ede0c", "index": 9 }, { "hash": "262063b6f35a5b73ea48e94ba873bb9a0a0382d1b08d825bb030de537781ed82", "index": 209 }, { "hash": "ee2d0be31ea3eedcc088029f9c7a3105166fb0244d7d59b24041b3a08731014d", "index": 0 }, { "hash": "b5ae5108dc1a2cbc44d79960e0d74002dee2eddfe5160d6bede69038b24352aa", "index": 13 }, { "hash": "ecebe13e5ded40390397d4c9186ab4116e5d1c11771e38da422e58346f47d7e0", "index": 0 }, { "hash": "3be8a7e47c02f42431b6d3e877d48f81eff72e4e180793ad0121d2f659d999ff", "index": 3 }, { "hash": "8b4f307ac2f1cce98807dbfb2fed53389204058f9ed7372b3ddbfad9e77d78f1", "index": 14 }], + "txid": "c4fa855ceefd0afe02391591962f316eafd9a9c8537b0340762eff9706ca8bfd", + "cycle_hash": "0000003247363ba381a7cab96452f702428228f6d04e77ca5a5baa1b247542ff", + "sig": "b684ad62d186a7f40f5a2262288b2a79a4a3f2b4453f87f123835ddc08d993a2659b66f5aa8434f44130e6f6db91325602a91d5f529e694b7aeeb91ac3f36d0c8c0943ea5a8130427100df2f1eb8074cecf8e685dfa86ee36a3db3a49282e262" + } + }, + }, +} diff --git a/pkgs/p2p_core/src/msg/chainlock.rs b/pkgs/p2p_core/src/msg/chainlock.rs new file mode 100644 index 00000000..1ab3cc9d --- /dev/null +++ b/pkgs/p2p_core/src/msg/chainlock.rs @@ -0,0 +1,51 @@ +// +// Copyright (c) 2026-present, The Dash Core developers +// SPDX-License-Identifier: MIT +// See the accompanying file LICENSE or https://opensource.org/license/MIT +// + +//! ChainLock signature messages. + +use crate::codec::codec_p2p; + +use dash_pkc::bls::BlsSigBytes; +use dash_primitives::BlockHash; +use dash_types::type_id::TypeId; +use getset::CopyGetters; + +codec_p2p! { +/// A ChainLock signature message. +#[derive(Clone, Copy, CopyGetters, Debug, Eq, Hash, PartialEq, TypeId)] +#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))] +pub struct ClSig { + /// Height of the locked block. + #[getset(get_copy = "pub")] + height: i32, + /// Hash of the locked block. + #[getset(get_copy = "pub")] + block_hash: BlockHash, + /// BLS signature. + #[getset(get_copy = "pub")] + sig: BlsSigBytes, +} +} + +#[cfg(all(test, feature = "serde"))] +mod tests { + use super::*; + use crate::{CommandString, P2pMsg}; + + use dash_dev::{assert_serde_rt, check_wire, Corpus}; + use rstest::rstest; + + #[rstest] + fn corpus_clsig() { + let corpus = Corpus::open(env!("CARGO_MANIFEST_DIR"), "chainlock"); + let items = corpus.entries::("clsig", |raw, details, label| { + check_wire(raw, details, label); + let decoded = P2pMsg::decode_payload(&CommandString::CLSIG, raw); + assert_eq!(decoded, Ok(P2pMsg::ClSig(*details)), "{label}: dispatch"); + }); + assert_serde_rt("clsig", &items); + } +} diff --git a/pkgs/p2p_core/src/msg/instantsend.rs b/pkgs/p2p_core/src/msg/instantsend.rs new file mode 100644 index 00000000..e4a4ace4 --- /dev/null +++ b/pkgs/p2p_core/src/msg/instantsend.rs @@ -0,0 +1,69 @@ +// +// Copyright (c) 2026-present, The Dash Core developers +// SPDX-License-Identifier: MIT +// See the accompanying file LICENSE or https://opensource.org/license/MIT +// + +//! InstantSend lock messages. + +use crate::codec::impl_p2p; +use crate::prelude::*; + +use dash_pkc::bls::BlsSigBytes; +use dash_primitives::{hash_impl, BlockHash, OutPoint, TxHash, MAX_DIP0001_BLOCK_SIZE}; +use dash_types::type_id::TypeId; +use dash_types::{impl_new, impl_vec}; +use getset::{CopyGetters, Getters}; + +impl_new! { +/// A deterministic InstantSend lock on a transaction. +#[derive(Clone, CopyGetters, Debug, Eq, Getters, Hash, PartialEq, TypeId)] +#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))] +pub struct IsdLock { + /// Lock version. + #[getset(get_copy = "pub")] + version: u8, + /// Outpoints the locked transaction spends. + #[getset(get = "pub")] + inputs: Vec, + /// Hash of the locked transaction. + #[getset(get_copy = "pub")] + txid: TxHash, + /// Cycle hash, a block hash. + #[getset(get_copy = "pub")] + cycle_hash: BlockHash, + /// BLS signature. + #[getset(get_copy = "pub")] + sig: BlsSigBytes, +} +} + +impl_vec!(IsdLock, { version, inputs: Self::MAX_INPUT_COUNT, txid, cycle_hash, sig }); +impl_p2p!(IsdLock); +hash_impl!(IsdLock); + +impl IsdLock { + /// The most inputs an `isdlock` carries. + pub const MAX_INPUT_COUNT: usize = + MAX_DIP0001_BLOCK_SIZE / (size_of::() + size_of::() + size_of::()); +} + +#[cfg(all(test, feature = "serde"))] +mod tests { + use super::*; + use crate::{CommandString, P2pMsg}; + + use dash_dev::{assert_serde_rt, check_wire, Corpus}; + use rstest::rstest; + + #[rstest] + fn corpus_isdlock() { + let corpus = Corpus::open(env!("CARGO_MANIFEST_DIR"), "instantsend"); + let items = corpus.entries::("isdlock", |raw, details, label| { + check_wire(raw, details, label); + let decoded = P2pMsg::decode_payload(&CommandString::ISDLOCK, raw); + assert_eq!(decoded, Ok(P2pMsg::IsdLock(details.clone())), "{label}: dispatch"); + }); + assert_serde_rt("isdlock", &items); + } +} diff --git a/pkgs/p2p_core/src/msg/mod.rs b/pkgs/p2p_core/src/msg/mod.rs index 33089cd5..0388bc9d 100644 --- a/pkgs/p2p_core/src/msg/mod.rs +++ b/pkgs/p2p_core/src/msg/mod.rs @@ -10,10 +10,12 @@ mod addr; mod blocks; mod bloom; mod cfilter; +mod chainlock; mod coinjoin; mod gov; mod headers; mod headers2; +mod instantsend; mod inv; mod mn_list; mod ping; @@ -34,10 +36,12 @@ pub use addr::{Addr, AddrV2Entry, AddrV2Msg, TimestampedAddr}; pub use blocks::{GetBlocks, SendCmpct}; pub use bloom::{BloomFlags, FilterAdd, FilterLoad, MerkleBlock, PartialMerkleTree}; pub use cfilter::{CFCheckpt, CFHeaders, CFilter, FilterHash, FilterHeader, GetCFCheckpt, GetCFHeaders, GetCFilters}; +pub use chainlock::ClSig; pub use coinjoin::SendDsq; pub use gov::{GovSync, GovSyncStatusCount}; pub use headers::{GetHeaders, Headers}; pub use headers2::{CompressedHeader, GetHeaders2, Headers2}; +pub use instantsend::IsdLock; pub use inv::{GetData, Inv, InvType, Inventory, NotFound}; pub use mn_list::{DeletedQuorum, GetMnListDiff, MnListDiff, MnListDiffPayload, QuorumClSig, SimplifiedMnListEntry}; pub use ping::{Ping, Pong}; @@ -111,6 +115,10 @@ define_p2p! { GetCFCheckpt(GetCFCheckpt) => GETCFCHECKPT "getcfcheckpt" @ 26, /// BIP157: compact filter checkpoints. CFCheckpt(CFCheckpt) => CFCHECKPT "cfcheckpt" @ 27, + /// ChainLock signature. + ClSig(ClSig) => CLSIG "clsig" @ 160, + /// InstantSend deterministic lock. + IsdLock(IsdLock) => ISDLOCK "isdlock" @ 161, } parsed_empty { @@ -183,10 +191,6 @@ define_p2p! { QGetData => QGETDATA "qgetdata" @ 158, /// LLMQ: quorum data. QData => QDATA "qdata" @ 159, - /// ChainLock signature. - ClSig => CLSIG "clsig" @ 160, - /// InstantSend deterministic lock. - IsdLock => ISDLOCK "isdlock" @ 161, /// Masternode authentication. MnAuth => MNAUTH "mnauth" @ 162, /// Request quorum rotation info. @@ -243,6 +247,12 @@ mod tests { /// An entry of an `addrv2`, an IPv4 address. const ADDRV2: [u8; 13] = [0, 0, 0, 0, 0, 1, 4, 1, 2, 3, 4, 0x27, 0x0f]; + /// An input of an `isdlock`, an outpoint. + const OUTPOINT: [u8; 36] = [0; 36]; + + /// The txid, cycle hash and signature that follow the inputs of an `isdlock`. + const ISDLOCK_TAIL: [u8; 160] = [0; 160]; + /// `prefix`, a count of `count`, that many `entry` and then `suffix`. fn payload(prefix: &[u8], count: usize, entry: &[u8], suffix: &[u8]) -> Vec { let mut size = Vec::new(); @@ -281,6 +291,7 @@ mod tests { #[case::getheaders2(|| check_count::(&VERSION, &HASH, &HASH, 101))] #[case::addr(|| check_count::(&[], &ADDR, &[], 1_000))] #[case::addrv2(|| check_count::(&[], &ADDRV2, &[], 1_000))] + #[case::isdlock(|| check_count::(&[1], &OUTPOINT, &ISDLOCK_TAIL, IsdLock::MAX_INPUT_COUNT))] fn counts_stop_at_the_limit(#[case] check: fn()) { check(); } @@ -294,6 +305,7 @@ mod tests { #[case::getheaders2(CommandString::GETHEADERS2, &VERSION, 101)] #[case::addr(CommandString::ADDR, &[], 1_000)] #[case::addrv2(CommandString::ADDRV2, &[], 1_000)] + #[case::isdlock(CommandString::ISDLOCK, &[1], IsdLock::MAX_INPUT_COUNT)] fn dispatch_keeps_the_limit(#[case] cmd: CommandString, #[case] prefix: &[u8], #[case] limit: usize) { let bare = payload(prefix, limit + 1, &[], &[]); assert_eq!( From 20bdabb9c99a2f0eb4c079f86d6202f51988fb2d Mon Sep 17 00:00:00 2001 From: pasta Date: Sat, 14 Nov 2026 19:41:22 +0000 Subject: [PATCH 2/2] p2p_core%feat!: type the mnauth, spork and platformban messages --- pkgs/p2p_core/corpus/masternode.json5 | 164 ++++++++++++++++++++++++++ pkgs/p2p_core/corpus/spork.json5 | 95 +++++++++++++++ pkgs/p2p_core/src/msg/masternode.rs | 107 +++++++++++++++++ pkgs/p2p_core/src/msg/mod.rs | 21 +++- pkgs/p2p_core/src/msg/spork.rs | 64 ++++++++++ 5 files changed, 445 insertions(+), 6 deletions(-) create mode 100644 pkgs/p2p_core/corpus/masternode.json5 create mode 100644 pkgs/p2p_core/corpus/spork.json5 create mode 100644 pkgs/p2p_core/src/msg/masternode.rs create mode 100644 pkgs/p2p_core/src/msg/spork.rs diff --git a/pkgs/p2p_core/corpus/masternode.json5 b/pkgs/p2p_core/corpus/masternode.json5 new file mode 100644 index 00000000..d62713cf --- /dev/null +++ b/pkgs/p2p_core/corpus/masternode.json5 @@ -0,0 +1,164 @@ +// +// Masternode vectors. Generated with the reference implementation +// (dashpay/dash v24.0.0, 938fba5028). +// +// Each section comes with the program used to generate it. +// +// To run a program, paste it at the end of the `net_tests` suite in +// `src/test/net_tests.cpp` at the specified revision, include `bls/bls.h`, +// `evo/mnauth.h` and `masternode/meta.h` there, build it with +// `make -C src test/test_dash` and run +// `./src/test/test_dash --run_test=net_tests/`. The output should match +// the section. +// + +{ +/* +BOOST_AUTO_TEST_CASE(mnauth_payloads) +{ + const bool legacy{bls::bls_legacy_scheme.exchange(false)}; + std::printf(" \"mnauth\": {\n"); + const auto print = [](const std::string& label, const CSerializedNetMsg& message) { + DataStream fields{message.data}; + CMNAuth mnauth; + fields >> mnauth; + DataStream sig{}; + sig << mnauth.sig; + std::printf(" \"%s\": {\n \"raw\": \"%s\",\n \"details\": {\n" + " \"pro_reg_tx_hash\": \"%s\",\n \"sig\": \"%s\"\n }\n },\n", + label.c_str(), HexStr(message.data).c_str(), mnauth.proRegTxHash.ToString().c_str(), + HexStr(sig).c_str()); + }; + CBLSSecretKey sk; + BOOST_REQUIRE(sk.SetHexStr("0303030303030303030303030303030303030303030303030303030303030303", false)); + struct Case { + std::string label; + uint256 pro_reg_tx_hash; + CBLSSignature sig; + }; + for (const auto& [label, pro_reg_tx_hash, sig] : std::vector{ + {"null", uint256{}, CBLSSignature{}}, + {"signed", uint256::ONE, sk.Sign(uint256::ONE, false)}, + {"signed_hash", uint256S("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"), + sk.Sign(uint256::TWO, false)}}) { + CMNAuth mnauth; + mnauth.proRegTxHash = pro_reg_tx_hash; + mnauth.sig = sig; + print(label, CNetMsgMaker{PROTOCOL_VERSION}.Make(NetMsgType::MNAUTH, mnauth)); + } + std::printf(" },\n"); + bls::bls_legacy_scheme.store(legacy); +} +*/ + "mnauth": { + // Null registration hash and signature. + "null": { + "raw": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "details": { + "pro_reg_tx_hash": "0000000000000000000000000000000000000000000000000000000000000000", + "sig": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + } + }, + // Registration hash one, signed. + "signed": { + "raw": "0100000000000000000000000000000000000000000000000000000000000000b2d6206cbe3e39e74e3a42aab2912e66c988282bc51d6df7691e11fb796555045dbc8b4510f197e986882133c840cd81162c4e36505e3c05422c4cd199c2099f438742e0a2675b547ee6f86a573cee3315a6d157a54096f74c0561be165ac912", + "details": { + "pro_reg_tx_hash": "0000000000000000000000000000000000000000000000000000000000000001", + "sig": "b2d6206cbe3e39e74e3a42aab2912e66c988282bc51d6df7691e11fb796555045dbc8b4510f197e986882133c840cd81162c4e36505e3c05422c4cd199c2099f438742e0a2675b547ee6f86a573cee3315a6d157a54096f74c0561be165ac912" + } + }, + // Patterned registration hash, signed. + "signed_hash": { + "raw": "ffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100904ee63be4fef3f12da8bc8e6d483cac54ff23f690bcbbc6c7f6cf63f084f37fac63944282dc4b1d67eb5fc579e4355e1839c411bccf9ddfb8351a425d8b7af14b50630986dd7a9da91293841b9c47adca2406c7c75a37230d2fa9b04044f040", + "details": { + "pro_reg_tx_hash": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", + "sig": "904ee63be4fef3f12da8bc8e6d483cac54ff23f690bcbbc6c7f6cf63f084f37fac63944282dc4b1d67eb5fc579e4355e1839c411bccf9ddfb8351a425d8b7af14b50630986dd7a9da91293841b9c47adca2406c7c75a37230d2fa9b04044f040" + } + }, + }, +/* +BOOST_AUTO_TEST_CASE(platformban_payloads) +{ + const bool legacy{bls::bls_legacy_scheme.exchange(false)}; + std::printf(" \"platformban\": {\n"); + const auto print = [](const CSerializedNetMsg& message) { + CDataStream fields{message.data, SER_NETWORK, PROTOCOL_VERSION}; + PlatformBanMessage ban; + fields >> ban; + DataStream sig{}; + sig << ban.m_signature; + std::printf(" \"%s\": {\n \"raw\": \"%s\",\n \"details\": {\n" + " \"pro_tx_hash\": \"%s\",\n \"requested_height\": %d,\n" + " \"quorum_hash\": \"%s\",\n \"sig\": \"%s\"\n }\n },\n", + ban.GetHash().ToString().c_str(), HexStr(message.data).c_str(), ban.m_protx_hash.ToString().c_str(), + ban.m_requested_height, ban.m_quorum_hash.ToString().c_str(), HexStr(sig).c_str()); + }; + CBLSSecretKey sk; + BOOST_REQUIRE(sk.SetHexStr("0505050505050505050505050505050505050505050505050505050505050505", false)); + struct Case { + uint256 pro_tx_hash; + int32_t requested_height; + uint256 quorum_hash; + CBLSSignature sig; + }; + for (const auto& [pro_tx_hash, requested_height, quorum_hash, sig] : std::vector{ + {uint256{}, 0, uint256{}, CBLSSignature{}}, + {uint256::ONE, 1000, uint256::TWO, sk.Sign(uint256::ONE, false)}, + {uint256S("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"), + std::numeric_limits::max(), + uint256S("ffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100"), sk.Sign(uint256::TWO, false)}, + {uint256::TWO, -1, uint256::ONE, sk.Sign(uint256::ONE, false)}}) { + PlatformBanMessage ban; + ban.m_protx_hash = pro_tx_hash; + ban.m_requested_height = requested_height; + ban.m_quorum_hash = quorum_hash; + ban.m_signature = sig; + print(CNetMsgMaker{PROTOCOL_VERSION}.Make(NetMsgType::PLATFORMBAN, ban)); + } + std::printf(" },\n"); + bls::bls_legacy_scheme.store(legacy); +} +*/ + "platformban": { + // Null hashes, zero height and signature. + "515f1053d98a4b5eb206ba75a350aa483fd20dc47b62654547b5b45829d89615": { + "raw": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "details": { + "pro_tx_hash": "0000000000000000000000000000000000000000000000000000000000000000", + "requested_height": 0, + "quorum_hash": "0000000000000000000000000000000000000000000000000000000000000000", + "sig": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + } + }, + // Hashes one and two, a positive height, signed. + "223b87091de1f69a063951ac10a9f036b091a939e175f6ef16f2da6ebdfd7017": { + "raw": "0100000000000000000000000000000000000000000000000000000000000000e80300000200000000000000000000000000000000000000000000000000000000000000813058aef9d01842697c37e671fd29af1cd250558e8637c610713798037e3e7135ce0ae33afc53480883fab28e6e9705117f90cadf2b7193439b5cd9adf15ed25be2906c420ad0fde43700becd3becd537bb79901fb3f180feb04cbce4dfa320", + "details": { + "pro_tx_hash": "0000000000000000000000000000000000000000000000000000000000000001", + "requested_height": 1000, + "quorum_hash": "0000000000000000000000000000000000000000000000000000000000000002", + "sig": "813058aef9d01842697c37e671fd29af1cd250558e8637c610713798037e3e7135ce0ae33afc53480883fab28e6e9705117f90cadf2b7193439b5cd9adf15ed25be2906c420ad0fde43700becd3becd537bb79901fb3f180feb04cbce4dfa320" + } + }, + // Maximum height with patterned hashes, signed. + "e5f25e8d384576e0eb1c2ad8d41b7306cb35adbd3e608b1f1321d7c2b6c752e5": { + "raw": "ffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100ffffff7f00112233445566778899aabbccddeeff00112233445566778899aabbccddeeffa92ce651433643b040784af6c826381df546d8641e3960b0ba15880175f25e62de17b63082260de738a0ee08bd07a43307d8f334ece32da05066194e89568abc577779ae5e9a32d774e7e079a7dec5c5e63020a61d6498ce1fd8bf2451233228", + "details": { + "pro_tx_hash": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", + "requested_height": 2147483647, + "quorum_hash": "ffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100", + "sig": "a92ce651433643b040784af6c826381df546d8641e3960b0ba15880175f25e62de17b63082260de738a0ee08bd07a43307d8f334ece32da05066194e89568abc577779ae5e9a32d774e7e079a7dec5c5e63020a61d6498ce1fd8bf2451233228" + } + }, + // Negative height with hashes two and one, signed. + "7a68ffc8f7b166563e1e87b697be0b6d8882e6d6553b6ab77df0fa04af14cd8d": { + "raw": "0200000000000000000000000000000000000000000000000000000000000000ffffffff0100000000000000000000000000000000000000000000000000000000000000813058aef9d01842697c37e671fd29af1cd250558e8637c610713798037e3e7135ce0ae33afc53480883fab28e6e9705117f90cadf2b7193439b5cd9adf15ed25be2906c420ad0fde43700becd3becd537bb79901fb3f180feb04cbce4dfa320", + "details": { + "pro_tx_hash": "0000000000000000000000000000000000000000000000000000000000000002", + "requested_height": -1, + "quorum_hash": "0000000000000000000000000000000000000000000000000000000000000001", + "sig": "813058aef9d01842697c37e671fd29af1cd250558e8637c610713798037e3e7135ce0ae33afc53480883fab28e6e9705117f90cadf2b7193439b5cd9adf15ed25be2906c420ad0fde43700becd3becd537bb79901fb3f180feb04cbce4dfa320" + } + }, + }, +} diff --git a/pkgs/p2p_core/corpus/spork.json5 b/pkgs/p2p_core/corpus/spork.json5 new file mode 100644 index 00000000..5669dc4f --- /dev/null +++ b/pkgs/p2p_core/corpus/spork.json5 @@ -0,0 +1,95 @@ +// +// Spork vectors. Generated with the reference implementation +// (dashpay/dash v24.0.0, 938fba5028). +// +// Each section comes with the program used to generate it. +// +// To run a program, paste it at the end of the `net_tests` suite in +// `src/test/net_tests.cpp` at the specified revision, include `key.h` and +// `spork.h` there, build it with `make -C src test/test_dash` and run +// `./src/test/test_dash --run_test=net_tests/`. The output should match +// the section. +// + +{ +/* +BOOST_AUTO_TEST_CASE(spork_payloads) +{ + std::printf(" \"spork\": {\n"); + const auto print = [](const std::string& label, const CSerializedNetMsg& message) { + DataStream fields{message.data}; + int32_t spork_id; + int64_t value, time_signed; + std::vector sig; + fields >> spork_id >> value >> time_signed >> sig; + std::printf(" \"%s\": {\n \"raw\": \"%s\",\n \"details\": {\n \"spork_id\": %d,\n" + " \"value\": %lld,\n \"time_signed\": %lld,\n \"sig\": \"%s\"\n" + " }\n },\n", + label.c_str(), HexStr(message.data).c_str(), spork_id, (long long)value, (long long)time_signed, + HexStr(sig).c_str()); + }; + CKey key; + const std::vector secret(32, 0x04); + key.Set(secret.begin(), secret.end(), true); + struct Case { + std::string label; + SporkId id; + SporkValue value; + int64_t time_signed; + bool sign; + }; + for (const auto& [label, id, value, time_signed, sign] : std::vector{ + {"unsigned", SPORK_2_INSTANTSEND_ENABLED, 0, 0, false}, + {"signed", SPORK_19_CHAINLOCKS_ENABLED, 4070908800, 1700000000, true}, + {"signed_wide", SPORK_INVALID, -2, 5000000000, true}, + {"negative_time", SPORK_23_QUORUM_POSE, 1, -1700000000, true}}) { + CSporkMessage spork{id, value, NodeClock::time_point{std::chrono::seconds{time_signed}}}; + if (sign) BOOST_REQUIRE(spork.Sign(key)); + print(label, CNetMsgMaker{PROTOCOL_VERSION}.Make(NetMsgType::SPORK, spork)); + } + std::printf(" },\n"); +} +*/ + "spork": { + // Unsigned, with an empty signature. + "unsigned": { + "raw": "112700000000000000000000000000000000000000", + "details": { + "spork_id": 10001, + "value": 0, + "time_signed": 0, + "sig": "" + } + }, + // Signed, with a value and timestamp in 32 bits. + "signed": { + "raw": "222700008023a5f20000000000f1536500000000411f7298557e2bbc600820981975fdd0d90aa05f48cf062ab6cf0c1377aa4e3a1c552e3a05fb1a8912816b68295a1211ae3a2b0c386279c38d7231bf3e40a6d891a4", + "details": { + "spork_id": 10018, + "value": 4070908800, + "time_signed": 1700000000, + "sig": "1f7298557e2bbc600820981975fdd0d90aa05f48cf062ab6cf0c1377aa4e3a1c552e3a05fb1a8912816b68295a1211ae3a2b0c386279c38d7231bf3e40a6d891a4" + } + }, + // Signed, with a negative id and value and a timestamp past 32 bits. + "signed_wide": { + "raw": "fffffffffeffffffffffffff00f2052a010000004120123b4bc6faa7357d402b25df5f8567e16c53fb9ee989c8685039b05c016e7e5e2fd0b58f0822138e18018ef25b457b11bb5d047de2f6e498f85420a2aa830fa2", + "details": { + "spork_id": -1, + "value": -2, + "time_signed": 5000000000, + "sig": "20123b4bc6faa7357d402b25df5f8567e16c53fb9ee989c8685039b05c016e7e5e2fd0b58f0822138e18018ef25b457b11bb5d047de2f6e498f85420a2aa830fa2" + } + }, + // Signed, with a negative timestamp. + "negative_time": { + "raw": "262700000100000000000000000fac9affffffff411f23f849572d5345ddc4e04e92ba6cb74c8c5039ec64c2690204d9417fce500510227aac53f13c5815e94b654c3aded290dd458caf8ff7808767efdc07ae7a86c9", + "details": { + "spork_id": 10022, + "value": 1, + "time_signed": -1700000000, + "sig": "1f23f849572d5345ddc4e04e92ba6cb74c8c5039ec64c2690204d9417fce500510227aac53f13c5815e94b654c3aded290dd458caf8ff7808767efdc07ae7a86c9" + } + }, + }, +} diff --git a/pkgs/p2p_core/src/msg/masternode.rs b/pkgs/p2p_core/src/msg/masternode.rs new file mode 100644 index 00000000..c12bafec --- /dev/null +++ b/pkgs/p2p_core/src/msg/masternode.rs @@ -0,0 +1,107 @@ +// +// Copyright (c) 2026-present, The Dash Core developers +// SPDX-License-Identifier: MIT +// See the accompanying file LICENSE or https://opensource.org/license/MIT +// + +//! Masternode messages `mnauth` and `platformban`. + +use crate::codec::{codec_p2p, MAX_P2P_PAYLOAD_SIZE}; + +use dash_num::Hash256; +use dash_pkc::bls::BlsSigBytes; +use dash_primitives::{codec_base, hash_impl, QuorumHash, TxHash}; +use dash_types::type_id::TypeId; +use dash_types::{impl_new, Hashable}; +use getset::CopyGetters; + +codec_p2p! { +/// Masternode authentication. +#[derive(Clone, Copy, CopyGetters, Debug, Eq, Hash, PartialEq, TypeId)] +#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))] +pub struct MnAuth { + /// Registration transaction hash of the masternode. + #[getset(get_copy = "pub")] + pro_reg_tx_hash: TxHash, + /// BLS signature. + #[getset(get_copy = "pub")] + sig: BlsSigBytes, +} +} + +impl_new! { +/// A `platformban` message. +#[derive(Clone, Copy, CopyGetters, Debug, Eq, Hash, PartialEq, TypeId)] +#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))] +pub struct PlatformBan { + /// Registration transaction hash of the masternode. + #[getset(get_copy = "pub")] + pro_tx_hash: TxHash, + /// Height at which the ban was requested. + #[getset(get_copy = "pub")] + requested_height: i32, + /// Hash of the quorum that signed the ban. + #[getset(get_copy = "pub")] + quorum_hash: QuorumHash, + /// BLS signature. + #[getset(get_copy = "pub")] + sig: BlsSigBytes, +} +} + +codec_base!(PlatformBan, MAX_P2P_PAYLOAD_SIZE, { + pro_tx_hash, + requested_height, + quorum_hash, + sig +}); + +codec_base! { + #[derive(TypeId)] + struct PlatformBanHash { + pro_tx_hash: TxHash, + requested_height: i32, + quorum_hash: QuorumHash, + } +} +hash_impl!(PlatformBanHash); + +impl Hashable for PlatformBan { + type Hash = Hash256; + + fn hash(&self) -> Self::Hash { + PlatformBanHash::new(self.pro_tx_hash, self.requested_height, self.quorum_hash).hash() + } +} + +#[cfg(all(test, feature = "serde"))] +mod tests { + use super::*; + use crate::{CommandString, P2pMsg}; + + use dash_dev::{assert_serde_rt, check_wire, Corpus}; + use rstest::rstest; + + #[rstest] + fn corpus_mnauth() { + let corpus = Corpus::open(env!("CARGO_MANIFEST_DIR"), "masternode"); + let items = corpus.entries::("mnauth", |raw, details, label| { + check_wire(raw, details, label); + let decoded = P2pMsg::decode_payload(&CommandString::MNAUTH, raw); + assert_eq!(decoded, Ok(P2pMsg::MnAuth(*details)), "{label}: dispatch"); + }); + assert_serde_rt("mnauth", &items); + } + + #[rstest] + fn corpus_platformban() { + let corpus = Corpus::open(env!("CARGO_MANIFEST_DIR"), "masternode"); + let items = corpus.entries::("platformban", |raw, details, label| { + check_wire(raw, details, label); + let decoded = P2pMsg::decode_payload(&CommandString::PLATFORMBAN, raw); + assert_eq!(decoded, Ok(P2pMsg::PlatformBan(*details)), "{label}: dispatch"); + assert_eq!(Hash256::from_hex(label), Ok(details.hash()), "{label}: hash"); + }); + assert_serde_rt("platformban", &items); + } +} diff --git a/pkgs/p2p_core/src/msg/mod.rs b/pkgs/p2p_core/src/msg/mod.rs index 0388bc9d..33e50150 100644 --- a/pkgs/p2p_core/src/msg/mod.rs +++ b/pkgs/p2p_core/src/msg/mod.rs @@ -17,9 +17,11 @@ mod headers; mod headers2; mod instantsend; mod inv; +mod masternode; mod mn_list; mod ping; mod quorum_sigs; +mod spork; mod tx_reconcile; mod version; @@ -43,9 +45,11 @@ pub use headers::{GetHeaders, Headers}; pub use headers2::{CompressedHeader, GetHeaders2, Headers2}; pub use instantsend::IsdLock; pub use inv::{GetData, Inv, InvType, Inventory, NotFound}; +pub use masternode::{MnAuth, PlatformBan}; pub use mn_list::{DeletedQuorum, GetMnListDiff, MnListDiff, MnListDiffPayload, QuorumClSig, SimplifiedMnListEntry}; pub use ping::{Ping, Pong}; pub use quorum_sigs::QSendRecSigs; +pub use spork::Spork; pub use tx_reconcile::SendTxRcncl; pub use version::{ServiceFlags, UserAgent, UserAgentTooLong, Version, VersionAddr}; @@ -119,6 +123,12 @@ define_p2p! { ClSig(ClSig) => CLSIG "clsig" @ 160, /// InstantSend deterministic lock. IsdLock(IsdLock) => ISDLOCK "isdlock" @ 161, + /// Spork value. + Spork(Spork) => SPORK "spork" @ 128, + /// Masternode authentication. + MnAuth(MnAuth) => MNAUTH "mnauth" @ 162, + /// DIP-0031: platform ban. + PlatformBan(PlatformBan) => PLATFORMBAN "platformban" @ 168, } parsed_empty { @@ -147,8 +157,6 @@ define_p2p! { GetBlockTxn => GETBLOCKTXN "getblocktxn" @ 10, /// Transaction. Tx => TX "tx" @ 21, - /// Spork broadcast/request. - Spork => SPORK "spork" @ 128, /// CoinJoin: accept denomination. Dsa => DSA "dsa" @ 131, /// CoinJoin: submit inputs. @@ -191,14 +199,10 @@ define_p2p! { QGetData => QGETDATA "qgetdata" @ 158, /// LLMQ: quorum data. QData => QDATA "qdata" @ 159, - /// Masternode authentication. - MnAuth => MNAUTH "mnauth" @ 162, /// Request quorum rotation info. GetQrInfo => GETQRINFO "getqrinfo" @ 166, /// Quorum rotation info. QrInfo => QRINFO "qrinfo" @ 167, - /// DIP-0031: platform ban. - PlatformBan => PLATFORMBAN "platformban" @ 168, } stub_empty { @@ -253,6 +257,9 @@ mod tests { /// The txid, cycle hash and signature that follow the inputs of an `isdlock`. const ISDLOCK_TAIL: [u8; 160] = [0; 160]; + /// The id, value and signing time before the signature of a `spork`. + const SPORK_HEAD: [u8; 20] = [0; 20]; + /// `prefix`, a count of `count`, that many `entry` and then `suffix`. fn payload(prefix: &[u8], count: usize, entry: &[u8], suffix: &[u8]) -> Vec { let mut size = Vec::new(); @@ -292,6 +299,7 @@ mod tests { #[case::addr(|| check_count::(&[], &ADDR, &[], 1_000))] #[case::addrv2(|| check_count::(&[], &ADDRV2, &[], 1_000))] #[case::isdlock(|| check_count::(&[1], &OUTPOINT, &ISDLOCK_TAIL, IsdLock::MAX_INPUT_COUNT))] + #[case::spork(|| check_count::(&SPORK_HEAD, &[0], &[], Spork::MAX_SIG_SIZE))] fn counts_stop_at_the_limit(#[case] check: fn()) { check(); } @@ -306,6 +314,7 @@ mod tests { #[case::addr(CommandString::ADDR, &[], 1_000)] #[case::addrv2(CommandString::ADDRV2, &[], 1_000)] #[case::isdlock(CommandString::ISDLOCK, &[1], IsdLock::MAX_INPUT_COUNT)] + #[case::spork(CommandString::SPORK, &SPORK_HEAD, Spork::MAX_SIG_SIZE)] fn dispatch_keeps_the_limit(#[case] cmd: CommandString, #[case] prefix: &[u8], #[case] limit: usize) { let bare = payload(prefix, limit + 1, &[], &[]); assert_eq!( diff --git a/pkgs/p2p_core/src/msg/spork.rs b/pkgs/p2p_core/src/msg/spork.rs new file mode 100644 index 00000000..07707468 --- /dev/null +++ b/pkgs/p2p_core/src/msg/spork.rs @@ -0,0 +1,64 @@ +// +// Copyright (c) 2026-present, The Dash Core developers +// SPDX-License-Identifier: MIT +// See the accompanying file LICENSE or https://opensource.org/license/MIT +// + +//! Spork messages. + +use crate::codec::impl_p2p; +use crate::prelude::*; + +use dash_pkc::ecdsa::ECDSA_REC_SIG_LEN; +use dash_primitives::hash_impl; +use dash_types::type_id::TypeId; +use dash_types::{impl_new, impl_vec}; +use getset::{CopyGetters, Getters}; + +impl_new! { +/// A spork value. +#[derive(Clone, CopyGetters, Debug, Eq, Getters, Hash, PartialEq, TypeId)] +#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))] +pub struct Spork { + #[getset(get_copy = "pub")] + spork_id: i32, + #[getset(get_copy = "pub")] + value: i64, + /// Signing time, in seconds since the epoch. + #[getset(get_copy = "pub")] + time_signed: i64, + /// Recoverable ECDSA signature, empty when unsigned. + #[cfg_attr(feature = "serde", serde(with = "dash_types::serialize::hex"))] + #[getset(get = "pub")] + sig: Vec, +} +} + +impl_vec!(Spork, { spork_id, value, time_signed, sig: Self::MAX_SIG_SIZE }); +impl_p2p!(Spork); +hash_impl!(Spork); + +impl Spork { + /// The longest signature a `spork` carries. + pub const MAX_SIG_SIZE: usize = ECDSA_REC_SIG_LEN; +} + +#[cfg(all(test, feature = "serde"))] +mod tests { + use super::*; + use crate::{CommandString, P2pMsg}; + + use dash_dev::{assert_serde_rt, check_wire, Corpus}; + use rstest::rstest; + + #[rstest] + fn corpus_spork() { + let corpus = Corpus::open(env!("CARGO_MANIFEST_DIR"), "spork"); + let items = corpus.entries::("spork", |raw, details, label| { + check_wire(raw, details, label); + let decoded = P2pMsg::decode_payload(&CommandString::SPORK, raw); + assert_eq!(decoded, Ok(P2pMsg::Spork(details.clone())), "{label}: dispatch"); + }); + assert_serde_rt("spork", &items); + } +}