]> Git Repo - VerusCoin.git/blob - src/cc/eval.cpp
Merge pull request #20 from VerusCoin/master
[VerusCoin.git] / src / cc / eval.cpp
1 /******************************************************************************
2  * Copyright © 2014-2018 The SuperNET Developers.                             *
3  *                                                                            *
4  * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at                  *
5  * the top-level directory of this distribution for the individual copyright  *
6  * holder information and the developer policies on copyright and licensing.  *
7  *                                                                            *
8  * Unless otherwise agreed in a custom licensing agreement, no part of the    *
9  * SuperNET software, including this file may be copied, modified, propagated *
10  * or distributed except according to the terms contained in the LICENSE file *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
16 #include <assert.h>
17 #include <cryptoconditions.h>
18
19 #include "primitives/block.h"
20 #include "primitives/transaction.h"
21 #include "script/cc.h"
22 #include "cc/eval.h"
23 #include "cc/utils.h"
24 #include "cc/CCinclude.h"
25 #include "main.h"
26 #include "chain.h"
27 #include "core_io.h"
28 #include "crosschain.h"
29
30
31 Eval* EVAL_TEST = 0;
32 struct CCcontract_info CCinfos[0x100];
33
34 bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn)
35 {
36     // DISABLE CRYPTO CONDITIONS FOR NOW
37     return false;
38
39     EvalRef eval;
40     bool out = eval->Dispatch(cond, tx, nIn);
41     //fprintf(stderr,"out %d vs %d isValid\n",(int32_t)out,(int32_t)eval->state.IsValid());
42     assert(eval->state.IsValid() == out);
43
44     if (eval->state.IsValid()) return true;
45
46     std::string lvl = eval->state.IsInvalid() ? "Invalid" : "Error!";
47     fprintf(stderr, "CC Eval %s %s: %s spending tx %s\n",
48             EvalToStr(cond->code[0]).data(),
49             lvl.data(),
50             eval->state.GetRejectReason().data(),
51             tx.vin[nIn].prevout.hash.GetHex().data());
52     if (eval->state.IsError()) fprintf(stderr, "Culprit: %s\n", EncodeHexTx(tx).data());
53     return false;
54 }
55
56
57 /*
58  * Test the validity of an Eval node
59  */
60 bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
61 {
62     struct CCcontract_info *cp;
63     if (cond->codeLength == 0)
64         return Invalid("empty-eval");
65
66     uint8_t ecode = cond->code[0];
67     cp = &CCinfos[(int32_t)ecode];
68     if ( cp->didinit == 0 )
69     {
70         CCinit(cp,ecode);
71         cp->didinit = 1;
72     }
73     std::vector<uint8_t> vparams(cond->code+1, cond->code+cond->codeLength);
74     switch ( ecode )
75     {
76         case EVAL_IMPORTPAYOUT:
77             return ImportPayout(vparams, txTo, nIn);
78             break;
79             
80         case EVAL_IMPORTCOIN:
81             return ImportCoin(vparams, txTo, nIn);
82             break;
83             
84         default:
85             return(ProcessCC(cp,this, vparams, txTo, nIn));
86             break;
87     }
88     return Invalid("invalid-code, dont forget to add EVAL_NEWCC to Eval::Dispatch");
89 }
90
91
92 bool Eval::GetSpendsConfirmed(uint256 hash, std::vector<CTransaction> &spends) const
93 {
94     // NOT IMPLEMENTED
95     return false;
96 }
97
98
99 bool Eval::GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const
100 {
101     bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock);
102     // there is a LOCK(cs_main) in the normal GetTransaction(), which leads to deadlocks
103     //bool fAllowSlow = false; // Don't allow slow
104     //return GetTransaction(hash, txOut, hashBlock, fAllowSlow);
105     return myGetTransaction(hash, txOut,hashBlock);
106 }
107
108
109 bool Eval::GetTxConfirmed(const uint256 &hash, CTransaction &txOut, CBlockIndex &block) const
110 {
111     uint256 hashBlock;
112     if (!GetTxUnconfirmed(hash, txOut, hashBlock))
113         return false;
114     if (hashBlock.IsNull() || !GetBlock(hashBlock, block))
115         return false;
116     return true;
117 }
118
119
120 unsigned int Eval::GetCurrentHeight() const
121 {
122     return chainActive.Height();
123 }
124
125 bool Eval::GetBlock(uint256 hash, CBlockIndex& blockIdx) const
126 {
127     auto r = mapBlockIndex.find(hash);
128     if (r != mapBlockIndex.end()) {
129         blockIdx = *r->second;
130         return true;
131     }
132     fprintf(stderr, "CC Eval Error: Can't get block from index\n");
133     return false;
134 }
135
136 extern int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp);
137
138
139 int32_t Eval::GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const
140 {
141     return komodo_notaries(pubkeys, height, timestamp);
142 }
143
144
145 bool Eval::CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t timestamp) const
146 {
147     if (tx.vin.size() < 11) return false;
148
149     uint8_t seenNotaries[64] = {0};
150     uint8_t notaries[64][33];
151     int nNotaries = GetNotaries(notaries, height, timestamp);
152
153     BOOST_FOREACH(const CTxIn &txIn, tx.vin)
154     {
155         // Get notary pubkey
156         CTransaction tx;
157         uint256 hashBlock;
158         if (!GetTxUnconfirmed(txIn.prevout.hash, tx, hashBlock)) return false;
159         if (tx.vout.size() < txIn.prevout.n) return false;
160         CScript spk = tx.vout[txIn.prevout.n].scriptPubKey;
161         if (spk.size() != 35) return false;
162         const unsigned char *pk = spk.data();
163         if (pk++[0] != 33) return false;
164         if (pk[33] != OP_CHECKSIG) return false;
165
166         // Check it's a notary
167         for (int i=0; i<nNotaries; i++) {
168             if (!seenNotaries[i]) {
169                 if (memcmp(pk, notaries[i], 33) == 0) {
170                     seenNotaries[i] = 1;
171                     goto found;
172                 }
173             }
174         }
175         return false;
176         found:;
177     }
178
179     return true;
180 }
181
182
183 /*
184  * Get MoM from a notarisation tx hash (on KMD)
185  */
186 bool Eval::GetNotarisationData(const uint256 notaryHash, NotarisationData &data) const
187 {
188     CTransaction notarisationTx;
189     CBlockIndex block;
190     if (!GetTxConfirmed(notaryHash, notarisationTx, block)) return false;
191     if (!CheckNotaryInputs(notarisationTx, block.nHeight, block.nTime)) return false;
192     if (!ParseNotarisationOpReturn(notarisationTx, data)) return false;
193     return true;
194 }
195
196 /*
197  * Get MoMoM corresponding to a notarisation tx hash (on assetchain)
198  */
199 bool Eval::GetProofRoot(uint256 kmdNotarisationHash, uint256 &momom) const
200 {
201     std::pair<uint256,NotarisationData> out;
202     if (!GetNextBacknotarisation(kmdNotarisationHash, out)) return false;
203     momom = out.second.MoMoM;
204     return true;
205 }
206
207
208 uint32_t Eval::GetAssetchainsCC() const
209 {
210     return ASSETCHAINS_CC;
211 }
212
213
214 std::string Eval::GetAssetchainsSymbol() const
215 {
216     return std::string(ASSETCHAINS_SYMBOL);
217 }
218
219
220 /*
221  * Notarisation data, ie, OP_RETURN payload in notarisation transactions
222  */
223 bool ParseNotarisationOpReturn(const CTransaction &tx, NotarisationData &data)
224 {
225     if (tx.vout.size() < 2) return false;
226     std::vector<unsigned char> vdata;
227     if (!GetOpReturnData(tx.vout[1].scriptPubKey, vdata)) return false;
228     bool out = E_UNMARSHAL(vdata, ss >> data);
229     return out;
230 }
231
232
233 /*
234  * Misc
235  */
236 std::string EvalToStr(EvalCode c)
237 {
238     FOREACH_EVAL(EVAL_GENERATE_STRING);
239     char s[10];
240     sprintf(s, "0x%x", c);
241     return std::string(s);
242
243 }
244
245
246 uint256 SafeCheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex)
247 {
248     if (nIndex == -1)
249         return uint256();
250     for (auto it(vMerkleBranch.begin()); it != vMerkleBranch.end(); ++it)
251     {
252         if (nIndex & 1) {
253             if (*it == hash) {
254                 // non canonical. hash may be equal to node but never on the right.
255                 return uint256();
256             }
257             hash = Hash(BEGIN(*it), END(*it), BEGIN(hash), END(hash));
258         }
259         else
260             hash = Hash(BEGIN(hash), END(hash), BEGIN(*it), END(*it));
261         nIndex >>= 1;
262     }
263     return hash;
264 }
265
266
267 uint256 GetMerkleRoot(const std::vector<uint256>& vLeaves)
268 {
269     bool fMutated;
270     std::vector<uint256> vMerkleTree;
271     return BuildMerkleTree(&fMutated, vLeaves, vMerkleTree);
272 }
This page took 0.037787 seconds and 4 git commands to generate.