Updates, working to get the network code going.

This commit is contained in:
JoelKatz
2011-11-30 21:29:38 -08:00
parent 284445a7fa
commit f3ed8b7ec9
9 changed files with 295 additions and 172 deletions

View File

@@ -2,6 +2,7 @@
#include "Config.h" #include "Config.h"
#include "PeerDoor.h" #include "PeerDoor.h"
#include "RPCDoor.h" #include "RPCDoor.h"
#include "BitcoinUtil.h"
#include "database/SqliteDatabase.h" #include "database/SqliteDatabase.h"
//#include <boost/log/trivial.hpp> //#include <boost/log/trivial.hpp>
#include <iostream> #include <iostream>
@@ -26,7 +27,6 @@ Application::Application()
mKnownNodes.load(); mKnownNodes.load();
//mUNL.load(); //mUNL.load();
mWallet.load(); mWallet.load();
mLedgerMaster.load();
mPeerDoor=NULL; mPeerDoor=NULL;
mRPCDoor=NULL; mRPCDoor=NULL;
} }

View File

@@ -4,9 +4,7 @@
#include "UniqueNodeList.h" #include "UniqueNodeList.h"
#include "ConnectionPool.h" #include "ConnectionPool.h"
#include "KnownNodeList.h" #include "KnownNodeList.h"
#include "LedgerMaster.h"
#include "TimingService.h" #include "TimingService.h"
#include "ValidationCollection.h"
#include "Wallet.h" #include "Wallet.h"
#include "database/database.h" #include "database/database.h"
@@ -22,10 +20,8 @@ class Application
UniqueNodeList mUNL; UniqueNodeList mUNL;
KnownNodeList mKnownNodes; KnownNodeList mKnownNodes;
Wallet mWallet; Wallet mWallet;
ValidationCollection mValidations;
Database* mDatabase; Database* mDatabase;
LedgerMaster mLedgerMaster;
ConnectionPool mConnectionPool; ConnectionPool mConnectionPool;
PeerDoor* mPeerDoor; PeerDoor* mPeerDoor;
@@ -40,9 +36,7 @@ public:
Application(); Application();
ConnectionPool& getConnectionPool(){ return(mConnectionPool); } ConnectionPool& getConnectionPool(){ return(mConnectionPool); }
LedgerMaster& getLedgerMaster(){ return(mLedgerMaster); }
UniqueNodeList& getUNL(){ return(mUNL); } UniqueNodeList& getUNL(){ return(mUNL); }
ValidationCollection& getValidationCollection(){ return(mValidations); }
Wallet& getWallet(){ return(mWallet); } Wallet& getWallet(){ return(mWallet); }
Database* getDB(){ return(mDatabase); } Database* getDB(){ return(mDatabase); }

304
Peer.cpp
View File

@@ -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() void Peer::sendHello()
{ {
newcoin::Hello* hello=new newcoin::Hello(); newcoin::Hello* hello=new newcoin::Hello();
@@ -165,114 +358,6 @@ void Peer::sendGetFullLedger(uint256& hash)
sendPacket(packet); 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) void Peer::receiveHello(newcoin::Hello& packet)
{ {
// TODO:6 add this guy to your KNL // TODO:6 add this guy to your KNL
@@ -328,6 +413,8 @@ void Peer::receiveFullLedger(newcoin::FullLedger& packet)
theApp->getLedgerMaster().addFullLedger(packet); theApp->getLedgerMaster().addFullLedger(packet);
} }
#endif
void Peer::connectTo(KnownNode& node) void Peer::connectTo(KnownNode& node)
{ {
tcp::endpoint endpoint( address::from_string(node.mIP), node.mPort); 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) ); boost::bind(&Peer::connected, this, asio::placeholders::error) );
} }

28
Peer.h
View File

@@ -30,7 +30,6 @@ class Peer : public boost::enable_shared_from_this<Peer>
std::list<PackedMessage::pointer> mSendQ; std::list<PackedMessage::pointer> mSendQ;
PackedMessage::pointer mSendingPacket; PackedMessage::pointer mSendingPacket;
Peer(boost::asio::io_service& io_service); Peer(boost::asio::io_service& io_service);
void handle_write(const boost::system::error_code& error, size_t bytes_transferred); 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<Peer>
void sendPacketForce(PackedMessage::pointer packet); void sendPacketForce(PackedMessage::pointer packet);
void sendHello(); void sendHello();
void sendTransaction(); void sendTransaction(newcoin::TMTransaction& packet);
void sendValidation(); void sendValidation();
void receiveHello(newcoin::Hello& packet);
void receiveTransaction(TransactionPtr trans); void recvHello(newcoin::TMHello& packet);
void receiveValidation(newcoin::Validation& packet); void recvTransaction(newcoin::TMTransaction& packet);
void receiveFullLedger(newcoin::FullLedger& packet); void recvValidation(newcoin::TMValidation& packet);
void receiveProposeLedger(newcoin::ProposeLedger& packet); void recvGetValidation(newcoin::TMGetValidations& packet);
void receiveGetFullLedger(newcoin::GetFullLedger& packet); void recvContact(newcoin::TMContact& packet);
void receiveGetValidations(newcoin::GetValidations& 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: public:
typedef boost::shared_ptr<Peer> pointer; typedef boost::shared_ptr<Peer> pointer;

View File

@@ -32,22 +32,24 @@ void TimingService::start(boost::asio::io_service& ioService)
void TimingService::handleLedger() void TimingService::handleLedger()
{ {
cout << "publish ledger" << endl; cout << "publish ledger" << endl;
#if 0
theApp->getLedgerMaster().startFinalization(); theApp->getLedgerMaster().startFinalization();
mLedgerTimer->expires_at(mLedgerTimer->expires_at() + boost::posix_time::seconds(theConfig.LEDGER_SECONDS)); mLedgerTimer->expires_at(mLedgerTimer->expires_at() + boost::posix_time::seconds(theConfig.LEDGER_SECONDS));
mLedgerTimer->async_wait(boost::bind(&TimingService::handleLedger, this)); mLedgerTimer->async_wait(boost::bind(&TimingService::handleLedger, this));
mPropTimer->expires_at(mLedgerTimer->expires_at() + boost::posix_time::seconds(theConfig.LEDGER_PROPOSAL_DELAY_SECONDS)); mPropTimer->expires_at(mLedgerTimer->expires_at() + boost::posix_time::seconds(theConfig.LEDGER_PROPOSAL_DELAY_SECONDS));
mPropTimer->async_wait(boost::bind(&TimingService::handleProp, this)); mPropTimer->async_wait(boost::bind(&TimingService::handleProp, this));
#endif
} }
void TimingService::handleProp() void TimingService::handleProp()
{ {
theApp->getLedgerMaster().sendProposal(); // theApp->getLedgerMaster().sendProposal();
} }
void TimingService::handleValid() void TimingService::handleValid()
{ {
theApp->getLedgerMaster().endFinalization(); // theApp->getLedgerMaster().endFinalization();
} }
int TimingService::getCurrentLedgerIndex() int TimingService::getCurrentLedgerIndex()

View File

@@ -10,18 +10,19 @@ LocalAccount::LocalAccount(bool) : mAmount(0), mSeqNum(0)
mAddress.SetPubKey(mPublicKey.GetPubKey()); mAddress.SetPubKey(mPublicKey.GetPubKey());
} }
#if 0
Wallet::Wallet() Wallet::Wallet()
{ {
} }
void Wallet::load() void Wallet::load()
{ {
// WRITEME
} }
#if 0
int64 Wallet::getBalance() int64 Wallet::getBalance()
{ {
int64 total = 0; int64 total = 0;

View File

@@ -4,6 +4,7 @@
// file license.txt or http://www.opensource.org/licenses/mit-license.php. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
#include "keystore.h" #include "keystore.h"
#include "BitcoinUtil.h"
#include <vector> #include <vector>

View File

@@ -31,14 +31,6 @@ enum MessageType {
} }
message TMBaseMessage {
required MessageType type = 1;
optional uint32 seq = 2;
optional uint32 querySeq = 3; // if this is a reply
required bytes innerMessage = 4;
}
// Sent on connect // Sent on connect
message TMHello { message TMHello {
required uint32 version = 1; required uint32 version = 1;
@@ -119,6 +111,29 @@ 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 message TMIndexedObject
{ {
@@ -151,7 +166,21 @@ message TMObjectByHash
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 { message TMPing {