Skip to content

Commit

Permalink
SCP : replace low/high slot lookup by callback
Browse files Browse the repository at this point in the history
  • Loading branch information
MonsieurNicolas committed Jan 16, 2020
1 parent f31df63 commit 5e02356
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
21 changes: 3 additions & 18 deletions src/herder/HerderImpl.cpp
Expand Up @@ -515,31 +515,16 @@ HerderImpl::recvSCPEnvelope(SCPEnvelope const& envelope,
void
HerderImpl::sendSCPStateToPeer(uint32 ledgerSeq, Peer::pointer peer)
{
if (getSCP().empty())
{
return;
}

if (getSCP().getLowSlotIndex() > std::numeric_limits<uint32_t>::max() ||
getSCP().getHighSlotIndex() >= std::numeric_limits<uint32_t>::max())
{
return;
}

auto minSeq =
std::max(ledgerSeq, static_cast<uint32_t>(getSCP().getLowSlotIndex()));
auto maxSeq = static_cast<uint32_t>(getSCP().getHighSlotIndex());

for (uint32_t seq = minSeq; seq <= maxSeq; seq++)
{
getSCP().processSlotsAscendingFrom(ledgerSeq, [&](uint64 seq) {
getSCP().processCurrentState(seq, [&](SCPEnvelope const& e) {
StellarMessage m;
m.type(SCP_MESSAGE);
m.envelope() = e;
peer->sendMessage(m);
return true;
});
}
return true;
});
}

void
Expand Down
32 changes: 15 additions & 17 deletions src/scp/SCP.cpp
Expand Up @@ -216,22 +216,6 @@ SCP::empty() const
return mKnownSlots.empty();
}

uint64
SCP::getLowSlotIndex() const
{
assert(!empty());
return mKnownSlots.begin()->first;
}

uint64
SCP::getHighSlotIndex() const
{
assert(!empty());
auto it = mKnownSlots.end();
it--;
return it->first;
}

void
SCP::processCurrentState(uint64 slotIndex,
std::function<bool(SCPEnvelope const&)> const& f)
Expand All @@ -243,6 +227,20 @@ SCP::processCurrentState(uint64 slotIndex,
}
}

void
SCP::processSlotsAscendingFrom(uint64 startingSlot,
std::function<bool(uint64)> const& f)
{
for (auto iter = mKnownSlots.lower_bound(startingSlot);
iter != mKnownSlots.end(); ++iter)
{
if (!f(iter->first))
{
break;
}
}
}

SCPEnvelope const*
SCP::getLatestMessage(NodeID const& id)
{
Expand Down Expand Up @@ -384,4 +382,4 @@ SCP::envToStr(SCPStatement const& st, bool fullKeys) const
oss << " }";
return oss.str();
}
}
}
8 changes: 4 additions & 4 deletions src/scp/SCP.h
Expand Up @@ -100,16 +100,16 @@ class SCP

// check if we are holding some slots
bool empty() const;
// return lowest slot index value
uint64 getLowSlotIndex() const;
// return highest slot index value
uint64 getHighSlotIndex() const;

// invokes f for all latest messages
// f returns false to stop processing, true otherwise
void processCurrentState(uint64 slotIndex,
std::function<bool(SCPEnvelope const&)> const& f);

// iterates through slots, starting from ledgerSeq
void processSlotsAscendingFrom(uint64 startIndex,
std::function<bool(uint64)> const& f);

// returns the latest message from a node
// or nullptr if not found
SCPEnvelope const* getLatestMessage(NodeID const& id);
Expand Down

0 comments on commit 5e02356

Please sign in to comment.