Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OTCron.hpp
Go to the documentation of this file.
1 /************************************************************
2  *
3  * OTCron.hpp
4  *
5  */
6 
7 /************************************************************
8  -----BEGIN PGP SIGNED MESSAGE-----
9  Hash: SHA1
10 
11  * OPEN TRANSACTIONS
12  *
13  * Financial Cryptography and Digital Cash
14  * Library, Protocol, API, Server, CLI, GUI
15  *
16  * -- Anonymous Numbered Accounts.
17  * -- Untraceable Digital Cash.
18  * -- Triple-Signed Receipts.
19  * -- Cheques, Vouchers, Transfers, Inboxes.
20  * -- Basket Currencies, Markets, Payment Plans.
21  * -- Signed, XML, Ricardian-style Contracts.
22  * -- Scripted smart contracts.
23  *
24  * Copyright (C) 2010-2013 by "Fellow Traveler" (A pseudonym)
25  *
26  * EMAIL:
28  *
29  * BITCOIN: 1NtTPVVjDsUfDWybS4BwvHpG2pdS9RnYyQ
30  *
31  * KEY FINGERPRINT (PGP Key in license file):
32  * 9DD5 90EB 9292 4B48 0484 7910 0308 00ED F951 BB8E
33  *
34  * OFFICIAL PROJECT WIKI(s):
35  * https://github.com/FellowTraveler/Moneychanger
36  * https://github.com/FellowTraveler/Open-Transactions/wiki
37  *
38  * WEBSITE:
39  * http://www.OpenTransactions.org/
40  *
41  * Components and licensing:
42  * -- Moneychanger..A Java client GUI.....LICENSE:.....GPLv3
43  * -- otlib.........A class library.......LICENSE:...LAGPLv3
44  * -- otapi.........A client API..........LICENSE:...LAGPLv3
45  * -- opentxs/ot....Command-line client...LICENSE:...LAGPLv3
46  * -- otserver......Server Application....LICENSE:....AGPLv3
47  * Github.com/FellowTraveler/Open-Transactions/wiki/Components
48  *
49  * All of the above OT components were designed and written by
50  * Fellow Traveler, with the exception of Moneychanger, which
51  * was contracted out to Vicky C ([email protected]).
52  * The open-source community has since actively contributed.
53  *
54  * -----------------------------------------------------
55  *
56  * LICENSE:
57  * This program is free software: you can redistribute it
58  * and/or modify it under the terms of the GNU Affero
59  * General Public License as published by the Free Software
60  * Foundation, either version 3 of the License, or (at your
61  * option) any later version.
62  *
63  * ADDITIONAL PERMISSION under the GNU Affero GPL version 3
64  * section 7: (This paragraph applies only to the LAGPLv3
65  * components listed above.) If you modify this Program, or
66  * any covered work, by linking or combining it with other
67  * code, such other code is not for that reason alone subject
68  * to any of the requirements of the GNU Affero GPL version 3.
69  * (==> This means if you are only using the OT API, then you
70  * don't have to open-source your code--only your changes to
71  * Open-Transactions itself must be open source. Similar to
72  * LGPLv3, except it applies to software-as-a-service, not
73  * just to distributing binaries.)
74  *
75  * Extra WAIVER for OpenSSL, Lucre, and all other libraries
76  * used by Open Transactions: This program is released under
77  * the AGPL with the additional exemption that compiling,
78  * linking, and/or using OpenSSL is allowed. The same is true
79  * for any other open source libraries included in this
80  * project: complete waiver from the AGPL is hereby granted to
81  * compile, link, and/or use them with Open-Transactions,
82  * according to their own terms, as long as the rest of the
83  * Open-Transactions terms remain respected, with regard to
84  * the Open-Transactions code itself.
85  *
86  * Lucre License:
87  * This code is also "dual-license", meaning that Ben Lau-
88  * rie's license must also be included and respected, since
89  * the code for Lucre is also included with Open Transactions.
90  * See Open-Transactions/src/otlib/lucre/LUCRE_LICENSE.txt
91  * The Laurie requirements are light, but if there is any
92  * problem with his license, simply remove the Lucre code.
93  * Although there are no other blind token algorithms in Open
94  * Transactions (yet. credlib is coming), the other functions
95  * will continue to operate.
96  * See Lucre on Github: https://github.com/benlaurie/lucre
97  * -----------------------------------------------------
98  * You should have received a copy of the GNU Affero General
99  * Public License along with this program. If not, see:
100  * http://www.gnu.org/licenses/
101  *
102  * If you would like to use this software outside of the free
103  * software license, please contact FellowTraveler.
104  * (Unfortunately many will run anonymously and untraceably,
105  * so who could really stop them?)
106  *
107  * DISCLAIMER:
108  * This program is distributed in the hope that it will be
109  * useful, but WITHOUT ANY WARRANTY; without even the implied
110  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
111  * PURPOSE. See the GNU Affero General Public License for
112  * more details.
113 
114  -----BEGIN PGP SIGNATURE-----
115  Version: GnuPG v1.4.9 (Darwin)
116 
117  iQIcBAEBAgAGBQJRSsfJAAoJEAMIAO35UbuOQT8P/RJbka8etf7wbxdHQNAY+2cC
118  vDf8J3X8VI+pwMqv6wgTVy17venMZJa4I4ikXD/MRyWV1XbTG0mBXk/7AZk7Rexk
119  KTvL/U1kWiez6+8XXLye+k2JNM6v7eej8xMrqEcO0ZArh/DsLoIn1y8p8qjBI7+m
120  aE7lhstDiD0z8mwRRLKFLN2IH5rAFaZZUvj5ERJaoYUKdn4c+RcQVei2YOl4T0FU
121  LWND3YLoH8naqJXkaOKEN4UfJINCwxhe5Ke9wyfLWLUO7NamRkWD2T7CJ0xocnD1
122  sjAzlVGNgaFDRflfIF4QhBx1Ddl6wwhJfw+d08bjqblSq8aXDkmFA7HeunSFKkdn
123  oIEOEgyj+veuOMRJC5pnBJ9vV+7qRdDKQWaCKotynt4sWJDGQ9kWGWm74SsNaduN
124  TPMyr9kNmGsfR69Q2Zq/FLcLX/j8ESxU+HYUB4vaARw2xEOu2xwDDv6jt0j3Vqsg
125  x7rWv4S/Eh18FDNDkVRChiNoOIilLYLL6c38uMf1pnItBuxP3uhgY6COm59kVaRh
126  nyGTYCDYD2TK+fI9o89F1297uDCwEJ62U0Q7iTDp5QuXCoxkPfv8/kX6lS6T3y9G
127  M9mqIoLbIQ1EDntFv7/t6fUTS2+46uCrdZWbQ5RjYXdrzjij02nDmJAm2BngnZvd
128  kamH0Y/n11lCvo1oQxM+
129  =uSzz
130  -----END PGP SIGNATURE-----
131  **************************************************************/
132 
133 // OTCron has a list of OTCronItems. (Really subclasses of that such as OTTrade
134 // and OTAgreement.)
135 
136 #ifndef OPENTXS_CORE_CRON_OTCRON_HPP
137 #define OPENTXS_CORE_CRON_OTCRON_HPP
138 
139 #include "../OTContract.hpp"
140 #include "../util/StringUtils.hpp"
141 #include "../util/Assert.hpp"
142 
143 namespace opentxs
144 {
145 
146 class OTCronItem;
147 class OTMarket;
149 
150 // mapOfCronItems: Mapped (uniquely) to transaction number.
151 // multimapOfCronItems: Mapped to date the item was added to Cron.
152 //
153 // (Any given CronItem will be found on BOTH lists.)
154 typedef std::map<int64_t, OTCronItem*> mapOfCronItems;
155 typedef std::multimap<time64_t, OTCronItem*> multimapOfCronItems;
156 
157 // Mapped (uniquely) to market ID.
158 typedef std::map<std::string, OTMarket*> mapOfMarkets;
159 
160 // Cron stores a bunch of these on this list,
161 // which the server refreshes from time to time.
162 typedef std::list<int64_t> listOfLongNumbers;
163 
164 class OTCron : public OTContract
165 {
166 private: // Private prevents erroneous use by other classes.
167  typedef OTContract ot_super;
168 
169 private:
170  mapOfMarkets m_mapMarkets; // A list of all valid markets.
171  mapOfCronItems m_mapCronItems; // Cron Items are found on both lists.
172  multimapOfCronItems m_multimapCronItems;
173  OTIdentifier m_SERVER_ID; // Always store this in any object that's
174  // associated with a specific server.
175 
176  listOfLongNumbers m_listTransactionNumbers; // I can't put receipts in
177  // people's inboxes without a
178  // supply of these.
179 
180  bool m_bIsActivated; // I don't want to start Cron processing until
181  // everything else is all loaded up and ready to go.
182 
183  OTPseudonym* m_pServerNym; // I'll need this for later.
184  static int32_t __trans_refill_amount; // Number of transaction numbers Cron
185  // will grab for itself, when it gets
186  // low, before each round.
187  static int32_t __cron_ms_between_process; // Number of milliseconds
188  // (ideally) between each "Cron
189  // Process" event.
190 
191  static int32_t __cron_max_items_per_nym; // Int. The maximum number of cron
192  // items any given Nym can have
193  // active at the same time.
194 
195 public:
196  static int32_t GetCronMsBetweenProcess()
197  {
198  return __cron_ms_between_process;
199  }
200  static void SetCronMsBetweenProcess(int32_t lMS)
201  {
202  __cron_ms_between_process = lMS;
203  }
204 
205  static int32_t GetCronRefillAmount()
206  {
207  return __trans_refill_amount;
208  }
209  static void SetCronRefillAmount(int32_t nAmount)
210  {
211  __trans_refill_amount = nAmount;
212  }
213  static int32_t GetCronMaxItemsPerNym()
214  {
215  return __cron_max_items_per_nym;
216  }
217  static void SetCronMaxItemsPerNym(int32_t nMax)
218  {
219  __cron_max_items_per_nym = nMax;
220  }
221  inline bool IsActivated() const
222  {
223  return m_bIsActivated;
224  }
225  inline bool ActivateCron()
226  {
227  if (!m_bIsActivated)
228  return m_bIsActivated = true;
229  else
230  return false;
231  }
232  // RECURRING TRANSACTIONS
233  //
234  EXPORT bool AddCronItem(OTCronItem& theItem, OTPseudonym* pActivator,
235  bool bSaveReceipt,
236  time64_t tDateAdded); // Date it was FIRST added to
237  // Cron.
238  EXPORT bool RemoveCronItem(int64_t lTransactionNum,
239  OTPseudonym& theRemover); // if returns false,
240  // item wasn't found.
241  EXPORT OTCronItem* GetItemByOfficialNum(int64_t lTransactionNum);
242  EXPORT OTCronItem* GetItemByValidOpeningNum(int64_t lOpeningNum);
243  EXPORT mapOfCronItems::iterator FindItemOnMap(int64_t lTransactionNum);
244  EXPORT multimapOfCronItems::iterator FindItemOnMultimap(
245  int64_t lTransactionNum);
246  // MARKETS
247  //
248  bool AddMarket(OTMarket& theMarket, bool bSaveMarketFile = true);
249  bool RemoveMarket(const OTIdentifier& MARKET_ID); // if returns false,
250  // market wasn't found.
251 
252  EXPORT OTMarket* GetMarket(const OTIdentifier& MARKET_ID);
253  OTMarket* GetOrCreateMarket(const OTIdentifier& ASSET_ID,
254  const OTIdentifier& CURRENCY_ID,
255  const int64_t& lScale);
256  // This is informational only. It returns OTStorage-type data objects,
257  // packed in a string.
258  //
259  EXPORT bool GetMarketList(OTASCIIArmor& ascOutput, int32_t& nMarketCount);
260  EXPORT bool GetNym_OfferList(OTASCIIArmor& ascOutput,
261  const OTIdentifier& NYM_ID,
262  int32_t& nOfferCount);
263  // TRANSACTION NUMBERS
264  //
265  // The server starts out putting a bunch of numbers in here so Cron can use
266  // them.
267  // Then the internal trades and payment plans get numbers from here as
268  // needed.
269  // Server MUST replenish from time-to-time, or Cron will stop working.
270  // Part of using Cron properly is to call ProcessCron() regularly, as well
271  // as to call AddTransactionNumber() regularly, in order to keep
272  // GetTransactionCount()
273  // at some minimum threshold.
274  EXPORT void AddTransactionNumber(const int64_t& lTransactionNum);
275  int64_t GetNextTransactionNumber();
276  EXPORT int32_t GetTransactionCount() const; // How many numbers do I
277  // currently have on the list?
278 
279  // Make sure every time you call this, you check the GetTransactionCount()
280  // first and replenish it
281  // to whatever your minimum supply is. (The transaction numbers in there
282  // must be enough to last for
283  // the entire ProcessCronItems() call, and all the trades and payment plans
284  // within, since it will not
285  // be replenished again at least until the call has finished.)
286  //
287  EXPORT void ProcessCronItems();
288 
289  inline void SetServerID(const OTIdentifier& SERVER_ID)
290  {
291  m_SERVER_ID = SERVER_ID;
292  }
293  inline const OTIdentifier& GetServerID() const
294  {
295  return m_SERVER_ID;
296  }
297 
298  inline void SetServerNym(OTPseudonym* pServerNym)
299  {
300  OT_ASSERT(nullptr != pServerNym);
301  m_pServerNym = pServerNym;
302  }
303  inline OTPseudonym* GetServerNym() const
304  {
305  return m_pServerNym;
306  }
307 
308  EXPORT bool LoadCron();
309  EXPORT bool SaveCron();
310 
311  EXPORT OTCron();
312  OTCron(const OTIdentifier& SERVER_ID);
313  OTCron(const char* szFilename);
314 
315  EXPORT virtual ~OTCron();
316 
317  void InitCron();
318 
319  virtual void Release();
320  void Release_Cron();
321 
322  // return -1 if error, 0 if nothing, and 1 if the node was processed.
323  virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader*& xml);
324 
325  virtual void UpdateContents(); // Before transmission or serialization, this
326  // is where the ledger saves its contents
327 
328  virtual bool SaveContractWallet(std::ofstream& ofs) const;
329 };
330 
331 } // namespace opentxs
332 
333 #endif // OPENTXS_CORE_CRON_OTCRON_HPP
bool RemoveMarket(const OTIdentifier &MARKET_ID)
OTMarket * GetOrCreateMarket(const OTIdentifier &ASSET_ID, const OTIdentifier &CURRENCY_ID, const int64_t &lScale)
Definition: OTCron.cpp:1070
EXPORT void AddTransactionNumber(const int64_t &lTransactionNum)
Definition: OTCron.cpp:415
static void SetCronRefillAmount(int32_t nAmount)
Definition: OTCron.hpp:209
int64_t GetNextTransactionNumber()
Definition: OTCron.cpp:422
virtual void UpdateContents()
Definition: OTCron.cpp:584
void SetServerID(const OTIdentifier &SERVER_ID)
Definition: OTCron.hpp:289
EXPORT bool SaveCron()
Definition: OTCron.cpp:179
std::map< std::string, OTMarket * > mapOfMarkets
Definition: OTCron.hpp:158
EXPORT bool AddCronItem(OTCronItem &theItem, OTPseudonym *pActivator, bool bSaveReceipt, time64_t tDateAdded)
Definition: OTCron.cpp:731
virtual bool SaveContractWallet(std::ofstream &ofs) const
Definition: OTCron.cpp:1224
EXPORT OTCronItem * GetItemByOfficialNum(int64_t lTransactionNum)
Definition: OTCron.cpp:938
static void SetCronMaxItemsPerNym(int32_t nMax)
Definition: OTCron.hpp:217
const OTIdentifier & GetServerID() const
Definition: OTCron.hpp:293
std::multimap< time64_t, OTCronItem * > multimapOfCronItems
Definition: OTCron.hpp:155
static int32_t GetCronMaxItemsPerNym()
Definition: OTCron.hpp:213
EXPORT OTCronItem * GetItemByValidOpeningNum(int64_t lOpeningNum)
Definition: OTCron.cpp:968
EXPORT mapOfCronItems::iterator FindItemOnMap(int64_t lTransactionNum)
Definition: OTCron.cpp:885
int64_t time64_t
Definition: Common.hpp:209
EXPORT OTCron()
Definition: OTCron.cpp:1141
EXPORT multimapOfCronItems::iterator FindItemOnMultimap(int64_t lTransactionNum)
Definition: OTCron.cpp:913
std::list< int64_t > listOfLongNumbers
Definition: OTCron.hpp:162
EXPORT void ProcessCronItems()
Definition: OTCron.cpp:648
bool AddMarket(OTMarket &theMarket, bool bSaveMarketFile=true)
Definition: OTCron.cpp:1006
void InitCron()
Definition: OTCron.cpp:1183
EXPORT bool LoadCron()
Definition: OTCron.cpp:165
virtual void Release()
Definition: OTCron.cpp:1188
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual int32_t ProcessXMLNode(irr::io::IrrXMLReader *&xml)
Definition: OTCron.cpp:434
bool ActivateCron()
Definition: OTCron.hpp:225
static void SetCronMsBetweenProcess(int32_t lMS)
Definition: OTCron.hpp:200
virtual EXPORT ~OTCron()
Definition: OTCron.cpp:1176
void Release_Cron()
Definition: OTCron.cpp:1195
bool IsActivated() const
Definition: OTCron.hpp:221
EXPORT int32_t GetTransactionCount() const
Definition: OTCron.cpp:408
OTPseudonym * GetServerNym() const
Definition: OTCron.hpp:303
static int32_t GetCronRefillAmount()
Definition: OTCron.hpp:205
void SetServerNym(OTPseudonym *pServerNym)
Definition: OTCron.hpp:298
EXPORT bool GetMarketList(OTASCIIArmor &ascOutput, int32_t &nMarketCount)
Definition: OTCron.cpp:281
EXPORT bool GetNym_OfferList(OTASCIIArmor &ascOutput, const OTIdentifier &NYM_ID, int32_t &nOfferCount)
Definition: OTCron.cpp:204
EXPORT OTMarket * GetMarket(const OTIdentifier &MARKET_ID)
Definition: OTCron.cpp:1110
static int32_t GetCronMsBetweenProcess()
Definition: OTCron.hpp:196
std::map< int64_t, OTCronItem * > mapOfCronItems
Definition: OTCron.hpp:148
EXPORT bool RemoveCronItem(int64_t lTransactionNum, OTPseudonym &theRemover)
Definition: OTCron.cpp:839