Electroneum
cryptonote_basic.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <boost/variant.hpp>
35 #include <boost/functional/hash/hash.hpp>
36 #include <vector>
37 #include <cstring> // memcmp
38 #include <sstream>
39 #include <atomic>
41 #include "serialization/variant.h"
42 #include "serialization/vector.h"
46 #include "serialization/crypto.h"
47 #include "serialization/keyvalue_serialization.h" // eepe named serialization
48 #include "cryptonote_config.h"
49 #include "crypto/crypto.h"
50 #include "crypto/hash.h"
51 #include "misc_language.h"
52 #include "tx_extra.h"
53 #include "ringct/rctTypes.h"
54 
55 namespace cryptonote
56 {
57 
58  const static crypto::hash null_hash = AUTO_VAL_INIT(null_hash);
59  const static crypto::hash8 null_hash8 = AUTO_VAL_INIT(null_hash8);
60  const static crypto::public_key null_pkey = AUTO_VAL_INIT(null_pkey);
61 
62  typedef std::vector<crypto::signature> ring_signature;
63 
64 
65  /* outputs */
66 
68  {
69  std::vector<crypto::public_key> keys;
70  std::vector<uint8_t> script;
71 
73  FIELD(keys)
74  FIELD(script)
76  };
77 
79  {
81  };
82 
83  struct txout_to_key
84  {
86  txout_to_key(const crypto::public_key &_key) : key(_key) { }
88  };
89 
90 
91  /* inputs */
92 
93  struct txin_gen
94  {
95  size_t height;
96 
100  };
101 
103  {
105  size_t prevout;
106  std::vector<uint8_t> sigset;
107 
109  FIELD(prev)
110  VARINT_FIELD(prevout)
111  FIELD(sigset)
112  END_SERIALIZE()
113  };
114 
116  {
118  size_t prevout;
120  std::vector<uint8_t> sigset;
121 
123  FIELD(prev)
124  VARINT_FIELD(prevout)
125  FIELD(script)
126  FIELD(sigset)
127  END_SERIALIZE()
128  };
129 
130  struct txin_to_key
131  {
132  uint64_t amount;
133  std::vector<uint64_t> key_offsets;
134  crypto::key_image k_image; // double spending protection
135 
137  VARINT_FIELD(amount)
138  FIELD(key_offsets)
139  FIELD(k_image)
140  END_SERIALIZE()
141  };
142 
143 
145 
147 
148  //typedef std::pair<uint64_t, txout> out_t;
149  struct tx_out
150  {
151  uint64_t amount;
153 
155  VARINT_FIELD(amount)
156  FIELD(target)
157  END_SERIALIZE()
158 
159 
160  };
161 
163  {
164 
165  public:
166  // tx information
167  size_t version;
168  uint64_t unlock_time; //number of block (or time), used as a limitation like: spend this tx not early then block/time
169 
170  std::vector<txin_v> vin;
171  std::vector<tx_out> vout;
172  //extra
173  std::vector<uint8_t> extra;
174 
177  if(version == 0 || CURRENT_TRANSACTION_VERSION < version) return false;
178  VARINT_FIELD(unlock_time)
179  FIELD(vin)
180  FIELD(vout)
181  FIELD(extra)
182  END_SERIALIZE()
183 
184  public:
186  };
187 
189  {
190  private:
191  // hash cash
192  mutable std::atomic<bool> hash_valid;
193  mutable std::atomic<bool> blob_size_valid;
194 
195  public:
196  std::vector<std::vector<crypto::signature> > signatures; //count signatures always the same as inputs count
198 
199  // hash cash
201  mutable size_t blob_size;
202 
203  transaction();
204  transaction(const transaction &t): transaction_prefix(t), hash_valid(false), blob_size_valid(false), signatures(t.signatures), rct_signatures(t.rct_signatures) { if (t.is_hash_valid()) { hash = t.hash; set_hash_valid(true); } if (t.is_blob_size_valid()) { blob_size = t.blob_size; set_blob_size_valid(true); } }
205  transaction &operator=(const transaction &t) { transaction_prefix::operator=(t); set_hash_valid(false); set_blob_size_valid(false); signatures = t.signatures; rct_signatures = t.rct_signatures; if (t.is_hash_valid()) { hash = t.hash; set_hash_valid(true); } if (t.is_blob_size_valid()) { blob_size = t.blob_size; set_blob_size_valid(true); } return *this; }
206  virtual ~transaction();
207  void set_null();
208  void invalidate_hashes();
209  bool is_hash_valid() const { return hash_valid.load(std::memory_order_acquire); }
210  void set_hash_valid(bool v) const { hash_valid.store(v,std::memory_order_release); }
211  bool is_blob_size_valid() const { return blob_size_valid.load(std::memory_order_acquire); }
212  void set_blob_size_valid(bool v) const { blob_size_valid.store(v,std::memory_order_release); }
213 
215  if (!typename Archive<W>::is_saving())
216  {
217  set_hash_valid(false);
218  set_blob_size_valid(false);
219  }
220 
221  FIELDS(*static_cast<transaction_prefix *>(this))
222 
223  if (version == 1)
224  {
225  ar.tag("signatures");
226  ar.begin_array();
227  PREPARE_CUSTOM_VECTOR_SERIALIZATION(vin.size(), signatures);
228  bool signatures_not_expected = signatures.empty();
229  if (!signatures_not_expected && vin.size() != signatures.size())
230  return false;
231 
232  for (size_t i = 0; i < vin.size(); ++i)
233  {
234  size_t signature_size = get_signature_size(vin[i]);
235  if (signatures_not_expected)
236  {
237  if (0 == signature_size)
238  continue;
239  else
240  return false;
241  }
242 
243  PREPARE_CUSTOM_VECTOR_SERIALIZATION(signature_size, signatures[i]);
244  if (signature_size != signatures[i].size())
245  return false;
246 
247  FIELDS(signatures[i]);
248 
249  if (vin.size() - i > 1)
250  ar.delimit_array();
251  }
252  ar.end_array();
253  }
254  else
255  {
256  ar.tag("rct_signatures");
257  if (!vin.empty())
258  {
259  ar.begin_object();
260  bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size());
261  if (!r || !ar.stream().good()) return false;
262  ar.end_object();
263  if (rct_signatures.type != rct::RCTTypeNull)
264  {
265  ar.tag("rctsig_prunable");
266  ar.begin_object();
267  r = rct_signatures.p.serialize_rctsig_prunable(ar, rct_signatures.type, vin.size(), vout.size(),
268  vin[0].type() == typeid(txin_to_key) ? boost::get<txin_to_key>(vin[0]).key_offsets.size() - 1 : 0);
269  if (!r || !ar.stream().good()) return false;
270  ar.end_object();
271  }
272  }
273  }
274  END_SERIALIZE()
275 
276  template<bool W, template <bool> class Archive>
277  bool serialize_base(Archive<W> &ar)
278  {
279  FIELDS(*static_cast<transaction_prefix *>(this))
280 
281  if (version == 1)
282  {
283  }
284  else
285  {
286  ar.tag("rct_signatures");
287  if (!vin.empty())
288  {
289  ar.begin_object();
290  bool r = rct_signatures.serialize_rctsig_base(ar, vin.size(), vout.size());
291  if (!r || !ar.stream().good()) return false;
292  ar.end_object();
293  }
294  }
295  return true;
296  }
297 
298  private:
299  static size_t get_signature_size(const txin_v& tx_in);
300  };
301 
302 
303  inline
305  {
306  set_null();
307  }
308 
309  inline
311  {
312  //set_null();
313  }
314 
315  inline
317  {
318  version = 1;
319  unlock_time = 0;
320  vin.clear();
321  vout.clear();
322  extra.clear();
323  signatures.clear();
324  rct_signatures.type = rct::RCTTypeNull;
325  set_hash_valid(false);
326  set_blob_size_valid(false);
327  }
328 
329  inline
331  {
332  set_hash_valid(false);
333  set_blob_size_valid(false);
334  }
335 
336  inline
338  {
339  struct txin_signature_size_visitor : public boost::static_visitor<size_t>
340  {
341  size_t operator()(const txin_gen& txin) const{return 0;}
342  size_t operator()(const txin_to_script& txin) const{return 0;}
343  size_t operator()(const txin_to_scripthash& txin) const{return 0;}
344  size_t operator()(const txin_to_key& txin) const {return txin.key_offsets.size();}
345  };
346 
347  return boost::apply_visitor(txin_signature_size_visitor(), tx_in);
348  }
349 
350 
351 
352  /************************************************************************/
353  /* */
354  /************************************************************************/
356  {
357  uint8_t major_version;
358  uint8_t minor_version; // now used as a voting mechanism, rather than how this particular block is built
359  uint64_t timestamp;
361  uint32_t nonce;
362 
364  VARINT_FIELD(major_version)
365  VARINT_FIELD(minor_version)
366  VARINT_FIELD(timestamp)
367  FIELD(prev_id)
368  FIELD(nonce)
369  END_SERIALIZE()
370  };
371 
372  struct block: public block_header
373  {
374  private:
375  // hash cash
376  mutable std::atomic<bool> hash_valid;
377 
378  public:
379  block(): block_header(), hash_valid(false) {}
380  block(const block &b): block_header(b), hash_valid(false), miner_tx(b.miner_tx), tx_hashes(b.tx_hashes) { if (b.is_hash_valid()) { hash = b.hash; set_hash_valid(true); } }
381  block &operator=(const block &b) { block_header::operator=(b); hash_valid = false; miner_tx = b.miner_tx; tx_hashes = b.tx_hashes; if (b.is_hash_valid()) { hash = b.hash; set_hash_valid(true); } return *this; }
382  void invalidate_hashes() { set_hash_valid(false); }
383  bool is_hash_valid() const { return hash_valid.load(std::memory_order_acquire); }
384  void set_hash_valid(bool v) const { hash_valid.store(v,std::memory_order_release); }
385 
387  std::vector<crypto::hash> tx_hashes;
388 
389  // hash cash
391 
393  if (!typename Archive<W>::is_saving())
394  set_hash_valid(false);
395 
396  FIELDS(*static_cast<block_header *>(this))
397  FIELD(miner_tx)
398  FIELD(tx_hashes)
399  END_SERIALIZE()
400  };
401 
402 
403  /************************************************************************/
404  /* */
405  /************************************************************************/
407  {
410 
412  FIELD(m_spend_public_key)
413  FIELD(m_view_public_key)
414  END_SERIALIZE()
415 
416  BEGIN_KV_SERIALIZE_MAP()
417  KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_spend_public_key)
418  KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_view_public_key)
419  END_KV_SERIALIZE_MAP()
420  };
421 
422  struct keypair
423  {
426 
427  static inline keypair generate()
428  {
429  keypair k;
430  generate_keys(k.pub, k.sec);
431  return k;
432  }
433  };
434  //---------------------------------------------------------------
435 
436 }
437 
440 
450 
460 
crypto::public_key pub
Definition: cryptonote_basic.h:424
block & operator=(const block &b)
Definition: cryptonote_basic.h:381
std::vector< crypto::hash > tx_hashes
Definition: cryptonote_basic.h:387
uint8_t minor_version
Definition: cryptonote_basic.h:358
boost::variant< txout_to_script, txout_to_scripthash, txout_to_key > txout_target_v
Definition: cryptonote_basic.h:146
Definition: cryptonote_basic.h:115
static size_t get_signature_size(const txin_v &tx_in)
Definition: cryptonote_basic.h:337
transaction()
Definition: cryptonote_basic.h:304
txout_to_script script
Definition: cryptonote_basic.h:119
Definition: unordered_containers_boost_serialization.h:38
crypto::secret_key sec
Definition: cryptonote_basic.h:425
static const crypto::hash8 null_hash8
Definition: cryptonote_basic.h:59
Definition: cryptonote_basic.h:422
bool serialize_rctsig_prunable(Archive< W > &ar, uint8_t type, size_t inputs, size_t outputs, size_t mixin)
Definition: rctTypes.h:252
Definition: cryptonote_basic.h:372
virtual ~transaction()
Definition: cryptonote_basic.h:310
bool is_blob_size_valid() const
Definition: cryptonote_basic.h:211
Definition: cryptonote_basic.h:355
uint64_t height
Definition: blockchain.cpp:87
uint8_t major_version
Definition: cryptonote_basic.h:357
static keypair generate()
Definition: cryptonote_basic.h:427
size_t prevout
Definition: cryptonote_basic.h:118
Definition: rctTypes.h:177
static const crypto::hash null_hash
Definition: cryptonote_basic.h:58
size_t prevout
Definition: cryptonote_basic.h:105
transaction(const transaction &t)
Definition: cryptonote_basic.h:204
std::vector< uint64_t > key_offsets
Definition: cryptonote_basic.h:133
Definition: cryptonote_basic.h:83
Definition: cryptonote_basic.h:67
Definition: cryptonote_basic.h:93
std::vector< crypto::signature > ring_signature
Definition: cryptonote_basic.h:62
std::vector< uint8_t > extra
Definition: cryptonote_basic.h:173
crypto::hash prev
Definition: cryptonote_basic.h:104
#define END_SERIALIZE()
self-explanatory
Definition: serialization.h:205
Definition: cryptonote_basic.h:162
std::vector< tx_out > vout
Definition: cryptonote_basic.h:171
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
bool is_hash_valid() const
Definition: cryptonote_basic.h:383
Definition: cryptonote_basic.h:102
Simple DSL AAPI based on.
std::vector< txin_v > vin
Definition: cryptonote_basic.h:170
crypto::public_key m_spend_public_key
Definition: cryptonote_basic.h:408
std::vector< uint8_t > script
Definition: cryptonote_basic.h:70
for dealing with variants
block()
Definition: cryptonote_basic.h:379
Definition: cryptonote_basic.h:78
uint64_t unlock_time
Definition: cryptonote_basic.h:168
void invalidate_hashes()
Definition: cryptonote_basic.h:382
crypto::public_key m_view_public_key
Definition: cryptonote_basic.h:409
void set_blob_size_valid(bool v) const
Definition: cryptonote_basic.h:212
rct::rctSig rct_signatures
Definition: cryptonote_basic.h:197
std::atomic< bool > hash_valid
Definition: cryptonote_basic.h:376
void set_hash_valid(bool v) const
Definition: cryptonote_basic.h:384
#define FIELDS(f)
does not add a tag to the serialized value
Definition: serialization.h:245
rctSigPrunable p
Definition: rctTypes.h:324
void invalidate_hashes()
Definition: cryptonote_basic.h:330
static const crypto::public_key null_pkey
Definition: cryptonote_basic.h:60
uint64_t amount
Definition: cryptonote_basic.h:132
int b
Definition: base.py:1
uint64_t timestamp
Definition: cryptonote_basic.h:359
Definition: cryptonote_basic.h:130
#define BEGIN_SERIALIZE_OBJECT()
begins the environment of the DSL \detailed for described the serialization of an object
Definition: serialization.h:186
secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key &recovery_key=secret_key(), bool recover=false)
Definition: crypto.h:162
Definition: rctTypes.h:323
size_t version
Definition: cryptonote_basic.h:167
#define false
Definition: stdbool.h:38
std::atomic< bool > blob_size_valid
Definition: cryptonote_basic.h:193
transaction miner_tx
Definition: cryptonote_basic.h:386
crypto::hash hash
Definition: cryptonote_basic.h:80
POD_CLASS public_key
Definition: crypto.h:58
void set_null()
Definition: cryptonote_basic.h:316
bool is_hash_valid() const
Definition: cryptonote_basic.h:209
BLOB_SERIALIZER(cryptonote::txout_to_key)
uint8_t version
Definition: blockchain.cpp:86
std::vector< std::vector< crypto::signature > > signatures
Definition: cryptonote_basic.h:196
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:106
Definition: cryptonote_basic.h:406
crypto::hash prev
Definition: cryptonote_basic.h:117
POD_CLASS hash8
Definition: hash.h:49
crypto::public_key key
Definition: cryptonote_basic.h:87
transaction & operator=(const transaction &t)
Definition: cryptonote_basic.h:205
POD_CLASS key_image
Definition: crypto.h:86
#define VARINT_FIELD(f)
tags and serializes the varint f
Definition: serialization.h:254
POD_CLASS secret_key
Definition: crypto.h:62
VARIANT_TAG(binary_archive, cryptonote::txin_gen, 0xff)
std::vector< uint8_t > sigset
Definition: cryptonote_basic.h:120
uint64_t amount
Definition: cryptonote_basic.h:151
uint8_t type
Definition: rctTypes.h:182
txout_target_v target
Definition: cryptonote_basic.h:152
crypto::key_image k_image
Definition: cryptonote_basic.h:134
crypto::hash hash
Definition: cryptonote_basic.h:200
void set_hash_valid(bool v) const
Definition: cryptonote_basic.h:210
std::atomic< bool > hash_valid
Definition: cryptonote_basic.h:192
block(const block &b)
Definition: cryptonote_basic.h:380
txout_to_key()
Definition: cryptonote_basic.h:85
#define BEGIN_SERIALIZE()
Begins the environment of the DSL \detailed for describing how to serialize an of an archive type.
Definition: serialization.h:177
std::vector< crypto::public_key > keys
Definition: cryptonote_basic.h:69
boost::variant< txin_gen, txin_to_script, txin_to_scripthash, txin_to_key > txin_v
Definition: cryptonote_basic.h:144
crypto::hash prev_id
Definition: cryptonote_basic.h:360
POD_CLASS hash
Definition: hash.h:46
JSON archive.
txout_to_key(const crypto::public_key &_key)
Definition: cryptonote_basic.h:86
#define CURRENT_TRANSACTION_VERSION
Definition: cryptonote_config.h:45
#define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec)
Definition: serialization.h:199
crypto::hash hash
Definition: cryptonote_basic.h:390
Definition: cryptonote_basic.h:149
bool serialize_rctsig_base(Archive< W > &ar, size_t inputs, size_t outputs)
Definition: rctTypes.h:192
#define FIELD(f)
tags the field with the variable name and then serializes it
Definition: serialization.h:234
size_t blob_size
Definition: cryptonote_basic.h:201
size_t height
Definition: cryptonote_basic.h:95
Definition: binary_archive.h:95
Definition: debug_archive.h:38
uint32_t nonce
Definition: cryptonote_basic.h:361
Definition: cryptonote_basic.h:188
a archive using the JSON standard
Definition: json_archive.h:112