From ad2174ca72384eaf85fa216fd197a7bcbf99839d Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Mon, 14 May 2012 22:12:17 -0700 Subject: [PATCH] Rework Transaction with static constructors & support Claim --- src/Transaction.cpp | 180 ++++++++++++++++++++++++-------------------- src/Transaction.h | 53 ++++++++++--- 2 files changed, 142 insertions(+), 91 deletions(-) diff --git a/src/Transaction.cpp b/src/Transaction.cpp index ef39a564c0..f1620b12e8 100644 --- a/src/Transaction.cpp +++ b/src/Transaction.cpp @@ -11,47 +11,6 @@ #include "Serializer.h" #include "SerializedTransaction.h" -Transaction::Transaction(const NewcoinAddress& naPublicKey, const NewcoinAddress& naPrivateKey, - const NewcoinAddress& naFromAccount, const NewcoinAddress& toAccount, - uint64 amount, - uint32 iSeq, uint32 ident, uint32 ledger) : mInLedger(0), mStatus(NEW) -{ - mAccountFrom = naFromAccount; - - mFromPubKey = naPublicKey; - assert(mFromPubKey.isValid()); - - mTransaction = boost::make_shared(ttMAKE_PAYMENT); - - mTransaction->setSigningPubKey(mFromPubKey); - mTransaction->setSourceAccount(mAccountFrom); - - mTransaction->setSequence(iSeq); - - mTransaction->setTransactionFee(100); // for now - - mTransaction->setITFieldAccount(sfDestination, toAccount); - mTransaction->setITFieldU64(sfAmount, amount); - if (ledger != 0) - { - mTransaction->makeITFieldPresent(sfTargetLedger); - mTransaction->setITFieldU32(sfTargetLedger, ledger); - } - if (ident != 0) - { - mTransaction->makeITFieldPresent(sfSourceTag); - mTransaction->setITFieldU32(sfSourceTag, ident); - } - - if (!sign(naPrivateKey)) - { -#ifdef DEBUG - std::cerr << "Unable to sign transaction" << std::endl; -#endif - mStatus = INCOMPLETE; - } -} - Transaction::Transaction(const SerializedTransaction::pointer sit, bool bValidate) : mInLedger(0), mStatus(INVALID), mTransaction(sit) { @@ -87,67 +46,126 @@ Transaction::pointer Transaction::sharedTransaction(const std::vector& signature, uint32 ledgerSeq, TransStatus st) : - mAccountFrom(fromID), mFromPubKey(pubKey), mInLedger(ledgerSeq), mStatus(st) +Transaction::Transaction( + TransactionType ttKind, + const NewcoinAddress& naPublicKey, + const NewcoinAddress& naSourceAccount, + uint32 uSeq, + uint64 uFee, + uint32 uSourceTag) : + mInLedger(0), mStatus(NEW) { - mTransaction = boost::make_shared(ttMAKE_PAYMENT); - mTransaction->setSignature(signature); - mTransaction->setTransactionFee(fee); - mTransaction->setSigningPubKey(pubKey); // BROKEN - mTransaction->setSourceAccount(mAccountFrom); // BROKEN - mTransaction->setSequence(fromSeq); - if (fromLedger != 0) - { - mTransaction->makeITFieldPresent(sfTargetLedger); - mTransaction->setITFieldU32(sfTargetLedger, fromLedger); - } - if (ident != 0) + mAccountFrom = naSourceAccount; + mFromPubKey = naPublicKey; + assert(mFromPubKey.isValid()); + + mTransaction = boost::make_shared(ttKind); + + mTransaction->setSigningPubKey(mFromPubKey); + mTransaction->setSourceAccount(mAccountFrom); + mTransaction->setSequence(uSeq); + mTransaction->setTransactionFee(uFee); + + if (uSourceTag) { mTransaction->makeITFieldPresent(sfSourceTag); - mTransaction->setITFieldU32(sfSourceTag, ident); + mTransaction->setITFieldU32(sfSourceTag, uSourceTag); } - mTransaction->setITFieldU64(sfAmount, amount); - mTransaction->setITFieldAccount(sfDestination, toID.getAccountID()); - updateID(); } -#endif + +Transaction::pointer Transaction::setPayment( + const NewcoinAddress& naPrivateKey, + const NewcoinAddress& toAccount, + uint64 uAmount, + uint32 ledger) +{ + mTransaction->setITFieldAccount(sfDestination, toAccount); + mTransaction->setITFieldU64(sfAmount, uAmount); + + if (ledger != 0) + { + mTransaction->makeITFieldPresent(sfTargetLedger); + mTransaction->setITFieldU32(sfTargetLedger, ledger); + } + + sign(naPrivateKey); + + return shared_from_this(); +} + +Transaction::pointer Transaction::sharedPayment( + const NewcoinAddress& naPublicKey, const NewcoinAddress& naPrivateKey, + const NewcoinAddress& naSourceAccount, + uint32 uSeq, + uint64 uFee, + uint32 uSourceTag, + const NewcoinAddress& toAccount, + uint64 uAmount, + uint32 ledger) +{ + pointer tResult = boost::make_shared(ttMAKE_PAYMENT, + naPublicKey, naSourceAccount, + uSeq, uFee, uSourceTag); + + return tResult->setPayment(naPrivateKey, toAccount, uAmount, ledger); +} + +Transaction::pointer Transaction::setClaim( + const NewcoinAddress& naPrivateKey, + const NewcoinAddress& naGeneratorID, + const std::vector& vucGenerator) +{ + sign(naPrivateKey); + + return shared_from_this(); +} + +Transaction::pointer Transaction::sharedClaim( + const NewcoinAddress& naPublicKey, const NewcoinAddress& naPrivateKey, + const NewcoinAddress& naSourceAccount, + uint32 uSourceTag, + const NewcoinAddress& naGeneratorID, + const std::vector& vucGenerator) +{ + pointer tResult = boost::make_shared(ttCLAIM, + naPublicKey, naSourceAccount, + 0, // Sequence of 0. + 0, // Free. + uSourceTag); + + return tResult->setClaim(naPrivateKey, naGeneratorID, vucGenerator); +} bool Transaction::sign(const NewcoinAddress& naAccountPrivate) { - if(!naAccountPrivate.isValid()) + bool bResult = true; + + if (!naAccountPrivate.isValid()) { #ifdef DEBUG std::cerr << "No private key for signing" << std::endl; #endif - return false; + bResult = false; } - -#if 0 - if( (mTransaction->getITFieldU64(sfAmount)==0) ) - { -#ifdef DEBUG - std::cerr << "Bad amount or destination" << std::endl; -#endif - assert(false); - return false; - } -#endif - - if(!getSTransaction()->sign(naAccountPrivate)) + else if (!getSTransaction()->sign(naAccountPrivate)) { #ifdef DEBUG std::cerr << "Failed to make signature" << std::endl; #endif assert(false); - return false; + bResult = false; } - updateID(); + if (bResult) + { + updateID(); + } + else + { + mStatus = INCOMPLETE; + } - return true; + return bResult; } bool Transaction::checkSign() const diff --git a/src/Transaction.h b/src/Transaction.h index 20f068c657..9a336db450 100644 --- a/src/Transaction.h +++ b/src/Transaction.h @@ -30,35 +30,66 @@ enum TransStatus INCOMPLETE = 8 // needs more signatures }; +// This class is for constructing and examining transactions. Transactions are static so manipulation functions are unnecessary. class Transaction : public boost::enable_shared_from_this { public: - typedef boost::shared_ptr pointer; private: uint256 mTransactionID; NewcoinAddress mAccountFrom; - NewcoinAddress mFromPubKey; + NewcoinAddress mFromPubKey; // Sign transaction with this. mSignPubKey + NewcoinAddress mSourcePrivate; // Sign transaction with this. - uint32 mInLedger; - TransStatus mStatus; + uint32 mInLedger; + TransStatus mStatus; SerializedTransaction::pointer mTransaction; + Transaction::pointer setPayment( + const NewcoinAddress& naPrivateKey, + const NewcoinAddress& toAccount, + uint64 uAmount, + uint32 ledger); + + Transaction::pointer setClaim( + const NewcoinAddress& naPrivateKey, + const NewcoinAddress& naGeneratorID, + const std::vector& vucGenerator); + public: Transaction(const SerializedTransaction::pointer st, bool bValidate); static Transaction::pointer sharedTransaction(const std::vector&vucTransaction, bool bValidate); - Transaction(const NewcoinAddress& naPublicKey, const NewcoinAddress& naPrivateKey, - const NewcoinAddress& naFromAccount, const NewcoinAddress& toAccount, - uint64 amount, - uint32 iSeq, uint32 ident, uint32 ledger); + Transaction( + TransactionType ttKind, + const NewcoinAddress& naPublicKey, + const NewcoinAddress& naSourceAccount, + uint32 uSeq, + uint64 uFee, + uint32 uSourceTag); + static Transaction::pointer sharedPayment( + const NewcoinAddress& naPublicKey, const NewcoinAddress& naPrivateKey, + const NewcoinAddress& naSourceAccount, + uint32 uSeq, + uint64 uFee, + uint32 uSourceTag, + const NewcoinAddress& toAccount, + uint64 uAmount, + uint32 ledger); + + static Transaction::pointer sharedClaim( + const NewcoinAddress& naPublicKey, const NewcoinAddress& naPrivateKey, + const NewcoinAddress& naSourceAccount, + uint32 uSourceTag, + const NewcoinAddress& naGeneratorID, + const std::vector& vucGenerator); #if 0 Transaction(const NewcoinAddress& fromID, const NewcoinAddress& toID, - CKey::pointer pubKey, uint64 amount, uint64 fee, uint32 fromSeq, uint32 fromLedger, + CKey::pointer pubKey, uint64 uAmount, uint64 fee, uint32 fromSeq, uint32 fromLedger, uint32 ident, const std::vector& signature, uint32 ledgerSeq, TransStatus st); #endif @@ -106,9 +137,11 @@ public: protected: static Transaction::pointer transactionFromSQL(const std::string& statement); +#if 0 Transaction(const uint256& transactionID, const NewcoinAddress& accountFrom, const NewcoinAddress& accountTo, - CKey::pointer key, uint64 amount, uint64 fee, uint32 fromAccountSeq, uint32 sourceLedger, + CKey::pointer key, uint64 uAmount, uint64 fee, uint32 fromAccountSeq, uint32 sourceLedger, uint32 ident, const std::vector& signature, uint32 inLedger, TransStatus status); +#endif }; #endif