A bit of a painful PackedMessage change, but this simplifies a lot of code.

It's worth it to take the pain now.
This commit is contained in:
JoelKatz
2012-05-28 13:07:19 -07:00
parent b144fa48b5
commit ae9db460ab
7 changed files with 74 additions and 81 deletions

View File

@@ -117,9 +117,9 @@ void LedgerAcquire::trigger(Peer::pointer peer)
#ifdef DEBUG
std::cerr << "need base" << std::endl;
#endif
boost::shared_ptr<newcoin::TMGetLedger> tmGL = boost::make_shared<newcoin::TMGetLedger>();
tmGL->set_ledgerhash(mHash.begin(), mHash.size());
tmGL->set_itype(newcoin::liBASE);
newcoin::TMGetLedger tmGL;
tmGL.set_ledgerhash(mHash.begin(), mHash.size());
tmGL.set_itype(newcoin::liBASE);
if (peer)
{
sendRequest(tmGL, peer);
@@ -136,11 +136,11 @@ void LedgerAcquire::trigger(Peer::pointer peer)
assert(mLedger);
if (mLedger->peekTransactionMap()->getHash().isZero())
{ // we need the root node
boost::shared_ptr<newcoin::TMGetLedger> tmGL = boost::make_shared<newcoin::TMGetLedger>();
tmGL->set_ledgerhash(mHash.begin(), mHash.size());
tmGL->set_ledgerseq(mLedger->getLedgerSeq());
tmGL->set_itype(newcoin::liTX_NODE);
*(tmGL->add_nodeids()) = SHAMapNode().getRawString();
newcoin::TMGetLedger tmGL;
tmGL.set_ledgerhash(mHash.begin(), mHash.size());
tmGL.set_ledgerseq(mLedger->getLedgerSeq());
tmGL.set_itype(newcoin::liTX_NODE);
*(tmGL.add_nodeids()) = SHAMapNode().getRawString();
if (peer)
{
sendRequest(tmGL, peer);
@@ -164,12 +164,12 @@ void LedgerAcquire::trigger(Peer::pointer peer)
}
else
{
boost::shared_ptr<newcoin::TMGetLedger> tmGL = boost::make_shared<newcoin::TMGetLedger>();
tmGL->set_ledgerhash(mHash.begin(), mHash.size());
tmGL->set_ledgerseq(mLedger->getLedgerSeq());
tmGL->set_itype(newcoin::liTX_NODE);
newcoin::TMGetLedger tmGL;
tmGL.set_ledgerhash(mHash.begin(), mHash.size());
tmGL.set_ledgerseq(mLedger->getLedgerSeq());
tmGL.set_itype(newcoin::liTX_NODE);
for (std::vector<SHAMapNode>::iterator it = nodeIDs.begin(); it != nodeIDs.end(); ++it)
*(tmGL->add_nodeids()) = it->getRawString();
*(tmGL.add_nodeids()) = it->getRawString();
if (peer)
{
sendRequest(tmGL, peer);
@@ -188,11 +188,11 @@ void LedgerAcquire::trigger(Peer::pointer peer)
assert(mLedger);
if (mLedger->peekAccountStateMap()->getHash().isZero())
{ // we need the root node
boost::shared_ptr<newcoin::TMGetLedger> tmGL = boost::make_shared<newcoin::TMGetLedger>();
tmGL->set_ledgerhash(mHash.begin(), mHash.size());
tmGL->set_ledgerseq(mLedger->getLedgerSeq());
tmGL->set_itype(newcoin::liAS_NODE);
*(tmGL->add_nodeids()) = SHAMapNode().getRawString();
newcoin::TMGetLedger tmGL;
tmGL.set_ledgerhash(mHash.begin(), mHash.size());
tmGL.set_ledgerseq(mLedger->getLedgerSeq());
tmGL.set_itype(newcoin::liAS_NODE);
*(tmGL.add_nodeids()) = SHAMapNode().getRawString();
if (peer)
{
sendRequest(tmGL, peer);
@@ -216,12 +216,12 @@ void LedgerAcquire::trigger(Peer::pointer peer)
}
else
{
boost::shared_ptr<newcoin::TMGetLedger> tmGL = boost::make_shared<newcoin::TMGetLedger>();
tmGL->set_ledgerhash(mHash.begin(), mHash.size());
tmGL->set_ledgerseq(mLedger->getLedgerSeq());
tmGL->set_itype(newcoin::liAS_NODE);
newcoin::TMGetLedger tmGL;
tmGL.set_ledgerhash(mHash.begin(), mHash.size());
tmGL.set_ledgerseq(mLedger->getLedgerSeq());
tmGL.set_itype(newcoin::liAS_NODE);
for (std::vector<SHAMapNode>::iterator it = nodeIDs.begin(); it != nodeIDs.end(); ++it)
*(tmGL->add_nodeids()) = it->getRawString();
*(tmGL.add_nodeids()) = it->getRawString();
if (peer)
{
sendRequest(tmGL, peer);
@@ -238,12 +238,12 @@ void LedgerAcquire::trigger(Peer::pointer peer)
resetTimer();
}
void PeerSet::sendRequest(boost::shared_ptr<newcoin::TMGetLedger> tmGL, Peer::pointer peer)
void PeerSet::sendRequest(const newcoin::TMGetLedger& tmGL, Peer::pointer peer)
{
peer->sendPacket(boost::make_shared<PackedMessage>(tmGL, newcoin::mtGET_LEDGER));
}
void PeerSet::sendRequest(boost::shared_ptr<newcoin::TMGetLedger> tmGL)
void PeerSet::sendRequest(const newcoin::TMGetLedger& tmGL)
{
boost::recursive_mutex::scoped_lock sl(mLock);
if (mPeers.empty()) return;

View File

@@ -29,8 +29,8 @@ protected:
PeerSet(const uint256& hash, int interval);
virtual ~PeerSet() { ; }
void sendRequest(boost::shared_ptr<newcoin::TMGetLedger> message);
void sendRequest(boost::shared_ptr<newcoin::TMGetLedger> message, Peer::pointer peer);
void sendRequest(const newcoin::TMGetLedger& message);
void sendRequest(const newcoin::TMGetLedger& message, Peer::pointer peer);
public:
const uint256& getHash() const { return mHash; }

View File

@@ -89,16 +89,15 @@ Transaction::pointer NetworkOPs::processTransaction(Transaction::pointer trans,
// no cache the account balance information and always get it from the current ledger
// theApp->getWallet().applyTransaction(trans);
boost::shared_ptr<newcoin::TMTransaction> tx = boost::make_shared<newcoin::TMTransaction>();
newcoin::TMTransaction tx;
Serializer s;
trans->getSTransaction()->getTransaction(s, false);
tx->set_rawtransaction(&s.getData().front(), s.getLength());
tx->set_status(newcoin::tsCURRENT);
tx->set_receivetimestamp(getNetworkTimeNC());
tx->set_ledgerindexpossible(trans->getLedger());
tx.set_rawtransaction(&s.getData().front(), s.getLength());
tx.set_status(newcoin::tsCURRENT);
tx.set_receivetimestamp(getNetworkTimeNC());
tx.set_ledgerindexpossible(trans->getLedger());
PackedMessage::pointer packet(new PackedMessage(PackedMessage::MessagePointer(tx), newcoin::mtTRANSACTION));
PackedMessage::pointer packet = boost::make_shared<PackedMessage>(tx, newcoin::mtTRANSACTION);
theApp->getConnectionPool().relayMessage(source, packet);
return trans;
@@ -375,14 +374,13 @@ int NetworkOPs::beginConsensus(Ledger::pointer closingLedger, bool isEarly)
#ifdef DEBUG
std::cerr << "Broadcasting ledger close" << std::endl;
#endif
boost::shared_ptr<newcoin::TMStatusChange> s = boost::make_shared<newcoin::TMStatusChange>();
s->set_newevent(newcoin::neCLOSING_LEDGER);
s->set_ledgerseq(closingLedger->getLedgerSeq());
s->set_networktime(getNetworkTimeNC());
newcoin::TMStatusChange s;
s.set_newevent(newcoin::neCLOSING_LEDGER);
s.set_ledgerseq(closingLedger->getLedgerSeq());
s.set_networktime(getNetworkTimeNC());
uint256 plhash = closingLedger->getParentHash();
s->set_previousledgerhash(plhash.begin(), plhash.size());
PackedMessage::pointer packet =
boost::make_shared<PackedMessage>(PackedMessage::MessagePointer(s), newcoin::mtSTATUS_CHANGE);
s.set_previousledgerhash(plhash.begin(), plhash.size());
PackedMessage::pointer packet = boost::make_shared<PackedMessage>(s, newcoin::mtSTATUS_CHANGE);
theApp->getConnectionPool().relayMessage(NULL, packet);
return mConsensus->startup();

View File

@@ -12,16 +12,15 @@ void PackedMessage::encodeHeader(unsigned size, int type)
mBuffer[5] = static_cast<boost::uint8_t>(type & 0xFF);
}
PackedMessage::PackedMessage(MessagePointer msg, int type) : mMsg(msg)
PackedMessage::PackedMessage(const ::google::protobuf::Message &message, int type)
{
unsigned msg_size = mMsg->ByteSize();
unsigned msg_size = message.ByteSize();
assert(msg_size);
mBuffer.resize(HEADER_SIZE + msg_size);
encodeHeader(msg_size, type);
if (msg_size)
{
mMsg->SerializeToArray(&mBuffer[HEADER_SIZE], msg_size);
message.SerializeToArray(&mBuffer[HEADER_SIZE], msg_size);
#ifdef DEBUG
std::cerr << "PackedMessage: type=" << type << ", datalen=" << msg_size << std::endl;
#endif

View File

@@ -39,8 +39,7 @@ public:
typedef boost::shared_ptr< ::google::protobuf::Message > MessagePointer;
typedef boost::shared_ptr<PackedMessage> pointer;
MessagePointer mMsg;
PackedMessage(MessagePointer msg, int type);
PackedMessage(const ::google::protobuf::Message& message, int type);
std::vector<uint8_t>& getBuffer() { return(mBuffer); }

View File

@@ -45,7 +45,7 @@ void Peer::handle_write(const boost::system::error_code& error, size_t bytes_tra
if (!mSendQ.empty())
{
PackedMessage::pointer packet=mSendQ.front();
PackedMessage::pointer packet = mSendQ.front();
if(packet)
{
sendPacketForce(packet);
@@ -232,7 +232,7 @@ void Peer::connected(const boost::system::error_code& error)
void Peer::sendPacketForce(PackedMessage::pointer packet)
{
mSendingPacket=packet;
mSendingPacket = packet;
boost::asio::async_write(mSocketSsl, boost::asio::buffer(packet->getBuffer()),
boost::bind(&Peer::handle_write, shared_from_this(),
boost::asio::placeholders::error,
@@ -419,8 +419,8 @@ void Peer::processReadBuffer()
case newcoin::mtPROPOSE_LEDGER:
{
boost::shared_ptr<newcoin::TMProposeSet> msg = boost::make_shared<newcoin::TMProposeSet>();
if(msg->ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE))
newcoin::TMProposeSet msg;
if(msg.ParseFromArray(&mReadbuf[HEADER_SIZE], mReadbuf.size() - HEADER_SIZE))
recvPropose(msg);
else std::cerr << "parse error: " << type << std::endl;
}
@@ -610,18 +610,18 @@ void Peer::recvTransaction(newcoin::TMTransaction& packet)
}
}
void Peer::recvPropose(boost::shared_ptr<newcoin::TMProposeSet> packet)
void Peer::recvPropose(newcoin::TMProposeSet& packet)
{
if ((packet->currenttxhash().size() != 32) || (packet->nodepubkey().size() < 28) ||
(packet->signature().size() < 56))
if ((packet.currenttxhash().size() != 32) || (packet.nodepubkey().size() < 28) ||
(packet.signature().size() < 56))
return;
uint32 closingSeq = packet->closingseq(), proposeSeq = packet->proposeseq();
uint32 closingSeq = packet.closingseq(), proposeSeq = packet.proposeseq();
uint256 currentTxHash;
memcpy(currentTxHash.begin(), packet->currenttxhash().data(), 32);
memcpy(currentTxHash.begin(), packet.currenttxhash().data(), 32);
if(theApp->getOPs().proposeLedger(closingSeq, proposeSeq, currentTxHash,
packet->nodepubkey(), packet->signature()))
packet.nodepubkey(), packet.signature()))
{ // FIXME: Not all nodes will want proposals
PackedMessage::pointer message = boost::make_shared<PackedMessage>(packet, newcoin::mtPROPOSE_LEDGER);
theApp->getConnectionPool().relayMessage(this, message);
@@ -718,7 +718,7 @@ void Peer::recvStatus(newcoin::TMStatusChange& packet)
void Peer::recvGetLedger(newcoin::TMGetLedger& packet)
{
SHAMap::pointer map;
boost::shared_ptr<newcoin::TMLedgerData> reply = boost::make_shared<newcoin::TMLedgerData>();
newcoin::TMLedgerData reply;
if (packet.itype() == newcoin::liTS_CANDIDATE)
{ // Request is for a transaction candidate set
@@ -736,8 +736,8 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet)
punishPeer(PP_INVALID_REQUEST);
return;
}
reply->set_ledgerhash(txHash.begin(), txHash.size());
reply->set_type(newcoin::liTS_CANDIDATE);
reply.set_ledgerhash(txHash.begin(), txHash.size());
reply.set_type(newcoin::liTS_CANDIDATE);
}
else
{ // Figure out what ledger they want
@@ -777,17 +777,16 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet)
// Fill out the reply
uint256 lHash = ledger->getHash();
reply->set_ledgerhash(lHash.begin(), lHash.size());
reply->set_ledgerseq(ledger->getLedgerSeq());
reply->set_type(packet.itype());
reply.set_ledgerhash(lHash.begin(), lHash.size());
reply.set_ledgerseq(ledger->getLedgerSeq());
reply.set_type(packet.itype());
if(packet.itype() == newcoin::liBASE)
{ // they want the ledger base data
Serializer nData(128);
ledger->addRaw(nData);
reply->add_nodes()->set_nodedata(nData.getDataPtr(), nData.getLength());
PackedMessage::pointer oPacket = boost::make_shared<PackedMessage>
(PackedMessage::MessagePointer(reply), newcoin::mtLEDGER);
reply.add_nodes()->set_nodedata(nData.getDataPtr(), nData.getLength());
PackedMessage::pointer oPacket = boost::make_shared<PackedMessage>(reply, newcoin::mtLEDGER);
sendPacket(oPacket);
return;
}
@@ -822,14 +821,13 @@ void Peer::recvGetLedger(newcoin::TMGetLedger& packet)
{
Serializer nID(33);
nodeIDIterator->addIDRaw(nID);
newcoin::TMLedgerNode* node = reply->add_nodes();
newcoin::TMLedgerNode* node = reply.add_nodes();
node->set_nodeid(nID.getDataPtr(), nID.getLength());
node->set_nodedata(&rawNodeIterator->front(), rawNodeIterator->size());
}
}
}
PackedMessage::pointer oPacket = boost::make_shared<PackedMessage>
(PackedMessage::MessagePointer(reply), newcoin::mtLEDGER);
PackedMessage::pointer oPacket = boost::make_shared<PackedMessage>(reply, newcoin::mtLEDGER);
sendPacket(oPacket);
}
@@ -915,27 +913,26 @@ void Peer::sendHello()
theApp->getWallet().getNodePrivate().signNodePrivate(mCookieHash, vchSig);
boost::shared_ptr<newcoin::TMHello> h = boost::make_shared<newcoin::TMHello>();
newcoin::TMHello h;
h->set_version(theConfig.VERSION);
h->set_ledgerindex(theApp->getOPs().getCurrentLedgerID());
h->set_nettime(theApp->getOPs().getNetworkTimeNC());
h->set_nodepublic(theApp->getWallet().getNodePublic().humanNodePublic());
h->set_nodeproof(&vchSig[0], vchSig.size());
h->set_ipv4port(theConfig.PEER_PORT);
h.set_version(theConfig.VERSION);
h.set_ledgerindex(theApp->getOPs().getCurrentLedgerID());
h.set_nettime(theApp->getOPs().getNetworkTimeNC());
h.set_nodepublic(theApp->getWallet().getNodePublic().humanNodePublic());
h.set_nodeproof(&vchSig[0], vchSig.size());
h.set_ipv4port(theConfig.PEER_PORT);
Ledger::pointer closedLedger = theApp->getMasterLedger().getClosedLedger();
assert(closedLedger && closedLedger->isClosed());
if (closedLedger->isClosed())
{
uint256 hash = closedLedger->getHash();
h->set_closedledger(hash.begin(), hash.GetSerializeSize());
h.set_closedledger(hash.begin(), hash.GetSerializeSize());
hash = closedLedger->getParentHash();
h->set_previousledger(hash.begin(), hash.GetSerializeSize());
h.set_previousledger(hash.begin(), hash.GetSerializeSize());
}
PackedMessage::pointer packet = boost::make_shared<PackedMessage>
(PackedMessage::MessagePointer(h), newcoin::mtHELLO);
PackedMessage::pointer packet = boost::make_shared<PackedMessage>(h, newcoin::mtHELLO);
sendPacket(packet);
}

View File

@@ -86,7 +86,7 @@ protected:
void recvGetLedger(newcoin::TMGetLedger& packet);
void recvLedger(newcoin::TMLedgerData& packet);
void recvStatus(newcoin::TMStatusChange& packet);
void recvPropose(boost::shared_ptr<newcoin::TMProposeSet> packet);
void recvPropose(newcoin::TMProposeSet& packet);
void recvHaveTxSet(newcoin::TMHaveTransactionSet& packet);
void getSessionCookie(std::string& strDst);