Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::Transactor Class Reference

#include <Transactor.hpp>

Public Member Functions

 Transactor (OTServer *server)
 
 ~Transactor ()
 
bool issueNextTransactionNumber (OTPseudonym &nym, int64_t &txNumber, bool storeNumber=true)
 
bool verifyTransactionNumber (OTPseudonym &nym, const int64_t &transactionNumber)
 
bool removeTransactionNumber (OTPseudonym &nym, const int64_t &transactionNumber, bool save=false)
 
bool removeIssuedNumber (OTPseudonym &nym, const int64_t &transactionNumber, bool save=false)
 
int64_t transactionNumber () const
 
void transactionNumber (int64_t value)
 
bool addAssetContract (OTAssetContract &contract)
 
OTAssetContractgetAssetContract (const OTIdentifier &id)
 
bool addBasketAccountID (const OTIdentifier &basketId, const OTIdentifier &basketAccountId, const OTIdentifier &basketContractId)
 
bool lookupBasketAccountID (const OTIdentifier &basketId, OTIdentifier &basketAccountId)
 
bool lookupBasketAccountIDByContractID (const OTIdentifier &basketContractId, OTIdentifier &basketAccountId)
 
bool lookupBasketContractIDByAccountID (const OTIdentifier &basketAccountId, OTIdentifier &basketContractId)
 
std::shared_ptr< OTAccountgetVoucherAccount (const OTIdentifier &assetTypeId)
 
MintgetMint (const OTIdentifier &assetTypeId, int32_t seriesCount)
 Lookup the current mint for any given asset type ID and series. More...
 

Friends

class MainFile
 

Detailed Description

Definition at line 153 of file Transactor.hpp.

Constructor & Destructor Documentation

opentxs::Transactor::Transactor ( OTServer server)
explicit

Definition at line 148 of file Transactor.cpp.

149  : transactionNumber_(0)
150  , server_(server)
151 {
152 }
opentxs::Transactor::~Transactor ( )

Definition at line 154 of file Transactor.cpp.

155 {
156  while (!contractsMap_.empty()) {
157  auto it = contractsMap_.begin();
158  OTAssetContract* pContract = it->second;
159  OT_ASSERT(nullptr != pContract);
160  contractsMap_.erase(it);
161  delete pContract;
162  pContract = nullptr;
163  }
164 
165  while (!mintsMap_.empty()) {
166  auto it = mintsMap_.begin();
167  Mint* pMint = it->second;
168  OT_ASSERT(nullptr != pMint);
169  mintsMap_.erase(it);
170  delete pMint;
171  pMint = nullptr;
172  }
173 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150

Member Function Documentation

bool opentxs::Transactor::addAssetContract ( OTAssetContract theContract)

OTServer will take ownership of theContract from this point on, and will be responsible for deleting it. MUST be allocated on the heap.

Definition at line 411 of file Transactor.cpp.

412 {
413  OTAssetContract* pContract = nullptr;
414 
415  OTString STR_CONTRACT_ID;
416  OTIdentifier CONTRACT_ID;
417  theContract.GetIdentifier(STR_CONTRACT_ID);
418  theContract.GetIdentifier(CONTRACT_ID);
419 
420  pContract = getAssetContract(CONTRACT_ID);
421 
422  // already exists
423  if (nullptr != pContract) // if not null
424  return false;
425 
426  contractsMap_[STR_CONTRACT_ID.Get()] = &theContract;
427 
428  return true;
429 }
OTAssetContract * getAssetContract(const OTIdentifier &id)
Definition: Transactor.cpp:394
bool opentxs::Transactor::addBasketAccountID ( const OTIdentifier basketId,
const OTIdentifier basketAccountId,
const OTIdentifier basketContractId 
)

Definition at line 432 of file Transactor.cpp.

435 {
436  OTIdentifier theBasketAcctID;
437 
438  if (lookupBasketAccountID(BASKET_ID, theBasketAcctID)) {
439  OTLog::Output(0, "User attempted to add Basket that already exists.\n");
440  return false;
441  }
442 
443  OTString strBasketID(BASKET_ID), strBasketAcctID(BASKET_ACCOUNT_ID),
444  strBasketContractID(BASKET_CONTRACT_ID);
445 
446  idToBasketMap_[strBasketID.Get()] = strBasketAcctID.Get();
447  contractIdToBasketAccountId_[strBasketContractID.Get()] =
448  strBasketAcctID.Get();
449 
450  return true;
451 }
static EXPORT void Output(int32_t nVerbosity, const char *szOutput)
Definition: OTLog.cpp:710
bool lookupBasketAccountID(const OTIdentifier &basketId, OTIdentifier &basketAccountId)
Definition: Transactor.cpp:511
OTAssetContract * opentxs::Transactor::getAssetContract ( const OTIdentifier ASSET_TYPE_ID)

The server supports various different asset types. Any user may create a new asset type by uploading the asset contract to the server. The server stores the contract in a directory and in its in-memory list of asset types. You can call this function to look up any asset contract by ID. If it returns nullptr, you can add it yourself by uploading the contract. But be sure that the public key in the contract, used to sign the contract, is also the public key of the Nym of the issuer. They must match. In the future I may create a special key category just for this purpose. Right now I'm using the "contract" key which is already used to verify any asset or server contract.

Definition at line 394 of file Transactor.cpp.

395 {
396  for (auto& it : contractsMap_) {
397  OTAssetContract* pContract = it.second;
398  OT_ASSERT(nullptr != pContract);
399 
400  OTIdentifier theContractID;
401  pContract->GetIdentifier(theContractID);
402 
403  if (theContractID == ASSET_TYPE_ID) return pContract;
404  }
405 
406  return nullptr;
407 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
Mint * opentxs::Transactor::getMint ( const OTIdentifier assetTypeId,
int32_t  seriesCount 
)

Lookup the current mint for any given asset type ID and series.

Definition at line 565 of file Transactor.cpp.

568 {
569  Mint* pMint = nullptr;
570 
571  for (auto& it : mintsMap_) {
572  pMint = it.second;
573  OT_ASSERT_MSG(nullptr != pMint,
574  "nullptr mint pointer in Transactor::getMint\n");
575 
576  OTIdentifier theID;
577  pMint->GetIdentifier(theID);
578 
579  if ((ASSET_TYPE_ID ==
580  theID) && // if the ID on the Mint matches the ID passed in
581  (nSeries == pMint->GetSeries())) // and the series also matches...
582  return pMint; // return the pointer right here, we're done.
583  }
584  // The mint isn't in memory for the series requested.
585  const OTString ASSET_ID_STR(ASSET_TYPE_ID);
586 
587  OTString strMintFilename;
588  strMintFilename.Format("%s%s%s%s%d", server_->m_strServerID.Get(),
589  OTLog::PathSeparator(), ASSET_ID_STR.Get(), ".",
590  nSeries);
591 
592  const char* szFoldername = OTFolders::Mint().Get();
593  const char* szFilename = strMintFilename.Get();
594  pMint = Mint::MintFactory(server_->m_strServerID,
595  server_->m_strServerUserID, ASSET_ID_STR);
596 
597  // You cannot hash the Mint to get its ID. (The ID is a hash of the asset
598  // contract.)
599  // Instead, you must READ the ID from the Mint file, and then compare it to
600  // the one expected
601  // to see if they match (similar to how Account IDs are verified.)
602 
603  OT_ASSERT_MSG(nullptr != pMint,
604  "Error allocating memory for Mint in Transactor::getMint");
605  OTString strSeries;
606  strSeries.Format("%s%d", ".", nSeries);
607  //
608  if (pMint->LoadMint(strSeries.Get())) {
609  if (pMint->VerifyMint(server_->m_nymServer)) // I don't verify the
610  // Mint's
611  // expiration date here, just its
612  // signature, ID, etc.
613  { // (Expiry dates are enforced on tokens during deposit--and checked
614  // against mint--
615  // but expiry dates are only enforced on the Mint itself during a
616  // withdrawal.)
617  // It's a multimap now...
618  // mintsMap_[ASSET_ID_STR.Get()] = pMint;
619 
620  mintsMap_.insert(
621  std::pair<std::string, Mint*>(ASSET_ID_STR.Get(), pMint));
622 
623  return pMint;
624  }
625  else {
627  "Error verifying Mint in Transactor::getMint:\n%s%s%s\n",
628  szFoldername, OTLog::PathSeparator(), szFilename);
629  }
630  }
631  else {
632  OTLog::vError("Error loading Mint in Transactor::getMint:\n%s%s%s\n",
633  szFoldername, OTLog::PathSeparator(), szFilename);
634  }
635 
636  if (nullptr != pMint) delete pMint;
637  pMint = nullptr;
638 
639  return nullptr;
640 }
static EXPORT const OTString & Mint()
Definition: OTFolders.cpp:323
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800
static EXPORT Mint * MintFactory()
Definition: Mint.cpp:154
static EXPORT const char * PathSeparator()
Definition: OTLog.cpp:408
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
EXPORT const char * Get() const
Definition: OTString.cpp:1045
std::shared_ptr< OTAccount > opentxs::Transactor::getVoucherAccount ( const OTIdentifier ASSET_TYPE_ID)

Looked up the voucher account (where cashier's cheques are issued for any given asset type) return a pointer to the account. Since it's SUPPOSED to exist, and since it's being requested, also will GENERATE it if it cannot be found, add it to the list, and return the pointer. Should always succeed.

Definition at line 536 of file Transactor.cpp.

538 {
539  std::shared_ptr<OTAccount> pAccount;
540  const OTIdentifier SERVER_USER_ID(server_->m_nymServer),
541  SERVER_ID(server_->m_strServerID);
542  bool bWasAcctCreated = false;
543  pAccount = voucherAccounts_.GetOrCreateAccount(
544  server_->m_nymServer, SERVER_USER_ID, ASSET_TYPE_ID, SERVER_ID,
545  bWasAcctCreated);
546  if (bWasAcctCreated) {
547  OTString strAcctID;
548  pAccount->GetIdentifier(strAcctID);
549  const OTString strAssetTypeID(ASSET_TYPE_ID);
550 
551  OTLog::vOutput(0, "OTServer::GetVoucherAccount: Successfully created "
552  "voucher account ID: %s Asset Type ID: %s\n",
553  strAcctID.Get(), strAssetTypeID.Get());
554 
555  if (!server_->mainFile_.SaveMainFile()) {
556  OTLog::Error("OTServer::GetVoucherAccount: Error saving main "
557  "server file containing new account ID!!\n");
558  }
559  }
560 
561  return pAccount;
562 }
EXPORT std::shared_ptr< OTAccount > GetOrCreateAccount(OTPseudonym &serverNym, const OTIdentifier &ACCOUNT_OWNER_ID, const OTIdentifier &ASSET_TYPE_ID, const OTIdentifier &SERVER_ID, bool &wasAcctCreated, int64_t stashTransNum=0)
static EXPORT void Error(const char *szError)
Definition: OTLog.cpp:831
static EXPORT void vOutput(int32_t nVerbosity, const char *szOutput,...)
Definition: OTLog.cpp:768
bool opentxs::Transactor::issueNextTransactionNumber ( OTPseudonym theNym,
int64_t &  lTransactionNumber,
bool  bStoreTheNumber = true 
)

Just as every request must be accompanied by a request number, so every transaction request must be accompanied by a transaction number. The request numbers can simply be incremented on both sides (per user.) But the transaction numbers must be issued by the server and they do not repeat from user to user. They are unique to transaction.

Users must ask the server to send them transaction numbers so that they can be used in transaction requests. The server keeps an internal counter and maintains a data file to store the latest one.

More specifically, the server file itself stores the latest transaction number (So it knows what number to issue and increment when the next request comes in.)

But once it issues the next number, that number needs to be recorded in the nym file for the user it was issued to, so that it can be verified later when he submits it for a transaction–and so it can be removed once the transaction is complete (so it won't work twice.)

The option to bSaveTheNumber defaults to true for this reason. But sometimes it will be sent to false, in cases where the number doesn't need to be saved because it's never going to be verified. For example, if the server creates a transaction number so it can put a transaction into your inbox, it's never going to have to verify that it actually put it into the inbox by checking it's own nymfile for that transaction number. Instead it would just check its own server signature on the inbox. But I digress...

Definition at line 211 of file Transactor.cpp.

214 {
215  OTIdentifier NYM_ID(theNym), SERVER_NYM_ID(server_->m_nymServer);
216 
217  // If theNym has the same ID as server_->m_nymServer, then we'll use
218  // server_->m_nymServer
219  // instead of theNym. (Since it's the same nym anyway, we'll stick to the
220  // one we already loaded so any changes don't get overwritten later.)
221  OTPseudonym* pNym = nullptr;
222 
223  if (NYM_ID == SERVER_NYM_ID)
224  pNym = &server_->m_nymServer;
225  else
226  pNym = &theNym;
227 
228  // transactionNumber_ stores the last VALID AND ISSUED transaction number.
229  // So first, we increment that, since we don't want to issue the same number
230  // twice.
231  transactionNumber_++;
232 
233  // Next, we save it to file.
234  if (!server_->mainFile_.SaveMainFile()) {
235  OTLog::Error("Error saving main server file.\n");
236  transactionNumber_--;
237  return false;
238  }
239 
240  // Each Nym stores the transaction numbers that have been issued to it.
241  // (On client AND server side.)
242  //
243  // So whenever the server issues a new number, it's to a specific Nym, then
244  // it is recorded in his Nym file before being sent to the client (where it
245  // is also recorded in his Nym file.) That way the server always knows
246  // which
247  // numbers are valid for each Nym.
248  else if (bStoreTheNumber && (false ==
249  pNym->AddTransactionNum(server_->m_nymServer,
250  server_->m_strServerID,
251  transactionNumber_,
252  true))) // bSave = true
253  {
254  OTLog::Error("Error adding transaction number to Nym file.\n");
255  transactionNumber_--;
256  server_->mainFile_.SaveMainFile(); // Save it back how it was, since
257  // we're not
258  // issuing this
259  // number after all.
260  return false;
261  }
262 
263  // SUCCESS?
264  // Now the server main file has saved the latest transaction number,
265  // and the number has been stored on the relevant nym file.
266  // NOW we set it onto the parameter and return true.
267  else {
268  lTransactionNumber = transactionNumber_;
269  return true;
270  }
271 }
static EXPORT void Error(const char *szError)
Definition: OTLog.cpp:831
bool opentxs::Transactor::lookupBasketAccountID ( const OTIdentifier BASKET_ID,
OTIdentifier BASKET_ACCOUNT_ID 
)

Use this to find the basket account for this server (which is unique to this server) using the basket ID to look it up (the Basket ID is the same for all servers)

Definition at line 511 of file Transactor.cpp.

513 {
514  // Server stores a map of BASKET_ID to BASKET_ACCOUNT_ID. Let's iterate
515  // through that map...
516  for (auto& it : idToBasketMap_) {
517  OTString strBasketID = it.first.c_str();
518  OTString strBasketAcctID = it.second.c_str();
519 
520  OTIdentifier id_BASKET(strBasketID), id_BASKET_ACCT(strBasketAcctID);
521 
522  if (BASKET_ID ==
523  id_BASKET) // if the basket ID passed in matches this one...
524  {
525  BASKET_ACCOUNT_ID = id_BASKET_ACCT;
526  return true;
527  }
528  }
529  return false;
530 }
bool opentxs::Transactor::lookupBasketAccountIDByContractID ( const OTIdentifier BASKET_CONTRACT_ID,
OTIdentifier BASKET_ACCOUNT_ID 
)

Use this to find the basket account ID for this server (which is unique to this server) using the contract ID to look it up. (The basket contract ID is unique to this server.)

Definition at line 457 of file Transactor.cpp.

459 {
460  // Server stores a map of BASKET_ID to BASKET_ACCOUNT_ID. Let's iterate
461  // through that map...
462  for (auto& it : contractIdToBasketAccountId_) {
463  OTString strBasketContractID = it.first.c_str();
464  OTString strBasketAcctID = it.second.c_str();
465 
466  OTIdentifier id_BASKET_CONTRACT(strBasketContractID),
467  id_BASKET_ACCT(strBasketAcctID);
468 
469  if (BASKET_CONTRACT_ID == id_BASKET_CONTRACT) // if the basket contract
470  // ID passed in matches
471  // this one...
472  {
473  BASKET_ACCOUNT_ID = id_BASKET_ACCT;
474  return true;
475  }
476  }
477  return false;
478 }
bool opentxs::Transactor::lookupBasketContractIDByAccountID ( const OTIdentifier BASKET_ACCOUNT_ID,
OTIdentifier BASKET_CONTRACT_ID 
)

Use this to find the basket account ID for this server (which is unique to this server) using the contract ID to look it up. (The basket contract ID is unique to this server.)

Definition at line 484 of file Transactor.cpp.

486 {
487  // Server stores a map of BASKET_ID to BASKET_ACCOUNT_ID. Let's iterate
488  // through that map...
489  for (auto& it : contractIdToBasketAccountId_) {
490  OTString strBasketContractID = it.first.c_str();
491  OTString strBasketAcctID = it.second.c_str();
492 
493  OTIdentifier id_BASKET_CONTRACT(strBasketContractID),
494  id_BASKET_ACCT(strBasketAcctID);
495 
496  if (BASKET_ACCOUNT_ID == id_BASKET_ACCT) // if the basket contract ID
497  // passed in matches this
498  // one...
499  {
500  BASKET_CONTRACT_ID = id_BASKET_CONTRACT;
501  return true;
502  }
503  }
504  return false;
505 }
bool opentxs::Transactor::removeIssuedNumber ( OTPseudonym theNym,
const int64_t &  lTransactionNumber,
bool  bSave = false 
)

Remove an issued number from the Nym record once that nym accepts the receipt from his inbox.

Definition at line 354 of file Transactor.cpp.

357 {
358  OTIdentifier NYM_ID(theNym), SERVER_NYM_ID(server_->m_nymServer);
359 
360  // If theNym has the same ID as server_->m_nymServer, then we'll use
361  // server_->m_nymServer
362  // instead of theNym. (Since it's the same nym anyway, we'll stick to the
363  // one we already loaded so any changes don't get overwritten later.)
364  OTPseudonym* pNym = nullptr;
365 
366  if (NYM_ID == SERVER_NYM_ID)
367  pNym = &server_->m_nymServer;
368  else
369  pNym = &theNym;
370 
371  bool bRemoved =
372  pNym->RemoveIssuedNum(server_->m_nymServer, server_->m_strServerID,
373  lTransactionNumber, bSave);
374 
375  return bRemoved;
376 }
EXPORT bool RemoveIssuedNum(OTPseudonym &SIGNER_NYM, const OTString &strServerID, const int64_t &lTransNum, bool bSave)
bool opentxs::Transactor::removeTransactionNumber ( OTPseudonym theNym,
const int64_t &  lTransactionNumber,
bool  bSave = false 
)

Remove a transaction number from the Nym record once it's officially used/spent.

Definition at line 320 of file Transactor.cpp.

323 {
324  OTIdentifier NYM_ID(theNym), SERVER_NYM_ID(server_->m_nymServer);
325 
326  // If theNym has the same ID as server_->m_nymServer, then we'll use
327  // server_->m_nymServer
328  // instead of theNym. (Since it's the same nym anyway, we'll stick to the
329  // one we already loaded so any changes don't get overwritten later.)
330  OTPseudonym* pNym = nullptr;
331 
332  if (NYM_ID == SERVER_NYM_ID)
333  pNym = &server_->m_nymServer;
334  else
335  pNym = &theNym;
336 
337  bool bRemoved = false;
338 
339  if (bSave)
340  bRemoved = pNym->RemoveTransactionNum(
341  server_->m_nymServer, server_->m_strServerID,
342  lTransactionNumber); // the version that passes in a signer nym --
343  // saves to local storage.
344  else
345  bRemoved = pNym->RemoveTransactionNum(
346  server_->m_strServerID,
347  lTransactionNumber); // the version that doesn't save.
348 
349  return bRemoved;
350 }
EXPORT bool RemoveTransactionNum(OTPseudonym &SIGNER_NYM, const OTString &strServerID, const int64_t &lTransNum)
int64_t opentxs::Transactor::transactionNumber ( ) const
inline

Definition at line 171 of file Transactor.hpp.

172  {
173  return transactionNumber_;
174  }
void opentxs::Transactor::transactionNumber ( int64_t  value)
inline

Definition at line 176 of file Transactor.hpp.

177  {
178  transactionNumber_ = value;
179  }
bool opentxs::Transactor::verifyTransactionNumber ( OTPseudonym theNym,
const int64_t &  lTransactionNumber 
)

Transaction numbers are now stored in the nym file (on client and server side) for whichever nym they were issued to. This function verifies whether or not the transaction number is present and valid for any specific nym (i.e. for the nym passed in.)

Definition at line 278 of file Transactor.cpp.

283 {
284  OTIdentifier NYM_ID(theNym), SERVER_NYM_ID(server_->m_nymServer);
285 
286  // If theNym has the same ID as server_->m_nymServer, then we'll use
287  // server_->m_nymServer
288  // instead of theNym. (Since it's the same nym anyway, we'll stick to the
289  // one we already loaded so any changes don't get overwritten later.)
290  OTPseudonym* pNym = nullptr;
291 
292  if (NYM_ID == SERVER_NYM_ID)
293  pNym = &server_->m_nymServer;
294  else
295  pNym = &theNym;
296  if (pNym->VerifyTransactionNum(server_->m_strServerID, lTransactionNumber))
297  return true;
298  else {
299  const OTString strNymID(NYM_ID);
300  const OTString strIssued(
301  pNym->VerifyIssuedNum(server_->m_strServerID, lTransactionNumber)
302  ? "(However, that number IS issued to that Nym... He must have "
303  "already used it.)\n"
304  : "(In fact, that number isn't even issued to that Nym, though "
305  "perhaps it was at some time in the past?)\n");
306 
307  OTLog::vError("%s: %ld not available for Nym %s to use. \n%s",
308  __FUNCTION__,
309  // " Oh, and FYI, tangentially, the
310  // current Trns# counter is: %ld\n",
311  lTransactionNumber, strNymID.Get(), strIssued.Get());
312  // transactionNumber_);
313  }
314 
315  return false;
316 }
static EXPORT void vError(const char *szError,...)
Definition: OTLog.cpp:800

Friends And Related Function Documentation

friend class MainFile
friend

Definition at line 155 of file Transactor.hpp.


The documentation for this class was generated from the following files: