Begin work to track transactions involvings spends from local accounts.

This commit is contained in:
JoelKatz
2012-01-08 23:58:13 -08:00
parent fa16bb430b
commit 2782c2dafe
5 changed files with 163 additions and 129 deletions

View File

@@ -18,7 +18,6 @@
class Ledger : public boost::enable_shared_from_this<Ledger> class Ledger : public boost::enable_shared_from_this<Ledger>
{ // The basic Ledger structure, can be opened, closed, or synching { // The basic Ledger structure, can be opened, closed, or synching
public: public:
typedef boost::shared_ptr<Ledger> pointer; typedef boost::shared_ptr<Ledger> pointer;
enum TransResult enum TransResult

130
LocalAccount.h Normal file
View File

@@ -0,0 +1,130 @@
#ifndef __LOCALACCOUNT__
#define __LOCALACCOUNT__
class LocalAccountEntry
{ // tracks keys for local accounts
public:
typedef boost::shared_ptr<LocalAccountEntry> pointer;
protected:
// core account information
CKey::pointer mPublicKey;
uint160 mAcctID;
std::string mName, mComment;
// family information
uint160 mAccountFamily;
int mAccountSeq;
// local usage tracking
uint64 mBalance; // The balance, from the last ledger
uint64 mBalanceLT; // The balance, after all local transactions
uint32 mTxnSeq; // The sequence number of the next transaction
public:
LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey);
// Database operations
bool read(); // reads any existing data
bool write(); // creates the record in the first place
bool updateName(); // writes changed name/comment
bool updateBalance(); // writes changed balance/seq
const uint160& getAccountID() const { return mAcctID; }
int getAccountSeq() const { return mAccountSeq; }
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
CKey::pointer getPubKey() { return mPublicKey; }
void update(uint64 balance, uint32 seq);
uint32 getTxnSeq() const { return mTxnSeq; }
uint32 incTxnSeq() { return mTxnSeq++; }
uint64 getBalance() const { return mBalance; }
void credit(uint64 amount) { mBalance+=amount; }
void debit(uint64 amount) { assert(mBalance>=amount); mBalance-=amount; }
};
class LocalAccountFamily
{ // tracks families of local accounts
public:
typedef boost::shared_ptr<LocalAccountFamily> pointer;
protected:
std::map<int, LocalAccountEntry::pointer> mAccounts;
uint160 mFamily; // the name for this account family
EC_POINT* mRootPubKey;
uint32 mLastSeq;
std::string mName, mComment;
BIGNUM* mRootPrivateKey;
public:
LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey);
~LocalAccountFamily();
const uint160& getFamily() const { return mFamily; }
void unlock(const BIGNUM* privateKey);
void lock();
bool isLocked() const { return mRootPrivateKey==NULL; }
void setSeq(uint32 s) { mLastSeq=s; }
uint32 getSeq() { return mLastSeq; }
void setName(const std::string& n) { mName=n; }
void setComment(const std::string& c) { mComment=c; }
std::map<int, LocalAccountEntry::pointer>& getAcctMap() { return mAccounts; }
LocalAccountEntry::pointer get(int seq);
uint160 getAccount(int seq, bool keep);
CKey::pointer getPrivateKey(int seq);
std::string getPubGenHex() const; // The text name of the public key
std::string getShortName() const { return mName; }
std::string getComment() const { return mComment; }
Json::Value getJson() const;
static std::string getSQLFields();
std::string getSQL() const;
static LocalAccountFamily::pointer readFamily(const uint160& family);
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 incAcctSeq(uint32 transAcctSeq);
CKey::pointer getPublicKey();
CKey::pointer getPrivateKey();
Json::Value getJson() const;
};
#endif

View File

@@ -14,8 +14,8 @@
#include "newcoin.pb.h" #include "newcoin.pb.h"
#include "Hanko.h" #include "Hanko.h"
#include "Serializer.h" #include "Serializer.h"
#include "Wallet.h"
#include "SHAMap.h" #include "SHAMap.h"
#include "LocalAccount.h"
/* /*
We could have made something that inherited from the protobuf transaction but this seemed simpler We could have made something that inherited from the protobuf transaction but this seemed simpler

View File

@@ -6,6 +6,7 @@
#include "boost/foreach.hpp" #include "boost/foreach.hpp"
#include "boost/lexical_cast.hpp" #include "boost/lexical_cast.hpp"
#include "boost/interprocess/sync/scoped_lock.hpp"
#include "Wallet.h" #include "Wallet.h"
#include "NewcoinAddress.h" #include "NewcoinAddress.h"
@@ -19,7 +20,7 @@
LocalAccountEntry::LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey) : LocalAccountEntry::LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey) :
mPublicKey(new CKey(accountFamily, rootPubKey, accountSeq)), mPublicKey(new CKey(accountFamily, rootPubKey, accountSeq)),
mAccountFamily(accountFamily), mAccountSeq(accountSeq), mAccountFamily(accountFamily), mAccountSeq(accountSeq),
mBalance(0), mLedgerSeq(0), mTxnSeq(0) mBalance(0), mBalanceLT(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);
@@ -788,3 +789,23 @@ 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<uint160, LocalAccount::pointer>::iterator it=mAccounts.begin(); it!=mAccounts.end(); ++it)
{
LocalAccount::pointer& lac=it->second;
AccountState::pointer acs=ledger->getAccountState(it->first);
if(!acs)
{ // account is not in the ledger
// WRITEME
}
else
{ // account is in the ledger
// WRITEME
}
}
if(mLedger<ledger->getLedgerSeq()) mLedger=ledger->getLedgerSeq();
}

136
Wallet.h
View File

@@ -14,139 +14,21 @@
#include "uint256.h" #include "uint256.h"
#include "Serializer.h" #include "Serializer.h"
#include "LocalAccount.h"
#include "LocalTransaction.h"
class LocalAccountEntry class Ledger;
{ // tracks keys for local accounts
public:
typedef boost::shared_ptr<LocalAccountEntry> pointer;
protected:
// core account information
CKey::pointer mPublicKey;
uint160 mAcctID;
std::string mName, mComment;
// family information
uint160 mAccountFamily;
int mAccountSeq;
// local usage tracking
uint64 mBalance; // The balance, last we checked/updated
uint32 mLedgerSeq; // The ledger seq when we updated the balance
uint32 mTxnSeq; // The sequence number of the next transaction
public:
LocalAccountEntry(const uint160& accountFamily, int accountSeq, EC_POINT* rootPubKey);
// Database operations
bool read(); // reads any existing data
bool write(); // creates the record in the first place
bool updateName(); // writes changed name/comment
bool updateBalance(); // writes changed balance/seq
const uint160& getAccountID() const { return mAcctID; }
int getAccountSeq() const { return mAccountSeq; }
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
CKey::pointer getPubKey() { return mPublicKey; }
void update(uint64 balance, uint32 seq);
uint32 getTxnSeq() const { return mTxnSeq; }
uint32 incTxnSeq() { return mTxnSeq++; }
uint64 getBalance() const { return mBalance; }
void credit(uint64 amount) { mBalance+=amount; }
void debit(uint64 amount) { assert(mBalance>=amount); mBalance-=amount; }
};
class LocalAccountFamily
{ // tracks families of local accounts
public:
typedef boost::shared_ptr<LocalAccountFamily> pointer;
protected:
std::map<int, LocalAccountEntry::pointer> mAccounts;
uint160 mFamily; // the name for this account family
EC_POINT* mRootPubKey;
uint32 mLastSeq;
std::string mName, mComment;
BIGNUM* mRootPrivateKey;
public:
LocalAccountFamily(const uint160& family, const EC_GROUP* group, const EC_POINT* pubKey);
~LocalAccountFamily();
const uint160& getFamily() const { return mFamily; }
void unlock(const BIGNUM* privateKey);
void lock();
bool isLocked() const { return mRootPrivateKey==NULL; }
void setSeq(uint32 s) { mLastSeq=s; }
uint32 getSeq() { return mLastSeq; }
void setName(const std::string& n) { mName=n; }
void setComment(const std::string& c) { mComment=c; }
std::map<int, LocalAccountEntry::pointer>& getAcctMap() { return mAccounts; }
LocalAccountEntry::pointer get(int seq);
uint160 getAccount(int seq, bool keep);
CKey::pointer getPrivateKey(int seq);
std::string getPubGenHex() const; // The text name of the public key
std::string getShortName() const { return mName; }
std::string getComment() const { return mComment; }
Json::Value getJson() const;
static std::string getSQLFields();
std::string getSQL() const;
static LocalAccountFamily::pointer readFamily(const uint160& family);
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 incAcctSeq(uint32 transAcctSeq);
CKey::pointer getPublicKey();
CKey::pointer getPrivateKey();
Json::Value getJson() const;
};
class Wallet class Wallet
{ {
protected: protected:
boost::recursive_mutex mLock; boost::recursive_mutex mLock;
std::map<uint160, LocalAccountFamily::pointer> mFamilies; std::map<uint160, LocalAccountFamily::pointer> mFamilies;
std::map<uint160, LocalAccount::pointer> mAccounts; std::map<uint160, LocalAccount::pointer> mAccounts;
std::map<uint256, LocalTransaction::pointer> mTransactions;
uint32 mLedger; // ledger we last synched to
LocalAccountFamily::pointer doPrivate(const uint256& passPhrase, bool do_create, bool do_unlock); LocalAccountFamily::pointer doPrivate(const uint256& passPhrase, bool do_create, bool do_unlock);
LocalAccountFamily::pointer doPublic(const std::string& pubKey, bool do_create, bool do_db); LocalAccountFamily::pointer doPublic(const std::string& pubKey, bool do_create, bool do_db);
@@ -155,7 +37,7 @@ protected:
const std::string& name, const std::string& comment); const std::string& name, const std::string& comment);
public: public:
Wallet() { ; } Wallet() : mLedger(0) { ; }
uint160 addFamily(const std::string& passPhrase, bool lock); uint160 addFamily(const std::string& passPhrase, bool lock);
uint160 addFamily(const uint256& passPhrase, bool lock); uint160 addFamily(const uint256& passPhrase, bool lock);
@@ -197,6 +79,8 @@ public:
static std::string privKeyToText(const uint256& privKey); static std::string privKeyToText(const uint256& privKey);
static uint256 textToPrivKey(const std::string&); static uint256 textToPrivKey(const std::string&);
void syncToLedger(bool force, Ledger* ledger);
static bool unitTest(); static bool unitTest();
}; };