Local accounts now have their balances and sequences managed from the active

ledger, not from the wallet.
This commit is contained in:
JoelKatz
2012-04-24 04:24:14 -07:00
parent f36d1fdc08
commit 0392d8ddcb
6 changed files with 42 additions and 70 deletions

View File

@@ -117,7 +117,7 @@ void Application::run()
// temporary // temporary
mWallet.load(); mWallet.load();
mWallet.syncToLedger(true, &(*secondLedger)); // mWallet.syncToLedger(true, &(*secondLedger));
// temporary // temporary
mIOService.run(); // This blocks mIOService.run(); // This blocks

View File

@@ -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 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() bool Ledger::unitTest()
{ {
#if 0 #if 0

View File

@@ -4,6 +4,8 @@
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include "AccountState.h"
class LocalAccountFamily; class LocalAccountFamily;
class LocalAccount class LocalAccount
@@ -21,11 +23,6 @@ protected:
boost::shared_ptr<LocalAccountFamily> mFamily; boost::shared_ptr<LocalAccountFamily> mFamily;
int mAccountFSeq; 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: public:
LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq); LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq);
@@ -48,16 +45,8 @@ public:
CKey::pointer getPrivateKey(); CKey::pointer getPrivateKey();
Json::Value getJson() const; Json::Value getJson() const;
void update(uint64 balance, uint32 seq); AccountState::pointer getAccountState() const;
uint32 getTxnSeq() const { return mTxnSeq; } uint64 getEffectiveBalance() const;
uint32 incTxnSeq() { return mTxnSeq++; }
int64 getEffectiveBalance() const { return static_cast<int64_t>(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();
}; };
class LocalAccountFamily : public boost::enable_shared_from_this<LocalAccountFamily> class LocalAccountFamily : public boost::enable_shared_from_this<LocalAccountFamily>

View File

@@ -14,6 +14,9 @@
Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAddress& toAccount, uint64 amount, Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAddress& toAccount, uint64 amount,
uint32 ident, uint32 ledger) : mInLedger(0), mStatus(NEW) 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(); mAccountFrom = fromLocalAccount->getAddress();
mTransaction = boost::make_shared<SerializedTransaction>(ttMAKE_PAYMENT); mTransaction = boost::make_shared<SerializedTransaction>(ttMAKE_PAYMENT);
@@ -22,8 +25,9 @@ Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAd
assert(mFromPubKey); assert(mFromPubKey);
mTransaction->setSigningAccount(mFromPubKey->GetPubKey()); mTransaction->setSigningAccount(mFromPubKey->GetPubKey());
mTransaction->setSequence(fromLocalAccount->getTxnSeq()); mTransaction->setSequence(accountState->getSeq());
assert(mTransaction->getSequence() != 0); assert(mTransaction->getSequence() != 0);
mTransaction->setTransactionFee(100); // for now mTransaction->setTransactionFee(100); // for now
mTransaction->setITFieldAccount(sfDestination, toAccount); mTransaction->setITFieldAccount(sfDestination, toAccount);

View File

@@ -24,11 +24,9 @@
// //
LocalAccount::LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int familySeq) : LocalAccount::LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int familySeq) :
mPublicKey(family->getPublicKey(familySeq)), mFamily(family), mAccountFSeq(familySeq), mPublicKey(family->getPublicKey(familySeq)), mFamily(family), mAccountFSeq(familySeq)
mLgrBalance(0), mTxnDelta(0), mTxnSeq(0)
{ {
mAcctID.setAccountPublic(mPublicKey->GetPubKey()); mAcctID.setAccountPublic(mPublicKey->GetPubKey());
if(theApp!=NULL) mPublicKey = theApp->getPubKeyCache().store(mAcctID, mPublicKey); if(theApp!=NULL) mPublicKey = theApp->getPubKeyCache().store(mAcctID, mPublicKey);
} }
@@ -37,7 +35,6 @@ std::string LocalAccount::getFullName() const
std::string ret(mFamily->getFamily().humanFamilyGenerator()); std::string ret(mFamily->getFamily().humanFamilyGenerator());
ret.append(":"); ret.append(":");
ret.append(boost::lexical_cast<std::string>(mAccountFSeq)); ret.append(boost::lexical_cast<std::string>(mAccountFSeq));
return ret; return ret;
} }
@@ -51,6 +48,18 @@ std::string LocalAccount::getFamilyName() const
return mFamily->getFamily().humanFamilyGenerator(); 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 LocalAccount::getJson() const
{ {
Json::Value ret(Json::objectValue); Json::Value ret(Json::objectValue);
@@ -61,11 +70,14 @@ Json::Value LocalAccount::getJson() const
ret["Issued"] = Json::Value(isIssued()); ret["Issued"] = Json::Value(isIssued());
ret["IsLocked"] = mFamily->isLocked(); ret["IsLocked"] = mFamily->isLocked();
uint64 eb=getEffectiveBalance(); AccountState::pointer as = getAccountState();
if(eb!=0) ret["Balance"]=boost::lexical_cast<std::string>(eb); if (as) ret["Account"] = "None";
else
uint32 sq=getTxnSeq(); {
if(sq!=0) ret["TxnSeq"]=boost::lexical_cast<std::string>(sq); Json::Value acct(Json::objectValue);
as->addJson(acct);
ret["Account"] = acct;
}
return ret; return ret;
} }
@@ -475,7 +487,6 @@ LocalAccount::pointer Wallet::getNewLocalAccount(const NewcoinAddress& family)
mAccounts.insert(std::make_pair(acct, lac)); mAccounts.insert(std::make_pair(acct, lac));
sl.unlock(); sl.unlock();
lac->syncLedger();
return lac; return lac;
} }
@@ -495,7 +506,6 @@ LocalAccount::pointer Wallet::getLocalAccount(const NewcoinAddress& family, int
mAccounts.insert(std::make_pair(acct, lac)); mAccounts.insert(std::make_pair(acct, lac));
sl.unlock(); sl.unlock();
lac->syncLedger();
return lac; return lac;
} }
@@ -743,25 +753,6 @@ bool Wallet::unitTest()
return true; 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<uint256, LocalTransaction::pointer>::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<NewcoinAddress, LocalAccount::pointer>::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(mLedger<ledger->getLedgerSeq()) mLedger=ledger->getLedgerSeq();
}
#if 0 #if 0
// We can't replicate the transaction logic in the wallet // We can't replicate the transaction logic in the wallet