]>
Commit | Line | Data |
---|---|---|
f345b953 | 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 | ||
2c8d8268 SS |
16 | #ifndef CC_EVAL_H |
17 | #define CC_EVAL_H | |
18 | ||
561f3e18 SS |
19 | #include <cryptoconditions.h> |
20 | ||
20c3ac51 | 21 | #include "cc/utils.h" |
561f3e18 SS |
22 | #include "chain.h" |
23 | #include "streams.h" | |
24 | #include "version.h" | |
25 | #include "consensus/validation.h" | |
2c8d8268 SS |
26 | #include "primitives/transaction.h" |
27 | ||
072dba5f | 28 | #define KOMODO_FIRSTFUNGIBLEID 100 |
2c8d8268 | 29 | |
39c9911e SS |
30 | /* |
31 | * Eval codes | |
32 | * | |
33 | * Add to below macro to generate new code. | |
34 | * | |
35 | * If at some point a new interpretation model is introduced, | |
36 | * there should be a code identifying it. For example, | |
37 | * a possible code is EVAL_BITCOIN_SCRIPT, where the entire binary | |
38 | * after the code is interpreted as a bitcoin script. | |
39 | */ | |
1188acf6 | 40 | #define FOREACH_EVAL(EVAL) \ |
41 | EVAL(EVAL_NONE, 0x0) \ | |
ca4a5f26 | 42 | EVAL(EVAL_STAKEGUARD, 0x1) \ |
56fe75cb | 43 | EVAL(EVAL_CURRENCY_DEFINITION, 0x2) \ |
c8c684e9 | 44 | EVAL(EVAL_NOTARY_EVIDENCE, 0x3) \ |
b2a98c42 MT |
45 | EVAL(EVAL_EARNEDNOTARIZATION, 0x4) \ |
46 | EVAL(EVAL_ACCEPTEDNOTARIZATION, 0x5) \ | |
09b977c6 | 47 | EVAL(EVAL_FINALIZE_NOTARIZATION, 0x6) \ |
41f170fd | 48 | EVAL(EVAL_CURRENCYSTATE, 0x7) \ |
989b1de1 | 49 | EVAL(EVAL_RESERVE_TRANSFER, 0x8) \ |
a6e612cc MT |
50 | EVAL(EVAL_RESERVE_OUTPUT, 0x9) \ |
51 | EVAL(EVAL_RESERVE_EXCHANGE, 0xa) \ | |
989b1de1 MT |
52 | EVAL(EVAL_RESERVE_DEPOSIT, 0xb) \ |
53 | EVAL(EVAL_CROSSCHAIN_EXPORT, 0xc) \ | |
54 | EVAL(EVAL_CROSSCHAIN_IMPORT, 0xd) \ | |
f0d54147 | 55 | EVAL(EVAL_IDENTITY_PRIMARY, 0xe) \ |
56 | EVAL(EVAL_IDENTITY_REVOKE, 0xf) \ | |
57 | EVAL(EVAL_IDENTITY_RECOVER, 0x10) \ | |
b7c685b8 | 58 | EVAL(EVAL_IDENTITY_COMMITMENT, 0x11) \ |
59 | EVAL(EVAL_IDENTITY_RESERVATION, 0x12) \ | |
09b977c6 | 60 | EVAL(EVAL_FINALIZE_EXPORT, 0x13) \ |
1e561fd3 | 61 | EVAL(EVAL_FEE_POOL, 0x14) \ |
c8c684e9 | 62 | EVAL(EVAL_NOTARY_SIGNATURE, 0x15) \ |
63 | EVAL(EVAL_VOTING_GOVERNANCE, 0x16) \ | |
64 | EVAL(EVAL_VOTING_VOTE, 0x17) \ | |
65 | EVAL(EVAL_VOTING_POLL, 0x18) \ | |
66 | EVAL(EVAL_VOTING_SPEND, 0x19) \ | |
67 | EVAL(EVAL_QUANTUM_KEY, 0x1a) \ | |
0cb91a8d | 68 | EVAL(EVAL_IMPORTPAYOUT, 0xe1) \ |
4802c859 MT |
69 | EVAL(EVAL_IMPORTCOIN, 0xe2) \ |
70 | EVAL(EVAL_ASSETS, 0xe3) \ | |
194ad5b8 | 71 | EVAL(EVAL_FAUCET, 0xe4) \ |
2fcf7a42 | 72 | EVAL(EVAL_REWARDS, 0xe5) \ |
69829385 | 73 | EVAL(EVAL_DICE, 0xe6) \ |
7137a022 | 74 | EVAL(EVAL_FSM, 0xe7) \ |
69829385 | 75 | EVAL(EVAL_AUCTION, 0xe8) \ |
da629dfe | 76 | EVAL(EVAL_LOTTO, 0xe9) \ |
77 | EVAL(EVAL_MOFN, 0xea) \ | |
78 | EVAL(EVAL_CHANNELS, 0xeb) \ | |
79 | EVAL(EVAL_ORACLES, 0xec) \ | |
80 | EVAL(EVAL_PRICES, 0xed) \ | |
81 | EVAL(EVAL_PEGS, 0xee) \ | |
82 | EVAL(EVAL_TRIGGERS, 0xef) \ | |
83 | EVAL(EVAL_PAYMENTS, 0xf0) \ | |
84 | EVAL(EVAL_GATEWAYS, 0xf1) | |
0cb91a8d | 85 | |
39c9911e SS |
86 | |
87 | typedef uint8_t EvalCode; | |
88 | ||
89 | ||
561f3e18 | 90 | class AppVM; |
9bf132a5 | 91 | class NotarisationData; |
561f3e18 SS |
92 | |
93 | ||
94 | class Eval | |
95 | { | |
96 | public: | |
97 | CValidationState state; | |
98 | ||
99 | bool Invalid(std::string s) { return state.Invalid(false, 0, s); } | |
100 | bool Error(std::string s) { return state.Error(s); } | |
101 | bool Valid() { return true; } | |
102 | ||
103 | /* | |
104 | * Test validity of a CC_Eval node | |
105 | */ | |
4ecaf167 | 106 | virtual bool Dispatch(const CC *cond, const CTransaction &tx, unsigned int nIn, bool fulfilled); |
561f3e18 SS |
107 | |
108 | /* | |
109 | * Dispute a payout using a VM | |
110 | */ | |
39c9911e | 111 | bool DisputePayout(AppVM &vm, std::vector<uint8_t> params, const CTransaction &disputeTx, unsigned int nIn); |
561f3e18 SS |
112 | |
113 | /* | |
114 | * Test an ImportPayout CC Eval condition | |
115 | */ | |
39c9911e | 116 | bool ImportPayout(std::vector<uint8_t> params, const CTransaction &importTx, unsigned int nIn); |
561f3e18 | 117 | |
0cb91a8d SS |
118 | /* |
119 | * Import coin from another chain with same symbol | |
120 | */ | |
121 | bool ImportCoin(std::vector<uint8_t> params, const CTransaction &importTx, unsigned int nIn); | |
122 | ||
561f3e18 SS |
123 | /* |
124 | * IO functions | |
125 | */ | |
47296322 SS |
126 | virtual bool GetTxUnconfirmed(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock) const; |
127 | virtual bool GetTxConfirmed(const uint256 &hash, CTransaction &txOut, CBlockIndex &block) const; | |
561f3e18 | 128 | virtual unsigned int GetCurrentHeight() const; |
47296322 | 129 | virtual bool GetSpendsConfirmed(uint256 hash, std::vector<CTransaction> &spends) const; |
561f3e18 | 130 | virtual bool GetBlock(uint256 hash, CBlockIndex& blockIdx) const; |
9bf132a5 SS |
131 | virtual int32_t GetNotaries(uint8_t pubkeys[64][33], int32_t height, uint32_t timestamp) const; |
132 | virtual bool GetNotarisationData(uint256 notarisationHash, NotarisationData &data) const; | |
e4f943d8 | 133 | virtual bool GetProofRoot(uint256 kmdNotarisationHash, uint256 &momom) const; |
561f3e18 | 134 | virtual bool CheckNotaryInputs(const CTransaction &tx, uint32_t height, uint32_t timestamp) const; |
0df96a2f SS |
135 | virtual uint32_t GetAssetchainsCC() const; |
136 | virtual std::string GetAssetchainsSymbol() const; | |
561f3e18 SS |
137 | }; |
138 | ||
139 | ||
20c3ac51 SS |
140 | extern Eval* EVAL_TEST; |
141 | ||
142 | ||
143 | /* | |
144 | * Get a pointer to an Eval to use | |
145 | */ | |
146 | typedef std::unique_ptr<Eval,void(*)(Eval*)> EvalRef_; | |
147 | class EvalRef : public EvalRef_ | |
148 | { | |
149 | public: | |
150 | EvalRef() : EvalRef_( | |
151 | EVAL_TEST ? EVAL_TEST : new Eval(), | |
152 | [](Eval* e){if (e!=EVAL_TEST) delete e;}) { } | |
153 | }; | |
154 | ||
155 | ||
156 | ||
4ecaf167 | 157 | bool RunCCEval(const CC *cond, const CTransaction &tx, unsigned int nIn, bool fulfilled); |
561f3e18 | 158 | |
2c8d8268 | 159 | |
9ef101bc SS |
160 | /* |
161 | * Virtual machine to use in the case of on-chain app evaluation | |
162 | */ | |
163 | class AppVM | |
164 | { | |
165 | public: | |
166 | /* | |
167 | * in: header - paramters agreed upon by all players | |
168 | * in: body - gamestate | |
169 | * out: length - length of game (longest wins) | |
170 | * out: payments - vector of CTxOut, always deterministically sorted. | |
171 | */ | |
172 | virtual std::pair<int,std::vector<CTxOut>> | |
173 | evaluate(std::vector<unsigned char> header, std::vector<unsigned char> body) = 0; | |
174 | }; | |
175 | ||
20c3ac51 SS |
176 | |
177 | extern char ASSETCHAINS_SYMBOL[65]; | |
178 | ||
179 | ||
9ef101bc | 180 | /* |
20c3ac51 | 181 | * Data from notarisation OP_RETURN from chain being notarised |
9ef101bc | 182 | */ |
20c3ac51 SS |
183 | class NotarisationData |
184 | { | |
9bf132a5 | 185 | public: |
06c960d2 | 186 | int IsBackNotarisation = 0; |
fe727b9d SS |
187 | uint256 blockHash = uint256(); |
188 | uint32_t height = 0; | |
189 | uint256 txHash = uint256(); | |
7efaea18 | 190 | char symbol[64]; |
fe727b9d SS |
191 | uint256 MoM = uint256(); |
192 | uint16_t MoMDepth = 0; | |
193 | uint16_t ccId = 0; | |
194 | uint256 MoMoM = uint256(); | |
195 | uint32_t MoMoMDepth = 0; | |
9bf132a5 | 196 | |
7efaea18 SS |
197 | NotarisationData(int IsBack=2) : IsBackNotarisation(IsBack) { |
198 | symbol[0] = '\0'; | |
199 | } | |
20c3ac51 SS |
200 | |
201 | ADD_SERIALIZE_METHODS; | |
202 | ||
203 | template <typename Stream, typename Operation> | |
9feb4b9e | 204 | inline void SerializationOp(Stream& s, Operation ser_action) { |
a748b1a2 SS |
205 | |
206 | bool IsBack = IsBackNotarisation; | |
3fdb3782 | 207 | if (2 == IsBackNotarisation) IsBack = DetectBackNotarisation(s, ser_action); |
a748b1a2 | 208 | |
20c3ac51 SS |
209 | READWRITE(blockHash); |
210 | READWRITE(height); | |
a748b1a2 | 211 | if (IsBack) |
20c3ac51 SS |
212 | READWRITE(txHash); |
213 | SerSymbol(s, ser_action); | |
a748b1a2 | 214 | if (s.size() == 0) return; |
20c3ac51 SS |
215 | READWRITE(MoM); |
216 | READWRITE(MoMDepth); | |
20c3ac51 | 217 | READWRITE(ccId); |
91d92922 | 218 | if (s.size() == 0) return; |
a748b1a2 | 219 | if (IsBack) { |
20c3ac51 SS |
220 | READWRITE(MoMoM); |
221 | READWRITE(MoMoMDepth); | |
222 | } | |
223 | } | |
224 | ||
225 | template <typename Stream> | |
226 | void SerSymbol(Stream& s, CSerActionSerialize act) | |
227 | { | |
228 | s.write(symbol, strlen(symbol)+1); | |
229 | } | |
230 | ||
231 | template <typename Stream> | |
232 | void SerSymbol(Stream& s, CSerActionUnserialize act) | |
233 | { | |
7efaea18 SS |
234 | size_t readlen = std::min(sizeof(symbol), s.size()); |
235 | char *nullPos = (char*) memchr(&s[0], 0, readlen); | |
20c3ac51 SS |
236 | if (!nullPos) |
237 | throw std::ios_base::failure("couldn't parse symbol"); | |
238 | s.read(symbol, nullPos-&s[0]+1); | |
239 | } | |
a748b1a2 SS |
240 | |
241 | template <typename Stream> | |
3fdb3782 | 242 | bool DetectBackNotarisation(Stream& s, CSerActionUnserialize act) |
a748b1a2 SS |
243 | { |
244 | if (ASSETCHAINS_SYMBOL[0]) return 1; | |
3fdb3782 SS |
245 | if (s.size() >= 72) { |
246 | if (strcmp("BTC", &s[68]) == 0) return 1; | |
247 | if (strcmp("KMD", &s[68]) == 0) return 1; | |
248 | } | |
a748b1a2 SS |
249 | return 0; |
250 | } | |
251 | ||
252 | template <typename Stream> | |
3fdb3782 | 253 | bool DetectBackNotarisation(Stream& s, CSerActionSerialize act) |
a748b1a2 SS |
254 | { |
255 | return !txHash.IsNull(); | |
256 | } | |
9bf132a5 | 257 | }; |
561f3e18 SS |
258 | |
259 | ||
20c3ac51 SS |
260 | bool ParseNotarisationOpReturn(const CTransaction &tx, NotarisationData &data); |
261 | ||
262 | ||
39c9911e SS |
263 | /* |
264 | * Eval code utilities. | |
265 | */ | |
266 | #define EVAL_GENERATE_DEF(L,I) const uint8_t L = I; | |
267 | #define EVAL_GENERATE_STRING(L,I) if (c == I) return #L; | |
268 | ||
269 | FOREACH_EVAL(EVAL_GENERATE_DEF); | |
270 | ||
271 | std::string EvalToStr(EvalCode c); | |
272 | ||
273 | ||
0cb91a8d SS |
274 | /* |
275 | * Merkle stuff | |
276 | */ | |
277 | uint256 SafeCheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMerkleBranch, int nIndex); | |
278 | ||
279 | ||
280 | class MerkleBranch | |
281 | { | |
282 | public: | |
283 | int nIndex; | |
284 | std::vector<uint256> branch; | |
285 | ||
286 | MerkleBranch() {} | |
287 | MerkleBranch(int i, std::vector<uint256> b) : nIndex(i), branch(b) {} | |
288 | uint256 Exec(uint256 hash) const { return SafeCheckMerkleBranch(hash, branch, nIndex); } | |
289 | ||
20c3ac51 SS |
290 | MerkleBranch& operator<<(MerkleBranch append) |
291 | { | |
292 | nIndex += append.nIndex << branch.size(); | |
293 | branch.insert(branch.end(), append.branch.begin(), append.branch.end()); | |
294 | return *this; | |
295 | } | |
296 | ||
0cb91a8d SS |
297 | ADD_SERIALIZE_METHODS; |
298 | ||
299 | template <typename Stream, typename Operation> | |
9feb4b9e | 300 | inline void SerializationOp(Stream& s, Operation ser_action) { |
0cb91a8d SS |
301 | READWRITE(VARINT(nIndex)); |
302 | READWRITE(branch); | |
303 | } | |
304 | }; | |
305 | ||
306 | ||
e4f943d8 SS |
307 | typedef std::pair<uint256,MerkleBranch> TxProof; |
308 | ||
bbf91faa | 309 | bool DefaultCCContextualPreCheck(const CTransaction &tx, int32_t outNum, CValidationState &state, uint32_t height); |
20c3ac51 | 310 | uint256 GetMerkleRoot(const std::vector<uint256>& vLeaves); |
b6461360 | 311 | struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode); |
4ecaf167 | 312 | bool ProcessCC(struct CCcontract_info *cp,Eval* eval, std::vector<uint8_t> paramsNull, const CTransaction &tx, unsigned int nIn, bool fulfilled); |
20c3ac51 SS |
313 | |
314 | ||
2c8d8268 | 315 | #endif /* CC_EVAL_H */ |