github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/runtime/nr/impl/nebulas_rank.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 21 #pragma once 22 23 #include "fs/blockchain/account/account_db.h" 24 #include "fs/blockchain/transaction/transaction_db.h" 25 #include "runtime/nr/graph/algo.h" 26 #include "runtime/util.h" 27 #include <boost/property_tree/ptree.hpp> 28 29 namespace neb { 30 namespace rt { 31 32 namespace nr { 33 34 struct nr_info_t { 35 address_t m_address; 36 floatxx_t m_in_outs; 37 floatxx_t m_median; 38 floatxx_t m_weight; 39 floatxx_t m_nr_score; 40 }; 41 42 struct rank_params_t { 43 int64_t m_a; 44 int64_t m_b; 45 int64_t m_c; 46 int64_t m_d; 47 floatxx_t m_theta; 48 floatxx_t m_mu; 49 floatxx_t m_lambda; 50 }; 51 52 using uintxx_t = uint64_t; 53 using transaction_db_ptr_t = std::unique_ptr<neb::fs::transaction_db>; 54 using account_db_ptr_t = std::unique_ptr<neb::fs::account_db>; 55 using nr_ret_type = 56 std::tuple<int32_t, std::string, std::vector<std::shared_ptr<nr_info_t>>>; 57 58 class nebulas_rank { 59 public: 60 static std::vector<std::shared_ptr<nr_info_t>> 61 get_nr_score(const transaction_db_ptr_t &tdb_ptr, 62 const account_db_ptr_t &adb_ptr, const rank_params_t &rp, 63 neb::block_height_t start_block, neb::block_height_t end_block); 64 65 static str_uptr_t get_nr_sum_str(const nr_ret_type &nr_ret); 66 67 static str_uptr_t nr_info_to_json(const nr_ret_type &nr_ret); 68 static nr_ret_type json_to_nr_info(const std::string &nr_result); 69 70 #ifdef NDEBUG 71 private: 72 #else 73 public: 74 #endif 75 static auto split_transactions_by_block_interval( 76 const std::vector<neb::fs::transaction_info_t> &txs, 77 int32_t block_interval = 128) 78 -> std::unique_ptr<std::vector<std::vector<neb::fs::transaction_info_t>>>; 79 80 static void filter_empty_transactions_this_interval( 81 std::vector<std::vector<neb::fs::transaction_info_t>> &txs); 82 83 static auto build_transaction_graphs( 84 const std::vector<std::vector<neb::fs::transaction_info_t>> &txs) 85 -> std::unique_ptr<std::vector<transaction_graph_ptr_t>>; 86 87 static auto 88 get_normal_accounts(const std::vector<neb::fs::transaction_info_t> &txs) 89 -> std::unique_ptr<std::unordered_set<address_t>>; 90 91 static auto get_account_balance_median( 92 const std::unordered_set<address_t> &accounts, 93 const std::vector<std::vector<neb::fs::transaction_info_t>> &txs, 94 const account_db_ptr_t &db_ptr) 95 -> std::unique_ptr<std::unordered_map<address_t, floatxx_t>>; 96 97 static auto get_account_weight( 98 const std::unordered_map<address_t, neb::rt::in_out_val_t> &in_out_vals, 99 const account_db_ptr_t &db_ptr) 100 -> std::unique_ptr<std::unordered_map<address_t, floatxx_t>>; 101 102 static auto get_account_rank( 103 const std::unordered_map<address_t, floatxx_t> &account_median, 104 const std::unordered_map<address_t, floatxx_t> &account_weight, 105 const rank_params_t &rp) 106 -> std::unique_ptr<std::unordered_map<address_t, floatxx_t>>; 107 108 static transaction_graph_ptr_t build_graph_from_transactions( 109 const std::vector<neb::fs::transaction_info_t> &trans); 110 111 static block_height_t get_max_height_this_block_interval( 112 const std::vector<neb::fs::transaction_info_t> &txs); 113 114 static floatxx_t f_account_weight(floatxx_t in_val, floatxx_t out_val); 115 116 static floatxx_t f_account_rank(int64_t a, int64_t b, int64_t c, int64_t d, 117 floatxx_t theta, floatxx_t mu, 118 floatxx_t lambda, floatxx_t S, floatxx_t R); 119 120 static void convert_nr_info_to_ptree(const nr_info_t &info, 121 boost::property_tree::ptree &pt); 122 123 static void full_fill_meta_info( 124 const std::vector<std::pair<std::string, std::string>> &meta, 125 boost::property_tree::ptree &root); 126 127 }; // class nebulas_rank 128 } // namespace nr 129 } // namespace rt 130 } // namespace neb 131