Tidy up some business logic:

* Add OverlayImpl::for_each to tidy up some call sites
* Add comment about computing the unique ID for message routing
* Remove unused code
This commit is contained in:
Vinnie Falco
2015-04-27 12:41:09 -07:00
committed by Tom Ritchford
parent 8e34a1f6a7
commit c77a2f335a
4 changed files with 78 additions and 66 deletions

View File

@@ -1619,28 +1619,6 @@ private:
<< "We should do delayed relay of this proposal,"
<< " but we cannot";
}
#if 0
// FIXME: We can't do delayed relay because we don't have the signature
std::set<Peer::id_t> peers
if (relay && getApp().getHashRouter ().swapSet (proposal.getSuppress (), set, SF_RELAYED))
{
WriteLog (lsDEBUG, LedgerConsensus) << "Stored proposal delayed relay";
protocol::TMProposeSet set;
set.set_proposeseq
set.set_currenttxhash (, 256 / 8);
previousledger
closetime
nodepubkey
signature
getApp ().overlay ().foreach (send_if_not (
std::make_shared<Message> (
set, protocol::mtPROPOSE_LEDGER),
peer_in_set(peers)));
}
#endif
}
}
}

View File

@@ -70,6 +70,15 @@ uint256 LedgerProposal::getSigningHash () const
return s.getSHA512Half ();
}
/*
The "id" is a unique value computed on all fields that contribute to
the signature, and including the signature. There is one caveat, the
"last closed ledger" field may be omitted. However, the signer still
computes the signature as if this field was present. Recipients of
the proposal need to inject the last closed ledger in order to
validate the signature. If the last closed ledger is left out, then
it is considered as all zeroes for the purposes of signing.
*/
// Compute a unique identifier for this signed proposal
uint256 LedgerProposal::computeSuppressionID (
uint256 const& proposeHash,

View File

@@ -174,6 +174,11 @@ public:
Peer::ptr
findPeerByShortID (Peer::id_t const& id) override;
//--------------------------------------------------------------------------
//
// OverlayImpl
//
void
add_active (std::shared_ptr<PeerImp> const& peer);
@@ -192,6 +197,22 @@ public:
void
onPeerDeactivate (Peer::id_t id, RippleAddress const& publicKey);
// UnaryFunc will be called as
// void(std::shared_ptr<PeerImp>&&)
//
template <class UnaryFunc>
void
for_each (UnaryFunc&& f)
{
std::lock_guard <decltype(mutex_)> lock (mutex_);
for (auto const& e : m_publicKeyMap)
{
auto sp = e.second.lock();
if (sp)
f(std::move(sp));
}
}
static
bool
isPeerUpgrade (beast::http::message const& request);

View File

@@ -1706,6 +1706,42 @@ PeerImp::checkValidation (Job&, STValidation::pointer val,
}
}
// Returns the set of peers that can help us get
// the TX tree with the specified root hash.
//
static
std::vector<std::shared_ptr<PeerImp>>
getPeersWithTree (OverlayImpl& ov,
uint256 const& rootHash, PeerImp const* skip)
{
std::vector<std::shared_ptr<PeerImp>> v;
ov.for_each([&](std::shared_ptr<PeerImp> const& p)
{
if (p->hasTxSet(rootHash) && p.get() != skip)
v.push_back(p);
});
return v;
}
// Returns the set of peers that claim
// to have the specified ledger.
//
static
std::vector<std::shared_ptr<PeerImp>>
getPeersWithLedger (OverlayImpl& ov,
uint256 const& ledgerHash, LedgerIndex ledger,
PeerImp const* skip)
{
std::vector<std::shared_ptr<PeerImp>> v;
ov.for_each([&](std::shared_ptr<PeerImp> const& p)
{
if (p->hasLedger(ledgerHash, ledger) &&
p.get() != skip)
v.push_back(p);
});
return v;
}
// VFALCO NOTE This function is way too big and cumbersome.
void
PeerImp::getLedger (std::shared_ptr<protocol::TMGetLedger> const& m)
@@ -1745,44 +1781,19 @@ PeerImp::getLedger (std::shared_ptr<protocol::TMGetLedger> const& m)
p_journal_.debug <<
"GetLedger: Routing Tx set request";
struct get_usable_peers
{
typedef Overlay::PeerSequence return_type;
Overlay::PeerSequence usablePeers;
uint256 const& txHash;
Peer const* skip;
get_usable_peers(uint256 const& hash, Peer const* s)
: txHash(hash), skip(s)
{ }
void operator() (Peer::ptr const& peer)
{
if (peer->hasTxSet (txHash) && (peer.get () != skip))
usablePeers.push_back (peer);
}
return_type operator() ()
{
return usablePeers;
}
};
Overlay::PeerSequence usablePeers (overlay_.foreach (
get_usable_peers (txHash, this)));
if (usablePeers.empty ())
auto const v = getPeersWithTree(
overlay_, txHash, this);
if (v.empty())
{
p_journal_.info <<
"GetLedger: Route TX set failed";
return;
}
Peer::ptr const& selectedPeer = usablePeers [
rand () % usablePeers.size ()];
auto const& p =
v[rand () % v.size ()];
packet.set_requestcookie (id ());
selectedPeer->send (std::make_shared<Message> (
p->send (std::make_shared<Message> (
packet, protocol::mtGET_LEDGER));
return;
}
@@ -1842,26 +1853,19 @@ PeerImp::getLedger (std::shared_ptr<protocol::TMGetLedger> const& m)
if (packet.has_ledgerseq ())
seq = packet.ledgerseq ();
Overlay::PeerSequence peerList = overlay_.getActivePeers ();
Overlay::PeerSequence usablePeers;
for (auto const& peer : peerList)
{
if (peer->hasLedger (ledgerhash, seq) && (peer.get () != this))
usablePeers.push_back (peer);
}
if (usablePeers.empty ())
auto const v = getPeersWithLedger(
overlay_, ledgerhash, seq, this);
if (v.empty ())
{
p_journal_.trace <<
"GetLedger: Cannot route";
return;
}
Peer::ptr const& selectedPeer = usablePeers [
rand () % usablePeers.size ()];
auto const& p = v[rand () % v.size ()];
packet.set_requestcookie (id ());
selectedPeer->send (
std::make_shared<Message> (packet, protocol::mtGET_LEDGER));
p->send (std::make_shared<Message>(
packet, protocol::mtGET_LEDGER));
p_journal_.debug <<
"GetLedger: Request routed";
return;