Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.86 KB | None | 0 0
  1. // Copyright (c) 2017, SUMOKOIN
  2. // Copyright (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. #include "include_base_utils.h"
  33. using namespace epee;
  34.  
  35. #include "cryptonote_basic_impl.h"
  36. #include "string_tools.h"
  37. #include "serialization/binary_utils.h"
  38. #include "serialization/vector.h"
  39. #include "cryptonote_format_utils.h"
  40. #include "cryptonote_config.h"
  41. #include "misc_language.h"
  42. #include "common/base58.h"
  43. #include "crypto/hash.h"
  44. #include "common/int-util.h"
  45. #include "common/dns_utils.h"
  46.  
  47.  
  48. namespace cryptonote {
  49.  
  50. struct integrated_address {
  51. account_public_address adr;
  52. crypto::hash8 payment_id;
  53.  
  54. BEGIN_SERIALIZE_OBJECT()
  55. FIELD(adr)
  56. FIELD(payment_id)
  57. END_SERIALIZE()
  58.  
  59. BEGIN_KV_SERIALIZE_MAP()
  60. KV_SERIALIZE(adr)
  61. KV_SERIALIZE(payment_id)
  62. END_KV_SERIALIZE_MAP()
  63. };
  64.  
  65. /************************************************************************/
  66. /* Cryptonote helper functions */
  67. /************************************************************************/
  68. //-----------------------------------------------------------------------------------------------
  69. size_t get_max_block_size()
  70. {
  71. return CRYPTONOTE_MAX_BLOCK_SIZE;
  72. }
  73. //-----------------------------------------------------------------------------------------------
  74. size_t get_max_tx_size()
  75. {
  76. return CRYPTONOTE_MAX_TX_SIZE;
  77. }
  78. //-----------------------------------------------------------------------------------------------
  79. bool get_block_reward(size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward, uint64_t height) {
  80.  
  81. uint64_t base_reward;
  82. uint64_t round_factor = 10000000; // 1 * pow(10, 7)
  83. uint64_t TOKEN_SUPPLY = version =< 3 ? MONEY_SUPPLY : TOKENS;
  84.  
  85. const uint64_t airdrop = CLASSIC_BLOCK_REWARD;
  86. if (height == ActualForkHeight) {
  87. reward = airdrop;
  88. return true;
  89. }
  90. if (height > 0)
  91. {
  92. if (height < (PEAK_COIN_EMISSION_HEIGHT + COIN_EMISSION_HEIGHT_INTERVAL)) {
  93. uint64_t interval_num = height / COIN_EMISSION_HEIGHT_INTERVAL;
  94. double money_supply_pct = 0.1888 + interval_num*(0.023 + interval_num*0.0032);
  95. base_reward = ((uint64_t)(TOKEN_SUPPLY * money_supply_pct)) >> EMISSION_SPEED_FACTOR;
  96. }
  97. else{
  98. base_reward = (TOKEN_SUPPLY - already_generated_coins) >> EMISSION_SPEED_FACTOR;
  99. }
  100. }
  101. else
  102. {
  103. base_reward = GENESIS_BLOCK_REWARD;
  104. }
  105.  
  106. if (base_reward < FINAL_SUBSIDY){
  107. if (TOKEN_SUPPLY > already_generated_coins){
  108. base_reward = FINAL_SUBSIDY;
  109. }
  110. else{
  111. base_reward = FINAL_SUBSIDY/2;
  112. }
  113. }
  114.  
  115. // rounding (floor) base reward
  116. base_reward = base_reward / round_factor * round_factor;
  117.  
  118. //make it soft
  119. if (median_size < CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE) {
  120. median_size = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE;
  121. }
  122.  
  123. if (current_block_size > 2 * median_size) {
  124. LOG_PRINT_L1("Block cumulative size is too big: " << current_block_size << ", expected less than " << 2 * median_size);
  125. return false;
  126. }
  127.  
  128. if (current_block_size <= (median_size < BLOCK_SIZE_GROWTH_FAVORED_ZONE ? median_size * 110 / 100 : median_size)) {
  129. reward = base_reward;
  130. return true;
  131. }
  132.  
  133. assert(median_size < std::numeric_limits<uint32_t>::max());
  134. assert(current_block_size < std::numeric_limits<uint32_t>::max());
  135.  
  136. uint64_t product_hi;
  137. // BUGFIX: 32-bit saturation bug (e.g. ARM7), the result was being
  138. // treated as 32-bit by default.
  139. uint64_t multiplicand = 2 * median_size - current_block_size;
  140. multiplicand *= current_block_size;
  141. uint64_t product_lo = mul128(base_reward, multiplicand, &product_hi);
  142.  
  143. uint64_t reward_hi;
  144. uint64_t reward_lo;
  145. div128_32(product_hi, product_lo, static_cast<uint32_t>(median_size), &reward_hi, &reward_lo);
  146. div128_32(reward_hi, reward_lo, static_cast<uint32_t>(median_size), &reward_hi, &reward_lo);
  147. assert(0 == reward_hi);
  148. assert(reward_lo < base_reward);
  149.  
  150. reward = reward_lo;
  151. return true;
  152. }
  153. //------------------------------------------------------------------------------------
  154. uint8_t get_account_address_checksum(const public_address_outer_blob& bl)
  155. {
  156. const unsigned char* pbuf = reinterpret_cast<const unsigned char*>(&bl);
  157. uint8_t summ = 0;
  158. for(size_t i = 0; i!= sizeof(public_address_outer_blob)-1; i++)
  159. summ += pbuf[i];
  160.  
  161. return summ;
  162. }
  163. //------------------------------------------------------------------------------------
  164. uint8_t get_account_integrated_address_checksum(const public_integrated_address_outer_blob& bl)
  165. {
  166. const unsigned char* pbuf = reinterpret_cast<const unsigned char*>(&bl);
  167. uint8_t summ = 0;
  168. for(size_t i = 0; i!= sizeof(public_integrated_address_outer_blob)-1; i++)
  169. summ += pbuf[i];
  170.  
  171. return summ;
  172. }
  173. //-----------------------------------------------------------------------
  174. std::string get_account_address_as_str(
  175. bool testnet
  176. , bool subaddress
  177. , account_public_address const & adr
  178. )
  179. {
  180. uint64_t address_prefix = testnet ?
  181. (subaddress ? config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX) :
  182. (subaddress ? config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX);
  183.  
  184. return tools::base58::encode_addr(address_prefix, t_serializable_object_to_blob(adr));
  185. }
  186. //-----------------------------------------------------------------------
  187. std::string get_account_integrated_address_as_str(
  188. bool testnet
  189. , bool subaddress
  190. , account_public_address const & adr
  191. , crypto::hash8 const & payment_id
  192. )
  193. {
  194. uint64_t integrated_address_prefix = testnet ? config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX;
  195.  
  196. integrated_address iadr = {
  197. adr, payment_id
  198. };
  199. return tools::base58::encode_addr(integrated_address_prefix, t_serializable_object_to_blob(iadr));
  200. }
  201. //-----------------------------------------------------------------------
  202. bool is_coinbase(const transaction& tx)
  203. {
  204. if(tx.vin.size() != 1)
  205. return false;
  206.  
  207. if(tx.vin[0].type() != typeid(txin_gen))
  208. return false;
  209.  
  210. return true;
  211. }
  212. //-----------------------------------------------------------------------
  213. bool get_account_address_from_str(
  214. address_parse_info& info
  215. , bool testnet
  216. , std::string const & str
  217. )
  218. {
  219. uint64_t address_prefix = testnet ?
  220. config::testnet::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX;
  221. uint64_t integrated_address_prefix = testnet ?
  222. config::testnet::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_INTEGRATED_ADDRESS_BASE58_PREFIX;
  223. uint64_t subaddress_prefix = testnet ?
  224. config::testnet::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX : config::CRYPTONOTE_PUBLIC_SUBADDRESS_BASE58_PREFIX;
  225.  
  226. if (2 * sizeof(public_address_outer_blob) != str.size())
  227. {
  228. blobdata data;
  229. uint64_t prefix;
  230. if (!tools::base58::decode_addr(str, prefix, data))
  231. {
  232. LOG_PRINT_L2("Invalid address format");
  233. return false;
  234. }
  235.  
  236. if (integrated_address_prefix == prefix)
  237. {
  238. info.is_subaddress = false;
  239. info.has_payment_id = true;
  240. }
  241. else if (address_prefix == prefix)
  242. {
  243. info.is_subaddress = false;
  244. info.has_payment_id = false;
  245. }
  246. else if (subaddress_prefix == prefix)
  247. {
  248. info.is_subaddress = true;
  249. info.has_payment_id = false;
  250. }
  251. else {
  252. LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << address_prefix
  253. << " or " << integrated_address_prefix
  254. << " or " << subaddress_prefix);
  255. return false;
  256. }
  257.  
  258. if (info.has_payment_id)
  259. {
  260. integrated_address iadr;
  261. if (!::serialization::parse_binary(data, iadr))
  262. {
  263. LOG_PRINT_L1("Account public address keys can't be parsed");
  264. return false;
  265. }
  266. info.address = iadr.adr;
  267. info.payment_id = iadr.payment_id;
  268. }
  269. else
  270. {
  271. if (!::serialization::parse_binary(data, info.address))
  272. {
  273. LOG_PRINT_L1("Account public address keys can't be parsed");
  274. return false;
  275. }
  276. }
  277.  
  278. if (!crypto::check_key(info.address.m_spend_public_key) || !crypto::check_key(info.address.m_view_public_key))
  279. {
  280. LOG_PRINT_L1("Failed to validate address keys");
  281. return false;
  282. }
  283. }
  284. else
  285. {
  286. // Old address format
  287. std::string buff;
  288. if (!string_tools::parse_hexstr_to_binbuff(str, buff))
  289. return false;
  290.  
  291. if (buff.size() != sizeof(public_address_outer_blob))
  292. {
  293. LOG_PRINT_L1("Wrong public address size: " << buff.size() << ", expected size: " << sizeof(public_address_outer_blob));
  294. return false;
  295. }
  296.  
  297. public_address_outer_blob blob = *reinterpret_cast<const public_address_outer_blob*>(buff.data());
  298.  
  299.  
  300. if (blob.m_ver > CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER)
  301. {
  302. LOG_PRINT_L1("Unknown version of public address: " << blob.m_ver << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER);
  303. return false;
  304. }
  305.  
  306. if (blob.check_sum != get_account_address_checksum(blob))
  307. {
  308. LOG_PRINT_L1("Wrong public address checksum");
  309. return false;
  310. }
  311.  
  312. //we success
  313. info.address = blob.m_address;
  314. info.is_subaddress = false;
  315. info.has_payment_id = false;
  316. }
  317.  
  318. return true;
  319. }
  320. //--------------------------------------------------------------------------------
  321. bool get_account_address_from_str_or_url(
  322. address_parse_info& info
  323. , bool testnet
  324. , const std::string& str_or_url
  325. , bool cli_confirm
  326. )
  327. {
  328. if (get_account_address_from_str(info, testnet, str_or_url))
  329. return true;
  330. bool dnssec_valid;
  331. std::string address_str = tools::dns_utils::get_account_address_as_str_from_url(str_or_url, dnssec_valid, cli_confirm);
  332. return !address_str.empty() &&
  333. get_account_address_from_str(info, testnet, address_str);
  334. }
  335.  
  336. bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b) {
  337. return cryptonote::get_transaction_hash(a) == cryptonote::get_transaction_hash(b);
  338. }
  339.  
  340. bool operator ==(const cryptonote::block& a, const cryptonote::block& b) {
  341. return cryptonote::get_block_hash(a) == cryptonote::get_block_hash(b);
  342. }
  343. }
  344.  
  345. //--------------------------------------------------------------------------------
  346. bool parse_hash256(const std::string str_hash, crypto::hash& hash)
  347. {
  348. std::string buf;
  349. bool res = epee::string_tools::parse_hexstr_to_binbuff(str_hash, buf);
  350. if (!res || buf.size() != sizeof(crypto::hash))
  351. {
  352. std::cout << "invalid hash format: <" << str_hash << '>' << std::endl;
  353. return false;
  354. }
  355. else
  356. {
  357. buf.copy(reinterpret_cast<char *>(&hash), sizeof(crypto::hash));
  358. return true;
  359. }
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement