github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/dummy_neb/dummy_driver.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/dummy_driver.h" 21 #include "cmd/dummy_neb/dummies/dummies.h" 22 #include "cmd/dummy_neb/dummy_common.h" 23 #include "cmd/dummy_neb/generator/generators.h" 24 25 dummy_driver::dummy_driver() : m_io_service(), m_block_interval_seconds(0) {} 26 27 void dummy_driver::add_dummy(const std::shared_ptr<dummy_base> &dummy) { 28 m_all_dummies.insert(std::make_pair(dummy->name(), dummy)); 29 } 30 dummy_driver::~dummy_driver() { LOG(INFO) << "To quit dummy driver"; } 31 32 std::vector<std::string> dummy_driver::get_all_dummy_names() const { 33 std::vector<std::string> ret; 34 for (auto it : m_all_dummies) { 35 ret.push_back(it.first); 36 } 37 return ret; 38 } 39 40 void dummy_driver::run(const std::string &dummy_name, uint64_t block_interval) { 41 if (m_block_interval_seconds != 0) 42 throw std::invalid_argument("already run"); 43 44 auto it = m_all_dummies.find(dummy_name); 45 if (it == m_all_dummies.end()) 46 throw std::invalid_argument("can't find dummy name"); 47 48 std::shared_ptr<dummy_base> dummy = it->second; 49 50 dummy->init_from_db(); 51 52 // This for generating block with interval. 53 m_block_interval_seconds = block_interval; 54 m_block_gen_timer = std::make_unique<neb::util::timer_loop>(&m_io_service); 55 LOG(INFO) << "start block gen timer with interval: " 56 << m_block_interval_seconds << ", for dummy: " << dummy_name; 57 m_block_gen_timer->register_timer_and_callback( 58 m_block_interval_seconds, [dummy]() { 59 auto block = dummy->generate_LIB_block(); 60 block->write_to_blockchain_db(); 61 block_height_t height = dummy->current_height(); 62 63 ipc_nbre_ir_transactions_create(nullptr, height); 64 for (auto &tx : block->all_transactions()) { 65 corepb::Data d = tx->data(); 66 if (d.type() == "protocol") { 67 std::string s = tx->SerializeAsString(); 68 ipc_nbre_ir_transactions_append(nullptr, height, s.data(), 69 s.size()); 70 LOG(INFO) << "append protocol tx"; 71 } 72 } 73 ipc_nbre_ir_transactions_send(nullptr, height); 74 LOG(INFO) << "gen block " << height; 75 }); 76 77 m_checker_gen_timer = std::make_unique<neb::util::timer_loop>(&m_io_service); 78 m_checker_gen_timer->register_timer_and_callback(1, [dummy]() { 79 // dummy->generate_checker_task(); 80 // checker_tasks::instance().randomly_schedule_no_running_tasks(); 81 // checker_tasks::instance().randomly_schedule_all_tasks(20); 82 }); 83 84 m_io_service.run(); 85 } 86 87 void dummy_driver::shutdown() { m_io_service.stop(); } 88 89 void dummy_driver::reset_dummy(const std::string &dummy_name) { 90 auto it = m_all_dummies.find(dummy_name); 91 if (it == m_all_dummies.end()) 92 throw std::invalid_argument("can't find dummy name"); 93 94 std::shared_ptr<dummy_base> dummy = it->second; 95 dummy->clean_db(); 96 } 97 98 std::shared_ptr<dummy_base> 99 dummy_driver::get_dummy_with_name(const std::string &dummy_name) const { 100 auto it = m_all_dummies.find(dummy_name); 101 if (it == m_all_dummies.end()) 102 throw std::invalid_argument("can't find dummy name"); 103 104 std::shared_ptr<dummy_base> dummy = it->second; 105 return dummy; 106 }