Monero
rpc_payment.h
Go to the documentation of this file.
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>
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  {
65  std::unordered_set<uint64_t> payments;
66  std::unordered_set<uint64_t> previous_payments;
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:
138  bool pay(const crypto::public_key &client, uint64_t ts, uint64_t payment, const std::string &rpc, bool same_ts, uint64_t &credits);
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:
181  std::string m_directory;
189  mutable boost::mutex mutex;
190  };
191 }
uint64_t credits_total
Definition: rpc_payment.h:70
uint64_t m_credits_used
Definition: rpc_payment.h:184
crypto::hash top
Definition: rpc_payment.h:62
uint64_t m_credits_per_hash_found
Definition: rpc_payment.h:179
uint64_t m_nonces_dupe
Definition: rpc_payment.h:188
uint64_t get_hashes(unsigned int seconds) const
Definition: rpc_payment.cpp:396
Definition: portable_binary_archive.hpp:29
uint64_t credits_used
Definition: rpc_payment.h:71
uint64_t nonces_good
Definition: rpc_payment.h:72
uint64_t m_diff
Definition: rpc_payment.h:178
Definition: cryptonote_basic.h:474
cryptonote::blobdata previous_hashing_blob
Definition: rpc_payment.h:56
std::map< K, V > & parent()
Definition: containers.h:63
uint64_t nonces_dupe
Definition: rpc_payment.h:75
uint64_t m_credits_total
Definition: rpc_payment.h:183
uint64_t credits
Definition: rpc_payment.h:64
uint64_t last_request_timestamp
Definition: rpc_payment.h:68
crypto namespace.
Definition: crypto.cpp:60
Definition: enums.h:67
uint64_t nonces_stale
Definition: rpc_payment.h:73
serializable_map< uint64_t, uint64_t > m_hashrate
Definition: rpc_payment.h:182
uint64_t m_nonces_stale
Definition: rpc_payment.h:186
Definition: rpc_payment.h:51
serializable_unordered_map< crypto::public_key, client_info > m_client_info
Definition: rpc_payment.h:180
crypto::hash previous_top
Definition: rpc_payment.h:63
crypto::hash previous_seed_hash
Definition: rpc_payment.h:59
#define END_SERIALIZE()
self-explanatory
Definition: serialization.h:150
std::string m_directory
Definition: rpc_payment.h:181
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:108
bool on_idle()
Definition: rpc_payment.cpp:423
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:44
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:183
uint64_t m_nonces_good
Definition: rpc_payment.h:185
rpc
Definition: console.py:53
#define VERSION_FIELD(v)
Definition: serialization.h:220
unsigned int uint32_t
Definition: stdint.h:126
void serialize(t_archive &a, const unsigned int ver)
Definition: rpc_payment.h:149
#define ts
Definition: skein.c:522
unsigned __int64 uint64_t
Definition: stdint.h:136
void prune_hashrate(unsigned int seconds)
Definition: rpc_payment.cpp:410
#define BEGIN_SERIALIZE_OBJECT()
begins the environment of the DSL for described the serialization of an object
Definition: serialization.h:131
const cryptonote::account_public_address & get_payment_address() const
Definition: rpc_payment.h:141
client_info()
Definition: rpc_payment.cpp:58
uint64_t balance(const crypto::public_key &client, int64_t delta=0)
Definition: rpc_payment.cpp:92
static MDB_envinfo info
Definition: mdb_load.c:37
std::unordered_set< uint64_t > previous_payments
Definition: rpc_payment.h:66
POD_CLASS public_key
Definition: crypto.h:61
cryptonote::blobdata hashing_blob
Definition: rpc_payment.h:55
uint64_t nonces_bad
Definition: rpc_payment.h:74
diff
Definition: gen_wide_data.py:42
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
std::string blobdata
Definition: blobdatatype.h:39
std::unordered_set< uint64_t > payments
Definition: rpc_payment.h:65
uint32_t address
Definition: getifaddr.c:269
Definition: cryptonote_basic.h:511
cryptonote::block block
Definition: rpc_payment.h:53
void serialize(t_archive &a, const unsigned int ver)
Definition: rpc_payment.h:80
Definition: containers.h:48
uint64_t seed_height
Definition: rpc_payment.h:58
crypto::hash seed_hash
Definition: rpc_payment.h:60
#define VARINT_FIELD(f)
tags and serializes the varint f
Definition: serialization.h:189
cryptonote::account_public_address m_address
Definition: rpc_payment.h:177
uint64_t update_time
Definition: rpc_payment.h:67
signed __int64 int64_t
Definition: stdint.h:135
uint32_t cookie
Definition: rpc_payment.h:61
cryptonote::block previous_block
Definition: rpc_payment.h:54
uint64_t previous_seed_height
Definition: rpc_payment.h:57
uint64_t block_template_update_time
Definition: rpc_payment.h:69
POD_CLASS hash
Definition: hash.h:49
unsigned int flush_by_age(time_t seconds=0)
Definition: rpc_payment.cpp:368
boost::mutex mutex
Definition: rpc_payment.h:189
#define FIELD(f)
tags the field with the variable name and then serializes it
Definition: serialization.h:169
Definition: rpc_payment.py:1
bool store(const std::string &directory=std::string()) const
Definition: rpc_payment.cpp:333
#define const
Definition: ipfrdr.c:80
Definition: containers.h:59
bool load(std::string directory)
Definition: rpc_payment.cpp:285
uint64_t m_nonces_bad
Definition: rpc_payment.h:187
static epee::net_utils::http::http_simple_client_template< dummy_client > client
Definition: http-client.cpp:62
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:132