github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/fs/blockchain/blockchain_api_test.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 "fs/blockchain/blockchain_api_test.h"
    21  #include "common/nebulas_currency.h"
    22  #include "fs/blockchain.h"
    23  #include "util/bc_generator.h"
    24  
    25  namespace neb {
    26  namespace fs {
    27  
    28  blockchain_api_test::~blockchain_api_test() {}
    29  
    30  std::unique_ptr<std::vector<transaction_info_t>>
    31  blockchain_api_test::get_block_transactions_api(block_height_t height) {
    32    auto ret = std::make_unique<std::vector<transaction_info_t>>();
    33  
    34    auto block = get_block_with_height(height);
    35  
    36    for (int i = 0; i < block->transactions_size(); ++i) {
    37      corepb::Transaction tx = block->transactions(i);
    38  
    39      transaction_info_t info;
    40      info.m_height = height;
    41      info.m_status = 1; // All generated tx are succ
    42      info.m_from = to_address(tx.from());
    43      info.m_to = to_address(tx.to());
    44      info.m_tx_value = storage_to_wei(string_to_byte(tx.value()));
    45      info.m_timestamp = tx.timestamp();
    46      ret->push_back(info);
    47    }
    48  
    49    return ret;
    50  }
    51  
    52  std::unique_ptr<corepb::Account>
    53  blockchain_api_test::get_account_api(const address_t &addr,
    54                                       block_height_t height) {
    55    auto ret = std::make_unique<corepb::Account>();
    56  
    57    auto accounts = util::generate_block::read_accounts_in_height(height);
    58  
    59    bool found_flag = false;
    60    for (auto &ap : accounts) {
    61      address_t ap_addr = to_address(ap->address());
    62      if (ap_addr == addr) {
    63        *ret = *ap;
    64        found_flag = true;
    65        break;
    66      }
    67    }
    68    if (!found_flag) {
    69      ret->set_address(std::to_string(addr));
    70      ret->set_balance(std::to_string(neb::wei_to_storage(0)));
    71    }
    72  
    73    return ret;
    74  }
    75  
    76  std::unique_ptr<corepb::Transaction>
    77  blockchain_api_test::get_transaction_api(const std::string &tx_hash,
    78                                           block_height_t height) {
    79    auto ret = std::make_unique<corepb::Transaction>();
    80  
    81    auto block = get_block_with_height(height);
    82  
    83    for (int i = 0; i < block->transactions_size(); ++i) {
    84      corepb::Transaction tx = block->transactions(i);
    85      if (tx.hash() == tx_hash) {
    86        *ret = tx;
    87        break;
    88      }
    89    }
    90  
    91    return ret;
    92  }
    93  
    94  std::shared_ptr<corepb::Block>
    95  blockchain_api_test::get_block_with_height(block_height_t height) {
    96    std::shared_ptr<corepb::Block> ret;
    97    if (m_block_cache.get(height, ret)) {
    98      return ret;
    99    } else {
   100      ret = util::generate_block::read_block_with_height(height);
   101      m_block_cache.set(height, ret);
   102      return ret;
   103    }
   104  }
   105  } // namespace fs
   106  } // namespace neb