|
| 1 | +// Copyright (c) 2026 The Dash Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <bls/bls.h> |
| 6 | +#include <evo/deterministicmns.h> |
| 7 | +#include <evo/netinfo.h> |
| 8 | +#include <evo/simplifiedmns.h> |
| 9 | +#include <evo/smldiff.h> |
| 10 | +#include <llmq/commitment.h> |
| 11 | +#include <script/script.h> |
| 12 | +#include <script/standard.h> |
| 13 | +#include <streams.h> |
| 14 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 15 | +#include <test/fuzz/fuzz.h> |
| 16 | +#include <test/fuzz/util.h> |
| 17 | +#include <test/fuzz/util_dash.h> |
| 18 | +#include <test/util/setup_common.h> |
| 19 | +#include <tinyformat.h> |
| 20 | +#include <version.h> |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <array> |
| 24 | +#include <cstddef> |
| 25 | +#include <cstdint> |
| 26 | +#include <stdexcept> |
| 27 | +#include <string> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +const TestingSetup* g_setup; |
| 33 | + |
| 34 | +std::vector<std::byte> SerializeMerkleTree(const CPartialMerkleTree& tree) |
| 35 | +{ |
| 36 | + CDataStream ds(SER_NETWORK, PROTOCOL_VERSION); |
| 37 | + ds << tree; |
| 38 | + return {ds.begin(), ds.end()}; |
| 39 | +} |
| 40 | + |
| 41 | +bool MutableTxEqual(const CMutableTransaction& lhs, const CMutableTransaction& rhs) |
| 42 | +{ |
| 43 | + return lhs.vin == rhs.vin && |
| 44 | + lhs.vout == rhs.vout && |
| 45 | + lhs.nVersion == rhs.nVersion && |
| 46 | + lhs.nType == rhs.nType && |
| 47 | + lhs.nLockTime == rhs.nLockTime && |
| 48 | + lhs.vExtraPayload == rhs.vExtraPayload; |
| 49 | +} |
| 50 | + |
| 51 | +} // namespace |
| 52 | + |
| 53 | +void initialize_simplified_mn_list_diff() |
| 54 | +{ |
| 55 | + static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(CBaseChainParams::REGTEST); |
| 56 | + g_setup = testing_setup.get(); |
| 57 | +} |
| 58 | + |
| 59 | +FUZZ_TARGET(simplified_mn_list_diff, .init = initialize_simplified_mn_list_diff) |
| 60 | +{ |
| 61 | + FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
| 62 | + |
| 63 | + const int source_height = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 10000); |
| 64 | + const uint256 source_hash = ConsumeUInt256(fuzzed_data_provider); |
| 65 | + CDeterministicMNList list_from(source_hash, source_height, 0); |
| 66 | + |
| 67 | + uint64_t next_internal_id = 1; |
| 68 | + uint64_t next_unique_tag = 1; |
| 69 | + const size_t initial_mn_count = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 8); |
| 70 | + for (size_t i = 0; i < initial_mn_count; ++i) { |
| 71 | + const MnType mn_type = fuzzed_data_provider.ConsumeBool() ? MnType::Evo : MnType::Regular; |
| 72 | + list_from.AddMN(MakeMasternode(next_internal_id++, next_unique_tag++, source_height, mn_type), /*fBumpTotalCount=*/true); |
| 73 | + } |
| 74 | + |
| 75 | + CDeterministicMNList list_to(list_from); |
| 76 | + const size_t operation_count = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 24); |
| 77 | + for (size_t i = 0; i < operation_count; ++i) { |
| 78 | + const uint8_t op = fuzzed_data_provider.ConsumeIntegralInRange<uint8_t>(0, 2); |
| 79 | + if (op == 0) { |
| 80 | + const MnType mn_type = fuzzed_data_provider.ConsumeBool() ? MnType::Evo : MnType::Regular; |
| 81 | + list_to.AddMN(MakeMasternode(next_internal_id++, next_unique_tag++, source_height, mn_type), /*fBumpTotalCount=*/true); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + const auto hashes = GetProTxHashes(list_to); |
| 86 | + if (hashes.empty()) continue; |
| 87 | + const size_t index = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, hashes.size() - 1); |
| 88 | + |
| 89 | + if (op == 1) { |
| 90 | + list_to.RemoveMN(hashes[index]); |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + const auto old_mn = list_to.GetMN(hashes[index]); |
| 95 | + if (!old_mn) continue; |
| 96 | + auto new_state = std::make_shared<CDeterministicMNState>(*old_mn->pdmnState); |
| 97 | + switch (fuzzed_data_provider.ConsumeIntegralInRange<uint8_t>(0, 9)) { |
| 98 | + case 0: |
| 99 | + new_state->confirmedHash = HashFromTag(next_unique_tag++ ^ 0x33333333ULL); |
| 100 | + break; |
| 101 | + case 1: { |
| 102 | + CBLSSecretKey sk; |
| 103 | + sk.MakeNewKey(); |
| 104 | + new_state->nVersion = ProTxVersion::BasicBLS; |
| 105 | + new_state->pubKeyOperator.Set(sk.GetPublicKey(), /*specificLegacyScheme=*/false); |
| 106 | + break; |
| 107 | + } |
| 108 | + case 2: |
| 109 | + new_state->keyIDVoting = CKeyID(Uint160FromTag(next_unique_tag++ ^ 0x06060606ULL)); |
| 110 | + break; |
| 111 | + case 3: { |
| 112 | + auto net_info = NetInfoInterface::MakeNetInfo(new_state->nVersion); |
| 113 | + if (net_info && net_info->AddEntry(NetInfoPurpose::CORE_P2P, AddressFromTag(next_unique_tag++)) == NetInfoStatus::Success) { |
| 114 | + new_state->netInfo = std::move(net_info); |
| 115 | + } |
| 116 | + break; |
| 117 | + } |
| 118 | + case 4: |
| 119 | + new_state->scriptPayout = CScript() << OP_DUP << OP_HASH160 << ToByteVector(Uint160FromTag(next_unique_tag++)) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 120 | + break; |
| 121 | + case 5: |
| 122 | + new_state->scriptOperatorPayout = CScript() << OP_DUP << OP_HASH160 << ToByteVector(Uint160FromTag(next_unique_tag++ ^ 0x08080808ULL)) << OP_EQUALVERIFY << OP_CHECKSIG; |
| 123 | + break; |
| 124 | + case 6: |
| 125 | + new_state->BanIfNotBanned(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 100000)); |
| 126 | + break; |
| 127 | + case 7: |
| 128 | + new_state->platformNodeID = Uint160FromTag(next_unique_tag++ ^ 0x12121212ULL); |
| 129 | + break; |
| 130 | + case 8: |
| 131 | + new_state->platformHTTPPort = fuzzed_data_provider.ConsumeIntegral<uint16_t>(); |
| 132 | + break; |
| 133 | + case 9: |
| 134 | + new_state->nRegisteredHeight = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 10000); |
| 135 | + break; |
| 136 | + } |
| 137 | + list_to.UpdateMN(*old_mn, new_state); |
| 138 | + } |
| 139 | + |
| 140 | + list_to.SetBlockHash(ConsumeUInt256(fuzzed_data_provider)); |
| 141 | + list_to.SetHeight(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 100000)); |
| 142 | + |
| 143 | + CSimplifiedMNListDiff diff; |
| 144 | + diff.baseBlockHash = list_from.GetBlockHash(); |
| 145 | + diff.blockHash = list_to.GetBlockHash(); |
| 146 | + diff.cbTx = CMutableTransaction{}; |
| 147 | + diff.cbTxMerkleTree = CPartialMerkleTree{}; |
| 148 | + |
| 149 | + list_to.ForEachMN(/*onlyValid=*/false, [&](const auto& to_mn) { |
| 150 | + const auto from_mn = list_from.GetMN(to_mn.proTxHash); |
| 151 | + if (!from_mn || to_mn.to_sml_entry() != from_mn->to_sml_entry()) { |
| 152 | + diff.mnList.emplace_back(to_mn.to_sml_entry()); |
| 153 | + } |
| 154 | + }); |
| 155 | + list_from.ForEachMN(/*onlyValid=*/false, [&](const auto& from_mn) { |
| 156 | + if (!list_to.GetMN(from_mn.proTxHash)) { |
| 157 | + diff.deletedMNs.emplace_back(from_mn.proTxHash); |
| 158 | + } |
| 159 | + }); |
| 160 | + |
| 161 | + CDataStream ds(SER_NETWORK, PROTOCOL_VERSION); |
| 162 | + ds << diff; |
| 163 | + CSimplifiedMNListDiff roundtrip; |
| 164 | + ds >> roundtrip; |
| 165 | + |
| 166 | + if (roundtrip.baseBlockHash != diff.baseBlockHash || roundtrip.blockHash != diff.blockHash || |
| 167 | + !MutableTxEqual(roundtrip.cbTx, diff.cbTx) || |
| 168 | + SerializeMerkleTree(roundtrip.cbTxMerkleTree) != SerializeMerkleTree(diff.cbTxMerkleTree) || |
| 169 | + roundtrip.deletedMNs != diff.deletedMNs || roundtrip.mnList.size() != diff.mnList.size() || |
| 170 | + roundtrip.nVersion != diff.nVersion || roundtrip.deletedQuorums != diff.deletedQuorums || |
| 171 | + roundtrip.newQuorums.size() != diff.newQuorums.size() || roundtrip.quorumsCLSigs != diff.quorumsCLSigs) { |
| 172 | + throw std::runtime_error("simplified_mn_list_diff: serialized fields mismatch"); |
| 173 | + } |
| 174 | + for (size_t i = 0; i < diff.mnList.size(); ++i) { |
| 175 | + if (roundtrip.mnList[i] != diff.mnList[i]) { |
| 176 | + throw std::runtime_error("simplified_mn_list_diff: mnList mismatch"); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + CDataStream ds_random(fuzzed_data_provider.ConsumeRemainingBytes<uint8_t>(), SER_NETWORK, PROTOCOL_VERSION); |
| 181 | + try { |
| 182 | + CSimplifiedMNListDiff random_diff; |
| 183 | + ds_random >> random_diff; |
| 184 | + } catch (const std::exception&) { |
| 185 | + } |
| 186 | +} |
0 commit comments