Fix the account state bug. Rewrite the relationship between families and

accounts. Remove the LocalAccountEntry class and make some huge
simplifications.
 Ledger.cpp     |   14 +++++--
 LocalAccount.h |   80 +++++++++++++++------------------------------
 Wallet.cpp     |  101 +++++++++++++++++++--------------------------------------
This commit is contained in:
JoelKatz
2012-01-18 11:43:25 -08:00
parent 2e0d47f782
commit b2b8caf6b9
3 changed files with 72 additions and 123 deletions

View File

@@ -95,6 +95,7 @@ bool Ledger::updateAccountState(AccountState::pointer state)
bool Ledger::addAccountState(AccountState::pointer state) bool Ledger::addAccountState(AccountState::pointer state)
{ {
assert(!mAccepted); assert(!mAccepted);
assert( (state->getBalance()==0) || (state->getSeq()>0) );
SHAMapItem::pointer item(new SHAMapItem(state->getAccountID(), state->getRaw())); SHAMapItem::pointer item(new SHAMapItem(state->getAccountID(), state->getRaw()));
return mAccountStateMap->addGiveItem(item); return mAccountStateMap->addGiveItem(item);
} }
@@ -269,11 +270,16 @@ Ledger::pointer Ledger::closeLedger(uint64 timeStamp)
return Ledger::pointer(new Ledger(*this, timeStamp)); return Ledger::pointer(new Ledger(*this, timeStamp));
} }
void LocalAccount::syncLedger(const uint160& acctID) void LocalAccount::syncLedger()
{ {
AccountState::pointer as=theApp->getMasterLedger().getAccountState(acctID); AccountState::pointer as=theApp->getMasterLedger().getAccountState(getAddress());
if(!as) setLedgerBalance(0); if(!as) mLgrBalance=0;
else syncLedger(as->getBalance(), as->getSeq()); else
{
mLgrBalance=as->getBalance();
if( (mLgrBalance!=0) && (mAccountSeq==0) ) mAccountSeq=1;
if(mAccountSeq<as->getSeq()) mAccountSeq=as->getSeq();
}
} }
bool Ledger::unitTest() bool Ledger::unitTest()

View File

@@ -1,10 +1,15 @@
#ifndef __LOCALACCOUNT__ #ifndef __LOCALACCOUNT__
#define __LOCALACCOUNT__ #define __LOCALACCOUNT__
class LocalAccountEntry #include "boost/enable_shared_from_this.hpp"
#include "boost/shared_ptr.hpp"
class LocalAccountFamily;
class LocalAccount
{ // tracks keys for local accounts { // tracks keys for local accounts
public: public:
typedef boost::shared_ptr<LocalAccountEntry> pointer; typedef boost::shared_ptr<LocalAccount> pointer;
protected: protected:
// core account information // core account information
@@ -13,7 +18,7 @@ protected:
std::string mName, mComment; std::string mName, mComment;
// family information // family information
uint160 mAccountFamily; boost::shared_ptr<LocalAccountFamily> mFamily;
int mAccountSeq; int mAccountSeq;
// local usage tracking // local usage tracking
@@ -22,7 +27,7 @@ protected:
uint32 mTxnSeq; // The sequence number of the next transaction uint32 mTxnSeq; // The sequence number of the next transaction
public: public:
LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey); LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq);
// Database operations // Database operations
bool read(); // reads any existing data bool read(); // reads any existing data
@@ -30,12 +35,21 @@ public:
bool updateName(); // writes changed name/comment bool updateName(); // writes changed name/comment
bool updateBalance(); // writes changed balance/seq bool updateBalance(); // writes changed balance/seq
const uint160& getAccountID() const { return mAcctID; } const uint160& getAddress() const { return mAcctID; }
int getAccountSeq() const { return mAccountSeq; } int getAcctSeq() const { return mAccountSeq; }
std::string getLocalAccountName() const; // The name used locally to identify this account std::string getLocalAccountName() const; // The name used locally to identify this account
std::string getAccountName() const; // The normal account name used to send to this account std::string getAccountName() const; // The normal account name used to send to this account
std::string getFullName() const;
std::string getShortName() const;
std::string getFamilyName() const;
CKey::pointer getPubKey() { return mPublicKey; } bool isLocked() const;
bool isIssued() const;
CKey::pointer getPublicKey() { return mPublicKey; }
CKey::pointer getPrivateKey();
Json::Value getJson() const;
void update(uint64 balance, uint32 seq); void update(uint64 balance, uint32 seq);
uint32 getTxnSeq() const { return mTxnSeq; } uint32 getTxnSeq() const { return mTxnSeq; }
@@ -45,20 +59,17 @@ public:
void credit(uint64 amount) { mTxnDelta+=amount; } void credit(uint64 amount) { mTxnDelta+=amount; }
void debit(uint64 amount) { mTxnDelta-=amount; } void debit(uint64 amount) { mTxnDelta-=amount; }
void setLedgerBalance(uint64_t lb) { mLgrBalance=lb; if(mTxnSeq==0) mTxnSeq=1; } void setLedgerBalance(uint64_t lb) { mLgrBalance=lb; if(mTxnSeq==0) mTxnSeq=1; }
void syncLedger(uint64_t lb, uint32 sq)
{ void syncLedger();
mLgrBalance=lb;
if(mTxnSeq<sq) mTxnSeq=sq;
}
}; };
class LocalAccountFamily class LocalAccountFamily : public boost::enable_shared_from_this<LocalAccountFamily>
{ // tracks families of local accounts { // tracks families of local accounts
public: public:
typedef boost::shared_ptr<LocalAccountFamily> pointer; typedef boost::shared_ptr<LocalAccountFamily> pointer;
protected: protected:
std::map<int, LocalAccountEntry::pointer> mAccounts; std::map<int, LocalAccount::pointer> mAccounts;
uint160 mFamily; // the name for this account family uint160 mFamily; // the name for this account family
EC_POINT* mRootPubKey; EC_POINT* mRootPubKey;
@@ -84,10 +95,11 @@ public:
void setName(const std::string& n) { mName=n; } void setName(const std::string& n) { mName=n; }
void setComment(const std::string& c) { mComment=c; } void setComment(const std::string& c) { mComment=c; }
std::map<int, LocalAccountEntry::pointer>& getAcctMap() { return mAccounts; } std::map<int, LocalAccount::pointer>& getAcctMap() { return mAccounts; }
LocalAccountEntry::pointer get(int seq); LocalAccount::pointer get(int seq);
uint160 getAccount(int seq, bool keep); uint160 getAccount(int seq, bool keep);
CKey::pointer getPrivateKey(int seq); CKey::pointer getPrivateKey(int seq);
CKey::pointer getPublicKey(int seq);
std::string getPubGenHex() const; // The text name of the public key std::string getPubGenHex() const; // The text name of the public key
std::string getShortName() const { return mName; } std::string getShortName() const { return mName; }
@@ -100,40 +112,4 @@ public:
void write(bool is_new); void write(bool is_new);
}; };
class LocalAccount
{ // tracks a single local account in a form that can be passed to other code.
public:
typedef boost::shared_ptr<LocalAccount> pointer;
protected:
LocalAccountFamily::pointer mFamily;
int mSeq;
public:
LocalAccount(LocalAccountFamily::pointer fam, int seq) : mFamily(fam), mSeq(seq) { ; }
uint160 getAddress() const;
bool isLocked() const;
std::string getShortName() const;
std::string getFullName() const;
std::string getFamilyName() const;
bool isIssued() const;
bool signRaw(Serializer::pointer);
bool signRaw(Serializer::pointer, std::vector<unsigned char>& signature);
bool checkSignRaw(Serializer::pointer data, std::vector<unsigned char>& signature);
uint32 getAcctSeq() const;
uint64 getBalance() const;
void setLedgerBalance(uint64_t ledgerBalance);
void syncLedger(uint64_t ledgerBalance, uint32_t ledgerSeq);
void incAcctSeq(uint32 transAcctSeq);
void syncLedger(const uint160& acctID);
CKey::pointer getPublicKey();
CKey::pointer getPrivateKey();
Json::Value getJson() const;
};
#endif #endif

View File

@@ -18,23 +18,22 @@
#define CHECK_NEW_FAMILIES 500 #define CHECK_NEW_FAMILIES 500
#endif #endif
LocalAccountEntry::LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey) : LocalAccount::LocalAccount(boost::shared_ptr<LocalAccountFamily> family, int accountSeq) :
mPublicKey(new CKey(accountFamily, rootPubKey, accountSeq)), mPublicKey(family->getPublicKey(accountSeq)), mFamily(family), mAccountSeq(accountSeq),
mAccountFamily(accountFamily), mAccountSeq(accountSeq),
mLgrBalance(0), mTxnDelta(0), mTxnSeq(0) mLgrBalance(0), mTxnDelta(0), mTxnSeq(0)
{ {
mAcctID=mPublicKey->GetAddress().GetHash160(); mAcctID=mPublicKey->GetAddress().GetHash160();
if(theApp!=NULL) mPublicKey=theApp->getPubKeyCache().store(mAcctID, mPublicKey); if(theApp!=NULL) mPublicKey=theApp->getPubKeyCache().store(mAcctID, mPublicKey);
} }
std::string LocalAccountEntry::getAccountName() const std::string LocalAccount::getAccountName() const
{ {
return mPublicKey->GetAddress().GetString(); return mPublicKey->GetAddress().GetString();
} }
std::string LocalAccountEntry::getLocalAccountName() const std::string LocalAccount::getLocalAccountName() const
{ {
return NewcoinAddress(mAccountFamily).GetString() + ":" + boost::lexical_cast<std::string>(mAccountSeq); return NewcoinAddress(mFamily->getFamily()).GetString() + ":" + boost::lexical_cast<std::string>(mAccountSeq);
} }
LocalAccountFamily::LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey) : LocalAccountFamily::LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey) :
@@ -52,13 +51,13 @@ LocalAccountFamily::~LocalAccountFamily()
uint160 LocalAccountFamily::getAccount(int seq, bool keep) uint160 LocalAccountFamily::getAccount(int seq, bool keep)
{ {
std::map<int, LocalAccountEntry::pointer>::iterator ait=mAccounts.find(seq); std::map<int, LocalAccount::pointer>::iterator ait=mAccounts.find(seq);
if(ait!=mAccounts.end()) return ait->second->getAccountID(); if(ait!=mAccounts.end()) return ait->second->getAddress();
LocalAccountEntry::pointer lae(new LocalAccountEntry(mFamily, seq, mRootPubKey)); LocalAccount::pointer lae(new LocalAccount(shared_from_this(), seq));
mAccounts.insert(std::make_pair(seq, lae)); mAccounts.insert(std::make_pair(seq, lae));
return lae->getAccountID(); return lae->getAddress();
} }
void LocalAccountFamily::unlock(const BIGNUM* privateKey) void LocalAccountFamily::unlock(const BIGNUM* privateKey)
@@ -98,6 +97,11 @@ void LocalAccountFamily::lock()
} }
} }
CKey::pointer LocalAccountFamily::getPublicKey(int seq)
{
return CKey::pointer(new CKey(mFamily, mRootPubKey, seq));
}
CKey::pointer LocalAccountFamily::getPrivateKey(int seq) CKey::pointer LocalAccountFamily::getPrivateKey(int seq)
{ {
if(!mRootPrivateKey) if(!mRootPrivateKey)
@@ -293,12 +297,12 @@ std::string LocalAccountFamily::getSQL() const
return ret; return ret;
} }
LocalAccountEntry::pointer LocalAccountFamily::get(int seq) LocalAccount::pointer LocalAccountFamily::get(int seq)
{ {
std::map<int, LocalAccountEntry::pointer>::iterator act=mAccounts.find(seq); std::map<int, LocalAccount::pointer>::iterator act=mAccounts.find(seq);
if(act!=mAccounts.end()) return act->second; if(act!=mAccounts.end()) return act->second;
LocalAccountEntry::pointer ret(new LocalAccountEntry(mFamily, seq, mRootPubKey)); LocalAccount::pointer ret(new LocalAccount(shared_from_this(), seq));
mAccounts.insert(std::make_pair(seq, ret)); mAccounts.insert(std::make_pair(seq, ret));
return ret; return ret;
} }
@@ -347,18 +351,16 @@ uint160 Wallet::findFamilyPK(const std::string& pubKey)
return fam->getFamily(); return fam->getFamily();
} }
uint160 LocalAccount::getAddress() const
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return uint160();
return la->getAccountID();
}
std::string LocalAccount::getShortName() const std::string LocalAccount::getShortName() const
{ {
std::string ret(mFamily->getShortName()); std::string ret, cmt;
if(!theApp->getWallet().getFamilyInfo(mFamily->getFamily(), ret, cmt))
ret=mFamily->getFamily().GetHex();
ret.append(":"); ret.append(":");
ret.append(boost::lexical_cast<std::string>(mSeq)); if(mName.empty())
ret.append(boost::lexical_cast<std::string>(mAccountSeq));
else ret.append(mName);
return ret; return ret;
} }
@@ -366,7 +368,7 @@ std::string LocalAccount::getFullName() const
{ {
std::string ret(mFamily->getFamily().GetHex()); std::string ret(mFamily->getFamily().GetHex());
ret.append(":"); ret.append(":");
ret.append(boost::lexical_cast<std::string>(mSeq)); ret.append(boost::lexical_cast<std::string>(mAccountSeq));
return ret; return ret;
} }
@@ -390,7 +392,7 @@ 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=getBalance(); uint64 eb=getEffectiveBalance();
if(eb!=0) ret["Balance"]=boost::lexical_cast<std::string>(eb); if(eb!=0) ret["Balance"]=boost::lexical_cast<std::string>(eb);
uint32 sq=getAcctSeq(); uint32 sq=getAcctSeq();
@@ -401,47 +403,12 @@ Json::Value LocalAccount::getJson() const
bool LocalAccount::isIssued() const bool LocalAccount::isIssued() const
{ {
return mSeq < mFamily->getSeq(); return mAccountSeq < mFamily->getSeq();
}
uint32 LocalAccount::getAcctSeq() const
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return 0;
return la->getAccountSeq();
}
uint64 LocalAccount::getBalance() const
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return 0;
return la->getEffectiveBalance();
}
void LocalAccount::syncLedger(uint64_t lb, uint32_t ls)
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return ;
la->syncLedger(lb, ls);
}
void LocalAccount::setLedgerBalance(uint64_t lb)
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return ;
la->setLedgerBalance(lb);
}
CKey::pointer LocalAccount::getPublicKey()
{
LocalAccountEntry::pointer la(mFamily->get(mSeq));
if(!la) return CKey::pointer();
return la->getPubKey();
} }
CKey::pointer LocalAccount::getPrivateKey() CKey::pointer LocalAccount::getPrivateKey()
{ {
return mFamily->getPrivateKey(mSeq); return mFamily->getPrivateKey(mAccountSeq);
} }
void Wallet::getFamilies(std::vector<uint160>& familyIDs) void Wallet::getFamilies(std::vector<uint160>& familyIDs)
@@ -560,7 +527,7 @@ LocalAccount::pointer Wallet::getNewLocalAccount(const uint160& family)
mAccounts.insert(std::make_pair(acct, lac)); mAccounts.insert(std::make_pair(acct, lac));
sl.unlock(); sl.unlock();
lac->syncLedger(acct); lac->syncLedger();
return lac; return lac;
} }
@@ -578,7 +545,7 @@ LocalAccount::pointer Wallet::getLocalAccount(const uint160& family, int seq)
mAccounts.insert(std::make_pair(acct, lac)); mAccounts.insert(std::make_pair(acct, lac));
sl.unlock(); sl.unlock();
lac->syncLedger(acct); lac->syncLedger();
return lac; return lac;
} }
@@ -594,7 +561,7 @@ LocalAccount::pointer Wallet::findAccountForTransaction(uint64 amount)
{ {
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
for(std::map<uint160, LocalAccount::pointer>::iterator it=mAccounts.begin(); it!=mAccounts.end(); ++it) for(std::map<uint160, LocalAccount::pointer>::iterator it=mAccounts.begin(); it!=mAccounts.end(); ++it)
if(!it->second->isLocked() && (it->second->getBalance()>=amount) ) if(!it->second->isLocked() && (it->second->getEffectiveBalance()>=amount) )
return it->second; return it->second;
return LocalAccount::pointer(); return LocalAccount::pointer();
} }
@@ -646,9 +613,9 @@ void Wallet::delFamily(const uint160& familyName)
std::map<uint160, LocalAccountFamily::pointer>::iterator fit=mFamilies.find(familyName); std::map<uint160, LocalAccountFamily::pointer>::iterator fit=mFamilies.find(familyName);
if(fit==mFamilies.end()) return; if(fit==mFamilies.end()) return;
std::map<int, LocalAccountEntry::pointer>& acctMap=fit->second->getAcctMap(); std::map<int, LocalAccount::pointer>& acctMap=fit->second->getAcctMap();
for(std::map<int, LocalAccountEntry::pointer>::iterator it=acctMap.begin(); it!=acctMap.end(); ++it) for(std::map<int, LocalAccount::pointer>::iterator it=acctMap.begin(); it!=acctMap.end(); ++it)
mAccounts.erase(it->second->getAccountID()); mAccounts.erase(it->second->getAddress());
mFamilies.erase(familyName); mFamilies.erase(familyName);
} }