github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/fs/blockchain/gtest_account_db.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/configuration.h"
    22  #include "fs/bc_storage_session.h"
    23  #include "fs/blockchain.h"
    24  #include "fs/blockchain/trie/trie.h"
    25  #include <gtest/gtest.h>
    26  
    27  TEST(test_fs, account_state) {
    28  
    29    std::string neb_db_path = neb::configuration::instance().neb_db_dir();
    30    neb::fs::bc_storage_session::instance().init(
    31        neb_db_path, neb::fs::storage_open_for_readonly);
    32    neb::fs::trie t;
    33  
    34    std::string root_hash_str =
    35        "6449f1c226e1e4d94837e1e813150b2500d0556ca67147e48c83126216362345";
    36    std::string addr_base58 = "n1HrPpwwH5gTA2d7QCkVjMw14YbN1NNNXHc";
    37  
    38    neb::bytes root_hash_bytes = neb::bytes::from_hex(root_hash_str);
    39    neb::bytes addr_bytes = neb::bytes::from_base58(addr_base58);
    40  
    41    // get block
    42    neb::bytes block_bytes =
    43        neb::fs::bc_storage_session::instance().get_bytes(root_hash_bytes);
    44    std::shared_ptr<corepb::Block> block = std::make_shared<corepb::Block>();
    45    bool ret = block->ParseFromArray(block_bytes.value(), block_bytes.size());
    46    if (!ret) {
    47      throw std::runtime_error("parse block failed");
    48    }
    49    // get block header account state
    50    std::string state_root_str = block->header().state_root();
    51    neb::bytes state_root_bytes = neb::string_to_byte(state_root_str);
    52  
    53    // get trie node
    54    neb::bytes trie_node_bytes;
    55    t.get_trie_node(state_root_bytes, addr_bytes, trie_node_bytes);
    56  
    57    std::shared_ptr<corepb::Account> corepb_account_ptr =
    58        std::make_shared<corepb::Account>();
    59    ret = corepb_account_ptr->ParseFromArray(trie_node_bytes.value(),
    60                                             trie_node_bytes.size());
    61    if (!ret) {
    62      throw std::runtime_error("parse corepb Account failed");
    63    }
    64    std::string addr_str = corepb_account_ptr->address();
    65    LOG(INFO) << neb::string_to_byte(addr_str).to_base58();
    66    std::string balance_str = corepb_account_ptr->balance();
    67    std::string hex_str = neb::string_to_byte(balance_str).to_hex();
    68    LOG(INFO) << hex_str;
    69  
    70    std::stringstream ss;
    71    ss << std::hex << hex_str;
    72    neb::wei_t tmp;
    73    ss >> tmp;
    74    LOG(INFO) << tmp;
    75  }
    76  
    77  TEST(test_fs, event) {}