|
| 1 | +// Copyright (c) 2023 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 <evo/assetlocktx.h> |
| 6 | +#include <evo/specialtx.h> |
| 7 | +#include <evo/creditpool.h> |
| 8 | + |
| 9 | +#include <consensus/params.h> |
| 10 | + |
| 11 | +#include <chainparams.h> |
| 12 | +#include <logging.h> |
| 13 | +#include <validation.h> |
| 14 | + |
| 15 | +#include <llmq/commitment.h> |
| 16 | +#include <llmq/signing.h> |
| 17 | +#include <llmq/utils.h> |
| 18 | +#include <llmq/quorums.h> |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | + |
| 22 | +/** |
| 23 | + * Common code for Asset Lock and Asset Unlock |
| 24 | + */ |
| 25 | +bool CheckAssetLockUnlockTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCreditPool& creditPool, TxValidationState& state) |
| 26 | +{ |
| 27 | + switch (tx.nType) { |
| 28 | + case TRANSACTION_ASSET_LOCK: |
| 29 | + return CheckAssetLockTx(tx, state); |
| 30 | + case TRANSACTION_ASSET_UNLOCK: |
| 31 | + return CheckAssetUnlockTx(tx, pindexPrev, creditPool, state); |
| 32 | + default: |
| 33 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-not-asset-locks-at-all"); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Asset Lock Transaction |
| 39 | + */ |
| 40 | +bool CheckAssetLockTx(const CTransaction& tx, TxValidationState& state) |
| 41 | +{ |
| 42 | + if (tx.nType != TRANSACTION_ASSET_LOCK) { |
| 43 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-type"); |
| 44 | + } |
| 45 | + |
| 46 | + CAmount returnAmount{0}; |
| 47 | + for (const CTxOut& txout : tx.vout) { |
| 48 | + const CScript& script = txout.scriptPubKey; |
| 49 | + if (script.empty() || script[0] != OP_RETURN) continue; |
| 50 | + |
| 51 | + if (script.size() != 2 || script[1] != 0) return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-non-empty-return"); |
| 52 | + |
| 53 | + if (txout.nValue == 0 || !MoneyRange(txout.nValue)) return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-opreturn-outofrange"); |
| 54 | + |
| 55 | + // Should be only one OP_RETURN |
| 56 | + if (returnAmount > 0) return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-multiple-return"); |
| 57 | + returnAmount = txout.nValue; |
| 58 | + } |
| 59 | + |
| 60 | + if (returnAmount == 0) return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-no-return"); |
| 61 | + |
| 62 | + CAssetLockPayload assetLockTx; |
| 63 | + if (!GetTxPayload(tx, assetLockTx)) { |
| 64 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-payload"); |
| 65 | + } |
| 66 | + |
| 67 | + if (assetLockTx.getVersion() == 0 || assetLockTx.getVersion() > CAssetLockPayload::CURRENT_VERSION) { |
| 68 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-version"); |
| 69 | + } |
| 70 | + |
| 71 | + if (assetLockTx.getCreditOutputs().empty()) { |
| 72 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-emptycreditoutputs"); |
| 73 | + } |
| 74 | + |
| 75 | + CAmount creditOutputsAmount = 0; |
| 76 | + for (const CTxOut& out : assetLockTx.getCreditOutputs()) { |
| 77 | + if (out.nValue == 0 || !MoneyRange(out.nValue) || !MoneyRange(creditOutputsAmount + out.nValue)) { |
| 78 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-credit-outofrange"); |
| 79 | + } |
| 80 | + |
| 81 | + creditOutputsAmount += out.nValue; |
| 82 | + if (!out.scriptPubKey.IsPayToPublicKeyHash()) { |
| 83 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-pubKeyHash"); |
| 84 | + } |
| 85 | + } |
| 86 | + if (creditOutputsAmount != returnAmount) { |
| 87 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-creditamount"); |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +std::string CAssetLockPayload::ToString() const |
| 94 | +{ |
| 95 | + std::string outputs{"["}; |
| 96 | + for (const CTxOut& tx: creditOutputs) { |
| 97 | + outputs.append(tx.ToString()); |
| 98 | + outputs.append(","); |
| 99 | + } |
| 100 | + outputs.back() = ']'; |
| 101 | + return strprintf("CAssetLockPayload(nVersion=%d,creditOutputs=%s)", nVersion, outputs.c_str()); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Asset Unlock Transaction (withdrawals) |
| 106 | + */ |
| 107 | + |
| 108 | +const std::string ASSETUNLOCK_REQUESTID_PREFIX = "plwdtx"; |
| 109 | + |
| 110 | +bool CAssetUnlockPayload::VerifySig(const uint256& msgHash, const CBlockIndex* pindexTip, TxValidationState& state) const |
| 111 | +{ |
| 112 | + // That quourm hash must be active at `requestHeight`, |
| 113 | + // and at the quorumHash must be active in either the current or previous quorum cycle |
| 114 | + // and the sig must validate against that specific quorumHash. |
| 115 | + |
| 116 | + Consensus::LLMQType llmqType = Params().GetConsensus().llmqTypeAssetLocks; |
| 117 | + |
| 118 | + // We check at most 2 quorums |
| 119 | + const auto quorums = llmq::quorumManager->ScanQuorums(llmqType, pindexTip, 2); |
| 120 | + bool isActive = std::any_of(quorums.begin(), quorums.end(), [&](const auto &q) { return q->qc->quorumHash == quorumHash; }); |
| 121 | + |
| 122 | + if (!isActive) { |
| 123 | + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-not-active-quorum"); |
| 124 | + } |
| 125 | + |
| 126 | + if (pindexTip->nHeight < requestedHeight || pindexTip->nHeight >= getHeightToExpiry()) { |
| 127 | + LogPrintf("Asset unlock tx %d with requested height %d could not be accepted on height: %d\n", |
| 128 | + index, requestedHeight, pindexTip->nHeight); |
| 129 | + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-too-late"); |
| 130 | + } |
| 131 | + |
| 132 | + const auto quorum = llmq::quorumManager->GetQuorum(llmqType, quorumHash); |
| 133 | + assert(quorum); |
| 134 | + |
| 135 | + const uint256 requestId = ::SerializeHash(std::make_pair(ASSETUNLOCK_REQUESTID_PREFIX, index)); |
| 136 | + |
| 137 | + const uint256 signHash = llmq::utils::BuildSignHash(llmqType, quorum->qc->quorumHash, requestId, msgHash); |
| 138 | + if (quorumSig.VerifyInsecure(quorum->qc->quorumPublicKey, signHash)) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-not-verified"); |
| 143 | +} |
| 144 | + |
| 145 | +bool CheckAssetUnlockTx(const CTransaction& tx, const CBlockIndex* pindexPrev, const CCreditPool& creditPool, TxValidationState& state) |
| 146 | +{ |
| 147 | + if (tx.nType != TRANSACTION_ASSET_UNLOCK) { |
| 148 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-type"); |
| 149 | + } |
| 150 | + |
| 151 | + if (!tx.vin.empty()) { |
| 152 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-have-input"); |
| 153 | + } |
| 154 | + |
| 155 | + if (tx.vout.size() > CAssetUnlockPayload::MAXIMUM_WITHDRAWALS) { |
| 156 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-too-many-outs"); |
| 157 | + } |
| 158 | + |
| 159 | + CAssetUnlockPayload assetUnlockTx; |
| 160 | + if (!GetTxPayload(tx, assetUnlockTx)) { |
| 161 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-payload"); |
| 162 | + } |
| 163 | + |
| 164 | + if (assetUnlockTx.getVersion() == 0 || assetUnlockTx.getVersion() > CAssetUnlockPayload::CURRENT_VERSION) { |
| 165 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-version"); |
| 166 | + } |
| 167 | + |
| 168 | + if (creditPool.indexes.Contains(assetUnlockTx.getIndex())) { |
| 169 | + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-duplicated-index"); |
| 170 | + } |
| 171 | + |
| 172 | + const CBlockIndex* pindexQuorum = WITH_LOCK(cs_main, return g_chainman.m_blockman.LookupBlockIndex(assetUnlockTx.getQuorumHash())); |
| 173 | + if (!pindexQuorum) { |
| 174 | + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-assetunlock-quorum-hash"); |
| 175 | + } |
| 176 | + |
| 177 | + // Copy transaction except `quorumSig` field to calculate hash |
| 178 | + CMutableTransaction tx_copy(tx); |
| 179 | + const CAssetUnlockPayload payload_copy{assetUnlockTx.getVersion(), assetUnlockTx.getIndex(), assetUnlockTx.getFee(), assetUnlockTx.getRequestedHeight(), assetUnlockTx.getQuorumHash(), CBLSSignature{}}; |
| 180 | + SetTxPayload(tx_copy, payload_copy); |
| 181 | + |
| 182 | + uint256 msgHash = tx_copy.GetHash(); |
| 183 | + |
| 184 | + return assetUnlockTx.VerifySig(msgHash, pindexPrev, state); |
| 185 | +} |
| 186 | + |
| 187 | +bool GetAssetUnlockFee(const CTransaction& tx, CAmount& txfee, TxValidationState& state) |
| 188 | +{ |
| 189 | + CAssetUnlockPayload assetUnlockTx; |
| 190 | + if (!GetTxPayload(tx, assetUnlockTx)) { |
| 191 | + return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-payload"); |
| 192 | + } |
| 193 | + const CAmount txfee_aux = assetUnlockTx.getFee(); |
| 194 | + if (txfee_aux == 0 || !MoneyRange(txfee_aux)) { |
| 195 | + return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-assetunlock-fee-outofrange"); |
| 196 | + } |
| 197 | + txfee = txfee_aux; |
| 198 | + return true; |
| 199 | +} |
| 200 | + |
| 201 | +std::string CAssetUnlockPayload::ToString() const |
| 202 | +{ |
| 203 | + return strprintf("CAssetUnlockPayload(nVersion=%d,index=%d,fee=%d.%08d,requestedHeight=%d,quorumHash=%d,quorumSig=%s", |
| 204 | + nVersion, index, fee / COIN, fee % COIN, requestedHeight, quorumHash.GetHex(), quorumSig.ToString().c_str()); |
| 205 | +} |
0 commit comments