Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.21 KB | None | 0 0
  1. #include <rai/common.hpp>
  2.  
  3. #include <rai/blockstore.hpp>
  4. #include <rai/lib/interface.h>
  5. #include <rai/node/common.hpp>
  6. #include <rai/versioning.hpp>
  7.  
  8. #include <boost/property_tree/json_parser.hpp>
  9.  
  10. #include <queue>
  11.  
  12. #include <ed25519-donna/ed25519.h>
  13.  
  14. // Genesis keys for network variants
  15. namespace
  16. {
  17. char const * test_private_key_data = "0187ECC01FCFC715DB930568BD4FE37E9B71FB51BA829963B68C5D242800F2FF";
  18. char const * test_public_key_data = "89E564D4ABE4E3C8185E2FCBE804461ABD940709B1AEB6A66CAC12DC7A001746"; // xrb_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku
  19. char const * beta_public_key_data = "0311B25E0D1E1D7724BBA5BD523954F1DBCFC01CB8671D55ED2D32C7549FB252"; // xrb_11rjpbh1t9ixgwkdqbfxcawobwgusz13sg595ocytdbkrxcbzekkcqkc3dn1
  20. char const * live_public_key_data = "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA"; // xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3
  21. char const * test_genesis_data = R"%%%({
  22. "type": "open",
  23. "source": "89E564D4ABE4E3C8185E2FCBE804461ABD940709B1AEB6A66CAC12DC7A001746",
  24. "representative": "lumo_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku",
  25. "account": "lumo_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku",
  26. "work": "00000000020bf0e8",
  27. "signature": "DED6BDA3472755B1F07AED09C2FE6C02600DE17A7163209B68974EFF58015AB0FA2E45FD7CE630ACDA7144817B7DDF24DFE48C14C8C3AC289C76A94518B5820B"
  28. })%%%";
  29.  
  30. char const * beta_genesis_data = R"%%%({
  31. "type": "open",
  32. "source": "0311B25E0D1E1D7724BBA5BD523954F1DBCFC01CB8671D55ED2D32C7549FB252",
  33. "representative": "lumo_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku",
  34. "account": "lumo_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku",
  35. "work": "869e17b2bfa36639",
  36. "signature": "34DF447C7F185673128C3516A657DFEC7906F16C68FB5A8879432E2E4FB908C8ED0DD24BBECFAB3C7852898231544A421DC8CB636EF66C82E1245083EB08EA0F"
  37. })%%%";
  38.  
  39. char const * live_genesis_data = R"%%%({
  40. "type": "open",
  41. "source": "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA",
  42. "representative": "lumo_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku",
  43. "account": "lumo_34h7emccqs95s1e7wdydx146e8oxki5imefgptm8sd1kujx117t8wyeihaku",
  44. "work": "62f05417dd3fb691",
  45. "signature": "9F0C933C8ADE004D808EA1985FA746A7E95BA2A38F867640F53EC8F180BDFE9E2C1268DEAD7C2664F356E37ABA362BC58E46DBA03E523A7B5A19E4B6EB12BB02"
  46. })%%%";
  47.  
  48. class ledger_constants
  49. {
  50. public:
  51. ledger_constants () :
  52. zero_key ("0"),
  53. test_genesis_key (test_private_key_data),
  54. rai_test_account (test_public_key_data),
  55. rai_beta_account (beta_public_key_data),
  56. rai_live_account (live_public_key_data),
  57. rai_test_genesis (test_genesis_data),
  58. rai_beta_genesis (beta_genesis_data),
  59. rai_live_genesis (live_genesis_data),
  60. genesis_account (rai::rai_network == rai::rai_networks::rai_test_network ? rai_test_account : rai::rai_network == rai::rai_networks::rai_beta_network ? rai_beta_account : rai_live_account),
  61. genesis_block (rai::rai_network == rai::rai_networks::rai_test_network ? rai_test_genesis : rai::rai_network == rai::rai_networks::rai_beta_network ? rai_beta_genesis : rai_live_genesis),
  62. genesis_amount (rai::uint128_t ("128000000000000000000000000000000000000")),
  63. burn_account (0)
  64. {
  65. CryptoPP::AutoSeededRandomPool random_pool;
  66. // Randomly generating these mean no two nodes will ever have the same sentinel values which protects against some insecure algorithms
  67. random_pool.GenerateBlock (not_a_block.bytes.data (), not_a_block.bytes.size ());
  68. random_pool.GenerateBlock (not_an_account.bytes.data (), not_an_account.bytes.size ());
  69. }
  70. rai::keypair zero_key;
  71. rai::keypair test_genesis_key;
  72. rai::account rai_test_account;
  73. rai::account rai_beta_account;
  74. rai::account rai_live_account;
  75. std::string rai_test_genesis;
  76. std::string rai_beta_genesis;
  77. std::string rai_live_genesis;
  78. rai::account genesis_account;
  79. std::string genesis_block;
  80. rai::uint128_t genesis_amount;
  81. rai::block_hash not_a_block;
  82. rai::account not_an_account;
  83. rai::account burn_account;
  84. };
  85. ledger_constants globals;
  86. }
  87.  
  88. size_t constexpr rai::send_block::size;
  89. size_t constexpr rai::receive_block::size;
  90. size_t constexpr rai::open_block::size;
  91. size_t constexpr rai::change_block::size;
  92. size_t constexpr rai::state_block::size;
  93.  
  94. rai::keypair const & rai::zero_key (globals.zero_key);
  95. rai::keypair const & rai::test_genesis_key (globals.test_genesis_key);
  96. rai::account const & rai::rai_test_account (globals.rai_test_account);
  97. rai::account const & rai::rai_beta_account (globals.rai_beta_account);
  98. rai::account const & rai::rai_live_account (globals.rai_live_account);
  99. std::string const & rai::rai_test_genesis (globals.rai_test_genesis);
  100. std::string const & rai::rai_beta_genesis (globals.rai_beta_genesis);
  101. std::string const & rai::rai_live_genesis (globals.rai_live_genesis);
  102.  
  103. rai::account const & rai::genesis_account (globals.genesis_account);
  104. std::string const & rai::genesis_block (globals.genesis_block);
  105. rai::uint128_t const & rai::genesis_amount (globals.genesis_amount);
  106. rai::block_hash const & rai::not_a_block (globals.not_a_block);
  107. rai::block_hash const & rai::not_an_account (globals.not_an_account);
  108. rai::account const & rai::burn_account (globals.burn_account);
  109.  
  110. rai::votes::votes (std::shared_ptr<rai::block> block_a) :
  111. id (block_a->root ())
  112. {
  113. rep_votes.insert (std::make_pair (rai::not_an_account, block_a));
  114. }
  115.  
  116. rai::tally_result rai::votes::vote (std::shared_ptr<rai::vote> vote_a)
  117. {
  118. rai::tally_result result;
  119. auto existing (rep_votes.find (vote_a->account));
  120. if (existing == rep_votes.end ())
  121. {
  122. // Vote on this block hasn't been seen from rep before
  123. result = rai::tally_result::vote;
  124. rep_votes.insert (std::make_pair (vote_a->account, vote_a->block));
  125. }
  126. else
  127. {
  128. if (!(*existing->second == *vote_a->block))
  129. {
  130. // Rep changed their vote
  131. result = rai::tally_result::changed;
  132. existing->second = vote_a->block;
  133. }
  134. else
  135. {
  136. // Rep vote remained the same
  137. result = rai::tally_result::confirm;
  138. }
  139. }
  140. return result;
  141. }
  142.  
  143. // Create a new random keypair
  144. rai::keypair::keypair ()
  145. {
  146. random_pool.GenerateBlock (prv.data.bytes.data (), prv.data.bytes.size ());
  147. ed25519_publickey (prv.data.bytes.data (), pub.bytes.data ());
  148. }
  149.  
  150. // Create a keypair given a hex string of the private key
  151. rai::keypair::keypair (std::string const & prv_a)
  152. {
  153. auto error (prv.data.decode_hex (prv_a));
  154. assert (!error);
  155. ed25519_publickey (prv.data.bytes.data (), pub.bytes.data ());
  156. }
  157.  
  158. // Serialize a block prefixed with an 8-bit typecode
  159. void rai::serialize_block (rai::stream & stream_a, rai::block const & block_a)
  160. {
  161. write (stream_a, block_a.type ());
  162. block_a.serialize (stream_a);
  163. }
  164.  
  165. std::unique_ptr<rai::block> rai::deserialize_block (MDB_val const & val_a)
  166. {
  167. rai::bufferstream stream (reinterpret_cast<uint8_t const *> (val_a.mv_data), val_a.mv_size);
  168. return deserialize_block (stream);
  169. }
  170.  
  171. rai::account_info::account_info () :
  172. head (0),
  173. rep_block (0),
  174. open_block (0),
  175. balance (0),
  176. modified (0),
  177. block_count (0)
  178. {
  179. }
  180.  
  181. rai::account_info::account_info (MDB_val const & val_a)
  182. {
  183. assert (val_a.mv_size == sizeof (*this));
  184. static_assert (sizeof (head) + sizeof (rep_block) + sizeof (open_block) + sizeof (balance) + sizeof (modified) + sizeof (block_count) == sizeof (*this), "Class not packed");
  185. std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
  186. }
  187.  
  188. rai::account_info::account_info (rai::block_hash const & head_a, rai::block_hash const & rep_block_a, rai::block_hash const & open_block_a, rai::amount const & balance_a, uint64_t modified_a, uint64_t block_count_a) :
  189. head (head_a),
  190. rep_block (rep_block_a),
  191. open_block (open_block_a),
  192. balance (balance_a),
  193. modified (modified_a),
  194. block_count (block_count_a)
  195. {
  196. }
  197.  
  198. void rai::account_info::serialize (rai::stream & stream_a) const
  199. {
  200. write (stream_a, head.bytes);
  201. write (stream_a, rep_block.bytes);
  202. write (stream_a, open_block.bytes);
  203. write (stream_a, balance.bytes);
  204. write (stream_a, modified);
  205. write (stream_a, block_count);
  206. }
  207.  
  208. bool rai::account_info::deserialize (rai::stream & stream_a)
  209. {
  210. auto error (read (stream_a, head.bytes));
  211. if (!error)
  212. {
  213. error = read (stream_a, rep_block.bytes);
  214. if (!error)
  215. {
  216. error = read (stream_a, open_block.bytes);
  217. if (!error)
  218. {
  219. error = read (stream_a, balance.bytes);
  220. if (!error)
  221. {
  222. error = read (stream_a, modified);
  223. if (!error)
  224. {
  225. error = read (stream_a, block_count);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. return error;
  232. }
  233.  
  234. bool rai::account_info::operator== (rai::account_info const & other_a) const
  235. {
  236. return head == other_a.head && rep_block == other_a.rep_block && open_block == other_a.open_block && balance == other_a.balance && modified == other_a.modified && block_count == other_a.block_count;
  237. }
  238.  
  239. bool rai::account_info::operator!= (rai::account_info const & other_a) const
  240. {
  241. return !(*this == other_a);
  242. }
  243.  
  244. rai::mdb_val rai::account_info::val () const
  245. {
  246. return rai::mdb_val (sizeof (*this), const_cast<rai::account_info *> (this));
  247. }
  248.  
  249. rai::block_counts::block_counts () :
  250. send (0),
  251. receive (0),
  252. open (0),
  253. change (0)
  254. {
  255. }
  256.  
  257. size_t rai::block_counts::sum ()
  258. {
  259. return send + receive + open + change + state;
  260. }
  261.  
  262. rai::pending_info::pending_info () :
  263. source (0),
  264. amount (0)
  265. {
  266. }
  267.  
  268. rai::pending_info::pending_info (MDB_val const & val_a)
  269. {
  270. assert (val_a.mv_size == sizeof (*this));
  271. static_assert (sizeof (source) + sizeof (amount) == sizeof (*this), "Packed class");
  272. std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
  273. }
  274.  
  275. rai::pending_info::pending_info (rai::account const & source_a, rai::amount const & amount_a) :
  276. source (source_a),
  277. amount (amount_a)
  278. {
  279. }
  280.  
  281. void rai::pending_info::serialize (rai::stream & stream_a) const
  282. {
  283. rai::write (stream_a, source.bytes);
  284. rai::write (stream_a, amount.bytes);
  285. }
  286.  
  287. bool rai::pending_info::deserialize (rai::stream & stream_a)
  288. {
  289. auto result (rai::read (stream_a, source.bytes));
  290. if (!result)
  291. {
  292. result = rai::read (stream_a, amount.bytes);
  293. }
  294. return result;
  295. }
  296.  
  297. bool rai::pending_info::operator== (rai::pending_info const & other_a) const
  298. {
  299. return source == other_a.source && amount == other_a.amount;
  300. }
  301.  
  302. rai::mdb_val rai::pending_info::val () const
  303. {
  304. return rai::mdb_val (sizeof (*this), const_cast<rai::pending_info *> (this));
  305. }
  306.  
  307. rai::pending_key::pending_key (rai::account const & account_a, rai::block_hash const & hash_a) :
  308. account (account_a),
  309. hash (hash_a)
  310. {
  311. }
  312.  
  313. rai::pending_key::pending_key (MDB_val const & val_a)
  314. {
  315. assert (val_a.mv_size == sizeof (*this));
  316. static_assert (sizeof (account) + sizeof (hash) == sizeof (*this), "Packed class");
  317. std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
  318. }
  319.  
  320. void rai::pending_key::serialize (rai::stream & stream_a) const
  321. {
  322. rai::write (stream_a, account.bytes);
  323. rai::write (stream_a, hash.bytes);
  324. }
  325.  
  326. bool rai::pending_key::deserialize (rai::stream & stream_a)
  327. {
  328. auto error (rai::read (stream_a, account.bytes));
  329. if (!error)
  330. {
  331. error = rai::read (stream_a, hash.bytes);
  332. }
  333. return error;
  334. }
  335.  
  336. bool rai::pending_key::operator== (rai::pending_key const & other_a) const
  337. {
  338. return account == other_a.account && hash == other_a.hash;
  339. }
  340.  
  341. rai::mdb_val rai::pending_key::val () const
  342. {
  343. return rai::mdb_val (sizeof (*this), const_cast<rai::pending_key *> (this));
  344. }
  345.  
  346. rai::block_info::block_info () :
  347. account (0),
  348. balance (0)
  349. {
  350. }
  351.  
  352. rai::block_info::block_info (MDB_val const & val_a)
  353. {
  354. assert (val_a.mv_size == sizeof (*this));
  355. static_assert (sizeof (account) + sizeof (balance) == sizeof (*this), "Packed class");
  356. std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
  357. }
  358.  
  359. rai::block_info::block_info (rai::account const & account_a, rai::amount const & balance_a) :
  360. account (account_a),
  361. balance (balance_a)
  362. {
  363. }
  364.  
  365. void rai::block_info::serialize (rai::stream & stream_a) const
  366. {
  367. rai::write (stream_a, account.bytes);
  368. rai::write (stream_a, balance.bytes);
  369. }
  370.  
  371. bool rai::block_info::deserialize (rai::stream & stream_a)
  372. {
  373. auto error (rai::read (stream_a, account.bytes));
  374. if (!error)
  375. {
  376. error = rai::read (stream_a, balance.bytes);
  377. }
  378. return error;
  379. }
  380.  
  381. bool rai::block_info::operator== (rai::block_info const & other_a) const
  382. {
  383. return account == other_a.account && balance == other_a.balance;
  384. }
  385.  
  386. rai::mdb_val rai::block_info::val () const
  387. {
  388. return rai::mdb_val (sizeof (*this), const_cast<rai::block_info *> (this));
  389. }
  390.  
  391. bool rai::vote::operator== (rai::vote const & other_a) const
  392. {
  393. return sequence == other_a.sequence && *block == *other_a.block && account == other_a.account && signature == other_a.signature;
  394. }
  395.  
  396. bool rai::vote::operator!= (rai::vote const & other_a) const
  397. {
  398. return !(*this == other_a);
  399. }
  400.  
  401. std::string rai::vote::to_json () const
  402. {
  403. std::stringstream stream;
  404. boost::property_tree::ptree tree;
  405. tree.put ("account", account.to_account ());
  406. tree.put ("signature", signature.number ());
  407. tree.put ("sequence", std::to_string (sequence));
  408. tree.put ("block", block->to_json ());
  409. boost::property_tree::write_json (stream, tree);
  410. return stream.str ();
  411. }
  412.  
  413. rai::amount_visitor::amount_visitor (MDB_txn * transaction_a, rai::block_store & store_a) :
  414. transaction (transaction_a),
  415. store (store_a)
  416. {
  417. }
  418.  
  419. void rai::amount_visitor::send_block (rai::send_block const & block_a)
  420. {
  421. balance_visitor prev (transaction, store);
  422. prev.compute (block_a.hashables.previous);
  423. result = prev.result - block_a.hashables.balance.number ();
  424. }
  425.  
  426. void rai::amount_visitor::receive_block (rai::receive_block const & block_a)
  427. {
  428. from_send (block_a.hashables.source);
  429. }
  430.  
  431. void rai::amount_visitor::open_block (rai::open_block const & block_a)
  432. {
  433. if (block_a.hashables.source != rai::genesis_account)
  434. {
  435. from_send (block_a.hashables.source);
  436. }
  437. else
  438. {
  439. result = rai::genesis_amount;
  440. }
  441. }
  442.  
  443. void rai::amount_visitor::state_block (rai::state_block const & block_a)
  444. {
  445. balance_visitor prev (transaction, store);
  446. prev.compute (block_a.hashables.previous);
  447. result = block_a.hashables.balance.number ();
  448. result = result < prev.result ? prev.result - result : result - prev.result;
  449. }
  450.  
  451. void rai::amount_visitor::change_block (rai::change_block const & block_a)
  452. {
  453. result = 0;
  454. }
  455.  
  456. void rai::amount_visitor::from_send (rai::block_hash const & hash_a)
  457. {
  458. auto source_block (store.block_get (transaction, hash_a));
  459. assert (source_block != nullptr);
  460. source_block->visit (*this);
  461. }
  462.  
  463. void rai::amount_visitor::compute (rai::block_hash const & block_hash)
  464. {
  465. auto block (store.block_get (transaction, block_hash));
  466. if (block != nullptr)
  467. {
  468. block->visit (*this);
  469. }
  470. else
  471. {
  472. if (block_hash == rai::genesis_account)
  473. {
  474. result = rai::genesis_amount;
  475. }
  476. else
  477. {
  478. assert (false);
  479. result = 0;
  480. }
  481. }
  482. }
  483.  
  484. rai::balance_visitor::balance_visitor (MDB_txn * transaction_a, rai::block_store & store_a) :
  485. transaction (transaction_a),
  486. store (store_a),
  487. current (0),
  488. result (0)
  489. {
  490. }
  491.  
  492. void rai::balance_visitor::send_block (rai::send_block const & block_a)
  493. {
  494. result += block_a.hashables.balance.number ();
  495. current = 0;
  496. }
  497.  
  498. void rai::balance_visitor::receive_block (rai::receive_block const & block_a)
  499. {
  500. amount_visitor source (transaction, store);
  501. source.compute (block_a.hashables.source);
  502. rai::block_info block_info;
  503. if (!store.block_info_get (transaction, block_a.hash (), block_info))
  504. {
  505. result += block_info.balance.number ();
  506. current = 0;
  507. }
  508. else
  509. {
  510. result += source.result;
  511. current = block_a.hashables.previous;
  512. }
  513. }
  514.  
  515. void rai::balance_visitor::open_block (rai::open_block const & block_a)
  516. {
  517. amount_visitor source (transaction, store);
  518. source.compute (block_a.hashables.source);
  519. result += source.result;
  520. current = 0;
  521. }
  522.  
  523. void rai::balance_visitor::change_block (rai::change_block const & block_a)
  524. {
  525. rai::block_info block_info;
  526. if (!store.block_info_get (transaction, block_a.hash (), block_info))
  527. {
  528. result += block_info.balance.number ();
  529. current = 0;
  530. }
  531. else
  532. {
  533. current = block_a.hashables.previous;
  534. }
  535. }
  536.  
  537. void rai::balance_visitor::state_block (rai::state_block const & block_a)
  538. {
  539. result = block_a.hashables.balance.number ();
  540. current = 0;
  541. }
  542.  
  543. void rai::balance_visitor::compute (rai::block_hash const & block_hash)
  544. {
  545. current = block_hash;
  546. while (!current.is_zero ())
  547. {
  548. auto block (store.block_get (transaction, current));
  549. assert (block != nullptr);
  550. block->visit (*this);
  551. }
  552. }
  553.  
  554. rai::representative_visitor::representative_visitor (MDB_txn * transaction_a, rai::block_store & store_a) :
  555. transaction (transaction_a),
  556. store (store_a),
  557. result (0)
  558. {
  559. }
  560.  
  561. void rai::representative_visitor::compute (rai::block_hash const & hash_a)
  562. {
  563. current = hash_a;
  564. while (result.is_zero ())
  565. {
  566. auto block (store.block_get (transaction, current));
  567. assert (block != nullptr);
  568. block->visit (*this);
  569. }
  570. }
  571.  
  572. void rai::representative_visitor::send_block (rai::send_block const & block_a)
  573. {
  574. current = block_a.previous ();
  575. }
  576.  
  577. void rai::representative_visitor::receive_block (rai::receive_block const & block_a)
  578. {
  579. current = block_a.previous ();
  580. }
  581.  
  582. void rai::representative_visitor::open_block (rai::open_block const & block_a)
  583. {
  584. result = block_a.hash ();
  585. }
  586.  
  587. void rai::representative_visitor::change_block (rai::change_block const & block_a)
  588. {
  589. result = block_a.hash ();
  590. }
  591.  
  592. void rai::representative_visitor::state_block (rai::state_block const & block_a)
  593. {
  594. result = block_a.hash ();
  595. }
  596.  
  597. rai::vote::vote (rai::vote const & other_a) :
  598. sequence (other_a.sequence),
  599. block (other_a.block),
  600. account (other_a.account),
  601. signature (other_a.signature)
  602. {
  603. }
  604.  
  605. rai::vote::vote (bool & error_a, rai::stream & stream_a)
  606. {
  607. if (!error_a)
  608. {
  609. error_a = rai::read (stream_a, account.bytes);
  610. if (!error_a)
  611. {
  612. error_a = rai::read (stream_a, signature.bytes);
  613. if (!error_a)
  614. {
  615. error_a = rai::read (stream_a, sequence);
  616. if (!error_a)
  617. {
  618. block = rai::deserialize_block (stream_a);
  619. error_a = block == nullptr;
  620. }
  621. }
  622. }
  623. }
  624. }
  625.  
  626. rai::vote::vote (bool & error_a, rai::stream & stream_a, rai::block_type type_a)
  627. {
  628. if (!error_a)
  629. {
  630. error_a = rai::read (stream_a, account.bytes);
  631. if (!error_a)
  632. {
  633. error_a = rai::read (stream_a, signature.bytes);
  634. if (!error_a)
  635. {
  636. error_a = rai::read (stream_a, sequence);
  637. if (!error_a)
  638. {
  639. block = rai::deserialize_block (stream_a, type_a);
  640. error_a = block == nullptr;
  641. }
  642. }
  643. }
  644. }
  645. }
  646.  
  647. rai::vote::vote (rai::account const & account_a, rai::raw_key const & prv_a, uint64_t sequence_a, std::shared_ptr<rai::block> block_a) :
  648. sequence (sequence_a),
  649. block (block_a),
  650. account (account_a),
  651. signature (rai::sign_message (prv_a, account_a, hash ()))
  652. {
  653. }
  654.  
  655. rai::vote::vote (MDB_val const & value_a)
  656. {
  657. rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value_a.mv_data), value_a.mv_size);
  658. auto error (rai::read (stream, account.bytes));
  659. assert (!error);
  660. error = rai::read (stream, signature.bytes);
  661. assert (!error);
  662. error = rai::read (stream, sequence);
  663. assert (!error);
  664. block = rai::deserialize_block (stream);
  665. assert (block != nullptr);
  666. }
  667.  
  668. rai::uint256_union rai::vote::hash () const
  669. {
  670. rai::uint256_union result;
  671. blake2b_state hash;
  672. blake2b_init (&hash, sizeof (result.bytes));
  673. blake2b_update (&hash, block->hash ().bytes.data (), sizeof (result.bytes));
  674. union
  675. {
  676. uint64_t qword;
  677. std::array<uint8_t, 8> bytes;
  678. };
  679. qword = sequence;
  680. blake2b_update (&hash, bytes.data (), sizeof (bytes));
  681. blake2b_final (&hash, result.bytes.data (), sizeof (result.bytes));
  682. return result;
  683. }
  684.  
  685. void rai::vote::serialize (rai::stream & stream_a, rai::block_type)
  686. {
  687. write (stream_a, account);
  688. write (stream_a, signature);
  689. write (stream_a, sequence);
  690. block->serialize (stream_a);
  691. }
  692.  
  693. void rai::vote::serialize (rai::stream & stream_a)
  694. {
  695. write (stream_a, account);
  696. write (stream_a, signature);
  697. write (stream_a, sequence);
  698. rai::serialize_block (stream_a, *block);
  699. }
  700.  
  701. rai::genesis::genesis ()
  702. {
  703. boost::property_tree::ptree tree;
  704. std::stringstream istream (rai::genesis_block);
  705. boost::property_tree::read_json (istream, tree);
  706. auto block (rai::deserialize_block_json (tree));
  707. assert (dynamic_cast<rai::open_block *> (block.get ()) != nullptr);
  708. open.reset (static_cast<rai::open_block *> (block.release ()));
  709. }
  710.  
  711. void rai::genesis::initialize (MDB_txn * transaction_a, rai::block_store & store_a) const
  712. {
  713. auto hash_l (hash ());
  714. assert (store_a.latest_begin (transaction_a) == store_a.latest_end ());
  715. store_a.block_put (transaction_a, hash_l, *open);
  716. store_a.account_put (transaction_a, genesis_account, { hash_l, open->hash (), open->hash (), rai::genesis_amount, rai::seconds_since_epoch (), 1 });
  717. store_a.representation_put (transaction_a, genesis_account, rai::genesis_amount);
  718. store_a.checksum_put (transaction_a, 0, 0, hash_l);
  719. store_a.frontier_put (transaction_a, hash_l, genesis_account);
  720. }
  721.  
  722. rai::block_hash rai::genesis::hash () const
  723. {
  724. return open->hash ();
  725. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement