forked from chips-blockchain/chips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cpp
More file actions
203 lines (156 loc) · 5.08 KB
/
eval.cpp
File metadata and controls
203 lines (156 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <assert.h>
#include <cryptoconditions.h>
#include "primitives/transaction.h"
#include "chainparams.h"
#include "script/cc.h"
#include "cc/eval.h"
#include "chain.h"
#include "core_io.h"
#include "validation.h"
Eval* EVAL_TEST = 0;
bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn)
{
Eval eval_;
Eval *eval = EVAL_TEST;
if (!eval) eval = &eval_;
bool out = eval->Dispatch(cond, tx, nIn);
assert(eval->state.IsValid() == out);
if (eval->state.IsValid()) return true;
std::string lvl = eval->state.IsInvalid() ? "Invalid" : "Error!";
fprintf(stderr, "CC Eval %s %s: %s spending tx %s\n",
EvalToStr(cond->code[0]).data(),
lvl.data(),
eval->state.GetRejectReason().data(),
tx.vin[nIn].prevout.hash.GetHex().data());
if (eval->state.IsError()) fprintf(stderr, "Culprit: %s\n", EncodeHexTx(tx).data());
return false;
}
/*
* Test the validity of an Eval node
*/
bool Eval::Dispatch(const CC *cond, const CTransaction &txTo, unsigned int nIn)
{
if (cond->codeLength == 0)
return Invalid("empty-eval");
uint8_t ecode = cond->code[0];
std::vector<uint8_t> vparams(cond->code+1, cond->code+cond->codeLength);
if (ecode == EVAL_IMPORTPAYOUT) {
return ImportPayout(vparams, txTo, nIn);
}
return Invalid("invalid-code");
}
bool Eval::GetSpendsConfirmed(uint256 hash, std::vector<CTransactionRef> &spends) const
{
// NOT IMPLEMENTED
return false;
}
bool Eval::GetTxUnconfirmed(const uint256 &hash, CTransactionRef &txOut, uint256 &hashBlock) const
{
bool fAllowSlow = false; // Don't allow slow
return GetTransaction(hash, txOut, Params().GetConsensus(), hashBlock, fAllowSlow);
}
bool Eval::GetTxConfirmed(const uint256 &hash, CTransactionRef &txOut, CBlockIndex &block) const
{
uint256 hashBlock;
if (!GetTxUnconfirmed(hash, txOut, hashBlock))
return false;
if (hashBlock.IsNull() || !GetBlock(hashBlock, block))
return false;
return true;
}
unsigned int Eval::GetCurrentHeight() const
{
return chainActive.Height();
}
bool Eval::GetBlock(uint256 hash, CBlockIndex& blockIdx) const
{
auto r = mapBlockIndex.find(hash);
if (r != mapBlockIndex.end()) {
blockIdx = *r->second;
return true;
}
fprintf(stderr, "CC Eval Error: Can't get block from index\n");
return false;
}
extern int32_t komodo_notaries(uint8_t pubkeys[64][33],int32_t height,uint32_t timestamp);
int32_t Eval::GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const
{
return komodo_notaries(pubkeys, height, timestamp);
}
bool Eval::CheckNotaryInputs(const CTransaction ¬aryTx, uint32_t height, uint32_t timestamp) const
{
if (notaryTx.vin.size() < 11) return false;
uint8_t seenNotaries[64] = {0};
uint8_t notaries[64][33];
int nNotaries = GetNotaries(notaries, height, timestamp);
for (size_t nin=0; nin<notaryTx.vin.size(); nin++) {
const CTxIn& txIn = notaryTx.vin[nin];
// Get notary pubkey
CTransactionRef tx;
uint256 hashBlock;
if (!GetTxUnconfirmed(txIn.prevout.hash, tx, hashBlock)) return false;
if (tx->vout.size() < txIn.prevout.n) return false;
CScript spk = tx->vout[txIn.prevout.n].scriptPubKey;
if (spk.size() != 35) return false;
const unsigned char *pk = spk.data();
if (pk++[0] != 33) return false;
if (pk[33] != OP_CHECKSIG) return false;
// Check it's a notary
for (int i=0; i<nNotaries; i++) {
if (!seenNotaries[i]) {
if (memcmp(pk, notaries[i], 33) == 0) {
seenNotaries[i] = 1;
goto found;
}
}
}
return false;
found:;
}
return true;
}
/*
* Get MoM from a notarisation tx hash
*/
bool Eval::GetNotarisationData(const uint256 notaryHash, NotarisationData &data) const
{
CTransactionRef notarisationTx;
CBlockIndex block;
if (!GetTxConfirmed(notaryHash, notarisationTx, block)) return false;
if (!CheckNotaryInputs(*notarisationTx, block.nHeight, block.nTime)) return false;
if (notarisationTx->vout.size() < 2) return false;
if (!data.Parse(notarisationTx->vout[1].scriptPubKey)) return false;
return true;
}
bool NotarisationData::Parse(const CScript scriptPK)
{
*this = NotarisationData();
std::vector<unsigned char> vdata;
if (!GetOpReturnData(scriptPK, vdata)) return false;
CDataStream ss(vdata, SER_NETWORK, PROTOCOL_VERSION);
try {
ss >> blockHash;
ss >> height;
//if (ASSETCHAINS_SYMBOL[0])
// ss >> txHash;
char *nullPos = (char*) memchr(&ss[0], 0, ss.size());
if (!nullPos) return false;
ss.read(symbol, nullPos-&ss[0]+1);
if (ss.size() < 36) return false;
ss >> MoM;
ss >> MoMDepth;
} catch (...) {
return false;
}
return true;
}
/*
* Misc
*/
std::string EvalToStr(EvalCode c)
{
FOREACH_EVAL(EVAL_GENERATE_STRING);
char s[10];
sprintf(s, "0x%x", c);
return std::string(s);
}