From ad8ff1280316d31e168275eee241e4818683d57d Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 20 Apr 2012 13:30:18 -0700 Subject: [PATCH] Fix mishandling of signing account. Fix failure to set transaction type. --- src/SerializedTransaction.cpp | 27 +++++++++++------ src/SerializedTransaction.h | 14 +++++---- src/Transaction.cpp | 55 +++++++++++++++++------------------ 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/src/SerializedTransaction.cpp b/src/SerializedTransaction.cpp index a4a94b3675..2282f051d4 100644 --- a/src/SerializedTransaction.cpp +++ b/src/SerializedTransaction.cpp @@ -1,9 +1,9 @@ #include "SerializedTransaction.h" -SerializedTransaction::SerializedTransaction(TransactionType type) +SerializedTransaction::SerializedTransaction(TransactionType type) : mType(type) { - mFormat=getTxnFormat(type); + mFormat = getTxnFormat(type); if (mFormat == NULL) throw std::runtime_error("invalid transaction type"); mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic)); @@ -31,13 +31,21 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length mMiddleTxn.giveObject(new STVariableLength("SigningAccount", sit.getVL())); mMiddleTxn.giveObject(new STUInt32("Sequence", sit.get32())); - int type = sit.get32(); - mMiddleTxn.giveObject(new STUInt32("Type", type)); - mFormat = getTxnFormat(static_cast(type)); + mType = static_cast(sit.get32()); + mMiddleTxn.giveObject(new STUInt32("Type", static_cast(mType))); + mFormat = getTxnFormat(mType); if (!mFormat) throw std::runtime_error("Transaction has invalid type"); mMiddleTxn.giveObject(new STUInt64("Fee", sit.get64())); mInnerTxn = STObject(mFormat->elements, sit, "InnerTransaction"); + updateSigningAccount(); +} + +void SerializedTransaction::updateSigningAccount() +{ + NewcoinAddress a; + a.setAccountPublic(peekRawSigningAccount()); + mSigningAccount = a.getAccountID(); } int SerializedTransaction::getLength() const @@ -81,7 +89,7 @@ bool SerializedTransaction::isEquivalent(const SerializedType& t) const { // Signatures are not compared const SerializedTransaction* v = dynamic_cast(&t); if (!v) return false; - if (type != v->type) return false; + if (mType != v->mType) return false; if (mMiddleTxn != v->mMiddleTxn) return false; if (mInnerTxn != v->mInnerTxn) return false; return true; @@ -171,7 +179,7 @@ void SerializedTransaction::setSequence(uint32 seq) v->setValue(seq); } -std::vector SerializedTransaction::getSigningAccount() const +std::vector SerializedTransaction::getRawSigningAccount() const { const STVariableLength* v = dynamic_cast(mMiddleTxn.peekAtPIndex(TransactionISigningAccount)); @@ -179,7 +187,7 @@ std::vector SerializedTransaction::getSigningAccount() const return v->getValue(); } -const std::vector& SerializedTransaction::peekSigningAccount() const +const std::vector& SerializedTransaction::peekRawSigningAccount() const { const STVariableLength* v= dynamic_cast(mMiddleTxn.peekAtPIndex(TransactionISigningAccount)); @@ -187,7 +195,7 @@ const std::vector& SerializedTransaction::peekSigningAccount() co return v->peekValue(); } -std::vector& SerializedTransaction::peekSigningAccount() +std::vector& SerializedTransaction::peekRawSigningAccount() { STVariableLength* v = dynamic_cast(mMiddleTxn.getPIndex(TransactionISigningAccount)); if (!v) throw std::runtime_error("corrupt transaction"); @@ -199,6 +207,7 @@ void SerializedTransaction::setSigningAccount(const std::vector& STVariableLength* v = dynamic_cast(mMiddleTxn.getPIndex(TransactionISigningAccount)); if (!v) throw std::runtime_error("corrupt transaction"); v->setValue(s); + updateSigningAccount(); } int SerializedTransaction::getITFieldIndex(SOE_Field field) const diff --git a/src/SerializedTransaction.h b/src/SerializedTransaction.h index 142e93f200..3ebe854240 100644 --- a/src/SerializedTransaction.h +++ b/src/SerializedTransaction.h @@ -15,11 +15,14 @@ public: typedef boost::shared_ptr pointer; protected: - TransactionType type; + uint160 mSigningAccount; + TransactionType mType; STVariableLength mSignature; STObject mMiddleTxn, mInnerTxn; TransactionFormat* mFormat; + void updateSigningAccount(); + public: SerializedTransaction(SerializerIterator& sit, int length); // -1=all remaining, 0=get from sit SerializedTransaction(TransactionType type); @@ -42,12 +45,13 @@ public: // middle transaction functions uint32 getVersion() const; void setVersion(uint32); - TransactionType getTxnType() const { return type; } + TransactionType getTxnType() const { return mType; } uint64 getTransactionFee() const; void setTransactionFee(uint64); - std::vector getSigningAccount() const; - const std::vector& peekSigningAccount() const; - std::vector& peekSigningAccount(); + uint160 getSigningAccount() const; + std::vector getRawSigningAccount() const; + const std::vector& peekRawSigningAccount() const; + std::vector& peekRawSigningAccount(); void setSigningAccount(const std::vector& s); std::string getTransactionType() const { return mFormat->t_name; } diff --git a/src/Transaction.cpp b/src/Transaction.cpp index bd54e6b1c2..a999e2dc8e 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -14,53 +14,52 @@ Transaction::Transaction(LocalAccount::pointer fromLocalAccount, const NewcoinAddress& toAccount, uint64 amount, uint32 ident, uint32 ledger) : mInLedger(0), mStatus(NEW) { - mAccountFrom=fromLocalAccount->getAddress(); - mAccountTo=toAccount; + mAccountFrom = fromLocalAccount->getAddress(); + mAccountTo = toAccount; - mTransaction=boost::make_shared(ttMAKE_PAYMENT); + mTransaction = boost::make_shared(ttMAKE_PAYMENT); - mFromPubKey=fromLocalAccount->getPublicKey(); + mFromPubKey = fromLocalAccount->getPublicKey(); assert(mFromPubKey); mTransaction->setSigningAccount(mFromPubKey->GetPubKey()); mTransaction->setSequence(fromLocalAccount->getTxnSeq()); - assert(mTransaction->getSequence()!=0); + assert(mTransaction->getSequence() != 0); mTransaction->setTransactionFee(100); // for now mTransaction->setITFieldVL(sfDestination, toAccount.getAccountPublic()); mTransaction->setITFieldU64(sfAmount, amount); - if(ledger!=0) + if (ledger != 0) { mTransaction->makeITFieldPresent(sfTargetLedger); mTransaction->setITFieldU32(sfTargetLedger, ledger); } - if(ident!=0) + if (ident != 0) { mTransaction->makeITFieldPresent(sfSourceTag); mTransaction->setITFieldU32(sfSourceTag, ident); } assert(mFromPubKey); - if(!sign(fromLocalAccount)) + if (!sign(fromLocalAccount)) { #ifdef DEBUG std::cerr << "Unable to sign transaction" << std::endl; #endif - mStatus=INCOMPLETE; + mStatus = INCOMPLETE; } } Transaction::Transaction(SerializedTransaction::pointer sit, bool validate) : mStatus(INVALID), mTransaction(sit) { - uint160 toAccountID; - uint160 fromAccountID; + uint160 toAccountID, fromAccountID; std::vector pubKey; try { - toAccountID=mTransaction->getITFieldH160(sfDestination); - pubKey=mTransaction->getSigningAccount(); - mTransactionID=mTransaction->getTransactionID(); + toAccountID = mTransaction->getITFieldH160(sfDestination); + pubKey = mTransaction->getRawSigningAccount(); + mTransactionID = mTransaction->getTransactionID(); } catch(...) { @@ -70,13 +69,13 @@ Transaction::Transaction(SerializedTransaction::pointer sit, bool validate) : mS mAccountTo.setAccountID(toAccountID); mAccountFrom.setAccountID(fromAccountID); - mFromPubKey=boost::make_shared(); - if(!mFromPubKey->SetPubKey(pubKey)) return; + mFromPubKey = boost::make_shared(); + if (!mFromPubKey->SetPubKey(pubKey)) return; mAccountFrom.setAccountPublic(pubKey); - mFromPubKey=theApp->getPubKeyCache().store(mAccountFrom, mFromPubKey); + mFromPubKey = theApp->getPubKeyCache().store(mAccountFrom, mFromPubKey); - if(!validate || checkSign()) - mStatus=NEW; + if (!validate || checkSign()) + mStatus = NEW; } Transaction::Transaction(const std::vector& raw, bool validate) : mStatus(INVALID) @@ -89,22 +88,22 @@ Transaction::Transaction(const std::vector& raw, bool validate) : { Serializer s(raw); SerializerIterator sit(s); - mTransaction=boost::make_shared(boost::ref(sit), -1); + mTransaction = boost::make_shared(boost::ref(sit), -1); - mFromPubKey=boost::make_shared(); - if(!mFromPubKey->SetPubKey(pubKey)) return; + mFromPubKey = boost::make_shared(); + if (!mFromPubKey->SetPubKey(pubKey)) return; mAccountFrom.setAccountPublic(pubKey); - mFromPubKey=theApp->getPubKeyCache().store(mAccountFrom, mFromPubKey); - if(!mFromPubKey->SetPubKey(pubKey)) return; + mFromPubKey = theApp->getPubKeyCache().store(mAccountFrom, mFromPubKey); + if (!mFromPubKey->SetPubKey(pubKey)) return; mAccountFrom.setAccountPublic(pubKey); - mFromPubKey=theApp->getPubKeyCache().store(mAccountFrom, mFromPubKey); + mFromPubKey = theApp->getPubKeyCache().store(mAccountFrom, mFromPubKey); } - catch(...) + catch (...) { return; } - if(!validate || checkSign()) - mStatus=NEW; + if (!validate || checkSign()) + mStatus = NEW; } Transaction::Transaction(const NewcoinAddress& fromID, const NewcoinAddress& toID,