From 0392d8ddcb879ebfea6062a485a6374a9cafb66b Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 24 Apr 2012 04:24:14 -0700 Subject: [PATCH] Local accounts now have their balances and sequences managed from the active ledger, not from the wallet. --- src/AccountState.h | 8 +++--- src/Application.cpp | 2 +- src/Ledger.cpp | 12 --------- src/LocalAccount.h | 19 +++---------- src/Transaction.cpp | 6 ++++- src/Wallet.cpp | 65 +++++++++++++++++++-------------------------- 6 files changed, 42 insertions(+), 70 deletions(-) diff --git a/src/AccountState.h b/src/AccountState.h index f08a8c80b6..c2c3190c2b 100644 --- a/src/AccountState.h +++ b/src/AccountState.h @@ -17,12 +17,12 @@ class AccountState { public: - typedef boost::shared_ptr pointer; + typedef boost::shared_ptr pointer; private: - NewcoinAddress mAccountID; - SerializedLedgerEntry::pointer mLedgerEntry; - bool mValid; + NewcoinAddress mAccountID; + SerializedLedgerEntry::pointer mLedgerEntry; + bool mValid; public: AccountState(const NewcoinAddress& AccountID); // For new accounts diff --git a/src/Application.cpp b/src/Application.cpp index eb1f53bde4..f4ebf63fda 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -117,7 +117,7 @@ void Application::run() // temporary mWallet.load(); - mWallet.syncToLedger(true, &(*secondLedger)); +// mWallet.syncToLedger(true, &(*secondLedger)); // temporary mIOService.run(); // This blocks diff --git a/src/Ledger.cpp b/src/Ledger.cpp index eb95633f6e..62b6ee8f99 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -182,18 +182,6 @@ Ledger::pointer Ledger::closeLedger(uint64 timeStamp) return Ledger::pointer(new Ledger(*this, timeStamp)); // can't use make_shared, constructor is protected } -void LocalAccount::syncLedger() -{ - AccountState::pointer as = theApp->getMasterLedger().getCurrentLedger()->getAccountState(getAddress()); - if(!as) mLgrBalance = 0; - else - { - mLgrBalance = as->getBalance(); - if ( (mLgrBalance != 0) && (mTxnSeq == 0) ) mTxnSeq = 1; - if (mTxnSeq < as->getSeq()) mTxnSeq = as->getSeq(); - } -} - bool Ledger::unitTest() { #if 0 diff --git a/src/LocalAccount.h b/src/LocalAccount.h index 482c39898b..ee292625d5 100644 --- a/src/LocalAccount.h +++ b/src/LocalAccount.h @@ -4,6 +4,8 @@ #include #include +#include "AccountState.h" + class LocalAccountFamily; class LocalAccount @@ -21,11 +23,6 @@ protected: boost::shared_ptr mFamily; int mAccountFSeq; - // local usage tracking - uint64 mLgrBalance; // The balance, from the last ledger - int64 mTxnDelta; // The balance changes from local/pending transactions - uint32 mTxnSeq; // The sequence number of the next transaction - public: LocalAccount(boost::shared_ptr family, int accountSeq); @@ -48,16 +45,8 @@ public: CKey::pointer getPrivateKey(); Json::Value getJson() const; - void update(uint64 balance, uint32 seq); - uint32 getTxnSeq() const { return mTxnSeq; } - uint32 incTxnSeq() { return mTxnSeq++; } - - int64 getEffectiveBalance() const { return static_cast(mLgrBalance) + mTxnDelta; } - void credit(uint64 amount) { mTxnDelta += amount; } - void debit(uint64 amount) { mTxnDelta -= amount; } - void setLedgerBalance(uint64_t lb) { mLgrBalance = lb; if (mTxnSeq == 0) mTxnSeq = 1; } - - void syncLedger(); + AccountState::pointer getAccountState() const; + uint64 getEffectiveBalance() const; }; class LocalAccountFamily : public boost::enable_shared_from_this diff --git a/src/Transaction.cpp b/src/Transaction.cpp index d44d3b5b8b..9066391864 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -14,6 +14,9 @@ Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAddress& toAccount, uint64 amount, uint32 ident, uint32 ledger) : mInLedger(0), mStatus(NEW) { + AccountState::pointer accountState = fromLocalAccount->getAccountState(); + if (!accountState) throw std::runtime_error("transaction on non-existent account"); + mAccountFrom = fromLocalAccount->getAddress(); mTransaction = boost::make_shared(ttMAKE_PAYMENT); @@ -22,8 +25,9 @@ Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAd assert(mFromPubKey); mTransaction->setSigningAccount(mFromPubKey->GetPubKey()); - mTransaction->setSequence(fromLocalAccount->getTxnSeq()); + mTransaction->setSequence(accountState->getSeq()); assert(mTransaction->getSequence() != 0); + mTransaction->setTransactionFee(100); // for now mTransaction->setITFieldAccount(sfDestination, toAccount); diff --git a/src/Wallet.cpp b/src/Wallet.cpp index ac163888f8..75056dad8e 100644 --- a/src/Wallet.cpp +++ b/src/Wallet.cpp @@ -24,12 +24,10 @@ // LocalAccount::LocalAccount(boost::shared_ptr family, int familySeq) : - mPublicKey(family->getPublicKey(familySeq)), mFamily(family), mAccountFSeq(familySeq), - mLgrBalance(0), mTxnDelta(0), mTxnSeq(0) + mPublicKey(family->getPublicKey(familySeq)), mFamily(family), mAccountFSeq(familySeq) { mAcctID.setAccountPublic(mPublicKey->GetPubKey()); - - if(theApp!=NULL) mPublicKey=theApp->getPubKeyCache().store(mAcctID, mPublicKey); + if(theApp!=NULL) mPublicKey = theApp->getPubKeyCache().store(mAcctID, mPublicKey); } std::string LocalAccount::getFullName() const @@ -37,7 +35,6 @@ std::string LocalAccount::getFullName() const std::string ret(mFamily->getFamily().humanFamilyGenerator()); ret.append(":"); ret.append(boost::lexical_cast(mAccountFSeq)); - return ret; } @@ -51,21 +48,36 @@ std::string LocalAccount::getFamilyName() const return mFamily->getFamily().humanFamilyGenerator(); } +AccountState::pointer LocalAccount::getAccountState() const +{ + return theApp->getOPs().getAccountState(mAcctID); +} + +uint64 LocalAccount::getEffectiveBalance() const +{ + AccountState::pointer as = getAccountState(); + if (!as) return 0; + return as->getBalance(); +} + Json::Value LocalAccount::getJson() const { Json::Value ret(Json::objectValue); - ret["Family"]=getFamilyName(); - ret["AccountID"]=getAddress().humanAccountID(); - ret["AccountPublic"]=getAddress().humanAccountPublic(); - ret["FullName"]=getFullName(); - ret["Issued"]=Json::Value(isIssued()); - ret["IsLocked"]=mFamily->isLocked(); + ret["Family"] = getFamilyName(); + ret["AccountID"] = getAddress().humanAccountID(); + ret["AccountPublic"] = getAddress().humanAccountPublic(); + ret["FullName"] = getFullName(); + ret["Issued"] = Json::Value(isIssued()); + ret["IsLocked"] = mFamily->isLocked(); - uint64 eb=getEffectiveBalance(); - if(eb!=0) ret["Balance"]=boost::lexical_cast(eb); - - uint32 sq=getTxnSeq(); - if(sq!=0) ret["TxnSeq"]=boost::lexical_cast(sq); + AccountState::pointer as = getAccountState(); + if (as) ret["Account"] = "None"; + else + { + Json::Value acct(Json::objectValue); + as->addJson(acct); + ret["Account"] = acct; + } return ret; } @@ -475,7 +487,6 @@ LocalAccount::pointer Wallet::getNewLocalAccount(const NewcoinAddress& family) mAccounts.insert(std::make_pair(acct, lac)); sl.unlock(); - lac->syncLedger(); return lac; } @@ -495,7 +506,6 @@ LocalAccount::pointer Wallet::getLocalAccount(const NewcoinAddress& family, int mAccounts.insert(std::make_pair(acct, lac)); sl.unlock(); - lac->syncLedger(); return lac; } @@ -743,25 +753,6 @@ bool Wallet::unitTest() return true; } -void Wallet::syncToLedger(bool force, Ledger* ledger) -{ - boost::recursive_mutex::scoped_lock sl(mLock); - if(!force && (mLedger>=ledger->getLedgerSeq())) return; - for(std::map::iterator xit=mTransactions.begin(); - xit!=mTransactions.end(); ++xit) - { // check each transaction, see if it's in the ledger or allowed in the ledger - // WRITEME - } - for(std::map::iterator ait=mAccounts.begin(); ait!=mAccounts.end(); ++ait) - { // check each account, see if our ledger balance matches - LocalAccount::pointer& lac=ait->second; - AccountState::pointer acs=ledger->getAccountState(ait->first); - if(!acs) lac->setLedgerBalance(0); - else lac->setLedgerBalance(acs->getBalance()); - } - if(mLedgergetLedgerSeq()) mLedger=ledger->getLedgerSeq(); -} - #if 0 // We can't replicate the transaction logic in the wallet