Skip to content

Commit

Permalink
Avoid unnecessary copying and dynamic memory allocations
Browse files Browse the repository at this point in the history
Co-authored-by: Chenna Keshava B S <ckbs.keshava56@gmail.com>
  • Loading branch information
nbougalis and Chenna Keshava B S committed Aug 25, 2022
1 parent 9aaa0df commit d318ab6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 61 deletions.
11 changes: 3 additions & 8 deletions src/ripple/app/consensus/RCLConsensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,10 @@ RCLConsensus::Adaptor::propose(RCLCxPeerPos::Proposal const& proposal)
prop.set_nodepubkey(
validatorKeys_.publicKey.data(), validatorKeys_.publicKey.size());

auto signingHash = sha512Half(
HashPrefix::proposal,
std::uint32_t(proposal.proposeSeq()),
proposal.closeTime().time_since_epoch().count(),
proposal.prevLedger(),
proposal.position());

auto sig = signDigest(
validatorKeys_.publicKey, validatorKeys_.secretKey, signingHash);
validatorKeys_.publicKey,
validatorKeys_.secretKey,
proposal.signingHash());

prop.set_signature(sig.data(), sig.size());

Expand Down
38 changes: 10 additions & 28 deletions src/ripple/app/consensus/RCLCxPeerPos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,23 @@ RCLCxPeerPos::RCLCxPeerPos(
Slice const& signature,
uint256 const& suppression,
Proposal&& proposal)
: data_{std::make_shared<Data>(
publicKey,
signature,
suppression,
std::move(proposal))}
: publicKey_(publicKey)
, suppression_(suppression)
, proposal_(std::move(proposal))
{
}
// The maximum allowed size of a signature is 72 bytes; we verify
// this elsewhere, but we want to be extra careful here:
assert(signature.size() != 0 && signature.size() <= signature_.capacity());

uint256
RCLCxPeerPos::signingHash() const
{
return sha512Half(
HashPrefix::proposal,
std::uint32_t(proposal().proposeSeq()),
proposal().closeTime().time_since_epoch().count(),
proposal().prevLedger(),
proposal().position());
if (signature.size() != 0 && signature.size() <= signature_.capacity())
signature_.assign(signature.begin(), signature.end());
}

bool
RCLCxPeerPos::checkSign() const
{
return verifyDigest(publicKey(), signingHash(), signature(), false);
return verifyDigest(
publicKey(), proposal_.signingHash(), signature(), false);
}

Json::Value
Expand Down Expand Up @@ -88,16 +82,4 @@ proposalUniqueId(
return s.getSHA512Half();
}

RCLCxPeerPos::Data::Data(
PublicKey const& publicKey,
Slice const& signature,
uint256 const& suppress,
Proposal&& proposal)
: publicKey_{publicKey}
, signature_{signature}
, suppression_{suppress}
, proposal_{std::move(proposal)}
{
}

} // namespace ripple
32 changes: 9 additions & 23 deletions src/ripple/app/consensus/RCLCxPeerPos.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/SecretKey.h>
#include <boost/container/static_vector.hpp>
#include <chrono>
#include <cstdint>
#include <string>
Expand Down Expand Up @@ -61,10 +62,6 @@ class RCLCxPeerPos
uint256 const& suppress,
Proposal&& proposal);

//! Create the signing hash for the proposal
uint256
signingHash() const;

//! Verify the signing hash of the proposal
bool
checkSign() const;
Expand All @@ -73,49 +70,38 @@ class RCLCxPeerPos
Slice
signature() const
{
return data_->signature_;
return {signature_.data(), signature_.size()};
}

//! Public key of peer that sent the proposal
PublicKey const&
publicKey() const
{
return data_->publicKey_;
return publicKey_;
}

//! Unique id used by hash router to suppress duplicates
uint256 const&
suppressionID() const
{
return data_->suppression_;
return suppression_;
}

Proposal const&
proposal() const
{
return data_->proposal_;
return proposal_;
}

//! JSON representation of proposal
Json::Value
getJson() const;

private:
struct Data : public CountedObject<Data>
{
PublicKey publicKey_;
Buffer signature_;
uint256 suppression_;
Proposal proposal_;

Data(
PublicKey const& publicKey,
Slice const& signature,
uint256 const& suppress,
Proposal&& proposal);
};

std::shared_ptr<Data> data_;
PublicKey publicKey_;
uint256 suppression_;
Proposal proposal_;
boost::container::static_vector<std::uint8_t, 72> signature_;

template <class Hasher>
void
Expand Down
29 changes: 27 additions & 2 deletions src/ripple/consensus/ConsensusProposal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_CONSENSUS_ConsensusProposal_H_INCLUDED
#define RIPPLE_CONSENSUS_ConsensusProposal_H_INCLUDED
#ifndef RIPPLE_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED
#define RIPPLE_CONSENSUS_CONSENSUSPROPOSAL_H_INCLUDED

#include <ripple/basics/base_uint.h>
#include <ripple/basics/chrono.h>
#include <ripple/json/json_value.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/jss.h>
#include <cstdint>
#include <optional>

namespace ripple {
/** Represents a proposed position taken during a round of consensus.
Expand Down Expand Up @@ -169,6 +172,7 @@ class ConsensusProposal
NetClock::time_point newCloseTime,
NetClock::time_point now)
{
signingHash_.reset();
position_ = newPosition;
closeTime_ = newCloseTime;
time_ = now;
Expand All @@ -185,6 +189,7 @@ class ConsensusProposal
void
bowOut(NetClock::time_point now)
{
signingHash_.reset();
time_ = now;
proposeSeq_ = seqLeave;
}
Expand All @@ -210,6 +215,23 @@ class ConsensusProposal
return ret;
}

//! The digest for this proposal, used for signing purposes.
uint256 const&
signingHash() const
{
if (!signingHash_)
{
signingHash_ = sha512Half(
HashPrefix::proposal,
std::uint32_t(proposeSeq()),
closeTime().time_since_epoch().count(),
prevLedger(),
position());
}

return signingHash_.value();
}

private:
//! Unique identifier of prior ledger this proposal is based on
LedgerID_t previousLedger_;
Expand All @@ -228,6 +250,9 @@ class ConsensusProposal

//! The identifier of the node taking this position
NodeID_t nodeID_;

//! The signing hash for this proposal
mutable std::optional<uint256> signingHash_;
};

template <class NodeID_t, class LedgerID_t, class Position_t>
Expand Down

0 comments on commit d318ab6

Please sign in to comment.