github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/generator/dip_ir_generator.cpp (about) 1 // Copyright (C) 2018 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or 6 // modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // (at your option) any later version. 10 // 11 // the go-nebulas library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with the go-nebulas library. If not, see 18 // <http://www.gnu.org/licenses/>. 19 // 20 #include "cmd/dummy_neb/generator/dip_ir_generator.h" 21 #include "fs/proto/ir.pb.h" 22 #include <sstream> 23 24 static std::string dip_slice = 25 "#include \"runtime/dip/dip_impl.h\" \n" 26 "\n" 27 "extern neb::rt::nr::nr_ret_type \n" 28 "entry_point_nr(neb::compatible_uint64_t " 29 "start_block, \n" 30 " neb::compatible_uint64_t end_block); \n" 31 "\n" 32 "neb::rt::dip::dip_ret_type entry_point_dip(neb::compatible_uint64_t " 33 "height) { \n" 34 " neb::rt::dip::dip_ret_type ret; \n" 35 " std::get<0>(ret) = 0; \n" 36 "\n" 37 " uint64_t block_nums_of_a_day = VAR_block_nums_of_a_day; \n" 38 " uint64_t days = VAR_days; \n" 39 " neb::compatible_uint64_t dip_start_block = VAR_dip_start_block; \n" 40 " neb::compatible_uint64_t block_interval = days * block_nums_of_a_day; \n" 41 " std::string reward_addr = \"VAR_reward_addr\"; \n" 42 " std::string coinbase_addr = \"VAR_coinbase_addr\"; \n" 43 "\n" 44 " auto to_version_t = [](uint32_t major_version, uint16_t minor_version, " 45 "\n" 46 " uint16_t patch_version) -> " 47 "neb::rt::dip::version_t { \n" 48 " return (0ULL + major_version) + ((0ULL + minor_version) << 32) + \n" 49 " ((0ULL + patch_version) << 48); \n" 50 " }; \n" 51 "\n" 52 " if (!height) { \n" 53 " std::get<1>(ret) = neb::rt::dip::dip_param_list( \n" 54 " dip_start_block, block_interval, reward_addr, coinbase_addr, \n" 55 " to_version_t(VERSION_major, VERSION_minor, VERSION_patch)); \n" 56 " return ret; \n" 57 " } \n" 58 "\n" 59 " if (height < dip_start_block + block_interval) { \n" 60 " std::get<1>(ret) = std::string(\"{\\\"err\\\":\\\"invalid " 61 "height\\\"}\"); \n" 62 " return ret; \n" 63 " } \n" 64 "\n" 65 " uint64_t interval_nums = (height - dip_start_block) / block_interval; \n" 66 " neb::compatible_uint64_t s = dip_start_block + block_interval * " 67 "interval_nums; \n" 68 " neb::compatible_uint64_t e = s - 1; \n" 69 " s -= block_interval; \n" 70 "\n" 71 " auto nr_ret = entry_point_nr(s, e); \n" 72 "\n" 73 " neb::rt::dip::dip_float_t alpha = VAR_alpha; \n" 74 " neb::rt::dip::dip_float_t beta = VAR_beta; \n" 75 " return neb::rt::dip::entry_point_dip_impl(s, e, " 76 "to_version_t(VERSION_major, VERSION_minor, VERSION_patch), height, " 77 "nr_ret, VAR_alpha, VAR_beta); \n" 78 "} \n"; 79 80 std::string gen_dip_with_params(uint64_t block_nums_of_a_day, uint64_t days, 81 uint64_t dip_start_block, 82 const std::string &reward_addr, 83 const std::string &coinbase_addr, float alpha, 84 float beta, int32_t major_version, 85 int32_t minor_version, int32_t patch_version) { 86 std::string ret = dip_slice; 87 std::stringstream ss; 88 #define R(t, name) \ 89 ss << t; \ 90 boost::replace_all(ret, name, std::to_string(t)); \ 91 ss.clear(); 92 93 R(block_nums_of_a_day, "VAR_block_nums_of_a_day"); 94 R(days, "VAR_days"); 95 R(dip_start_block, "VAR_dip_start_block"); 96 R(alpha, "VAR_alpha"); 97 R(beta, "VAR_beta"); 98 R(major_version, "VERSION_major"); 99 R(minor_version, "VERSION_minor"); 100 R(patch_version, "VERSION_patch"); 101 #undef R 102 103 #define R_STR(t, name) \ 104 ss << t; \ 105 boost::replace_all(ret, name, t); \ 106 ss.clear(); 107 R_STR(reward_addr, "VAR_reward_addr"); 108 R_STR(coinbase_addr, "VAR_coinbase_addr"); 109 #undef R_STR 110 111 return ret; 112 } 113 114 dip_ir_generator::dip_ir_generator(generate_block *block, const address_t &addr) 115 : generator_base(block->get_all_accounts(), block, 0, 1), 116 m_dip_admin_addr(addr) { 117 m_block_nums_of_a_day = 5; 118 m_days = 1; 119 m_dip_start_block = 15; 120 m_alpha = 8e-3; 121 m_beta = 1; 122 m_reward_addr = m_dip_admin_addr.to_base58(); 123 m_coinbase_addr = m_dip_admin_addr.to_base58(); 124 } 125 126 dip_ir_generator::~dip_ir_generator() {} 127 128 std::shared_ptr<corepb::Account> dip_ir_generator::gen_account() { 129 return nullptr; 130 } 131 std::shared_ptr<corepb::Transaction> dip_ir_generator::gen_tx() { 132 133 std::string payload = 134 gen_dip_with_params(m_block_nums_of_a_day, m_days, m_dip_start_block, 135 m_reward_addr, m_coinbase_addr, m_alpha, m_beta, 136 m_major_version, m_minor_version, m_patch_version); 137 nbre::NBREIR ir; 138 ir.set_name("dip"); 139 neb::version v(m_major_version, m_minor_version, m_patch_version); 140 ir.set_version(v.data()); 141 ir.set_height(m_block->height()); 142 ir.set_ir(payload); 143 ir.set_ir_type(neb::ir_type::cpp); 144 145 auto deps_ptr = ir.add_depends(); 146 deps_ptr->set_name("nr"); 147 deps_ptr->set_version(m_nr_version); 148 149 std::string ir_str = ir.SerializeAsString(); 150 return m_block->add_protocol_transaction(m_dip_admin_addr, 151 neb::string_to_byte(ir_str)); 152 } 153 checker_tasks::task_container_ptr_t dip_ir_generator::gen_tasks() { 154 return nullptr; 155 } 156