2 #include <cryptoconditions.h>
4 #include "primitives/block.h"
5 #include "primitives/transaction.h"
12 #include "crosschain.h"
18 bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn)
22 bool out = eval->Dispatch(cond, tx, nIn);
23 assert(eval->state.IsValid() == out);
25 if (eval->state.IsValid()) return true;
27 std::string lvl = eval->state.IsInvalid() ? "Invalid" : "Error!";
28 fprintf(stderr, "CC Eval %s %s: %s spending tx %s\n",
29 EvalToStr(cond->code[0]).data(),
31 eval->state.GetRejectReason().data(),
32 tx.vin[nIn].prevout.hash.GetHex().data());
33 if (eval->state.IsError()) fprintf(stderr, "Culprit: %s\n", EncodeHexTx(tx).data());
39 * Test the validity of an Eval node
41 bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
43 if (cond->codeLength == 0)
44 return Invalid("empty-eval");
46 uint8_t ecode = cond->code[0];
47 std::vector<uint8_t> vparams(cond->code+1, cond->code+cond->codeLength);
49 if (ecode == EVAL_IMPORTPAYOUT) {
50 return ImportPayout(vparams, txTo, nIn);
53 if (ecode == EVAL_IMPORTCOIN) {
54 return ImportCoin(vparams, txTo, nIn);
57 return Invalid("invalid-code");
61 bool Eval::GetSpendsConfirmed(uint256 hash, std::vector<CTransaction> &spends) const
68 bool Eval::GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const
70 bool fAllowSlow = false; // Don't allow slow
71 return GetTransaction(hash, txOut, hashBlock, fAllowSlow);
75 bool Eval::GetTxConfirmed(const uint256 &hash, CTransaction &txOut, CBlockIndex &block) const
78 if (!GetTxUnconfirmed(hash, txOut, hashBlock))
80 if (hashBlock.IsNull() || !GetBlock(hashBlock, block))
86 unsigned int Eval::GetCurrentHeight() const
88 return chainActive.Height();
92 bool Eval::GetBlock(uint256 hash, CBlockIndex& blockIdx) const
94 auto r = mapBlockIndex.find(hash);
95 if (r != mapBlockIndex.end()) {
96 blockIdx = *r->second;
99 fprintf(stderr, "CC Eval Error: Can't get block from index\n");
104 extern int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp);
107 int32_t Eval::GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const
109 return komodo_notaries(pubkeys, height, timestamp);
113 bool Eval::CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t timestamp) const
115 if (tx.vin.size() < 11) return false;
117 uint8_t seenNotaries[64] = {0};
118 uint8_t notaries[64][33];
119 int nNotaries = GetNotaries(notaries, height, timestamp);
121 BOOST_FOREACH(const CTxIn &txIn, tx.vin)
126 if (!GetTxUnconfirmed(txIn.prevout.hash, tx, hashBlock)) return false;
127 if (tx.vout.size() < txIn.prevout.n) return false;
128 CScript spk = tx.vout[txIn.prevout.n].scriptPubKey;
129 if (spk.size() != 35) return false;
130 const unsigned char *pk = spk.data();
131 if (pk++[0] != 33) return false;
132 if (pk[33] != OP_CHECKSIG) return false;
134 // Check it's a notary
135 for (int i=0; i<nNotaries; i++) {
136 if (!seenNotaries[i]) {
137 if (memcmp(pk, notaries[i], 33) == 0) {
152 * Get MoM from a notarisation tx hash (on KMD)
154 bool Eval::GetNotarisationData(const uint256 notaryHash, NotarisationData &data) const
156 CTransaction notarisationTx;
158 if (!GetTxConfirmed(notaryHash, notarisationTx, block)) return false;
159 if (!CheckNotaryInputs(notarisationTx, block.nHeight, block.nTime)) return false;
160 if (!ParseNotarisationOpReturn(notarisationTx, data)) return false;
165 * Get MoMoM corresponding to a notarisation tx hash (on assetchain)
167 bool Eval::GetProofRoot(uint256 kmdNotarisationHash, uint256 &momom) const
169 std::pair<uint256,NotarisationData> out;
170 if (!GetNextBacknotarisation(kmdNotarisationHash, out)) return false;
171 momom = out.second.MoMoM;
176 uint32_t Eval::GetAssetchainsCC() const
178 return ASSETCHAINS_CC;
182 std::string Eval::GetAssetchainsSymbol() const
184 return std::string(ASSETCHAINS_SYMBOL);
189 * Notarisation data, ie, OP_RETURN payload in notarisation transactions
191 bool ParseNotarisationOpReturn(const CTransaction &tx, NotarisationData &data)
193 if (tx.vout.size() < 2) return false;
194 std::vector<unsigned char> vdata;
195 if (!GetOpReturnData(tx.vout[1].scriptPubKey, vdata)) return false;
196 bool out = E_UNMARSHAL(vdata, ss >> data);
204 std::string EvalToStr(EvalCode c)
206 FOREACH_EVAL(EVAL_GENERATE_STRING);
208 sprintf(s, "0x%x", c);
209 return std::string(s);
214 uint256 SafeCheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
218 for (auto it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
222 // non canonical. hash may be equal to node but never on the right.
225 hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
228 hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
235 uint256 GetMerkleRoot(const std::vector<uint256>& vLeaves)
238 std::vector<uint256> vMerkleTree;
239 return BuildMerkleTree(&fMutated, vLeaves, vMerkleTree);