github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/cmd/rocksdb/blockchain.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 "common/version.h"
    22  #include "fs/ir_manager/ir_manager.h"
    23  #include "fs/proto/block.pb.h"
    24  #include "fs/proto/ir.pb.h"
    25  #include "fs/rocksdb_storage.h"
    26  #include "fs/util.h"
    27  #include <boost/format.hpp>
    28  #include <boost/program_options.hpp>
    29  
    30  namespace po = boost::program_options;
    31  
    32  int main(int argc, char *argv[]) {
    33  
    34    po::options_description desc("Rocksdb read and write");
    35    desc.add_options()("help", "show help message")(
    36        "db_path", po::value<std::string>(), "Database file directory")(
    37        "height", po::value<neb::block_height_t>(), "block height");
    38  
    39    po::variables_map vm;
    40    po::store(po::parse_command_line(argc, argv, desc), vm);
    41    po::notify(vm);
    42  
    43    if (vm.count("help")) {
    44      std::cout << desc << "\n";
    45      return 1;
    46    }
    47  
    48    if (!vm.count("db_path")) {
    49      std::cout << "You must specify \"db_path\"!" << std::endl;
    50      return 1;
    51    }
    52    if (!vm.count("height")) {
    53      std::cout << "You must specify \"height\"!" << std::endl;
    54      return 1;
    55    }
    56  
    57    std::string db_path = vm["db_path"].as<std::string>();
    58    neb::fs::rocksdb_storage rs;
    59    rs.open_database(db_path, neb::fs::storage_open_for_readonly);
    60  
    61    auto f_lib_height = [&]() {
    62      auto block_hash_bytes = rs.get("blockchain_lib");
    63      auto block_bytes = rs.get_bytes(block_hash_bytes);
    64  
    65      std::shared_ptr<corepb::Block> block = std::make_shared<corepb::Block>();
    66      bool ret = block->ParseFromArray(block_bytes.value(), block_bytes.size());
    67      LOG(INFO) << "blockchain lib height: " << block->height();
    68    };
    69    f_lib_height();
    70  
    71    neb::block_height_t height = vm["height"].as<neb::block_height_t>();
    72    auto f_block_hash = [&](neb::block_height_t height) {
    73      auto block_hash_bytes =
    74          rs.get_bytes(neb::number_to_byte<neb::bytes>(height));
    75      LOG(INFO) << neb::byte_to_number<neb::block_height_t>(block_hash_bytes);
    76    };
    77    f_block_hash(height);
    78  
    79    return 0;
    80  }