It makes much more sense to keep the from account pointer in the transaction

This commit is contained in:
JoelKatz
2011-11-18 20:07:57 -08:00
parent fa60ccd2df
commit 4d8894f71a
3 changed files with 46 additions and 24 deletions

View File

@@ -1,19 +1,24 @@
#ifndef __ACCOUNT__ #ifndef __ACCOUNT__
#define __ACCOUNT__ #define __ACCOUNT__
#include <boost/shared_ptr.hpp>
#include "key.h"
#include "uint256.h" #include "uint256.h"
class Account class Account
{ {
public:
typedef boost::shared_ptr<Account> pointer;
private: private:
uint160 mAddress; uint160 mAddress;
CKey pubKey; CKey pubKey;
public: public:
bool CheckSignRaw(const std::vector<unsigned char> &toSign, bool checkSignRaw(const std::vector<unsigned char> &toSign,
const std::vector<unsigned char> &signature) const; const std::vector<unsigned char> &signature) const;
const uint160& GetAddress(void) const { return mAddress; } const uint160& getAddress(void) const { return mAddress; }
CKey& peekPubKey() { return pubKey; } CKey& peekPubKey() { return pubKey; }
}; };

View File

@@ -16,41 +16,44 @@ Transaction::Transaction(TransStatus status, LocalAccount& fromLocalAccount, Acc
mAccountTo(toAccount), mAmount(amount), mFromAccountSeq(fromSeq), mSourceLedger(ledger), mAccountTo(toAccount), mAmount(amount), mFromAccountSeq(fromSeq), mSourceLedger(ledger),
mIdent(ident), mInLedger(0), mStatus(NEW) mIdent(ident), mInLedger(0), mStatus(NEW)
{ {
assert(fromAccount.GetAddress()==fromLocalAccount.mAddress); assert(fromAccount.getAddress()==fromLocalAccount.mAddress);
assert(fromLocalAccount.mAmount>=amount); assert(fromLocalAccount.mAmount>=amount);
assert((fromSeq+1)==fromLocalAccount.mSeqNum); assert((fromSeq+1)==fromLocalAccount.mSeqNum);
mAccountFrom=fromAccount.GetAddress(); mAccountFrom=fromAccount.getAddress();
sign(fromLocalAccount, fromAccount); sign(fromLocalAccount);
} }
bool Transaction::sign(LocalAccount& fromLocalAccount, Account& fromAccount) bool Transaction::sign(LocalAccount& fromLocalAccount)
{ {
if( (mAmount==0) || (mSourceLedger==0) || (mAccountTo==0) ) if( (mAmount==0) || (mSourceLedger==0) || (mAccountTo==0) )
return false; return false;
if((mAccountFrom!=fromLocalAccount.mAddress)||(mAccountFrom!=fromAccount.GetAddress())) if(mpAccountFrom == Account::pointer())
return false;
if((mpAccountFrom->getAddress()!=fromLocalAccount.mAddress) || (mAccountFrom!=mpAccountFrom->getAddress()))
return false; return false;
Serializer::pointer signBuf(getRawUnsigned(fromAccount)); Serializer::pointer signBuf(getRawUnsigned());
if(!signBuf->makeSignature(mSignature, fromLocalAccount.peekPrivKey())) if(!signBuf->makeSignature(mSignature, fromLocalAccount.peekPrivKey()))
return false; return false;
signBuf->addRaw(mSignature); signBuf->addRaw(mSignature);
mTransactionID=signBuf->getSHA512Half(); mTransactionID=signBuf->getSHA512Half();
} }
bool Transaction::checkSign(Account& fromAccount) const bool Transaction::checkSign() const
{ {
if(mAccountFrom!=fromAccount.GetAddress()) return false; if(mpAccountFrom == Account::pointer()) return false;
if(mpAccountFrom->getAddress()!=mAccountFrom) return false;
Serializer::pointer toSign(getRawUnsigned(fromAccount)); Serializer::pointer toSign(getRawUnsigned());
return toSign->checkSignature(mSignature, fromAccount.peekPubKey()); return toSign->checkSignature(mSignature, mpAccountFrom->peekPubKey());
} }
Serializer::pointer Transaction::getRawUnsigned(Account& fromAccount) const Serializer::pointer Transaction::getRawUnsigned() const
{ {
Serializer::pointer ret(new Serializer(104)); Serializer::pointer ret(new Serializer(104));
ret->add32(0x54584e00u); ret->add32(0x54584e00u);
ret->addRaw(fromAccount.peekPubKey().GetPubKey()); ret->addRaw(mpAccountFrom->peekPubKey().GetPubKey());
ret->add64(mAmount); ret->add64(mAmount);
ret->add32(mFromAccountSeq); ret->add32(mFromAccountSeq);
ret->add32(mInLedger); ret->add32(mInLedger);
@@ -58,7 +61,15 @@ Serializer::pointer Transaction::getRawUnsigned(Account& fromAccount) const
return ret; return ret;
} }
void Transaction::updateID(Account& fromAccount) Serializer::pointer Transaction::getRawSigned() const
{ {
mTransactionID=getRawSigned(fromAccount)->getSHA512Half(); Serializer::pointer ret(getRawUnsigned());
ret->addRaw(mSignature);
return ret;
}
void Transaction::updateID()
{
if( (mpAccountFrom!=Account::pointer()) && (mpAccountFrom->getAddress()==mAccountFrom))
mTransactionID=getRawSigned()->getSHA512Half();
} }

View File

@@ -1,13 +1,15 @@
#ifndef __TRANSACTION__ #ifndef __TRANSACTION__
#define __TRANSACTION__ #define __TRANSACTION__
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/cstdint.hpp>
#include "uint256.h" #include "uint256.h"
#include "newcoin.pb.h" #include "newcoin.pb.h"
#include "Hanko.h" #include "Hanko.h"
#include "Serializer.h" #include "Serializer.h"
#include <boost/shared_ptr.hpp> #include "Account.h"
#include <boost/enable_shared_from_this.hpp>
#include <boost/cstdint.hpp>
/* /*
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
@@ -41,6 +43,8 @@ private:
uint32 mInLedger; uint32 mInLedger;
TransStatus mStatus; TransStatus mStatus;
Account::pointer mpAccountFrom;
public: public:
Transaction(); Transaction();
Transaction(const std::vector<unsigned char> rawTransaction); Transaction(const std::vector<unsigned char> rawTransaction);
@@ -48,12 +52,14 @@ public:
Transaction(TransStatus Status, LocalAccount& fromLocal, Account& from, Transaction(TransStatus Status, LocalAccount& fromLocal, Account& from,
uint32 fromSeq, const uint160& to, uint64 amount, uint32 ident, uint32 ledger); uint32 fromSeq, const uint160& to, uint64 amount, uint32 ident, uint32 ledger);
bool sign(LocalAccount& fromLocalAccount, Account& fromAccount); void setFromAccountPointer(Account::pointer af) { mpAccountFrom=af; }
bool checkSign(Account& fromAccount) const;
void updateID(Account& fromAccount);
Serializer::pointer getRawUnsigned(Account& from) const; bool sign(LocalAccount& fromLocalAccount);
Serializer::pointer getRawSigned(Account& from) const; bool checkSign() const;
void updateID();
Serializer::pointer getRawUnsigned() const;
Serializer::pointer getRawSigned() const;
const uint256& getID() const { return mTransactionID; } const uint256& getID() const { return mTransactionID; }
const uint160& getFromAccount() const { return mAccountFrom; } const uint160& getFromAccount() const { return mAccountFrom; }