Rework AccountState to fit new model.

This commit is contained in:
JoelKatz
2012-04-23 15:54:56 -07:00
parent a641149834
commit cd73263f2f
2 changed files with 30 additions and 56 deletions

View File

@@ -1,42 +1,32 @@
#include <boost/lexical_cast.hpp>
#include "AccountState.h" #include "AccountState.h"
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#include "Ledger.h"
#include "Serializer.h" #include "Serializer.h"
AccountState::AccountState(const std::vector<unsigned char>& v) AccountState::AccountState(const NewcoinAddress& id) : mAccountID(id), mValid(false)
{ {
Serializer s(v); if (!id.IsValid()) return;
mValid=false; mLedgerEntry = boost::make_shared<SerializedLedgerEntry>(ltACCOUNT_ROOT);
uint160 acct160 = mAccountID.getAccountID(); mLedgerEntry->setIndex(Ledger::getAccountRootIndex(id));
mLedgerEntry->setIFieldAccount(sfAccount, id);
if(!s.get160(acct160, 0)) { assert(false); return; }
if(!s.get64(mBalance, 20)) { assert(false); return; }
if(!s.get32(mAccountSeq, 28)) { assert(false); return; }
mValid = true; mValid = true;
} }
AccountState::AccountState(const NewcoinAddress& id) : mAccountID(id), mBalance(0), mAccountSeq(0), mValid(true) AccountState::AccountState(SerializedLedgerEntry::pointer ledgerEntry) : mLedgerEntry(ledgerEntry), mValid(false)
{ ; } {
if (!mLedgerEntry) return;
std::vector<unsigned char> AccountState::getRaw() const if (mLedgerEntry->getType()!=ltACCOUNT_ROOT) return;
{ // 20-byte acct ID, 8-byte balance, 4-byte sequence mAccountID = mLedgerEntry->getValueFieldAccount(sfAccount);
Serializer s(32); if (mAccountID.IsValid()) mValid = true;
s.add160(mAccountID.getAccountID());
s.add64(mBalance);
s.add32(mAccountSeq);
return s.getData();
} }
void AccountState::addJson(Json::Value& val) void AccountState::addJson(Json::Value& val)
{ {
Json::Value as(Json::objectValue); val = mLedgerEntry->getJson(0);
if(!mValid) val["Invalid"]=true;
as["AccountID"]=mAccountID.humanAccountID();
as["Balance"]=boost::lexical_cast<std::string>(mBalance);
as["SendSequence"]=mAccountSeq;
if(!mValid) as["Invalid"]=true;
val[mAccountID.humanAccountID()]=as;
} }
// vim:ts=4 // vim:ts=4

View File

@@ -1,7 +1,8 @@
#ifndef __ACCOUNTSTATE__ #ifndef __ACCOUNTSTATE__
#define __ACCOUNTSTATE__ #define __ACCOUNTSTATE__
// An account's state in one or more accepted ledgers // An account's state
// Used to track information about a local account
#include <vector> #include <vector>
@@ -10,8 +11,8 @@
#include "../json/value.h" #include "../json/value.h"
#include "types.h" #include "types.h"
#include "uint256.h"
#include "NewcoinAddress.h" #include "NewcoinAddress.h"
#include "SerializedLedger.h"
class AccountState class AccountState
{ {
@@ -20,37 +21,20 @@ public:
private: private:
NewcoinAddress mAccountID; NewcoinAddress mAccountID;
uint64 mBalance; SerializedLedgerEntry::pointer mLedgerEntry;
uint32 mAccountSeq;
bool mValid; bool mValid;
public: public:
AccountState(const NewcoinAddress& mAccountID); // new account AccountState(const NewcoinAddress& AccountID); // For new accounts
AccountState(const std::vector<unsigned char>&); // raw form AccountState(SerializedLedgerEntry::pointer ledgerEntry); // For accounts in a ledger
const NewcoinAddress& getAccountID() const { return mAccountID; } const NewcoinAddress& getAccountID() const { return mAccountID; }
uint64 getBalance() const { return mBalance; } uint64 getBalance() const { return mLedgerEntry->getIFieldU64(sfBalance); }
uint32 getSeq() const { return mAccountSeq; } uint32 getSeq() const { return mLedgerEntry->getIFieldU32(sfSequence); }
void credit(const uint64& a) SerializedLedgerEntry::pointer getSLE() { return mLedgerEntry; }
{ const SerializedLedgerEntry& peekSLE() const { return *mLedgerEntry; }
mBalance+=a; SerializedLedgerEntry& peekSLE() { return *mLedgerEntry; }
if(!mAccountSeq) mAccountSeq=1; // an account with non-0 balance cannot have 0 sequence
}
void charge(const uint64& a)
{
assert(mBalance>=a);
mBalance-=a;
}
void incSeq()
{
mAccountSeq++;
}
void decSeq()
{
assert(mAccountSeq!=0);
mAccountSeq--;
}
std::vector<unsigned char> getRaw() const; std::vector<unsigned char> getRaw() const;
void addJson(Json::Value& value); void addJson(Json::Value& value);