From 60b798c4b7bc6373d93eb36dc507aba4c72d5239 Mon Sep 17 00:00:00 2001 From: Michael Toutonghi Date: Sat, 13 Oct 2018 14:23:20 -0700 Subject: [PATCH] Update stake cheat spend --- src/cc/CCutils.cpp | 16 ++++++------ src/cc/StakeGuard.cpp | 6 ++--- src/script/script.cpp | 61 ++++++++++++++++++++++++++----------------- src/script/script.h | 1 + 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 028437ddb..c5746f328 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -204,7 +204,6 @@ bool GetCCParams(Eval* eval, const CTransaction &tx, uint32_t nIn, CTransaction &txOut, std::vector> &preConditions, std::vector> ¶ms) { uint256 blockHash; - bool isValid = false; if (myGetTransaction(tx.vin[nIn].prevout.hash, txOut, blockHash) && txOut.vout.size() > tx.vin[nIn].prevout.n) { @@ -220,22 +219,23 @@ bool GetCCParams(Eval* eval, const CTransaction &tx, uint32_t nIn, params.clear(); if (tx.vout.size() > 0 && tx.vout[tx.vout.size() - 1].scriptPubKey.IsOpReturn()) { - tx.vout[tx.vout.size() - 1].scriptPubKey.GetOpretData(params); - if (params.size() == 1) + if (tx.vout[tx.vout.size() - 1].scriptPubKey.GetOpretData(params) && params.size() == 1) { CScript scr = CScript(params[0]); - if (scr.IsOpReturn()) + // printf("Stake cheat parameter opret:\n%s\n", scr.ToString().c_str()); + if (!scr.GetPushedData(scr.begin(), params)) { - params.clear(); - scr.GetOpretData(params); + return false; } + else return true; } + else return false; } - isValid = true; + else return true; } } } - return isValid; + return false; } CPubKey CCtxidaddr(char *txidaddr,uint256 txid) diff --git a/src/cc/StakeGuard.cpp b/src/cc/StakeGuard.cpp index ceb2ed1a6..f91c1c0b9 100644 --- a/src/cc/StakeGuard.cpp +++ b/src/cc/StakeGuard.cpp @@ -286,7 +286,6 @@ bool ValidateMatchingStake(const CTransaction &ccTx, uint32_t voutNum, const CTr // this attaches an opret to a mutable transaction that provides the necessary evidence of a signed, cheating stake transaction bool MakeCheatEvidence(CMutableTransaction &mtx, const CTransaction &ccTx, uint32_t voutNum, const CTransaction &cheatTx) { - std::vector vch; CDataStream s = CDataStream(SER_DISK, CLIENT_VERSION); bool isCheater = false; @@ -298,7 +297,7 @@ bool MakeCheatEvidence(CMutableTransaction &mtx, const CTransaction &ccTx, uint3 CScript vData = CScript(); cheatTx.Serialize(s); vch = std::vector(s.begin(), s.end()); - vData << OPRETTYPE_STAKECHEAT << vch; + vData << ((int64_t)OPRETTYPE_STAKECHEAT) << vch; vOut.scriptPubKey << OP_RETURN << std::vector(vData.begin(), vData.end()); vOut.nValue = 0; mtx.vout.push_back(vOut); @@ -396,7 +395,8 @@ bool StakeGuardValidate(struct CCcontract_info *cp, Eval* eval, const CTransacti signedByFirstKey = (IsCCFulfilled(cc, &fc) != 0); - if (!(signedByFirstKey = (vc[0] != 0)) && params.size() == 2 && params[0].size() > 0 && params[0][0] == OPRETTYPE_STAKECHEAT) + if ((!signedByFirstKey && ccp.evalCode == EVAL_STAKEGUARD && ccp.vKeys.size() == 2 && ccp.version == COptCCParams::VERSION) && + params.size() == 2 && params[0].size() > 0 && params[0][0] == OPRETTYPE_STAKECHEAT) { CDataStream s = CDataStream(std::vector(params[1].begin(), params[1].end()), SER_DISK, CLIENT_VERSION); bool checkOK = false; diff --git a/src/script/script.cpp b/src/script/script.cpp index f3fc8311f..481454a75 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -295,6 +295,41 @@ bool CScript::GetBalancedData(const_iterator& pc, std::vector>& vData) const +{ + vector data; + opcodetype opcode; + std::vector vch1 = std::vector(1); + + vData.clear(); + + while (pc < end()) + { + if (GetOp(pc, opcode, data)) + { + if (opcode == OP_0) + { + vch1[0] = 0; + vData.push_back(vch1); + } + else if (opcode >= OP_1 && opcode <= OP_16) + { + vch1[0] = (opcode - OP_1) + 1; + vData.push_back(vch1); + } + else if (opcode > 0 && opcode <= OP_PUSHDATA4 && data.size() > 0) + { + vData.push_back(data); + } + else + return false; + } + } + return vData.size() != 0; +} + // this returns true if either there is nothing left and pc points at the end // if there is data, it also returns all the values as byte vectors in a list of vectors bool CScript::GetOpretData(std::vector>& vData) const @@ -302,34 +337,12 @@ bool CScript::GetOpretData(std::vector>& vData) const vector data; opcodetype opcode; CScript::const_iterator pc = this->begin(); - std::vector vch1 = std::vector(1); - - vData.clear(); if (GetOp(pc, opcode, data) && opcode == OP_RETURN) { - while (pc < end()) - { - if (GetOp(pc, opcode, data)) - { - if (opcode == OP_0) - { - vch1[0] = 0; - vData.push_back(vch1); - } - else if (opcode >= OP_1 && opcode <= OP_16) - { - vch1[0] = (opcode - OP_1) + 1; - vData.push_back(data); - } - else - { - vData.push_back(data); - } - } - } - return vData.size() != 0; + return GetPushedData(pc, vData); } + else return false; } bool CScript::IsPayToCryptoCondition(CScript *pCCSubScript, std::vector>& vParams) const diff --git a/src/script/script.h b/src/script/script.h index fee9dce83..52d25b3d8 100644 --- a/src/script/script.h +++ b/src/script/script.h @@ -577,6 +577,7 @@ public: bool IsPayToPublicKey() const; bool IsPayToScriptHash() const; + bool GetPushedData(CScript::const_iterator pc, std::vector>& vData) const; bool IsOpReturn() const { return size() > 0 && (*this)[0] == OP_RETURN; } bool GetOpretData(std::vector>& vData) const;