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