github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/generator/auth_table_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/auth_table_generator.h"
    21  #include "fs/proto/ir.pb.h"
    22  #include <sstream>
    23  
    24  static std::string ir_slice1 =
    25      "#include <string>  \n"
    26      "#include <tuple> \n"
    27      "#include <vector> \n"
    28      "typedef std::string name_t; \n"
    29      "typedef uint64_t version_t; \n"
    30      "typedef std::string address_t; \n"
    31      "typedef uint64_t height_t; \n"
    32      "typedef std::tuple<name_t, address_t, height_t, height_t> "
    33      "row_t; \n"
    34      "std::vector<row_t> entry_point_auth() { \n"
    35      " auto to_version_t = [](uint32_t major_version, uint16_t minor_version, "
    36      "\n"
    37      "                         uint16_t patch_version) -> version_t { \n"
    38      "    return (0ULL + major_version) + ((0ULL + minor_version) << 32) + \n"
    39      "           ((0ULL + patch_version) << 48); \n"
    40      "  };";
    41  
    42  static std::string ir_slice_2 =
    43      "std::vector<row_t> auth_table = {NR_ITEM, \n DIP_ITEM}; \n";
    44  static std::string ir_slice_nr =
    45      "std::make_tuple(\"nr\", std::string(VAR.begin(), VAR.end()), 1ULL, "
    46      "0xFFFFFFFFFFFFFFFFULL)";
    47  static std::string ir_slice_dip =
    48      "std::make_tuple(\"dip\", std::string(VAR.begin(), VAR.end()), 1ULL, "
    49      "0xFFFFFFFFFFFFFFFFULL)";
    50  static std::string ir_slice_end = "return auth_table;}";
    51  
    52  static std::string gen_admin_var(const address_t &addr,
    53                                   const std::string &var_name) {
    54    std::stringstream ss;
    55    ss << "\n\tauto " << var_name << " = {";
    56    for (size_t i = 0; i < addr.size(); ++i) {
    57      ss << "0x" << std::hex << static_cast<uint32_t>(addr[i]);
    58      if (i != addr.size() - 1) {
    59        ss << ", ";
    60      }
    61    }
    62    ss << "};\n";
    63    return ss.str();
    64  }
    65  std::string gen_auth_table_ir(const address_t &nr_admin,
    66                                const address_t &dip_admin) {
    67    std::string nr_var = gen_admin_var(nr_admin, "nr_admin_addr");
    68    std::string dip_var = gen_admin_var(dip_admin, "dip_admin_addr");
    69  
    70    std::stringstream ss;
    71    ss << ir_slice1;
    72    ss << nr_var;
    73    ss << dip_var;
    74    std::string nr_item = ir_slice_nr;
    75    boost::replace_all(nr_item, "VAR", "nr_admin_addr");
    76    std::string dip_item = ir_slice_dip;
    77    boost::replace_all(dip_item, "VAR", "dip_admin_addr");
    78  
    79    std::string ir_slice_items = ir_slice_2;
    80    boost::replace_all(ir_slice_items, "NR_ITEM", nr_item);
    81    boost::replace_all(ir_slice_items, "DIP_ITEM", dip_item);
    82    ss << ir_slice_items;
    83    ss << ir_slice_end;
    84    return ss.str();
    85  }
    86  
    87  neb::bytes gen_auth_table_payload(const address_t &nr_admin,
    88                                    const address_t &dip_admin) {
    89    std::string payload = gen_auth_table_ir(nr_admin, dip_admin);
    90    nbre::NBREIR ir;
    91    ir.set_name("auth");
    92    ir.set_version(0);
    93    ir.set_height(0);
    94    ir.set_ir(payload);
    95    ir.set_ir_type(neb::ir_type::cpp);
    96  
    97    std::string ir_str = ir.SerializeAsString();
    98    return neb::string_to_byte(ir_str);
    99  }
   100  
   101  auth_table_generator::auth_table_generator(all_accounts *accounts,
   102                                             generate_block *block)
   103      : generator_base(accounts, block, 0, 1) {}
   104  
   105  auth_table_generator::~auth_table_generator() {}
   106  
   107  std::shared_ptr<corepb::Account> auth_table_generator::gen_account() {
   108    return nullptr;
   109  }
   110  
   111  std::shared_ptr<corepb::Transaction> auth_table_generator::gen_tx() {
   112    auto ir = gen_auth_table_payload(m_nr_admin_addr, m_dip_admin_addr);
   113    return m_block->add_protocol_transaction(m_auth_admin_addr, ir);
   114  }
   115  
   116  checker_tasks::task_container_ptr_t auth_table_generator::gen_tasks() {
   117    return nullptr;
   118  }
   119