From f3ed8b7ec9d65f877c208529adca0d9da8c30e92 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 30 Nov 2011 21:29:38 -0800 Subject: [PATCH] Updates, working to get the network code going. --- Application.cpp | 2 +- Application.h | 6 - Peer.cpp | 304 ++++++++++++++++++++++++++++++---------------- Peer.h | 30 +++-- TimingService.cpp | 6 +- TimingService.h | 2 +- Wallet.cpp | 7 +- keystore.cpp | 1 + newcoin.proto | 109 +++++++++++------ 9 files changed, 295 insertions(+), 172 deletions(-) diff --git a/Application.cpp b/Application.cpp index 1eff4609ae..bf2f95c774 100644 --- a/Application.cpp +++ b/Application.cpp @@ -2,6 +2,7 @@ #include "Config.h" #include "PeerDoor.h" #include "RPCDoor.h" +#include "BitcoinUtil.h" #include "database/SqliteDatabase.h" //#include #include @@ -26,7 +27,6 @@ Application::Application() mKnownNodes.load(); //mUNL.load(); mWallet.load(); - mLedgerMaster.load(); mPeerDoor=NULL; mRPCDoor=NULL; } diff --git a/Application.h b/Application.h index 2ad54b09a2..e4c5e9b968 100644 --- a/Application.h +++ b/Application.h @@ -4,9 +4,7 @@ #include "UniqueNodeList.h" #include "ConnectionPool.h" #include "KnownNodeList.h" -#include "LedgerMaster.h" #include "TimingService.h" -#include "ValidationCollection.h" #include "Wallet.h" #include "database/database.h" @@ -22,10 +20,8 @@ class Application UniqueNodeList mUNL; KnownNodeList mKnownNodes; Wallet mWallet; - ValidationCollection mValidations; Database* mDatabase; - LedgerMaster mLedgerMaster; ConnectionPool mConnectionPool; PeerDoor* mPeerDoor; @@ -40,9 +36,7 @@ public: Application(); ConnectionPool& getConnectionPool(){ return(mConnectionPool); } - LedgerMaster& getLedgerMaster(){ return(mLedgerMaster); } UniqueNodeList& getUNL(){ return(mUNL); } - ValidationCollection& getValidationCollection(){ return(mValidations); } Wallet& getWallet(){ return(mWallet); } Database* getDB(){ return(mDatabase); } diff --git a/Peer.cpp b/Peer.cpp index beca656ec4..2e58022d49 100644 --- a/Peer.cpp +++ b/Peer.cpp @@ -82,6 +82,199 @@ void Peer::sendPacket(PackedMessage::pointer packet) } } +void Peer::start_read_header() +{ + mReadbuf.resize(HEADER_SIZE); + asio::async_read(mSocket, asio::buffer(mReadbuf), + boost::bind(&Peer::handle_read_header, shared_from_this(), + asio::placeholders::error)); +} +void Peer::start_read_body(unsigned msg_len) +{ + // m_readbuf already contains the header in its first HEADER_SIZE + // bytes. Expand it to fit in the body as well, and start async + // read into the body. + // + mReadbuf.resize(HEADER_SIZE + msg_len); + asio::mutable_buffers_1 buf = asio::buffer(&mReadbuf[HEADER_SIZE], msg_len); + asio::async_read(mSocket, buf, + boost::bind(&Peer::handle_read_body, shared_from_this(), + asio::placeholders::error)); +} + +void Peer::handle_read_header(const boost::system::error_code& error) +{ + if(!error) + { + unsigned msg_len = PackedMessage::getLength(mReadbuf); + start_read_body(msg_len); + }else cout << "Peer::connected Error: " << error << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; +} + +void Peer::handle_read_body(const boost::system::error_code& error) +{ + if(!error) + { + processReadBuffer(); + start_read_header(); + }else cout << "Peer::connected Error: " << error << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; +} + + +void Peer::processReadBuffer() +{ + int type=PackedMessage::getType(mReadbuf); + switch(type) + { + case newcoin::HELLO: + { + newcoin::TMHello msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvHello(msg); + else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; + } + break; + + case newcoin::ERROR_MSG: + { + newcoin::TMErrorMsg msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvErrorMessage(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::PING: + { + newcoin::TMPing msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvPing(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::GET_CONTACTS: + { + newcoin::TMGetContacts msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvGetContacts(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::CONTACT: + { + newcoin::TMContact msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvContact(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::SEARCH_TRANSACTION: + { + newcoin::TMSearchTransaction msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvSearchTransaction(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::GET_ACCOUNT: + { + newcoin::TMGetAccount msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvGetAccount(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::ACCOUNT: + { + newcoin::TMAccount msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvAccount(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::TRANSACTION: + { + newcoin::TMTransaction msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvTransaction(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::GET_LEDGER: + { + newcoin::TMGetLedger msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvGetLedger(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::LEDGER: + { + newcoin::TMLedger msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvLedger(msg); + else cout << "pars error: " << type << endl; + } + +#if 0 + case newcoin::PROPOSE_LEDGER: + { + newcoin::TM msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recv(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::CLOSE_LEDGER: + { + newcoin::TM msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recv(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::GET_VALIDATION: + { + newcoin::TM msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recv(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::VALIDATION: + { + newcoin::TM msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recv(msg); + else cout << "pars error: " << type << endl; + } + +#endif + + case newcoin::GET_OBJECT: + { + newcoin::TMGetObjectByHash msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvGetObjectByHash(msg); + else cout << "pars error: " << type << endl; + } + + case newcoin::OBJECT: + { + newcoin::TMObjectByHash msg; + if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) + recvObjectByHash(msg); + else cout << "pars error: " << type << endl; + } + + default: + cout << "Unknown Msg: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; + } +} + + + +#if 0 + void Peer::sendHello() { newcoin::Hello* hello=new newcoin::Hello(); @@ -165,114 +358,6 @@ void Peer::sendGetFullLedger(uint256& hash) sendPacket(packet); } -void Peer::start_read_header() -{ - mReadbuf.resize(HEADER_SIZE); - asio::async_read(mSocket, asio::buffer(mReadbuf), - boost::bind(&Peer::handle_read_header, shared_from_this(), - asio::placeholders::error)); -} -void Peer::start_read_body(unsigned msg_len) -{ - // m_readbuf already contains the header in its first HEADER_SIZE - // bytes. Expand it to fit in the body as well, and start async - // read into the body. - // - mReadbuf.resize(HEADER_SIZE + msg_len); - asio::mutable_buffers_1 buf = asio::buffer(&mReadbuf[HEADER_SIZE], msg_len); - asio::async_read(mSocket, buf, - boost::bind(&Peer::handle_read_body, shared_from_this(), - asio::placeholders::error)); -} - -void Peer::handle_read_header(const boost::system::error_code& error) -{ - if(!error) - { - unsigned msg_len = PackedMessage::getLength(mReadbuf); - start_read_body(msg_len); - }else cout << "Peer::connected Error: " << error << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; -} - -void Peer::handle_read_body(const boost::system::error_code& error) -{ - if(!error) - { - processReadBuffer(); - start_read_header(); - }else cout << "Peer::connected Error: " << error << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; -} - -void Peer::processReadBuffer() -{ - int type=PackedMessage::getType(mReadbuf); - switch(type) - { - case newcoin::HELLO: - { - newcoin::Hello hello; - if(hello.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveHello(hello); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - } - break; - case newcoin::TRANSACTION: - { - TransactionPtr trans(new newcoin::Transaction()); - if(trans->ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveTransaction(trans); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - - } - break; - case newcoin::FULL_LEDGER: - { - newcoin::FullLedger ledger; - if(ledger.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveFullLedger(ledger); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - - } - break; - case newcoin::VALIDATION: - { - newcoin::Validation validation; - if(validation.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveValidation(validation); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - } - break; - case newcoin::PROPOSE_LEDGER: - { - newcoin::ProposeLedger prop; - if(prop.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveProposeLedger(prop); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - } - break; - - case newcoin::GET_FULL_LEDGER: - { - newcoin::GetFullLedger getFullLedger; - if(getFullLedger.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveGetFullLedger(getFullLedger); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - - } - case newcoin::GET_VALIDATIONS: - { - newcoin::GetValidations getValid; - if(getValid.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE)) - receiveGetValidations(getValid); - else cout << "parse error: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - - } - default: - cout << "Unknown Msg: " << type << endl; //else BOOST_LOG_TRIVIAL(info) << "Error: " << error; - } -} - - void Peer::receiveHello(newcoin::Hello& packet) { // TODO:6 add this guy to your KNL @@ -328,6 +413,8 @@ void Peer::receiveFullLedger(newcoin::FullLedger& packet) theApp->getLedgerMaster().addFullLedger(packet); } +#endif + void Peer::connectTo(KnownNode& node) { tcp::endpoint endpoint( address::from_string(node.mIP), node.mPort); @@ -335,3 +422,4 @@ void Peer::connectTo(KnownNode& node) boost::bind(&Peer::connected, this, asio::placeholders::error) ); } + diff --git a/Peer.h b/Peer.h index fc70309230..2979dc1f93 100644 --- a/Peer.h +++ b/Peer.h @@ -30,7 +30,6 @@ class Peer : public boost::enable_shared_from_this std::list mSendQ; PackedMessage::pointer mSendingPacket; - Peer(boost::asio::io_service& io_service); void handle_write(const boost::system::error_code& error, size_t bytes_transferred); @@ -44,17 +43,26 @@ class Peer : public boost::enable_shared_from_this void sendPacketForce(PackedMessage::pointer packet); - void sendHello(); - void sendTransaction(); + void sendTransaction(newcoin::TMTransaction& packet); void sendValidation(); - void receiveHello(newcoin::Hello& packet); - void receiveTransaction(TransactionPtr trans); - void receiveValidation(newcoin::Validation& packet); - void receiveFullLedger(newcoin::FullLedger& packet); - void receiveProposeLedger(newcoin::ProposeLedger& packet); - void receiveGetFullLedger(newcoin::GetFullLedger& packet); - void receiveGetValidations(newcoin::GetValidations& packet); + + void recvHello(newcoin::TMHello& packet); + void recvTransaction(newcoin::TMTransaction& packet); + void recvValidation(newcoin::TMValidation& packet); + void recvGetValidation(newcoin::TMGetValidations& packet); + void recvContact(newcoin::TMContact& packet); + void recvGetContacts(newcoin::TMGetContacts& packet); + void recvIndexedObject(newcoin::TMIndexedObject& packet); + void recvGetObjectByHash(newcoin::TMGetObjectByHash& packet); + void recvObjectByHash(newcoin::TMObjectByHash& packet); + void recvPing(newcoin::TMPing& packet); + void recvErrorMessage(newcoin::TMErrorMsg& packet); + void recvSearchTransaction(newcoin::TMSearchTransaction& packet); + void recvGetAccount(newcoin::TMGetAccount& packet); + void recvAccount(newcoin::TMAccount& packet); + void recvGetLedger(newcoin::TMGetLedger& packet); + void recvLedger(newcoin::TMLedger& packet); public: typedef boost::shared_ptr pointer; @@ -89,4 +97,4 @@ public: }; -#endif \ No newline at end of file +#endif diff --git a/TimingService.cpp b/TimingService.cpp index 62b0c65f7f..e55aa3b6af 100644 --- a/TimingService.cpp +++ b/TimingService.cpp @@ -32,22 +32,24 @@ void TimingService::start(boost::asio::io_service& ioService) void TimingService::handleLedger() { cout << "publish ledger" << endl; +#if 0 theApp->getLedgerMaster().startFinalization(); mLedgerTimer->expires_at(mLedgerTimer->expires_at() + boost::posix_time::seconds(theConfig.LEDGER_SECONDS)); mLedgerTimer->async_wait(boost::bind(&TimingService::handleLedger, this)); mPropTimer->expires_at(mLedgerTimer->expires_at() + boost::posix_time::seconds(theConfig.LEDGER_PROPOSAL_DELAY_SECONDS)); mPropTimer->async_wait(boost::bind(&TimingService::handleProp, this)); +#endif } void TimingService::handleProp() { - theApp->getLedgerMaster().sendProposal(); +// theApp->getLedgerMaster().sendProposal(); } void TimingService::handleValid() { - theApp->getLedgerMaster().endFinalization(); +// theApp->getLedgerMaster().endFinalization(); } int TimingService::getCurrentLedgerIndex() diff --git a/TimingService.h b/TimingService.h index b160ab7908..125effd3b6 100644 --- a/TimingService.h +++ b/TimingService.h @@ -23,4 +23,4 @@ public: static int getCurrentLedgerIndex(); }; -#endif \ No newline at end of file +#endif diff --git a/Wallet.cpp b/Wallet.cpp index 70d8044097..f2d8027561 100644 --- a/Wallet.cpp +++ b/Wallet.cpp @@ -10,18 +10,19 @@ LocalAccount::LocalAccount(bool) : mAmount(0), mSeqNum(0) mAddress.SetPubKey(mPublicKey.GetPubKey()); } -#if 0 - Wallet::Wallet() { } + void Wallet::load() { - + // WRITEME } +#if 0 + int64 Wallet::getBalance() { int64 total = 0; diff --git a/keystore.cpp b/keystore.cpp index 8689136ebc..a2a0b090e1 100644 --- a/keystore.cpp +++ b/keystore.cpp @@ -4,6 +4,7 @@ // file license.txt or http://www.opensource.org/licenses/mit-license.php. #include "keystore.h" +#include "BitcoinUtil.h" #include diff --git a/newcoin.proto b/newcoin.proto index ee3aee8b23..8ca8538085 100644 --- a/newcoin.proto +++ b/newcoin.proto @@ -7,35 +7,27 @@ enum MessageType { PING= 2; // network presence detection - GET_CONTACTS= 10; + GET_CONTACTS= 10; CONTACT= 11; // operations for 'small' nodes SEARCH_TRANSACTION= 20; GET_ACCOUNT= 21; - ACCOUNT= 22; + ACCOUNT= 22; // transaction and ledger processing TRANSACTION= 30; - GET_LEDGER= 31; - LEDGER= 32; + GET_LEDGER= 31; + LEDGER= 32; PROPOSE_LEDGER= 33; CLOSE_LEDGER= 34; // data replication and synchronization GET_VALIDATIONS= 40; - VALIDATION= 41; - GET_OBJECT= 42; - OBJECT= 43; -} - - -message TMBaseMessage { - required MessageType type = 1; - optional uint32 seq = 2; - optional uint32 querySeq = 3; // if this is a reply - required bytes innerMessage = 4; + VALIDATION= 41; + GET_OBJECT= 42; + OBJECT= 43; } @@ -55,25 +47,25 @@ you must first combine coins from one address to another. */ enum TransactionStatus { - NEW = 1; // origin node did/could not validate - CURRENT = 2; // scheduled to go in this ledger - COMMITED = 3; // in a closed ledger + NEW = 1; // origin node did/could not validate + CURRENT = 2; // scheduled to go in this ledger + COMMITED = 3; // in a closed ledger REJECT_CONFLICT = 4; REJECT_INVALID = 5; REJECT_FUNDS = 6; - HELD_SEQ = 7; - HELD_LEDGER = 8; // held for future ledger + HELD_SEQ = 7; + HELD_LEDGER = 8; // held for future ledger } message TMTransaction { - required bytes from = 1; - required bytes dest = 2; - required uint64 amount = 3; + required bytes from = 1; + required bytes dest = 2; + required uint64 amount = 3; required uint32 sourceLedgerIndex = 4; - required uint32 seqNum = 5; - required uint32 ident = 6; - required bytes pubKey = 7; - required bytes sig = 8; + required uint32 seqNum = 5; + required uint32 ident = 6; + required bytes pubKey = 7; + required bytes sig = 8; required TransactionStatus status = 9; optional uint64 receiveTimestamp = 10; optional uint32 ledgerIndexPossible = 11; // the node may not know @@ -89,7 +81,7 @@ message TMValidation { optional uint64 timestamp = 3; // only in proposed ledgers optional uint32 confidence = 4; // only in proposed ledgers required bytes hanko = 5; - required bytes sig = 6; + required bytes sig = 6; } @@ -103,13 +95,13 @@ message TMGetValidations { message TMContact { - required bytes pubKey = 1; + required bytes pubKey = 1; required uint32 softwareVersion = 2; required uint32 protoVersion = 3; - required uint64 nodeFlags = 4; - required uint64 timestamp = 5; - repeated bytes nodeInfo = 6; - required bytes signature = 7; + required uint64 nodeFlags = 4; + required uint64 timestamp = 5; + repeated bytes nodeInfo = 6; + required bytes signature = 7; } // request node information @@ -119,20 +111,43 @@ message TMGetContacts { } +message TMSearchTransaction { + required uint32 maxTrans =1; + optional bytes toAccount =2; + optional bytes fromAccount =3; + optional uint32 minLedger =4; + optional bytes fromAcctSeq =5; + repeated bytes transID =6; +} + +message TMGetAccount { + repeated bytes acctID =1; +} + +message Account { + required bytes accountID =1; + required uint64 balance =2; + required uint32 accountSeq =3; + required uint32 ledgerSeq =4; +} + +message TMAccount{ + repeated Account accounts =1; +} message TMIndexedObject { enum ObjectType { - TRANSACTION = 1; + TRANSACTION = 1; TRANSACTION_NODE = 2; // a node in a transaction tree TRANSACTION_LEAF = 3; // a leaf in a transaction tree - ACCOUNT = 4; // a single account state (with balance/sequence) + ACCOUNT = 4; // a single account state (with balance/sequence) ACCOUNT_NODE = 5; // a node in an account state tree ACCOUNT_LEAF = 6; // a leaf in an account state tree - LEDGER = 7; + LEDGER = 7; } - required bytes hash = 1; + required bytes hash = 1; required ObjectType type = 2; } @@ -148,10 +163,24 @@ message TMGetObjectByHash message TMObjectByHash { required TMIndexedObject object = 1; - required bytes data = 2; + required bytes data = 2; } +message LedgerNodes { + required bytes nodeid = 1; + required bytes nodedata = 2; +} +message TMGetLedger { + optional bytes hash = 1; + optional uint32 seq = 2; + repeated LedgerNodes nodes = 3; +} + +message TMLedger { + required bytes hash = 1; + repeated LedgerNodes nodes = 2; +} message TMPing { @@ -167,6 +196,6 @@ message TMPing { message TMErrorMsg { - optional int32 errorCode = 1; - optional string message = 2; + optional int32 errorCode = 1; + optional string message = 2; }