github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/dummies/dummy_base.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/dummies/dummy_base.h"
    21  #include "common/configuration.h"
    22  #include "fs/blockchain.h"
    23  #include "fs/util.h"
    24  #include "util/command.h"
    25  
    26  dummy_base::dummy_base(const std::string &name)
    27      : m_name(name), m_current_height(0) {}
    28  
    29  dummy_base::~dummy_base() {}
    30  
    31  const std::string &dummy_base::db_path() const {
    32    if (m_db_path.empty()) {
    33      auto dir = neb::configuration::instance().neb_db_dir();
    34      m_db_path = neb::fs::join_path(dir, std::string("dummy_") + m_name +
    35                                              std::string("_.db"));
    36      LOG(INFO) << "neb deb path: " << m_db_path;
    37    }
    38    return m_db_path;
    39  }
    40  
    41  void dummy_base::init_from_db() {
    42    bc_storage_session::instance().init(db_path(),
    43                                        neb::fs::storage_open_for_readwrite);
    44    try {
    45      auto block = neb::fs::blockchain::load_LIB_block();
    46      m_current_height = block->height();
    47      m_current_height++;
    48    } catch (const std::exception &e) {
    49      LOG(INFO) << "init from empty db";
    50    }
    51    LOG(INFO) << "init block lib height is " << m_current_height;
    52  }
    53  
    54  void dummy_base::clean_db() {
    55    LOG(INFO) << "clean db";
    56    std::stringstream ss;
    57    ss << "rm -rf " << db_path();
    58    neb::util::command_executor::execute_command(ss.str());
    59    ss.clear();
    60    ss << "rm -rf " << neb::configuration::instance().nbre_db_dir();
    61    neb::util::command_executor::execute_command(ss.str());
    62  }
    63  
    64  void dummy_base::random_increase_version(neb::version &v) {
    65    int k = std::rand() % 3;
    66    switch (k) {
    67    case 0:
    68      v.major_version() += 1;
    69      break;
    70    case 1:
    71      v.minor_version() += 1;
    72      break;
    73    case 2:
    74      v.patch_version() += 1;
    75      break;
    76    }
    77  }