PIVX Core  5.6.99
P2P Digital Currency
stakeinput.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2022 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #include "stakeinput.h"
6 
7 #include "chain.h"
8 #include "txdb.h"
9 #include "validation.h"
10 
11 static bool HasStakeMinAgeOrDepth(int nHeight, uint32_t nTime, const CBlockIndex* pindex)
12 {
13  const Consensus::Params& consensus = Params().GetConsensus();
14  if (consensus.NetworkUpgradeActive(nHeight + 1, Consensus::UPGRADE_ZC_PUBLIC) &&
15  !consensus.HasStakeMinAgeOrDepth(nHeight, nTime, pindex->nHeight, pindex->nTime)) {
16  return error("%s : min age violation - height=%d - time=%d, nHeightBlockFrom=%d, nTimeBlockFrom=%d",
17  __func__, nHeight, nTime, pindex->nHeight, pindex->nTime);
18  }
19  return true;
20 }
21 
22 CPivStake* CPivStake::NewPivStake(const CTxIn& txin, int nHeight, uint32_t nTime)
23 {
24  if (txin.IsZerocoinSpend()) {
25  error("%s: unable to initialize CPivStake from zerocoin spend", __func__);
26  return nullptr;
27  }
28 
29  // Look for the stake input in the coins cache first
30  const Coin& coin = pcoinsTip->AccessCoin(txin.prevout);
31  if (!coin.IsSpent()) {
32  const CBlockIndex* pindexFrom = mapBlockIndex.at(chainActive[coin.nHeight]->GetBlockHash());
33  // Check that the stake has the required depth/age
34  if (!HasStakeMinAgeOrDepth(nHeight, nTime, pindexFrom)) {
35  return nullptr;
36  }
37  // All good
38  return new CPivStake(coin.out, txin.prevout, pindexFrom);
39  }
40 
41  // Otherwise find the previous transaction in database
42  uint256 hashBlock;
43  CTransactionRef txPrev;
44  if (!GetTransaction(txin.prevout.hash, txPrev, hashBlock, true)) {
45  error("%s : INFO: read txPrev failed, tx id prev: %s", __func__, txin.prevout.hash.GetHex());
46  return nullptr;
47  }
48  const CBlockIndex* pindexFrom = nullptr;
49  if (mapBlockIndex.count(hashBlock)) {
50  CBlockIndex* pindex = mapBlockIndex.at(hashBlock);
51  if (chainActive.Contains(pindex)) pindexFrom = pindex;
52  }
53  // Check that the input is in the active chain
54  if (!pindexFrom) {
55  error("%s : Failed to find the block index for stake origin", __func__);
56  return nullptr;
57  }
58  // Check that the stake has the required depth/age
59  if (!HasStakeMinAgeOrDepth(nHeight, nTime, pindexFrom)) {
60  return nullptr;
61  }
62  // All good
63  return new CPivStake(txPrev->vout[txin.prevout.n], txin.prevout, pindexFrom);
64 }
65 
67 {
68  out = outputFrom;
69  return true;
70 }
71 
73 {
75 }
76 
78 {
79  return outputFrom.nValue;
80 }
81 
83 {
84  //The unique identifier for a PIV stake is the outpoint
85  CDataStream ss(SER_NETWORK, 0);
87  return ss;
88 }
89 
90 //The block that the UTXO was added to the chain
92 {
93  // Sanity check, pindexFrom is set on the constructor.
94  if (!pindexFrom) throw std::runtime_error("CPivStake: uninitialized pindexFrom");
95  return pindexFrom;
96 }
97 
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
const CChainParams & Params()
Return the currently selected parameters.
uint256 hash
Definition: transaction.h:35
uint32_t n
Definition: transaction.h:36
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
uint32_t nTime
Definition: chain.h:196
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:151
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:435
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:72
const CBlockIndex * GetIndexFrom() const override
Definition: stakeinput.cpp:91
bool GetTxOutFrom(CTxOut &out) const override
Definition: stakeinput.cpp:66
CAmount GetValue() const override
Definition: stakeinput.cpp:77
const CTxOut outputFrom
Definition: stakeinput.h:35
const COutPoint outpointFrom
Definition: stakeinput.h:36
CDataStream GetUniqueness() const override
Definition: stakeinput.cpp:82
static CPivStake * NewPivStake(const CTxIn &txin, int nHeight, uint32_t nTime)
Definition: stakeinput.cpp:22
CTxIn GetTxIn() const
Definition: stakeinput.cpp:72
CPivStake(const CTxOut &_from, const COutPoint &_outPointFrom, const CBlockIndex *_pindexFrom)
Definition: stakeinput.h:39
const CBlockIndex * pindexFrom
Definition: stakeinput.h:19
An input of a transaction.
Definition: transaction.h:94
bool IsZerocoinSpend() const
Definition: transaction.cpp:43
COutPoint prevout
Definition: transaction.h:96
An output of a transaction.
Definition: transaction.h:137
CAmount nValue
Definition: transaction.h:139
A UTXO entry.
Definition: coins.h:32
CTxOut out
unspent transaction output
Definition: coins.h:41
bool IsSpent() const
Definition: coins.h:86
uint32_t nHeight
at which height the containing transaction was included in the active block chain
Definition: coins.h:44
std::string GetHex() const
Definition: uint256.cpp:21
256-bit opaque blob.
Definition: uint256.h:138
@ UPGRADE_ZC_PUBLIC
Definition: params.h:33
@ SER_NETWORK
Definition: serialize.h:174
Parameters that influence chain consensus.
Definition: params.h:171
bool HasStakeMinAgeOrDepth(const int contextHeight, const uint32_t contextTime, const int utxoFromBlockHeight, const uint32_t utxoFromBlockTime) const
Definition: params.h:237
bool NetworkUpgradeActive(int nHeight, Consensus::UpgradeIndex idx) const
Returns true if the given network upgrade is active as of the given block height.
Definition: params.cpp:12
bool error(const char *fmt, const Args &... args)
Definition: system.h:77
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456
bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, uint256 &hashBlock, bool fAllowSlow, CBlockIndex *blockIndex)
Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock.
Definition: validation.cpp:671
std::unique_ptr< CCoinsViewCache > pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
Definition: validation.cpp:206
BlockMap mapBlockIndex
Definition: validation.cpp:82
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:84