PPCoin: Check stake target and signature for all accepted block
[novacoin.git] / src / main.h
index bd07ceb..f6db3b3 100644 (file)
@@ -53,6 +53,7 @@ static const int fHaveUPnP = false;
 
 extern CCriticalSection cs_main;
 extern std::map<uint256, CBlockIndex*> mapBlockIndex;
+extern std::set<COutPoint> setStakeSeen;
 extern uint256 hashGenesisBlock;
 extern CBlockIndex* pindexGenesisBlock;
 extern int nBestHeight;
@@ -360,7 +361,13 @@ public:
         return (nValue == -1);
     }
 
-    bool IsCoinStake() const
+    bool SetEmpty()
+    {
+        nValue = 0;
+        scriptPubKey.clear();
+    }
+
+    bool IsEmpty() const
     {
         return (nValue == 0 && scriptPubKey.empty());
     }
@@ -383,7 +390,7 @@ public:
 
     std::string ToString() const
     {
-        if (IsCoinStake()) return "CTxOut(coinstake)";
+        if (IsEmpty()) return "CTxOut(empty)";
         if (scriptPubKey.size() < 6)
             return "CTxOut(error)";
         return strprintf("CTxOut(nValue=%s, scriptPubKey=%s)", FormatMoney(nValue).c_str(), scriptPubKey.ToString().substr(0,30).c_str());
@@ -504,7 +511,7 @@ public:
     bool IsCoinStake() const
     {
         // ppcoin: the coin stake transaction is marked with the first output empty
-        return (vout.size() == 2 && vout[0].IsCoinStake());
+        return (vin.size() > 0 && vout.size() == 2 && vout[0].IsEmpty());
     }
 
     int GetSigOpCount() const
@@ -632,7 +639,8 @@ public:
     std::string ToString() const
     {
         std::string str;
-        str += strprintf("CTransaction(hash=%s, nTime=%d, ver=%d, vin.size=%d, vout.size=%d, nLockTime=%d)\n",
+        str += IsCoinBase()? "Coinbase" : (IsCoinStake()? "Coinstake" : "CTransaction");
+        str += strprintf("(hash=%s, nTime=%d, ver=%d, vin.size=%d, vout.size=%d, nLockTime=%d)\n",
             GetHash().ToString().substr(0,10).c_str(),
             nTime,
             nVersion,
@@ -667,7 +675,7 @@ protected:
 public:
     bool RemoveFromMemoryPool();
     bool GetCoinAge(CTxDB& txdb, uint64& nCoinAge) const;  // ppcoin: get transaction coin age
-    bool CheckProofOfStake(CTxDB& txdb, unsigned int nBits) const;
+    bool CheckProofOfStake(unsigned int nBits) const;
 };
 
 
@@ -889,6 +897,11 @@ public:
         return !IsProofOfStake();
     }
 
+    COutPoint GetProofOfStake() const
+    {
+        return IsProofOfStake()? vtx[1].vin[0].prevout : COutPoint();
+    }
+
     // ppcoin: get max transaction timestamp
     int64 GetMaxTransactionTime() const
     {
@@ -1037,8 +1050,9 @@ public:
     bool SignBlock(const CKeyStore& keystore)
     {
         std::vector<std::pair<opcodetype, valtype> > vSolution;
+        const CTxOut& txout = IsProofOfStake()? vtx[1].vout[1] : vtx[0].vout[0];
 
-        if (!Solver(vtx[0].vout[0].scriptPubKey, vSolution))
+        if (!Solver(txout.scriptPubKey, vSolution))
             return false;
         BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
         {
@@ -1063,8 +1077,9 @@ public:
             return vchBlockSig.empty();
 
         std::vector<std::pair<opcodetype, valtype> > vSolution;
+        const CTxOut& txout = IsProofOfStake()? vtx[1].vout[1] : vtx[0].vout[0];
 
-        if (!Solver(vtx[0].vout[0].scriptPubKey, vSolution))
+        if (!Solver(txout.scriptPubKey, vSolution))
             return false;
         BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
         {
@@ -1117,6 +1132,7 @@ public:
     int nHeight;
     int nCheckpoint;    // ppcoin: chain auto checkpoint height
     bool fProofOfStake; // ppcoin: is the block of proof-of-stake type
+    COutPoint prevoutStake;
 
     // block header
     int nVersion;
@@ -1137,6 +1153,7 @@ public:
         nChainTrust = 0;
         nCheckpoint = 0;
         fProofOfStake = true;
+        prevoutStake.SetNull();
 
         nVersion       = 0;
         hashMerkleRoot = 0;
@@ -1156,6 +1173,10 @@ public:
         nChainTrust = 0;
         nCheckpoint = 0;
         fProofOfStake = block.IsProofOfStake();
+        if (fProofOfStake)
+            prevoutStake = block.vtx[1].vin[0].prevout;
+        else
+            prevoutStake.SetNull();
 
         nVersion       = block.nVersion;
         hashMerkleRoot = block.hashMerkleRoot;
@@ -1257,8 +1278,9 @@ public:
 
     std::string ToString() const
     {
-        return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, nCheckpoint=%d, fProofOfStake=%d merkle=%s, hashBlock=%s)",
-            pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight, nCheckpoint, fProofOfStake,
+        return strprintf("CBlockIndex(nprev=%08x, pnext=%08x, nFile=%d, nBlockPos=%-6d nChainTrust=%"PRI64d" nHeight=%d, nCheckpoint=%d, fProofOfStake=%d prevoutStake=(%s) merkle=%s, hashBlock=%s)",
+            pprev, pnext, nFile, nBlockPos, nChainTrust, nHeight, nCheckpoint,
+            fProofOfStake, prevoutStake.ToString().c_str(),
             hashMerkleRoot.ToString().substr(0,10).c_str(),
             GetBlockHash().ToString().substr(0,20).c_str());
     }
@@ -1304,6 +1326,7 @@ public:
         READWRITE(nHeight);
         READWRITE(nCheckpoint);
         READWRITE(fProofOfStake);
+        READWRITE(prevoutStake);
 
         // block header
         READWRITE(this->nVersion);