github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/generator/nr_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/nr_ir_generator.h"
    21  #include "fs/proto/ir.pb.h"
    22  #include <sstream>
    23  
    24  static std::string ir_slice =
    25      "#include \"runtime/nr/impl/nr_impl.h\" \n"
    26      "neb::rt::nr::nr_ret_type entry_point_nr(neb::compatible_uint64_t "
    27      "start_block, \n"
    28      "                           neb::compatible_uint64_t end_block) { \n"
    29      "  neb::rt::nr::nr_ret_type ret; \n"
    30      "  std::get<0>(ret) = 0; \n"
    31      "  if (start_block > end_block) { \n"
    32      "    std::get<1>(ret) = std::string(\"{\\\"err\\\":\\\"start height must "
    33      "less than end height\\\"}\"); \n"
    34      "    return ret; \n"
    35      "  } \n"
    36      "  uint64_t block_nums_of_a_day = 24 * 3600 / 15; \n"
    37      "  uint64_t days = 7; \n"
    38      "  uint64_t block_interval = days * block_nums_of_a_day; \n"
    39      "  if (start_block + block_interval < end_block) { \n"
    40      "    std::get<1>(ret) = std::string(\"{\\\"err\\\":\\\"nr block interval "
    41      "out of range\\\"}\"); \n"
    42      "    return ret; \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::nr::version_t { \n"
    48      "    return (0ULL + major_version) + ((0ULL + minor_version) << 32) + \n"
    49      "           ((0ULL + patch_version) << 48); \n"
    50      "  }; \n"
    51      "  neb::compatible_int64_t a = VAR_a; \n"
    52      "  neb::compatible_int64_t b = VAR_b; \n"
    53      "  neb::compatible_int64_t c = VAR_c; \n"
    54      "  neb::compatible_int64_t d = VAR_d; \n"
    55      "  neb::rt::nr::nr_float_t theta = VAR_theta; \n"
    56      "  neb::rt::nr::nr_float_t mu = VAR_mu; \n"
    57      "  neb::rt::nr::nr_float_t lambda = VAR_lambda; \n"
    58      "  return neb::rt::nr::entry_point_nr_impl(start_block, end_block, \n"
    59      "                                          to_version_t(VERSION_major, "
    60      "VERSION_minor, VERSION_patch), a, b, c, "
    61      "d, \n"
    62      "                                          theta, mu, lambda); \n"
    63      "} \n";
    64  
    65  std::string gen_ir_with_params(int64_t a, int64_t b, int64_t c, int64_t d,
    66                                 float theta, float mu, float lambda,
    67                                 int32_t major_version, int32_t minor_version,
    68                                 int32_t patch_version) {
    69    std::string ret = ir_slice;
    70    std::stringstream ss;
    71  #define R(t, name)                                                             \
    72    ss << t;                                                                     \
    73    boost::replace_all(ret, name, std::to_string(t));                            \
    74    ss.clear();
    75  
    76    R(a, "VAR_a");
    77    R(b, "VAR_b");
    78    R(c, "VAR_c");
    79    R(d, "VAR_d");
    80    R(theta, "VAR_theta");
    81    R(mu, "VAR_mu");
    82    R(lambda, "VAR_lambda");
    83    R(major_version, "VERSION_major");
    84    R(minor_version, "VERSION_minor");
    85    R(patch_version, "VERSION_patch");
    86  #undef R
    87  
    88    return ret;
    89  }
    90  
    91  nr_ir_generator::nr_ir_generator(generate_block *block, const address_t &addr)
    92      : generator_base(block->get_all_accounts(), block, 0, 1),
    93        m_nr_admin_addr(addr) {
    94    m_a = 100;
    95    m_b = 2;
    96    m_c = 6;
    97    m_d = -9;
    98    m_theta = 1;
    99    m_mu = 1;
   100    m_lambda = 2;
   101  }
   102  
   103  nr_ir_generator::~nr_ir_generator() {}
   104  
   105  std::shared_ptr<corepb::Account> nr_ir_generator::gen_account() {
   106    return nullptr;
   107  }
   108  std::shared_ptr<corepb::Transaction> nr_ir_generator::gen_tx() {
   109  
   110    std::string payload =
   111        gen_ir_with_params(m_a, m_b, m_c, m_d, m_theta, m_mu, m_lambda,
   112                           m_major_version, m_minor_version, m_patch_version);
   113    nbre::NBREIR ir;
   114    ir.set_name("nr");
   115    neb::version v(m_major_version, m_minor_version, m_patch_version);
   116    ir.set_version(v.data());
   117    ir.set_height(m_block->height());
   118    ir.set_ir(payload);
   119    ir.set_ir_type(neb::ir_type::cpp);
   120  
   121    std::string ir_str = ir.SerializeAsString();
   122    return m_block->add_protocol_transaction(m_nr_admin_addr,
   123                                             neb::string_to_byte(ir_str));
   124  }
   125  checker_tasks::task_container_ptr_t nr_ir_generator::gen_tasks() {
   126    return nullptr;
   127  }
   128