From 7f44d1569ede304f255f1633d90c531017330887 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 25 Oct 2012 18:59:07 -0700 Subject: [PATCH] Computing our node public and private keys every time we needed them was silly and expensive. --- src/Config.cpp | 7 +++++++ src/Config.h | 2 +- src/LedgerConsensus.cpp | 11 ++++++----- src/LedgerConsensus.h | 2 +- src/LedgerProposal.cpp | 7 +++---- src/LedgerProposal.h | 4 ++-- src/NetworkOPs.cpp | 4 ++-- src/RPCHandler.cpp | 8 +++++++- src/SerializedValidation.cpp | 17 +++++++---------- src/SerializedValidation.h | 4 ++-- 10 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 12eac48859..91a3acbe82 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -234,7 +234,14 @@ void Config::load() WEBSOCKET_PORT = boost::lexical_cast(strTemp); if (sectionSingleB(secConfig, SECTION_VALIDATION_SEED, strTemp)) + { VALIDATION_SEED.setSeedGeneric(strTemp); + if (VALIDATION_SEED.isValid()) + { + VALIDATION_PUB = RippleAddress::createNodePublic(VALIDATION_SEED); + VALIDATION_PRIV = RippleAddress::createNodePrivate(VALIDATION_SEED); + } + } (void) sectionSingleB(secConfig, SECTION_PEER_SSL_CIPHER_LIST, PEER_SSL_CIPHER_LIST); diff --git a/src/Config.h b/src/Config.h index ae9cc73d1d..2bc0662e5f 100644 --- a/src/Config.h +++ b/src/Config.h @@ -93,7 +93,7 @@ public: bool RPC_ALLOW_REMOTE; // Validation - RippleAddress VALIDATION_SEED; + RippleAddress VALIDATION_SEED, VALIDATION_PUB, VALIDATION_PRIV; // Fees uint64 FEE_DEFAULT; // Default fee. diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 0b83ee3b2d..dea7aabb4b 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -222,7 +222,8 @@ bool LCTransaction::updateVote(int percentTime, bool proposing) LedgerConsensus::LedgerConsensus(const uint256& prevLCLHash, Ledger::ref previousLedger, uint32 closeTime) : mState(lcsPRE_CLOSE), mCloseTime(closeTime), mPrevLedgerHash(prevLCLHash), mPreviousLedger(previousLedger), - mValSeed(theConfig.VALIDATION_SEED), mCurrentMSeconds(0), mClosePercent(0), mHaveCloseTimeConsensus(false), + mValPublic(theConfig.VALIDATION_PUB), mValPrivate(theConfig.VALIDATION_PRIV), + mCurrentMSeconds(0), mClosePercent(0), mHaveCloseTimeConsensus(false), mConsensusStartTime(boost::posix_time::microsec_clock::universal_time()) { cLog(lsDEBUG) << "Creating consensus object"; @@ -234,12 +235,11 @@ LedgerConsensus::LedgerConsensus(const uint256& prevLCLHash, Ledger::ref previou mCloseResolution = ContinuousLedgerTiming::getNextLedgerTimeResolution( mPreviousLedger->getCloseResolution(), mPreviousLedger->getCloseAgree(), previousLedger->getLedgerSeq() + 1); - if (mValSeed.isValid()) + if (mValPublic.isValid() && mValPrivate.isValid()) { cLog(lsINFO) << "Entering consensus process, validating"; mValidating = true; mProposing = theApp->getOPs().getOperatingMode() == NetworkOPs::omFULL; - mValPublic = RippleAddress::createNodePublic(mValSeed); } else { @@ -380,7 +380,7 @@ void LedgerConsensus::takeInitialPosition(Ledger& initialLedger) if (mValidating) mOurPosition = boost::make_shared - (mValSeed, initialLedger.getParentHash(), txSet, mCloseTime); + (mValPublic, mValPrivate, initialLedger.getParentHash(), txSet, mCloseTime); else mOurPosition = boost::make_shared(initialLedger.getParentHash(), txSet, mCloseTime); @@ -1141,7 +1141,8 @@ void LedgerConsensus::accept(SHAMap::ref set) { uint256 signingHash; SerializedValidation::pointer v = boost::make_shared - (newLCLHash, theApp->getOPs().getValidationTimeNC(), mValSeed, mProposing, boost::ref(signingHash)); + (newLCLHash, theApp->getOPs().getValidationTimeNC(), mValPublic, mValPrivate, + mProposing, boost::ref(signingHash)); v->setTrusted(); theApp->isNew(signingHash); // suppress it if we receive it theApp->getValidations().addValidation(v); diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index 4b3e87e068..38a2dde9f2 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -87,7 +87,7 @@ protected: Ledger::pointer mPreviousLedger; LedgerAcquire::pointer mAcquiringLedger; LedgerProposal::pointer mOurPosition; - RippleAddress mValSeed, mValPublic; + RippleAddress mValPublic, mValPrivate; bool mProposing, mValidating, mHaveCorrectLCL; int mCurrentMSeconds, mClosePercent, mCloseResolution; diff --git a/src/LedgerProposal.cpp b/src/LedgerProposal.cpp index 680c6a2c64..775f7b93dd 100644 --- a/src/LedgerProposal.cpp +++ b/src/LedgerProposal.cpp @@ -20,11 +20,10 @@ LedgerProposal::LedgerProposal(const uint256& pLgr, uint32 seq, const uint256& t } -LedgerProposal::LedgerProposal(const RippleAddress& naSeed, const uint256& prevLgr, - const uint256& position, uint32 closeTime) : +LedgerProposal::LedgerProposal(const RippleAddress& naPub, const RippleAddress& naPriv, + const uint256& prevLgr, const uint256& position, uint32 closeTime) : mPreviousLedger(prevLgr), mCurrentHash(position), mCloseTime(closeTime), mProposeSeq(0), - mPublicKey(RippleAddress::createNodePublic(naSeed)), - mPrivateKey(RippleAddress::createNodePrivate(naSeed)) + mPublicKey(naPub), mPrivateKey(naPriv) { // OPTIMIZEME: This is expensive. We create both the public and private keys separately each time mPeerID = mPublicKey.getNodeID(); mTime = boost::posix_time::second_clock::universal_time(); diff --git a/src/LedgerProposal.h b/src/LedgerProposal.h index 34ff239b75..4119a444d6 100644 --- a/src/LedgerProposal.h +++ b/src/LedgerProposal.h @@ -35,8 +35,8 @@ public: uint32 closeTime, const RippleAddress& naPeerPublic); // our first proposal - LedgerProposal(const RippleAddress& privKey, const uint256& prevLedger, const uint256& position, - uint32 closeTime); + LedgerProposal(const RippleAddress& pubKey, const RippleAddress& privKey, + const uint256& prevLedger, const uint256& position, uint32 closeTime); // an unsigned "dummy" proposal for nodes not validating LedgerProposal(const uint256& prevLedger, const uint256& position, uint32 closeTime); diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index 4a9b0046d1..f2fa89c7c6 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -881,10 +881,10 @@ Json::Value NetworkOPs::getServerInfo() default: info["serverState"] = "unknown"; } - if (!theConfig.VALIDATION_SEED.isValid()) + if (!theConfig.VALIDATION_PUB.isValid()) info["serverState"] = "none"; else - info["validationPKey"] = RippleAddress::createNodePublic(theConfig.VALIDATION_SEED).humanNodePublic(); + info["validationPKey"] = theConfig.VALIDATION_PUB.humanNodePublic(); if (mNeedNetworkLedger) info["networkLedger"] = "waiting"; diff --git a/src/RPCHandler.cpp b/src/RPCHandler.cpp index f08fa0d8c5..43aad83c58 100644 --- a/src/RPCHandler.cpp +++ b/src/RPCHandler.cpp @@ -2063,14 +2063,20 @@ Json::Value RPCHandler::doValidationSeed(const Json::Value& params) { std::cerr << "Unset validation seed." << std::endl; theConfig.VALIDATION_SEED.clear(); + theConfig.VALIDATION_PUB.clear(); + theConfig.VALIDATION_PRIV.clear(); } else if (!theConfig.VALIDATION_SEED.setSeedGeneric(params[0u].asString())) { + theConfig.VALIDATION_PUB.clear(); + theConfig.VALIDATION_PRIV.clear(); return rpcError(rpcBAD_SEED); } else { - obj["validation_public_key"] = RippleAddress::createNodePublic(theConfig.VALIDATION_SEED).humanNodePublic(); + theConfig.VALIDATION_PUB = RippleAddress::createNodePublic(theConfig.VALIDATION_SEED); + theConfig.VALIDATION_PRIV = RippleAddress::createNodePrivate(theConfig.VALIDATION_SEED); + obj["validation_public_key"] = theConfig.VALIDATION_PUB.humanNodePublic(); obj["validation_seed"] = theConfig.VALIDATION_SEED.humanSeed(); obj["validation_key"] = theConfig.VALIDATION_SEED.humanSeed1751(); } diff --git a/src/SerializedValidation.cpp b/src/SerializedValidation.cpp index 4c796b45a4..313577a2c4 100644 --- a/src/SerializedValidation.cpp +++ b/src/SerializedValidation.cpp @@ -37,25 +37,22 @@ SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSi } SerializedValidation::SerializedValidation(const uint256& ledgerHash, uint32 signTime, - const RippleAddress& naSeed, bool isFull, uint256& signingHash) + const RippleAddress& naPub, const RippleAddress& naPriv, bool isFull, uint256& signingHash) : STObject(sValidationFormat, sfValidation), mTrusted(false) { setFieldH256(sfLedgerHash, ledgerHash); setFieldU32(sfSigningTime, signTime); - if (naSeed.isValid()) - { // OPTIMIZEME: Should compute the node public key just once and pass it in - RippleAddress np = RippleAddress::createNodePublic(naSeed); - setFieldVL(sfSigningPubKey, np.getNodePublic()); - mNodeID = np.getNodeID(); - assert(mNodeID.isNonZero()); - } + + setFieldVL(sfSigningPubKey, naPub.getNodePublic()); + mNodeID = naPub.getNodeID(); + assert(mNodeID.isNonZero()); + if (!isFull) setFlag(sFullFlag); signingHash = getSigningHash(); std::vector signature; - // OPTIMIZEME: Should compute the node private key just once and pass it in - RippleAddress::createNodePrivate(naSeed).signNodePrivate(signingHash, signature); + naPriv.signNodePrivate(signingHash, signature); setFieldVL(sfSignature, signature); // XXX Check if this can fail. // if (!RippleAddress::createNodePrivate(naSeed).signNodePrivate(getSigningHash(), mSignature.peekValue())) diff --git a/src/SerializedValidation.h b/src/SerializedValidation.h index 61808c2f1b..eae4dffad8 100644 --- a/src/SerializedValidation.h +++ b/src/SerializedValidation.h @@ -21,8 +21,8 @@ public: // These throw if the object is not valid SerializedValidation(SerializerIterator& sit, bool checkSignature = true); - SerializedValidation(const uint256& ledgerHash, uint32 signTime, const RippleAddress& naSeed, bool isFull, - uint256& signingHash); + SerializedValidation(const uint256& ledgerHash, uint32 signTime, const RippleAddress& naPub, + const RippleAddress& naPriv, bool isFull, uint256& signingHash); uint256 getLedgerHash() const; uint32 getSignTime() const;