-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfullsupernodelist.h
More file actions
152 lines (123 loc) · 5.01 KB
/
Copy pathfullsupernodelist.h
File metadata and controls
152 lines (123 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef FULLSUPERNODELIST_H
#define FULLSUPERNODELIST_H
#include "rta/supernode.h"
#include "rta/DaemonRpcClient.h"
#include "lib/graft/context.h"
#include <cryptonote_config.h>
#include <string>
#include <vector>
#include <future>
#include <unordered_map>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
namespace graft {
namespace utils {
class ThreadPool;
}
class FullSupernodeList
{
public:
static constexpr int32_t TIERS = 4;
static constexpr int32_t ITEMS_PER_TIER = 1;
static constexpr int32_t AUTH_SAMPLE_SIZE = TIERS * ITEMS_PER_TIER;
static constexpr int64_t AUTH_SAMPLE_HASH_HEIGHT = 20; // block number for calculating auth sample should be calculated as current block height - AUTH_SAMPLE_HASH_HEIGHT;
static constexpr int64_t ANNOUNCE_TTL_SECONDS = 60 * 60; // if more than ANNOUNCE_TTL_SECONDS passed from last annouce - supernode excluded from auth sample selection
FullSupernodeList(const std::string &daemon_address, boost::shared_ptr<boost::asio::io_service> ios, graft::GlobalContextMap & ctxMap, bool testnet = false);
~FullSupernodeList();
void close();
/**
* @brief add - adds supernode object to a list and owns it. caller doesn't need to delete an object
* @param item - pointer to a Supernode object
* @return true if item was added. false if already in list
*/
bool add(Supernode * item);
bool add(SupernodePtr item);
/*!
* \brief loadFromDir - loads list from a directory. Directory should contain wallet key files
* \param base_dir - path to the base directory with wallet files
* \return - number of loaded supernode wallets
*/
size_t loadFromDir(const std::string &base_dir);
/*!
* \brief loadFromDirThreaded - loads list from directory.
* \param base_dir - directory where to search for wallets
* \param found_wallets - number of found wallets
* \return - number of loaded supernodes
*/
size_t loadFromDirThreaded(const std::string &base_dir, size_t &found_wallets);
/*!
* \brief remove - removes Supernode from list. closes it's wallet and frees memory
* \param address - supernode address
* \return - true if supernode removed
*/
bool remove(const std::string &address);
/*!
* \brief size - number of supernodes in list
* \return
*/
size_t size() const;
/*!
* \brief exists - checks if supernode with given address exists in list
* \param address - supernode address
* \return - true if exists
*/
bool exists(const std::string &address) const;
/*!
* \brief update - updates supernode's key images. this will probably cause stake amount change
* \param address - supernode's address
* \param key_images - list of key images
* \return - true of successfully updated
*/
bool update(const std::string &address, const std::vector<Supernode::SignedKeyImage> &key_images);
/*!
* \brief get - returns supernode instance (pointer)
* \param address - supernode's address
* \return - shared pointer to supernode or empty pointer (nullptr) is no such address
*/
SupernodePtr get(const std::string &address) const;
/*!
* \brief buildAuthSample - builds auth sample (8 supernodes) for given block height
* \param height - block height used to perform selection
* \param out - vector of supernode pointers
* \return - true on success
*/
bool buildAuthSample(uint64_t height, std::vector<SupernodePtr> &out);
/*!
* \brief items - returns address list of known supernodes
* \return
*/
std::vector<std::string> items() const;
/*!
* \brief getBlockHash - returns block hash for given height
* \param height - block height
* \param hash - output hash value
* \return - true on success
*/
bool getBlockHash(uint64_t height, std::string &hash);
/*!
* \brief refreshAsync - starts asynchronous parallel refresh all supernodes using internal threadpool.
* number of parallel jobs equals to number of hardware CPU cores
*
* \return - std::future to wait for result
*/
std::future<void> refreshAsync();
/*!
* \brief refreshedItems - returns number of refreshed supernodes
* \return
*/
size_t refreshedItems() const;
private:
bool loadWallet(const std::string &wallet_path);
private:
std::unordered_map<std::string, SupernodePtr> m_list;
std::string m_daemon_address;
bool m_testnet;
DaemonRpcClient m_rpc_client;
std::unique_ptr<utils::ThreadPool> m_tp;
std::atomic_size_t m_refresh_counter;
mutable graft::Context m_ctx;
};
using FullSupernodeListPtr = boost::shared_ptr<FullSupernodeList>;
std::ostream& operator<<(std::ostream& os, const std::vector<SupernodePtr> supernodes);
} // namespace graft
#endif // FULLSUPERNODELIST_H