github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/util/bc_generator.h (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 #pragma once 21 #include "common/address.h" 22 #include "common/nebulas_currency.h" 23 #include "fs/blockchain/blockchain_api.h" 24 #include "fs/proto/block.pb.h" 25 #include <random> 26 27 namespace neb { 28 namespace util { 29 30 class all_accounts { 31 public: 32 void add_account(const std::shared_ptr<corepb::Account> &account); 33 34 corepb::Account *random_contract_account() const; 35 corepb::Account *random_user_account() const; 36 37 inline address_t random_contract_addr() const { 38 auto p = random_contract_account(); 39 if (!p) { 40 return address_t(); 41 } 42 return to_address(p->address()); 43 } 44 inline address_t random_user_addr() const { 45 auto p = random_user_account(); 46 if (!p) { 47 return address_t(); 48 } 49 return to_address(p->address()); 50 } 51 52 inline size_t size() const { return m_all_accounts.size(); } 53 54 uint64_t get_nonce(const address_t &addr); 55 void increase_nonce(const address_t &addr); 56 57 template <typename Func> void for_each_account(Func &&f) { 58 std::for_each( 59 m_all_accounts.begin(), m_all_accounts.end(), 60 [f](const std::pair<address_t, std::shared_ptr<corepb::Account>> 61 &item) { f(item.second); }); 62 } 63 64 void increase_balance(const address_t &addr, const wei &val); 65 bool decrease_balance(const address_t &addr, const wei &val); 66 67 protected: 68 corepb::Account *random_account() const; 69 70 protected: 71 std::unordered_map<address_t, std::shared_ptr<corepb::Account>> 72 m_all_accounts; 73 std::vector<address_t> m_all_addresses; 74 mutable std::default_random_engine m_random_generator; 75 }; 76 77 address_t get_address_from_account(corepb::Account *account); 78 79 class generate_block { 80 public: 81 generate_block(all_accounts *accounts, uint64_t height); 82 83 std::shared_ptr<corepb::Account> gen_user_account(const nas &v = 10000_nas); 84 85 std::shared_ptr<corepb::Account> 86 add_deploy_transaction(const address_t &owner, const bytes &payload); 87 88 std::shared_ptr<corepb::Transaction> 89 add_protocol_transaction(const address_t &owner, const bytes &payload); 90 91 std::shared_ptr<corepb::Transaction> 92 add_binary_transaction(const address_t &from, const address_t &to, 93 const nas &value); 94 95 std::shared_ptr<corepb::Transaction> 96 add_call_transaction(const address_t &from, const address_t &to); 97 98 void write_to_blockchain_db(); 99 100 inline uint64_t height() const { return m_height; } 101 102 all_accounts *get_all_accounts() { return m_all_accounts; } 103 104 const std::vector<std::shared_ptr<corepb::Transaction>> & 105 all_transactions() const { 106 return m_transactions; 107 } 108 109 static std::vector<std::shared_ptr<corepb::Account>> 110 read_accounts_in_height(block_height_t height); 111 112 static std::shared_ptr<corepb::Block> 113 read_block_with_height(block_height_t height); 114 115 116 protected: 117 all_accounts *m_all_accounts; 118 uint64_t m_height; 119 typedef std::shared_ptr<corepb::Transaction> transaction_ptr; 120 std::vector<transaction_ptr> m_transactions; 121 }; 122 } // namespace util 123 } // namespace neb