github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/runtime/nr/impl/nr_handler.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  
    21  #include "runtime/nr/impl/nr_handler.h"
    22  #include "common/configuration.h"
    23  #include "core/ir_warden.h"
    24  #include "fs/proto/ir.pb.h"
    25  #include "jit/jit_driver.h"
    26  #include "runtime/nr/impl/nebulas_rank.h"
    27  #include <ff/functionflow.h>
    28  
    29  namespace neb {
    30  namespace rt {
    31  namespace nr {
    32  
    33  nr_handler::nr_handler() {}
    34  
    35  void nr_handler::run_if_default(block_height_t start_block,
    36                                  block_height_t end_block,
    37                                  const std::string &nr_handle) {
    38    // ff::para<> p;
    39    // p([this, start_block, end_block, nr_handle]() {
    40    try {
    41      jit_driver &jd = jit_driver::instance();
    42      auto nr_ret = jd.run_ir<nr_ret_type>(
    43          "nr", start_block, neb::configuration::instance().nr_func_name(),
    44          start_block, end_block);
    45      m_nr_result.set(nr_handle, nr_ret);
    46      } catch (const std::exception &e) {
    47        LOG(INFO) << "jit driver execute nr failed " << e.what();
    48      }
    49      //});
    50  }
    51  
    52  void nr_handler::run_if_specify(block_height_t start_block,
    53                                  block_height_t end_block, uint64_t nr_version,
    54                                  const std::string &nr_handle) {
    55    // ff::para<> p;
    56    // p([this, start_block, end_block, nr_version, nr_handle]() {
    57    try {
    58      std::string nr_name = "nr";
    59      std::vector<nbre::NBREIR> irs;
    60      auto ir = neb::core::ir_warden::instance().get_ir_by_name_version(
    61          nr_name, nr_version);
    62      irs.push_back(*ir);
    63  
    64      std::stringstream ss;
    65      ss << nr_name << nr_version;
    66      std::string name_version = ss.str();
    67  
    68      jit_driver &jd = jit_driver::instance();
    69      auto nr_ret = jd.run<nr_ret_type>(
    70          name_version, irs, neb::configuration::instance().nr_func_name(),
    71          start_block, end_block);
    72      m_nr_result.set(nr_handle, nr_ret);
    73      } catch (const std::exception &e) {
    74        LOG(INFO) << "jit driver execute nr failed " << e.what();
    75      }
    76      //});
    77  }
    78  
    79  void nr_handler::start(const std::string &nr_handle) {
    80  
    81    if (!nr_handle.empty() && m_nr_result.exists(nr_handle)) {
    82      return;
    83    }
    84  
    85    neb::bytes nr_handle_bytes = neb::bytes::from_hex(nr_handle);
    86    size_t bytes = sizeof(uint64_t) / sizeof(byte_t);
    87    assert(nr_handle_bytes.size() == 3 * bytes);
    88  
    89    const auto &s = neb::bytes(nr_handle_bytes.value(), bytes);
    90    const auto &e = neb::bytes(nr_handle_bytes.value() + bytes, bytes);
    91    const auto &v = neb::bytes(nr_handle_bytes.value() + 2 * bytes, bytes);
    92  
    93    uint64_t start_block = neb::byte_to_number<uint64_t>(s);
    94    uint64_t end_block = neb::byte_to_number<uint64_t>(e);
    95    uint64_t nr_version = neb::byte_to_number<uint64_t>(v);
    96  
    97    if (!nr_version) {
    98      run_if_default(start_block, end_block, nr_handle);
    99      return;
   100    }
   101  
   102    run_if_specify(start_block, end_block, nr_version, nr_handle);
   103  }
   104  
   105  nr_ret_type nr_handler::get_nr_result(const std::string &nr_handle) {
   106  
   107    nr_ret_type nr_result;
   108    auto ret = m_nr_result.get(nr_handle, nr_result);
   109    LOG(INFO) << "nr result ret\n" << ret << "\nwith handle " << nr_handle;
   110    if (!ret) {
   111      auto err_str = std::string(
   112          "{\"err\":\"nr hash expired or nr result not complete yet\"}");
   113      std::get<0>(nr_result) = 0;
   114      std::get<1>(nr_result) = err_str;
   115    }
   116    return nr_result;
   117  }
   118  } // namespace nr
   119  } // namespace rt
   120  } // namespace neb