Monero
rpc_payment.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2020, 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>
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"
45 
46 namespace cryptonote
47 {
49  {
50  public:
51  struct client_info
52  {
58  uint64_t seed_height;
61  uint32_t cookie;
64  uint64_t credits;
65  std::unordered_set<uint64_t> payments;
66  std::unordered_set<uint64_t> previous_payments;
67  uint64_t 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;
86  a & seed_height;
88  a & seed_hash;
90  a & cookie;
91  a & top;
92  a & previous_top;
93  a & credits;
94  a & payments;
96  a & 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 
108  VERSION_FIELD(0)
109  FIELD(block)
118  FIELD(top)
121  FIELD(payments)
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);
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 
162  VERSION_FIELD(0)
171  END_SERIALIZE()
172 
173  bool load(std::string directory);
174  bool store(const std::string &directory = std::string()) const;
175 
176  private:
178  uint64_t m_diff;
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 }
cryptonote::rpc_payment::client_info::update_time
uint64_t update_time
Definition: rpc_payment.h:67
cryptonote::rpc_payment::client_info::payments
std::unordered_set< uint64_t > payments
Definition: rpc_payment.h:65
cryptonote::rpc_payment::client_info::previous_hashing_blob
cryptonote::blobdata previous_hashing_blob
Definition: rpc_payment.h:56
cryptonote::rpc_payment::client_info::previous_payments
std::unordered_set< uint64_t > previous_payments
Definition: rpc_payment.h:66
cryptonote::rpc_payment::load
bool load(std::string directory)
Definition: rpc_payment.cpp:286
cryptonote::rpc_payment::client_info::cookie
uint32_t cookie
Definition: rpc_payment.h:61
cryptonote::rpc_payment::get_hashes
uint64_t get_hashes(unsigned int seconds) const
Definition: rpc_payment.cpp:394
pair.h
cryptonote_basic.h
ts
#define ts
Definition: skein.c:522
cryptonote::rpc_payment::m_nonces_bad
uint64_t m_nonces_bad
Definition: rpc_payment.h:187
cryptonote::rpc_payment::m_diff
uint64_t m_diff
Definition: rpc_payment.h:178
serializable_map::parent
std::map< K, V > & parent()
Definition: containers.h:63
string.h
cryptonote::rpc_payment::m_credits_per_hash_found
uint64_t m_credits_per_hash_found
Definition: rpc_payment.h:179
cryptonote::rpc_payment::m_nonces_dupe
uint64_t m_nonces_dupe
Definition: rpc_payment.h:188
cryptonote::rpc_payment::m_credits_total
uint64_t m_credits_total
Definition: rpc_payment.h:183
crypto
crypto namespace.
Definition: crypto.cpp:60
cryptonote::rpc_payment::m_nonces_stale
uint64_t m_nonces_stale
Definition: rpc_payment.h:186
boost
Definition: unordered_containers_boost_serialization.h:38
cryptonote::rpc_payment
Definition: rpc_payment.h:49
cryptonote::rpc_payment::pay
bool pay(const crypto::public_key &client, uint64_t ts, uint64_t payment, const std::string &rpc, bool same_ts, uint64_t &credits)
Definition: rpc_payment.cpp:107
cryptonote::rpc_payment::client_info::previous_seed_height
uint64_t previous_seed_height
Definition: rpc_payment.h:57
serializable_unordered_map
Definition: containers.h:49
END_SERIALIZE
#define END_SERIALIZE()
self-explanatory
Definition: serialization.h:215
cryptonote::rpc_payment::submit_nonce
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)
Definition: rpc_payment.cpp:182
cryptonote::rpc_payment::client_info::nonces_good
uint64_t nonces_good
Definition: rpc_payment.h:72
cryptonote::rpc_payment::m_directory
std::string m_directory
Definition: rpc_payment.h:181
cryptonote::rpc_payment::m_client_info
serializable_unordered_map< crypto::public_key, client_info > m_client_info
Definition: rpc_payment.h:180
cryptonote::rpc_payment::client_info::hashing_blob
cryptonote::blobdata hashing_blob
Definition: rpc_payment.h:55
crypto.h
build_protob.const
const
Definition: build_protob.py:9
cryptonote::rpc_payment::m_hashrate
serializable_map< uint64_t, uint64_t > m_hashrate
Definition: rpc_payment.h:182
cryptonote::rpc_payment::client_info::nonces_bad
uint64_t nonces_bad
Definition: rpc_payment.h:74
cryptonote::rpc_payment::client_info::previous_top
crypto::hash previous_top
Definition: rpc_payment.h:63
VERSION_FIELD
#define VERSION_FIELD(v)
Definition: serialization.h:295
cryptonote::rpc_payment::on_idle
bool on_idle()
Definition: rpc_payment.cpp:421
cryptonote::rpc_payment::client_info::previous_block
cryptonote::block previous_block
Definition: rpc_payment.h:54
cryptonote::rpc_payment::client_info::credits_used
uint64_t credits_used
Definition: rpc_payment.h:71
info
CXA_THROW_INFO_T * info
Definition: stack_trace.cpp:90
cryptonote::rpc_payment::client_info
Definition: rpc_payment.h:52
cryptonote::rpc_payment::client_info::top
crypto::hash top
Definition: rpc_payment.h:62
cryptonote::rpc_payment::client_info::last_request_timestamp
uint64_t last_request_timestamp
Definition: rpc_payment.h:68
cryptonote::rpc_payment::serialize
void serialize(t_archive &a, const unsigned int ver)
Definition: rpc_payment.h:149
serializable_map
Definition: containers.h:60
BEGIN_SERIALIZE_OBJECT
#define BEGIN_SERIALIZE_OBJECT()
begins the environment of the DSL \detailed for described the serialization of an object
Definition: serialization.h:191
cryptonote::block
Definition: cryptonote_basic.h:464
cryptonote::rpc_payment::m_nonces_good
uint64_t m_nonces_good
Definition: rpc_payment.h:185
cryptonote::rpc_payment::prune_hashrate
void prune_hashrate(unsigned int seconds)
Definition: rpc_payment.cpp:408
cryptonote
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
VARINT_FIELD
#define VARINT_FIELD(f)
tags and serializes the varint f
Definition: serialization.h:264
cryptonote::rpc_payment::get_payment_address
const cryptonote::account_public_address & get_payment_address() const
Definition: rpc_payment.h:141
blobdatatype.h
crypto::public_key
POD_CLASS public_key
Definition: crypto.h:61
containers.h
cryptonote::blobdata
std::string blobdata
Definition: blobdatatype.h:39
cryptonote::rpc_payment::balance
uint64_t balance(const crypto::public_key &client, int64_t delta=0)
Definition: rpc_payment.cpp:91
cryptonote::rpc_payment::client_info::block_template_update_time
uint64_t block_template_update_time
Definition: rpc_payment.h:69
std
Definition: blockchain_ancestry.cpp:72
cryptonote::rpc_payment::client_info::credits
uint64_t credits
Definition: rpc_payment.h:64
cryptonote::rpc_payment::client_info::seed_height
uint64_t seed_height
Definition: rpc_payment.h:58
cryptonote::account_public_address
Definition: cryptonote_basic.h:501
MakeCryptoOps.a
string a
Definition: MakeCryptoOps.py:15
cryptonote::rpc_payment::client_info::previous_seed_hash
crypto::hash previous_seed_hash
Definition: rpc_payment.h:59
cryptonote::rpc_payment::client_info::nonces_dupe
uint64_t nonces_dupe
Definition: rpc_payment.h:75
cryptonote::rpc_payment::client_info::credits_total
uint64_t credits_total
Definition: rpc_payment.h:70
FIELD
#define FIELD(f)
tags the field with the variable name and then serializes it
Definition: serialization.h:244
cryptonote::rpc_payment::mutex
boost::mutex mutex
Definition: rpc_payment.h:189
cryptonote::rpc_payment::client_info::client_info
client_info()
Definition: rpc_payment.cpp:57
cryptonote::rpc_payment::client_info::seed_hash
crypto::hash seed_hash
Definition: rpc_payment.h:60
cryptonote::rpc_payment::m_address
cryptonote::account_public_address m_address
Definition: rpc_payment.h:177
cryptonote::rpc_payment::client_info::block
cryptonote::block block
Definition: rpc_payment.h:53
cryptonote::rpc_payment::get_info
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)
Definition: rpc_payment.cpp:131
cryptonote::rpc_payment::m_credits_used
uint64_t m_credits_used
Definition: rpc_payment.h:184
cryptonote::rpc_payment::client_info::nonces_stale
uint64_t nonces_stale
Definition: rpc_payment.h:73
cryptonote::rpc_payment::store
bool store(const std::string &directory=std::string()) const
Definition: rpc_payment.cpp:331
cryptonote::rpc_payment::flush_by_age
unsigned int flush_by_age(time_t seconds=0)
Definition: rpc_payment.cpp:366
cryptonote::rpc_payment::client_info::serialize
void serialize(t_archive &a, const unsigned int ver)
Definition: rpc_payment.h:80
crypto::hash
POD_CLASS hash
Definition: hash.h:48