PRCYCoin  2.0.0.7rc1
P2P Digital Currency
stakeinput.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 The PIVX 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 "chain.h"
6 #include "main.h"
7 #include "stakeinput.h"
8 #include "wallet/wallet.h"
9 
11 bool CPrcyStake::SetInput(CTransaction txPrev, unsigned int n)
12 {
13  this->txFrom = txPrev;
14  this->nPosition = n;
15  return true;
16 }
17 
19 {
20  tx = txFrom;
21  return true;
22 }
23 
24 bool CPrcyStake::CreateTxIn(CWallet* pwallet, CTxIn& txIn, uint256 hashTxOut)
25 {
26  CPubKey sharedSec;
27  txIn = CTxIn(txFrom.GetHash(), nPosition);
28 
29  const CWalletTx* pWalletTx = pwalletMain->GetWalletTx(txFrom.GetHash());
30  pwalletMain->computeSharedSec(txFrom, pWalletTx->vout[nPosition], sharedSec);
31 
32  //copy encryption key so that full nodes can decode the amount in the txin
33  std::copy(sharedSec.begin(), sharedSec.begin() + 33, std::back_inserter(txIn.encryptionKey));
34 
35  CScript scriptPubKeyKernel = txFrom.vout[nPosition].scriptPubKey;
36 
37  if (!pwalletMain->generateKeyImage(scriptPubKeyKernel, txIn.keyImage)) {
38  LogPrintf("CreateCoinStake : cannot generate key image\n");
39  return false;
40  }
41  return true;
42 }
43 
45 {
46  if (txFrom.IsCoinBase() || txFrom.IsCoinStake()) {
47  return txFrom.vout[nPosition].nValue;
48  }
49 
50  const CWalletTx* pWalletTx = pwalletMain->GetWalletTx(txFrom.GetHash());
51  if (!pWalletTx) {
52  return 0;
53  }
54 
55  return pwalletMain->getCTxOutValue(*pWalletTx, pWalletTx->vout[nPosition]);
56 }
57 
58 bool CPrcyStake::CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal)
59 {
60  std::vector<valtype> vSolutions;
61  txnouttype whichType;
62  CScript scriptPubKeyKernel = txFrom.vout[nPosition].scriptPubKey;
63 
64  CKey view, spend;
67  CPubKey viewPub = view.GetPubKey();
68  CPubKey spendPub = spend.GetPubKey();
69 
70  if (!Solver(scriptPubKeyKernel, whichType, vSolutions)) {
71  LogPrintf("CreateCoinStake : failed to parse kernel\n");
72  return false;
73  }
74 
75  if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH)
76  return false; // only support pay to public key and pay to address
77 
78  CScript scriptPubKey;
79  if (whichType == TX_PUBKEYHASH) // pay to address type
80  {
81  //convert to pay to public key type
82  CKey key;
83  if (!pwallet->GetKey(uint160(vSolutions[0]), key))
84  return false;
85 
86  scriptPubKey << key.GetPubKey() << OP_CHECKSIG;
87  } else
88  scriptPubKey = scriptPubKeyKernel;
89 
90  //first UTXO for the staked amount
91  //create a new pubkey
92  CKey myTxPriv;
93  myTxPriv.MakeNewKey(true);
94  CPubKey txPub = myTxPriv.GetPubKey();
95  CPubKey newPub;
96  CWallet::ComputeStealthDestination(myTxPriv, viewPub, spendPub, newPub);
97  scriptPubKey = GetScriptForDestination(newPub);
98  CTxOut out(0, scriptPubKey);
99  std::copy(txPub.begin(), txPub.end(), std::back_inserter(out.txPub));
100  vout.emplace_back(out);
101 
102  //second UTXO for staking reward
103  //create a new pubkey
104  CKey myTxPrivStaking;
105  myTxPrivStaking.MakeNewKey(true);
106  CPubKey txPubStaking = myTxPrivStaking.GetPubKey();
107  CPubKey newPubStaking;
108  CWallet::ComputeStealthDestination(myTxPrivStaking, viewPub, spendPub, newPubStaking);
109  CScript scriptPubKeyOutStaking = GetScriptForDestination(newPubStaking);
110  CTxOut outStaking(0, scriptPubKeyOutStaking);
111  std::copy(txPubStaking.begin(), txPubStaking.end(), std::back_inserter(outStaking.txPub));
112  vout.emplace_back(outStaking);
113 
114  // Calculate if we need to split the output
115  /*if (nTotal / 2 > (CAmount)(pwallet->nStakeSplitThreshold * COIN))
116  vout.emplace_back(CTxOut(0, scriptPubKey));*/
117 
118  return true;
119 }
120 
121 bool CPrcyStake::GetModifier(uint64_t& nStakeModifier)
122 {
123  int nStakeModifierHeight = 0;
124  int64_t nStakeModifierTime = 0;
125  GetIndexFrom();
126  if (!pindexFrom)
127  return error("%s: failed to get index from", __func__);
128 
129  if (!GetKernelStakeModifier(pindexFrom->GetBlockHash(), nStakeModifier, nStakeModifierHeight, nStakeModifierTime, false))
130  return error("CheckStakeKernelHash(): failed to get kernel stake modifier \n");
131 
132  return true;
133 }
134 
136 {
137  //The unique identifier for a PRCY stake is the outpoint
138  CDataStream ss(SER_NETWORK, 0);
139  ss << nPosition << txFrom.GetHash();
140  return ss;
141 }
142 
143 //The block that the UTXO was added to the chain
145 {
146  uint256 hashBlock = 0;
147  CTransaction tx;
148  if (GetTransaction(txFrom.GetHash(), tx, hashBlock, true)) {
149  // If the index is in the chain, then set it as the "index from"
150  if (mapBlockIndex.count(hashBlock)) {
151  CBlockIndex* pindex = mapBlockIndex.at(hashBlock);
152  if (chainActive.Contains(pindex))
153  pindexFrom = pindex;
154  }
155  } else {
156  LogPrintf("%s : failed to find tx %s\n", __func__, txFrom.GetHash().GetHex());
157  }
158 
159  return pindexFrom;
160 }
CTxIn
An input of a transaction.
Definition: transaction.h:83
CPrcyStake::CreateTxIn
bool CreateTxIn(CWallet *pwallet, CTxIn &txIn, uint256 hashTxOut=0) override
Definition: stakeinput.cpp:24
CTxIn::encryptionKey
std::vector< unsigned char > encryptionKey
Definition: transaction.h:96
CPrcyStake::GetUniqueness
CDataStream GetUniqueness() override
Definition: stakeinput.cpp:135
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:40
GetKernelStakeModifier
bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t &nStakeModifier, int &nStakeModifierHeight, int64_t &nStakeModifierTime, bool fPrintProofOfStake)
Definition: kernel.cpp:245
base_uint::GetHex
std::string GetHex() const
Definition: arith_uint256.cpp:155
chainActive
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:70
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Definition: standard.cpp:285
wallet.h
CPrcyStake::GetModifier
bool GetModifier(uint64_t &nStakeModifier) override
Definition: stakeinput.cpp:121
CStakeInput::pindexFrom
CBlockIndex * pindexFrom
Definition: stakeinput.h:19
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:159
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:269
TX_PUBKEYHASH
@ TX_PUBKEYHASH
Definition: standard.h:62
CPrcyStake::GetTxFrom
bool GetTxFrom(CTransaction &tx) override
Definition: stakeinput.cpp:18
CPubKey::begin
const unsigned char * begin() const
Definition: pubkey.h:95
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:359
CWallet::ComputeStealthDestination
static bool ComputeStealthDestination(const CKey &secret, const CPubKey &pubViewKey, const CPubKey &pubSpendKey, CPubKey &des)
Definition: wallet.cpp:6618
CPrcyStake::GetIndexFrom
CBlockIndex * GetIndexFrom() override
Definition: stakeinput.cpp:144
CTxOut
An output of a transaction.
Definition: transaction.h:164
CPrcyStake::GetValue
CAmount GetValue() override
Definition: stakeinput.cpp:44
GetTransaction
bool GetTransaction(const uint256 &hash, CTransaction &tx, 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: main.cpp:2010
CTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:286
CWallet::mySpendPrivateKey
bool mySpendPrivateKey(CKey &spend) const
Definition: wallet.cpp:7004
TX_PUBKEY
@ TX_PUBKEY
Definition: standard.h:61
CPubKey::end
const unsigned char * end() const
Definition: pubkey.h:96
CWallet::computeSharedSec
bool computeSharedSec(const CTransaction &tx, const CTxOut &out, CPubKey &sharedSec) const
Definition: wallet.cpp:4252
CWallet::GetWalletTx
const CWalletTx * GetWalletTx(const uint256 &hash) const
Definition: wallet.cpp:167
CAmount
int64_t CAmount
Amount in PRCY (Can be negative)
Definition: amount.h:17
LogPrintf
#define LogPrintf(...)
Definition: logging.h:147
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:79
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:155
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:363
chain.h
CWallet::generateKeyImage
bool generateKeyImage(const CPubKey &pub, CKeyImage &img) const
Definition: wallet.cpp:7162
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:359
CPrcyStake::CreateTxOuts
bool CreateTxOuts(CWallet *pwallet, std::vector< CTxOut > &vout, CAmount nTotal) override
Definition: stakeinput.cpp:58
stakeinput.h
CPrcyStake::nPosition
unsigned int nPosition
Definition: stakeinput.h:36
uint160
160-bit unsigned big integer.
Definition: uint256.h:27
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
CKey
An encapsulated private key.
Definition: key.h:39
main.h
Solver
bool Solver(const CKeyStore &keystore, const CScript &scriptPubKey, uint256 hash, int nHashType, CScript &scriptSigRet, txnouttype &whichTypeRet)
Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
Definition: sign.cpp:50
key
CKey key
Definition: bip38tooldialog.cpp:173
CPrcyStake::txFrom
CTransaction txFrom
Definition: stakeinput.h:35
CWallet
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:243
CWalletTx
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:792
CChain::Contains
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:626
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:34
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:342
CPrcyStake::SetInput
bool SetInput(CTransaction txPrev, unsigned int n)
PRCY Stake.
Definition: stakeinput.cpp:11
txnouttype
txnouttype
Definition: standard.h:57
CTxIn::keyImage
CKeyImage keyImage
Definition: transaction.h:97
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:162
pwalletMain
CWallet * pwalletMain
Definition: wallet.cpp:49
CWallet::GetKey
bool GetKey(const CKeyID &address, CKey &keyOut) const
GetKey implementation that can derive a HD private key on the fly.
Definition: wallet.cpp:6906
CWallet::getCTxOutValue
CAmount getCTxOutValue(const CTransaction &tx, const CTxOut &out) const
Definition: wallet.cpp:7202
error
bool error(const char *fmt, const Args &... args)
Definition: util.h:61
CWallet::myViewPrivateKey
bool myViewPrivateKey(CKey &view) const
Definition: wallet.cpp:7025
mapBlockIndex
BlockMap mapBlockIndex
Definition: main.cpp:67
CTxOut::txPub
std::vector< unsigned char > txPub
Definition: transaction.h:173
CTransaction::IsCoinStake
bool IsCoinStake() const
Definition: transaction.cpp:143