github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/runtime/dip/dip_handler.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 "common/address.h"
    24  #include "common/common.h"
    25  #include "core/net_ipc/nipc_pkg.h"
    26  #include "fs/rocksdb_storage.h"
    27  #include "runtime/dip/dip_reward.h"
    28  #include "util/singleton.h"
    29  #include "util/thread_safe_map.h"
    30  #include "util/thread_safe_vector.h"
    31  #include <ff/network.h>
    32  
    33  define_nt(start_block, neb::block_height_t);
    34  define_nt(block_interval, neb::block_height_t);
    35  define_nt(reward_addr, std::string);
    36  define_nt(coinbase_addr, std::string);
    37  
    38  namespace neb {
    39  namespace rt {
    40  namespace dip {
    41  
    42  typedef ::ff::net::ntpackage<1, start_block, block_interval, reward_addr,
    43                               coinbase_addr, p_version>
    44      dip_params_t;
    45  
    46  class dip_handler : public util::singleton<dip_handler> {
    47  public:
    48    dip_handler();
    49  
    50    void check_dip_params(block_height_t height);
    51    void deploy(version_t version, block_height_t available_height);
    52    void start(block_height_t height, const dip_params_t *dip_params = nullptr);
    53  
    54    std::shared_ptr<dip_params_t> get_dip_params(block_height_t height);
    55    str_sptr_t get_dip_reward(neb::block_height_t height);
    56  
    57    str_sptr_t get_nr_result(neb::block_height_t height);
    58    str_sptr_t get_nr_sum(neb::block_height_t height);
    59  
    60  private:
    61    std::shared_ptr<dip_params_t> get_dip_params_previous(block_height_t height);
    62    std::string get_dip_reward_when_missing(block_height_t hash_height,
    63                                            const dip_params_t &dip_params);
    64  
    65    dip_ret_type run_dip_ir(const std::string &name, version_t version,
    66                            block_height_t ir_height, block_height_t var_height);
    67  
    68    void load_storage(const std::string &key,
    69                      thread_safe_map<block_height_t, str_sptr_t> &mem_cache,
    70                      size_t storage_max_size = 1024);
    71    void dump_storage(const std::string &key, block_height_t height,
    72                      const str_sptr_t &val_ptr,
    73                      thread_safe_map<block_height_t, str_sptr_t> &mem_cache,
    74                      size_t storage_max_size = 1024);
    75  
    76  private:
    77    neb::fs::rocksdb_storage *m_storage;
    78    mutable std::mutex m_mutex;
    79    thread_safe_map<block_height_t, str_sptr_t> m_nr_sum;
    80    thread_safe_map<block_height_t, str_sptr_t> m_nr_result;
    81  
    82    thread_safe_map<block_height_t, str_sptr_t> m_dip_reward;
    83    thread_safe_map<block_height_t, bool> m_in_process;
    84    // dip params info list
    85    thread_safe_vector<std::shared_ptr<dip_params_t>> m_dip_params_list;
    86  
    87    bool m_has_curr;
    88    // suppose version and available height are in increasing order
    89    std::queue<std::pair<version_t, block_height_t>> m_incoming;
    90  };
    91  } // namespace dip
    92  } // namespace rt
    93  } // namespace neb