Debian Codegraph @

monero/src/rpc/rpc_payment.h

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #pragma once
30
31 #include <string>
32 #include <unordered_set>
33 #include <unordered_map>
34 #include <map>
35 #include <boost/thread/mutex.hpp>
36 #include <boost/serialization/version.hpp>
37 #include "cryptonote_basic/blobdatatype.h"
38 #include "cryptonote_basic/cryptonote_basic.h"
39 #include <boost/serialization/list.hpp>
40 #include <boost/serialization/vector.hpp>
41 #include "serialization/crypto.h"
42 #include "serialization/string.h"
43 #include "serialization/pair.h"
44 #include "serialization/containers.h"
45
46 namespace cryptonote
47 {
48 class rpc_payment
49 {
50 public:
51 struct client_info
52 {
53 cryptonote::block block;
54 cryptonote::block previous_block;
55 cryptonote::blobdata hashing_blob;
56 cryptonote::blobdata previous_hashing_blob;
57 uint64_t previous_seed_height;
58 uint64_t seed_height;
59 crypto::hash previous_seed_hash;
60 crypto::hash seed_hash;
61 uint32_t cookie;
62 crypto::hash top;
63 crypto::hash previous_top;
64 uint64_t credits;
65 std::unordered_set<uint64_t> payments;
66 std::unordered_set<uint64_t> previous_payments;
67 uint64_t update_time;
68 uint64_t last_request_timestamp;
69 uint64_t block_template_update_time;
70 uint64_t credits_total;
71 uint64_t credits_used;
72 uint64_t nonces_good;
73 uint64_t nonces_stale;
74 uint64_t nonces_bad;
75 uint64_t nonces_dupe;
76
77 client_info();
78
79 template <class t_archive>
80 inline void serialize(t_archive &a, const unsigned int ver)
81 {
82 a & block;
83 a & previous_block;
84 a & hashing_blob;
85 a & previous_hashing_blob;
86 a & seed_height;
87 a & previous_seed_height;
88 a & seed_hash;
89 a & previous_seed_hash;
90 a & cookie;
91 a & top;
92 a & previous_top;
93 a & credits;
94 a & payments;
95 a & previous_payments;
96 a & update_time;
97 a & last_request_timestamp;
98 a & block_template_update_time;
99 a & credits_total;
100 a & credits_used;
101 a & nonces_good;
102 a & nonces_stale;
103 a & nonces_bad;
104 a & nonces_dupe;
105 }
106
107 BEGIN_SERIALIZE_OBJECT()
108 VERSION_FIELD(0)
109 FIELD(block)
110 FIELD(previous_block)
111 FIELD(hashing_blob)
112 FIELD(previous_hashing_blob)
113 VARINT_FIELD(seed_height)
114 VARINT_FIELD(previous_seed_height)
115 FIELD(seed_hash)
116 FIELD(previous_seed_hash)
117 VARINT_FIELD(cookie)
118 FIELD(top)
119 FIELD(previous_top)
120 VARINT_FIELD(credits)
121 FIELD(payments)
122 FIELD(previous_payments)
123 FIELD(update_time)
124 FIELD(last_request_timestamp)
125 FIELD(block_template_update_time)
126 VARINT_FIELD(credits_total)
127 VARINT_FIELD(credits_used)
128 VARINT_FIELD(nonces_good)
129 VARINT_FIELD(nonces_stale)
130 VARINT_FIELD(nonces_bad)
131 VARINT_FIELD(nonces_dupe)
132 END_SERIALIZE()
133 };
134
135 public:
136 rpc_payment(const cryptonote::account_public_address &address, uint64_t diff, uint64_t credits_per_hash_found);
137 uint64_t balance(const crypto::public_key &client, int64_t delta = 0);
138 bool pay(const crypto::public_key &client, uint64_t ts, uint64_t payment, const std::string &rpc, bool same_ts, uint64_t &credits);
139 bool get_info(const crypto::public_key &client, const std::function<bool(const cryptonote::blobdata&, cryptonote::block&, uint64_t &seed_height, crypto::hash &seed_hash)> &get_block_template, cryptonote::blobdata &hashing_blob, uint64_t &seed_height, crypto::hash &seed_hash, const crypto::hash &top, uint64_t &diff, uint64_t &credits_per_hash_found, uint64_t &credits, uint32_t &cookie);
140 bool submit_nonce(const crypto::public_key &client, uint32_t nonce, const crypto::hash &top, int64_t &error_code, std::string &error_message, uint64_t &credits, crypto::hash &hash, cryptonote::block &block, uint32_t cookie, bool &stale);
141 const cryptonote::account_public_address &get_payment_address() const { return m_address; }
142 bool foreach(const std::function<bool(const crypto::public_key &client, const client_info &info)> &f) const;
143 unsigned int flush_by_age(time_t seconds = 0);
144 uint64_t get_hashes(unsigned int seconds) const;
145 void prune_hashrate(unsigned int seconds);
146 bool on_idle();
147
148 template <class t_archive>
149 inline void serialize(t_archive &a, const unsigned int ver)
150 {
151 a & m_client_info.parent();
152 a & m_hashrate.parent();
153 a & m_credits_total;
154 a & m_credits_used;
155 a & m_nonces_good;
156 a & m_nonces_stale;
157 a & m_nonces_bad;
158 a & m_nonces_dupe;
159 }
160
161 BEGIN_SERIALIZE_OBJECT()
162 VERSION_FIELD(0)
163 FIELD(m_client_info)
164 FIELD(m_hashrate)
165 VARINT_FIELD(m_credits_total)
166 VARINT_FIELD(m_credits_used)
167 VARINT_FIELD(m_nonces_good)
168 VARINT_FIELD(m_nonces_stale)
169 VARINT_FIELD(m_nonces_bad)
170 VARINT_FIELD(m_nonces_dupe)
171 END_SERIALIZE()
172
173 bool load(std::string directory);
174 bool store(const std::string &directory = std::string()) const;
175
176 private:
177 cryptonote::account_public_address m_address;
178 uint64_t m_diff;
179 uint64_t m_credits_per_hash_found;
180 serializable_unordered_map<crypto::public_key, client_info> m_client_info;
181 std::string m_directory;
182 serializable_map<uint64_t, uint64_t> m_hashrate;
183 uint64_t m_credits_total;
184 uint64_t m_credits_used;
185 uint64_t m_nonces_good;
186 uint64_t m_nonces_stale;
187 uint64_t m_nonces_bad;
188 uint64_t m_nonces_dupe;
189 mutable boost::mutex mutex;
190 };
191 }