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

#include <AccountList.hpp>

Public Member Functions

EXPORT AccountList ()
 
 AccountList (OTAccount::AccountType acctType)
 
EXPORT ~AccountList ()
 
EXPORT int32_t GetCountAccountIDs () const
 
EXPORT void Release ()
 
EXPORT void Release_AcctList ()
 
EXPORT void Serialize (OTString &append) const
 
EXPORT int32_t ReadFromXMLNode (irr::io::IrrXMLReader *&xml, const OTString &acctType, const OTString &acctCount)
 
void SetType (OTAccount::AccountType acctType)
 
EXPORT std::shared_ptr< OTAccountGetOrCreateAccount (OTPseudonym &serverNym, const OTIdentifier &ACCOUNT_OWNER_ID, const OTIdentifier &ASSET_TYPE_ID, const OTIdentifier &SERVER_ID, bool &wasAcctCreated, int64_t stashTransNum=0)
 

Detailed Description

Definition at line 152 of file AccountList.hpp.

Constructor & Destructor Documentation

opentxs::AccountList::AccountList ( )

Definition at line 162 of file AccountList.cpp.

163  : acctType_(OTAccount::voucher)
164 {
165 }
opentxs::AccountList::AccountList ( OTAccount::AccountType  acctType)

Definition at line 167 of file AccountList.cpp.

168  : acctType_(acctType)
169 {
170 }
opentxs::AccountList::~AccountList ( )

Definition at line 172 of file AccountList.cpp.

173 {
175 }
EXPORT void Release_AcctList()

Member Function Documentation

EXPORT int32_t opentxs::AccountList::GetCountAccountIDs ( ) const
inline

Definition at line 159 of file AccountList.hpp.

160  {
161  return static_cast<int32_t>(mapAcctIDs_.size());
162  }
std::shared_ptr< OTAccount > opentxs::AccountList::GetOrCreateAccount ( OTPseudonym serverNym,
const OTIdentifier ACCOUNT_OWNER_ID,
const OTIdentifier ASSET_TYPE_ID,
const OTIdentifier SERVER_ID,
bool &  wasAcctCreated,
int64_t  stashTransNum = 0 
)

Definition at line 274 of file AccountList.cpp.

280 {
281  std::shared_ptr<OTAccount> account;
282  wasAcctCreated = false;
283 
284  if (OTAccount::stash == acctType_) {
285  if (stashTransNum <= 0) {
286  otErr
287  << "AccountList::GetOrCreateAccount: Failed attempt to create "
288  "stash account without cron item #.\n";
289  return account;
290  }
291  }
292 
293  // First, we'll see if there's already an account ID available for the
294  // requested asset type ID.
295  std::string assetTypeIdString = OTString(assetTypeId).Get();
296 
297  OTString acctTypeString;
298  TranslateAccountTypeToString(acctType_, acctTypeString);
299 
300  auto acctIDsIt = mapAcctIDs_.find(assetTypeIdString);
301  // Account ID *IS* already there for this asset type
302  if (mapAcctIDs_.end() != acctIDsIt) {
303  // grab account ID
304  std::string accountIdString = acctIDsIt->second;
305  auto weakIt = mapWeakAccts_.find(accountIdString);
306 
307  // FOUND the weak ptr to the account! Maybe it's already loaded
308  if (mapWeakAccts_.end() != weakIt) {
309  try {
310  std::shared_ptr<OTAccount> weakAccount(weakIt->second);
311 
312  // If success, then we have a shared pointer. But it's worrying
313  // (TODO) because this should have
314  // gone out of scope and been destroyed by whoever ELSE was
315  // using it. The fact that it's still here...
316  // well I'm glad not to double-load it, but I wonder why it's
317  // still here? And we aren't walking on anyone's
318  // toes, right? If this were multi-threaded, then I'd explicitly
319  // lock a mutex here, honestly. But since things
320  // happen one at a time on OT, I'll settle for a warning for
321  // now. I'm assuming that if the account's loaded
322  // already somewhere, it's just a pointer sitting there, and
323  // we're not walking on each other's toes.
324  if (weakAccount) {
325  otOut
326  << "AccountList::GetOrCreateAccount: Warning: account ("
327  << accountIdString
328  << ") was already in memory so I gave you a "
329  "pointer to the existing one. (But who else has a "
330  "copy of it?) \n";
331  return weakAccount;
332  }
333  }
334  catch (...) {
335  }
336 
337  // Though the weak pointer was there, the resource must have since
338  // been destroyed, because I cannot lock a new shared ptr onto it.
339  // Therefore remove it from the map, and RE-LOAD IT.
340  mapWeakAccts_.erase(weakIt);
341  }
342 
343  // DIDN'T find the acct pointer, even though we had the ID.
344  // (Or it was there, but we couldn't lock a shared_ptr onto it, so we
345  // erased it...)
346  // So let's load it now. After all, the Account ID *does* exist...
347  OTString acctIDString(accountIdString.c_str());
348  OTIdentifier accountID(acctIDString);
349 
350  // The Account ID exists, but we don't have the pointer to a loaded
351  // account for it. So, let's load it.
352  OTAccount* loadedAccount =
353  OTAccount::LoadExistingAccount(accountID, serverId);
354 
355  if (!loadedAccount) {
356  otErr << "Failed trying to load " << acctTypeString
357  << " account with account ID: " << acctIDString << '\n';
358  }
359  else if (!loadedAccount->VerifySignature(serverNym)) {
360  otErr << "Failed verifying server's signature on " << acctTypeString
361  << " account with account ID: " << acctIDString << '\n';
362  }
363  else if (!loadedAccount->VerifyOwnerByID(accountOwnerId)) {
364  OTString strOwnerID(accountOwnerId);
365  otErr << "Failed verifying owner ID (" << strOwnerID << ") on "
366  << acctTypeString << " account ID: " << acctIDString << '\n';
367  }
368  else {
369  otLog3 << "Successfully loaded " << acctTypeString
370  << " account ID: " << acctIDString
371  << " Asset Type ID: " << assetTypeIdString << "\n";
372 
373  account = std::shared_ptr<OTAccount>(loadedAccount);
374  // save a weak pointer to the acct, so we'll never load it twice,
375  // but we'll also know if it's been deleted.
376  mapWeakAccts_[acctIDString.Get()] =
377  std::weak_ptr<OTAccount>(account);
378  }
379  return account;
380  }
381 
382  // Not found. There's no account ID yet for that asset type ID. That means
383  // we can create it.
384  OTMessage message;
385  accountOwnerId.GetString(message.m_strNymID);
386  assetTypeId.GetString(message.m_strAssetID);
387  serverId.GetString(message.m_strServerID);
388 
389  OTAccount* createdAccount = OTAccount::GenerateNewAccount(
390  accountOwnerId, serverId, serverNym, message, acctType_, stashTransNum);
391 
392  if (!createdAccount) {
393  otErr << " AccountList::GetOrCreateAccount: Failed trying to generate"
394  << acctTypeString
395  << " account with asset type ID: " << assetTypeIdString << "\n";
396  }
397  else {
398  OTString acctIDString;
399  createdAccount->GetIdentifier(acctIDString);
400 
401  otOut << "Successfully created " << acctTypeString
402  << " account ID: " << acctIDString
403  << " Asset Type ID: " << assetTypeIdString << "\n";
404 
405  account = std::shared_ptr<OTAccount>(createdAccount);
406 
407  // save a weak pointer to the acct, so we'll never load it twice,
408  // but we'll also know if it's been deleted.
409  mapWeakAccts_[acctIDString.Get()] = std::weak_ptr<OTAccount>(account);
410  // Save the new acct ID in a map, keyed by asset type ID.
411  mapAcctIDs_[message.m_strAssetID.Get()] = acctIDString.Get();
412 
413  wasAcctCreated = true;
414  }
415 
416  return account;
417 }
static EXPORT OTAccount * GenerateNewAccount(const OTIdentifier &userId, const OTIdentifier &serverId, const OTPseudonym &serverNym, const OTMessage &message, AccountType acctType=simple, int64_t stashTransNum=0)
Definition: OTAccount.cpp:531
OTLOG_IMPORT OTLogStream otOut
OTLOG_IMPORT OTLogStream otLog3
void TranslateAccountTypeToString(OTAccount::AccountType type, OTString &acctType)
Definition: OTAccount.cpp:697
OTLOG_IMPORT OTLogStream otErr
static EXPORT OTAccount * LoadExistingAccount(const OTIdentifier &accountId, const OTIdentifier &serverId)
Definition: OTAccount.cpp:480
int32_t opentxs::AccountList::ReadFromXMLNode ( irr::io::IrrXMLReader *&  xml,
const OTString acctType,
const OTString acctCount 
)

Definition at line 198 of file AccountList.cpp.

201 {
202  if (!acctType.Exists()) {
203  otErr << "AccountList::ReadFromXMLNode: Failed: Empty accountList "
204  "'type' attribute.\n";
205  return -1;
206  }
207 
208  acctType_ = TranslateAccountTypeStringToEnum(acctType);
209 
210  if (OTAccount::err_acct == acctType_) {
211  otErr << "AccountList::ReadFromXMLNode: Failed: accountList 'type' "
212  "attribute contains unknown value.\n";
213  return -1;
214  }
215 
216  // Load up the account IDs.
217  int32_t count = acctCount.Exists() ? atoi(acctCount.Get()) : 0;
218  if (count > 0) {
219  while (count-- > 0) {
220  if (!OTContract::SkipToElement(xml)) {
221  otOut
222  << "AccountList::ReadFromXMLNode: Failure: Unable to find "
223  "expected element.\n";
224  return -1;
225  }
226 
227  if ((xml->getNodeType() == EXN_ELEMENT) &&
228  (!strcmp("accountEntry", xml->getNodeName()))) {
229  OTString assetTypeID = xml->getAttributeValue(
230  "assetTypeID"); // Asset Type ID of this account.
231  OTString accountID = xml->getAttributeValue(
232  "accountID"); // Account ID for this account.
233 
234  if (!assetTypeID.Exists() || !accountID.Exists()) {
235  otErr << "Error loading accountEntry: Either the "
236  "assetTypeID (" << assetTypeID
237  << "), or the accountID (" << accountID
238  << ") was EMPTY.\n";
239  return -1;
240  }
241 
242  mapAcctIDs_.insert(
243  std::make_pair(assetTypeID.Get(), accountID.Get()));
244  }
245  else {
246  otErr << "Expected accountEntry element in accountList.\n";
247  return -1;
248  }
249  }
250  }
251 
252  if (!OTContract::SkipAfterLoadingField(xml)) // </accountList>
253  {
254  otOut << "*** AccountList::ReadFromXMLNode: Bad data? Expected "
255  "EXN_ELEMENT_END here, but "
256  "didn't get it. Returning false.\n";
257  return -1;
258  }
259 
260  return 1;
261 }
OTLOG_IMPORT OTLogStream otOut
OTAccount::AccountType TranslateAccountTypeStringToEnum(const OTString &acctType)
Definition: OTAccount.cpp:672
static bool SkipAfterLoadingField(irr::io::IrrXMLReader *&xml)
static bool SkipToElement(irr::io::IrrXMLReader *&xml)
OTLOG_IMPORT OTLogStream otErr
void opentxs::AccountList::Release ( )

Definition at line 269 of file AccountList.cpp.

270 {
272 }
EXPORT void Release_AcctList()
void opentxs::AccountList::Release_AcctList ( )

Definition at line 263 of file AccountList.cpp.

264 {
265  mapAcctIDs_.clear();
266  mapWeakAccts_.clear();
267 }
void opentxs::AccountList::Serialize ( OTString append) const

Definition at line 177 of file AccountList.cpp.

178 {
179  OTString acctType;
180  TranslateAccountTypeToString(acctType_, acctType);
181 
182  append.Concatenate("<accountList type=\"%s\" count=\"%d\" >\n\n",
183  acctType.Get(), mapAcctIDs_.size());
184 
185  for (auto& it : mapAcctIDs_) {
186  std::string assetTypeId = it.first;
187  std::string accountId = it.second;
188  OT_ASSERT((assetTypeId.size() > 0) && (accountId.size() > 0));
189 
190  append.Concatenate(
191  "<accountEntry assetTypeID=\"%s\" accountID=\"%s\" />\n\n",
192  assetTypeId.c_str(), accountId.c_str());
193  }
194 
195  append.Concatenate("</accountList>\n\n");
196 }
void TranslateAccountTypeToString(OTAccount::AccountType type, OTString &acctType)
Definition: OTAccount.cpp:697
#define OT_ASSERT(x)
Definition: Assert.hpp:150
void opentxs::AccountList::SetType ( OTAccount::AccountType  acctType)
inline

Definition at line 173 of file AccountList.hpp.

174  {
175  acctType_ = acctType;
176  }

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