|
18 | 18 | #include "zpiv/zpivmodule.h" |
19 | 19 |
|
20 | 20 |
|
21 | | -bool CheckZerocoinSpend(const CTransaction& tx, bool fVerifySignature, CValidationState& state, bool fFakeSerialAttack) |
| 21 | +bool CheckZerocoinSpend(const CTransactionRef _tx, bool fVerifySignature, CValidationState& state, bool fFakeSerialAttack) |
22 | 22 | { |
| 23 | + const CTransaction& tx = *_tx; |
23 | 24 | //max needed non-mint outputs should be 2 - one for redemption address and a possible 2nd for change |
24 | 25 | if (tx.vout.size() > 2) { |
25 | 26 | int outs = 0; |
@@ -135,6 +136,65 @@ bool CheckPublicCoinSpendVersion(int version) { |
135 | 136 | return version == CurrentPublicCoinSpendVersion(); |
136 | 137 | } |
137 | 138 |
|
| 139 | +bool ContextualCheckZerocoinTx(const CTransactionRef& tx, CValidationState& state, const Consensus::Params& consensus, int nHeight) |
| 140 | +{ |
| 141 | + // zerocoin enforced via block time. First block with a zc mint is 863735 |
| 142 | + const bool fZerocoinEnforced = (nHeight >= consensus.ZC_HeightStart); |
| 143 | + const bool fPublicSpendEnforced = consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_ZC_PUBLIC); |
| 144 | + const bool fRejectMintsAndPrivateSpends = !fZerocoinEnforced || fPublicSpendEnforced; |
| 145 | + const bool fRejectPublicSpends = !fPublicSpendEnforced || consensus.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_V5_0); |
| 146 | + |
| 147 | + const bool hasPrivateSpendInputs = !tx->vin.empty() && tx->vin[0].IsZerocoinSpend(); |
| 148 | + const bool hasPublicSpendInputs = !tx->vin.empty() && tx->vin[0].IsZerocoinPublicSpend(); |
| 149 | + const std::string& txId = tx->GetHash().ToString(); |
| 150 | + |
| 151 | + int nSpendCount{0}; |
| 152 | + for (const CTxIn& in : tx->vin) { |
| 153 | + if (in.IsZerocoinSpend()) { |
| 154 | + if (fRejectMintsAndPrivateSpends) |
| 155 | + return state.DoS(100, error("%s: zerocoin spend tx %s not accepted at height %d", |
| 156 | + __func__, txId, nHeight), REJECT_INVALID, "bad-txns-zc-private-spend"); |
| 157 | + if (!hasPrivateSpendInputs) |
| 158 | + return state.DoS(100, error("%s: zerocoin spend tx %s has mixed spend inputs", |
| 159 | + __func__, txId), REJECT_INVALID, "bad-txns-zc-private-spend-mixed-types"); |
| 160 | + if (++nSpendCount > consensus.ZC_MaxSpendsPerTx) |
| 161 | + return state.DoS(100, error("%s: zerocoin spend tx %s has more than %d inputs", |
| 162 | + __func__, txId, consensus.ZC_MaxSpendsPerTx), REJECT_INVALID, "bad-txns-zc-private-spend-max-inputs"); |
| 163 | + |
| 164 | + } else if (in.IsZerocoinPublicSpend()) { |
| 165 | + if (fRejectPublicSpends) |
| 166 | + return state.DoS(100, error("%s: zerocoin public spend tx %s not accepted at height %d", |
| 167 | + __func__, txId, nHeight), REJECT_INVALID, "bad-txns-zc-public-spend"); |
| 168 | + if (!hasPublicSpendInputs) |
| 169 | + return state.DoS(100, error("%s: zerocoin spend tx %s has mixed spend inputs", |
| 170 | + __func__, txId), REJECT_INVALID, "bad-txns-zc-public-spend-mixed-types"); |
| 171 | + if (++nSpendCount > consensus.ZC_MaxPublicSpendsPerTx) |
| 172 | + return state.DoS(100, error("%s: zerocoin spend tx %s has more than %d inputs", |
| 173 | + __func__, txId, consensus.ZC_MaxPublicSpendsPerTx), REJECT_INVALID, "bad-txns-zc-public-spend-max-inputs"); |
| 174 | + |
| 175 | + } else { |
| 176 | + // this is a transparent input |
| 177 | + if (hasPrivateSpendInputs || hasPublicSpendInputs) |
| 178 | + return state.DoS(100, error("%s: zerocoin spend tx %s has mixed spend inputs", |
| 179 | + __func__, txId), REJECT_INVALID, "bad-txns-zc-spend-mixed-types"); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if (hasPrivateSpendInputs || hasPublicSpendInputs) { |
| 184 | + if (!CheckZerocoinSpend(tx, false, state, false)) |
| 185 | + return false; // failure reason logged in validation state |
| 186 | + } |
| 187 | + |
| 188 | + for (const CTxOut& o : tx->vout) { |
| 189 | + if (o.IsZerocoinMint() && fRejectMintsAndPrivateSpends) { |
| 190 | + return state.DoS(100, error("%s: zerocoin mint tx %s not accepted at height %d", |
| 191 | + __func__, txId, nHeight), REJECT_INVALID, "bad-txns-zc-mint"); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return true; |
| 196 | +} |
| 197 | + |
138 | 198 | bool ContextualCheckZerocoinSpend(const CTransaction& tx, const libzerocoin::CoinSpend* spend, int nHeight, const uint256& hashBlock) |
139 | 199 | { |
140 | 200 | if(!ContextualCheckZerocoinSpendNoSerialCheck(tx, spend, nHeight, hashBlock)){ |
|
0 commit comments