Skip to content

Commit f14569f

Browse files
committed
[Zerocoin] remove CTransaction::IsZerocoinSpend and CTransaction::IsZerocoinMint
- uniform IsZerocoinSpend/IsZerocoinMint CScript functions - refactor CTxIn::IsZerocoinSpend / CTxOut::IsZerocoinMint encapsulating CScript methods - add CTransaction methods: HasZerocoinMintOutputs / HasZerocoinSpendInputs - refactor CTransaction::ContainsZerocoins from previous 'HasZerocoin' functions - fix implementation of CTransaction::GetZerocoinMinted (from CTxOut) - remove extra checks for empty scripts (as those are done in CScript functions)
1 parent ec7993e commit f14569f

24 files changed

+209
-188
lines changed

src/blocksignature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool CheckBlockSignature(const CBlock& block)
6767
* UTXO: The public key that signs must match the public key associated with the first utxo of the coinstake tx.
6868
*/
6969
CPubKey pubkey;
70-
bool fzPIVStake = block.vtx[1].IsZerocoinSpend();
70+
bool fzPIVStake = block.vtx[1].vin[0].IsZerocoinSpend();
7171
if (fzPIVStake) {
7272
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(block.vtx[1].vin[0]);
7373
pubkey = spend.getPubKey();

src/bloom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
219219
opcodetype opcode;
220220
if (!txin.scriptSig.GetOp(pc, opcode, data))
221221
break;
222-
if (txin.scriptSig.IsZerocoinSpend()){
222+
if (txin.IsZerocoinSpend()) {
223223
CDataStream s(vector<unsigned char>(txin.scriptSig.begin() + 44, txin.scriptSig.end()),
224224
SER_NETWORK, PROTOCOL_VERSION);
225225

src/coins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
224224
return 0;
225225

226226
//todo are there any security precautions to take here?
227-
if (tx.IsZerocoinSpend())
227+
if (tx.HasZerocoinSpendInputs())
228228
return tx.GetZerocoinSpent();
229229

230230
CAmount nResult = 0;
@@ -236,7 +236,7 @@ CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
236236

237237
bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
238238
{
239-
if (!tx.IsCoinBase() && !tx.IsZerocoinSpend()) {
239+
if (!tx.IsCoinBase() && !tx.HasZerocoinSpendInputs()) {
240240
for (unsigned int i = 0; i < tx.vin.size(); i++) {
241241
const COutPoint& prevout = tx.vin[i].prevout;
242242
const CCoins* coins = AccessCoins(prevout.hash);

src/coins.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class CCoins
272272
//! check whether a particular output is still available
273273
bool IsAvailable(unsigned int nPos) const
274274
{
275-
return (nPos < vout.size() && !vout[nPos].IsNull() && !vout[nPos].scriptPubKey.IsZerocoinMint());
275+
return (nPos < vout.size() && !vout[nPos].IsNull() && !vout[nPos].IsZerocoinMint());
276276
}
277277

278278
//! check whether the entire CCoins is spent

src/kernel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ bool CheckProofOfStake(const CBlock block, uint256& hashProofOfStake, std::uniqu
361361
const CTxIn& txin = tx.vin[0];
362362

363363
//Construct the stakeinput object
364-
if (tx.IsZerocoinSpend()) {
364+
if (txin.IsZerocoinSpend()) {
365365
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(txin);
366366
if (spend.getSpendType() != libzerocoin::SpendType::STAKE)
367367
return error("%s: spend is using the wrong SpendType (%d)", __func__, (int)spend.getSpendType());

src/main.cpp

Lines changed: 93 additions & 97 deletions
Large diffs are not rendered by default.

src/masternode-payments.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ bool CMasternodeBlockPayees::IsTransactionValid(const CTransaction& txNew)
544544
nMasternode_Drift_Count = mnodeman.size() + Params().MasternodeCountDrift();
545545
}
546546

547-
CAmount requiredMasternodePayment = GetMasternodePayment(nBlockHeight, nReward, nMasternode_Drift_Count, txNew.IsZerocoinSpend());
547+
CAmount requiredMasternodePayment = GetMasternodePayment(nBlockHeight, nReward, nMasternode_Drift_Count, txNew.HasZerocoinSpendInputs());
548548

549549
//require at least 6 signatures
550550
BOOST_FOREACH (CMasternodePayee& payee, vecPayments)

src/miner.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,13 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
219219
CAmount nTotalIn = 0;
220220
bool fMissingInputs = false;
221221
uint256 txid = tx.GetHash();
222+
bool hasZerocoinSpends = tx.HasZerocoinSpendInputs();
223+
if (hasZerocoinSpends)
224+
nTotalIn = tx.GetZerocoinSpent();
225+
222226
for (const CTxIn& txin : tx.vin) {
223227
//zerocoinspend has special vin
224-
if (tx.IsZerocoinSpend()) {
225-
nTotalIn = tx.GetZerocoinSpent();
226-
228+
if (hasZerocoinSpends) {
227229
//Give a high priority to zerocoinspends to get into the next block
228230
//Priority = (age^6+100000)*amount - gives higher priority to zpivs that have been in mempool long
229231
//and higher priority to zpivs that are large in value
@@ -346,7 +348,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
346348
double dPriorityDelta = 0;
347349
CAmount nFeeDelta = 0;
348350
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
349-
if (!tx.IsZerocoinSpend() && fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
351+
if (!tx.HasZerocoinSpendInputs() && fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
350352
continue;
351353

352354
// Prioritise by fee once past the priority size or we run out of high-priority
@@ -362,14 +364,14 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn, CWallet* pwallet,
362364
continue;
363365

364366
// double check that there are no double spent zPIV spends in this block or tx
365-
if (tx.IsZerocoinSpend()) {
367+
if (tx.HasZerocoinSpendInputs()) {
366368
int nHeightTx = 0;
367369
if (IsTransactionInChain(tx.GetHash(), nHeightTx))
368370
continue;
369371

370372
bool fDoubleSerial = false;
371373
for (const CTxIn& txIn : tx.vin) {
372-
if (txIn.scriptSig.IsZerocoinSpend()) {
374+
if (txIn.IsZerocoinSpend()) {
373375
libzerocoin::CoinSpend spend = TxInToZerocoinSpend(txIn);
374376
bool fUseV1Params = libzerocoin::ExtractVersionFromSerial(spend.getCoinSerialNumber()) < libzerocoin::PrivateCoin::PUBKEY_VERSION;
375377
if (!spend.HasValidSerial(Params().Zerocoin_Params(fUseV1Params)))

src/primitives/block.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,5 @@ void CBlock::print() const
143143

144144
bool CBlock::IsZerocoinStake() const
145145
{
146-
return IsProofOfStake() && vtx[1].IsZerocoinSpend();
147-
}
146+
return IsProofOfStake() && vtx[1].HasZerocoinSpendInputs();
147+
}

src/primitives/transaction.cpp

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,18 @@ CTxIn::CTxIn(const libzerocoin::CoinSpend& spend, libzerocoin::CoinDenomination
6060
nSequence = denom;
6161
}
6262

63+
bool CTxIn::IsZerocoinSpend() const
64+
{
65+
return prevout.hash == 0 && scriptSig.IsZerocoinSpend();
66+
}
67+
6368
std::string CTxIn::ToString() const
6469
{
6570
std::string str;
6671
str += "CTxIn(";
6772
str += prevout.ToString();
6873
if (prevout.IsNull())
69-
if(scriptSig.IsZerocoinSpend())
74+
if(IsZerocoinSpend())
7075
str += strprintf(", zerocoinspend %s...", HexStr(scriptSig).substr(0, 25));
7176
else
7277
str += strprintf(", coinbase %s", HexStr(scriptSig));
@@ -98,6 +103,19 @@ uint256 CTxOut::GetHash() const
98103
return SerializeHash(*this);
99104
}
100105

106+
bool CTxOut::IsZerocoinMint() const
107+
{
108+
return scriptPubKey.IsZerocoinMint();
109+
}
110+
111+
CAmount CTxOut::GetZerocoinMinted() const
112+
{
113+
if (!IsZerocoinMint())
114+
return CAmount(0);
115+
116+
return nValue;
117+
}
118+
101119
std::string CTxOut::ToString() const
102120
{
103121
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30));
@@ -146,17 +164,35 @@ CTransaction& CTransaction::operator=(const CTransaction &tx) {
146164
return *this;
147165
}
148166

167+
bool CTransaction::HasZerocoinSpendInputs() const
168+
{
169+
for (const CTxIn& txin: vin) {
170+
if (txin.IsZerocoinSpend())
171+
return true;
172+
}
173+
return false;
174+
}
175+
176+
bool CTransaction::HasZerocoinMintOutputs() const
177+
{
178+
for(const CTxOut& txout : vout) {
179+
if (txout.IsZerocoinMint())
180+
return true;
181+
}
182+
return false;
183+
}
184+
149185
bool CTransaction::IsCoinStake() const
150186
{
151187
if (vin.empty())
152188
return false;
153189

154190
// ppcoin: the coin stake transaction is marked with the first output empty
155-
bool fAllowNull = vin[0].scriptSig.IsZerocoinSpend();
191+
bool fAllowNull = vin[0].IsZerocoinSpend();
156192
if (vin[0].prevout.IsNull() && !fAllowNull)
157193
return false;
158194

159-
return (vin.size() > 0 && vout.size() >= 2 && vout[0].IsEmpty());
195+
return (vout.size() >= 2 && vout[0].IsEmpty());
160196
}
161197

162198
CAmount CTransaction::GetValueOut() const
@@ -178,14 +214,12 @@ CAmount CTransaction::GetValueOut() const
178214

179215
CAmount CTransaction::GetZerocoinMinted() const
180216
{
217+
CAmount nValueOut = 0;
181218
for (const CTxOut& txOut : vout) {
182-
if(!txOut.scriptPubKey.IsZerocoinMint())
183-
continue;
184-
185-
return txOut.nValue;
219+
nValueOut += txOut.GetZerocoinMinted();
186220
}
187221

188-
return CAmount(0);
222+
return nValueOut;
189223
}
190224

191225
bool CTransaction::UsesUTXO(const COutPoint out)
@@ -209,12 +243,9 @@ std::list<COutPoint> CTransaction::GetOutPoints() const
209243

210244
CAmount CTransaction::GetZerocoinSpent() const
211245
{
212-
if(!IsZerocoinSpend())
213-
return 0;
214-
215246
CAmount nValueOut = 0;
216247
for (const CTxIn& txin : vin) {
217-
if(!txin.scriptSig.IsZerocoinSpend())
248+
if(!txin.IsZerocoinSpend())
218249
continue;
219250

220251
nValueOut += txin.nSequence * COIN;
@@ -227,7 +258,7 @@ int CTransaction::GetZerocoinMintCount() const
227258
{
228259
int nCount = 0;
229260
for (const CTxOut& out : vout) {
230-
if (out.scriptPubKey.IsZerocoinMint())
261+
if (out.IsZerocoinMint())
231262
nCount++;
232263
}
233264
return nCount;

0 commit comments

Comments
 (0)