From 06d69888fd3aebd8d903a690f14bec7b516d2bbb Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 19 Jan 2012 15:56:06 -0800 Subject: [PATCH] Complete initial implementation of transaction relaying. --- ConnectionPool.cpp | 2 +- NetworkOPs.cpp | 15 +++++++++++++-- NetworkOPs.h | 17 +++++++++++++++-- Peer.cpp | 36 ++++++++++++++++++++++++++++++++---- Peer.h | 3 ++- 5 files changed, 63 insertions(+), 10 deletions(-) diff --git a/ConnectionPool.cpp b/ConnectionPool.cpp index e6c0a1755..c56c36dcd 100644 --- a/ConnectionPool.cpp +++ b/ConnectionPool.cpp @@ -38,7 +38,7 @@ bool ConnectionPool::isMessageKnown(PackedMessage::pointer msg) */ -void ConnectionPool::relayMessage(Peer* fromPeer,PackedMessage::pointer msg) +void ConnectionPool::relayMessage(Peer* fromPeer, PackedMessage::pointer msg) { BOOST_FOREACH(Peer::pointer peer, mPeers) { diff --git a/NetworkOPs.cpp b/NetworkOPs.cpp index 2e22b7021..deb2fbf8b 100644 --- a/NetworkOPs.cpp +++ b/NetworkOPs.cpp @@ -24,7 +24,7 @@ uint32 NetworkOPs::getCurrentLedgerID() return theApp->getMasterLedger().getCurrentLedger()->getLedgerSeq(); } -Transaction::pointer NetworkOPs::processTransaction(Transaction::pointer trans) +Transaction::pointer NetworkOPs::processTransaction(Transaction::pointer trans, Peer* source) { Transaction::pointer dbtx=Transaction::load(trans->getID()); if(dbtx) return dbtx; @@ -57,8 +57,19 @@ Transaction::pointer NetworkOPs::processTransaction(Transaction::pointer trans) if(r==Ledger::TR_SUCCESS) { trans->setStatus(INCLUDED); - // WRITEME: send to others theApp->getWallet().applyTransaction(trans); + + newcoin::TMTransaction *tx=new newcoin::TMTransaction(); + + Serializer::pointer s(trans->getSigned()); + tx->set_rawtransaction(&s->getData().front(), s->getLength()); + tx->set_status(newcoin::tsCURRENT); + tx->set_receivetimestamp(getNetworkTime()); + tx->set_ledgerindexpossible(trans->getLedger()); + + PackedMessage::pointer packet(new PackedMessage(PackedMessage::MessagePointer(tx), newcoin::mtTRANSACTION)); + theApp->getConnectionPool().relayMessage(source, packet); + return trans; } diff --git a/NetworkOPs.h b/NetworkOPs.h index 29044aeb9..aa8b16868 100644 --- a/NetworkOPs.h +++ b/NetworkOPs.h @@ -6,22 +6,35 @@ // Operations that clients may wish to perform against the network +class Peer; + class NetworkOPs { enum Fault - { + { // exceptions these functions can throw IO_ERROR=1, NO_NETWORK=2, }; + enum OperatingMode + { // how we process transactions or account balance requests + FAULTED=0, // we are unable to process requests (not ready or no network) + FULL_LOCAL=1, // we are in full local sync + PART_LOCAL=2, // we can validate remote data but have to request it + REMOTE=3 // we have to trust remote nodes + }; + public: + // context information + OperatingMode getOperatingMode(); + // network information uint64 getNetworkTime(); uint32 getCurrentLedgerID(); // transaction operations - Transaction::pointer processTransaction(Transaction::pointer transaction); + Transaction::pointer processTransaction(Transaction::pointer transaction, Peer* source=NULL); Transaction::pointer findTransactionByID(const uint256& transactionID); int findTransactionsBySource(std::list&, const uint160& sourceAccount, uint32 minSeq, uint32 maxSeq); diff --git a/Peer.cpp b/Peer.cpp index 49d0c1a14..2c5c2de79 100644 --- a/Peer.cpp +++ b/Peer.cpp @@ -1,11 +1,14 @@ + +#include + +#include +//#include +#include + #include "Peer.h" #include "KnownNodeList.h" #include "Config.h" #include "Application.h" -#include -//#include -#include -#include #include "Conversion.h" using namespace std; @@ -279,6 +282,31 @@ void Peer::recvHello(newcoin::TMHello& packet) void Peer::recvTransaction(newcoin::TMTransaction& packet) { +#ifdef DEBUG + std::cerr << "Got transaction from peer" << std::endl; +#endif + + std::string rawTx=packet.rawtransaction(); + std::vector rTx(rawTx.size()); + memcpy(&rTx.front(), rawTx.data(), rawTx.size()); + Transaction::pointer tx(new Transaction(rTx, true)); + + if(tx->getStatus()==INVALID) + { // transaction fails basic validity tests +#ifdef DEBUG + std::cerr << "Transaction from peer fails validity tests" << std::endl; +#endif + return; + } + + tx=theApp->getOPs().processTransaction(tx, this); + + if(tx->getStatus()!=INCLUDED) + { // transaction wasn't accepted into ledger +#ifdef DEBUG + std::cerr << "Transaction from peer won't go in ledger" << std::endl; +#endif + } } void Peer::recvValidation(newcoin::TMValidation& packet) diff --git a/Peer.h b/Peer.h index fd1df5ded..6a98dc421 100644 --- a/Peer.h +++ b/Peer.h @@ -5,11 +5,12 @@ #include #include #include + #include "newcoin.pb.h" #include "PackedMessage.h" #include "Ledger.h" #include "Transaction.h" -#include "list" +#include "NetworkOPs.h" class Peer : public boost::enable_shared_from_this {