github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/fs/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 "fs/blockchain.h"
    22  #include "common/byte.h"
    23  #include "fs/bc_storage_session.h"
    24  
    25  namespace neb {
    26  namespace fs {
    27  std::unique_ptr<corepb::Block> blockchain::load_LIB_block() {
    28    return load_block_with_tag_string(
    29        std::string(Block_LIB, std::allocator<char>()));
    30  }
    31  
    32  std::unique_ptr<corepb::Block>
    33  blockchain::load_block_with_height(block_height_t height) {
    34    std::unique_ptr<corepb::Block> block = std::make_unique<corepb::Block>();
    35  
    36    neb::bytes height_hash = bc_storage_session::instance().get_bytes(
    37        neb::number_to_byte<neb::bytes>(height));
    38  
    39    neb::bytes block_bytes =
    40        bc_storage_session::instance().get_bytes(height_hash);
    41  
    42    bool ret = block->ParseFromArray(block_bytes.value(), block_bytes.size());
    43    if (!ret) {
    44      throw std::runtime_error("parse block failed");
    45    }
    46    return block;
    47  }
    48  
    49  std::unique_ptr<corepb::Block>
    50  blockchain::load_block_with_tag_string(const std::string &tag) {
    51  
    52    std::unique_ptr<corepb::Block> block = std::make_unique<corepb::Block>();
    53    neb::bytes tail_hash =
    54        bc_storage_session::instance().get_bytes(neb::string_to_byte(tag));
    55  
    56    neb::bytes block_bytes = bc_storage_session::instance().get_bytes(tail_hash);
    57  
    58    bool ret = block->ParseFromArray(block_bytes.value(), block_bytes.size());
    59    if (!ret) {
    60      throw std::runtime_error("parse block failed");
    61    }
    62    return block;
    63  }
    64  
    65  void blockchain::write_LIB_block(corepb::Block *block) {
    66    if (block == nullptr)
    67      return;
    68  
    69    block_height_t height = block->height();
    70    auto height_hash = block->header().hash();
    71    bc_storage_session::instance().put_bytes(number_to_byte<neb::bytes>(height),
    72                                             string_to_byte(height_hash));
    73  
    74    bc_storage_session::instance().put_bytes(
    75        string_to_byte(height_hash), string_to_byte(block->SerializeAsString()));
    76  
    77    std::string key_str = std::string(Block_LIB, std::allocator<char>());
    78    bc_storage_session::instance().put_bytes(string_to_byte(key_str),
    79                                             string_to_byte(height_hash));
    80  }
    81  } // namespace fs
    82  } // namespace neb
    83