Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer ledger by branch #2300

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
36de9a2
[FOLD] Prepare preferred ledger by branch test:
bachase Dec 7, 2017
5e35cf9
[FOLD] Add LedgerTrie and tests
bachase Dec 7, 2017
72e7ac9
[FOLD] Switch to Adaptor design in Validations:
bachase Dec 15, 2017
0685beb
[FOLD] Add full/partial to validation type
bachase Dec 15, 2017
62a9c73
[FOLD] Simplify Validations test harness:
bachase Dec 15, 2017
892caad
[FOLD] Hoist and rename AddOutcome to ValStatus
bachase Dec 15, 2017
085e400
[FOLD] Make lock use explicit
bachase Dec 15, 2017
2abf766
[FOLD] Require full validations for trusted calculations
bachase Dec 15, 2017
f4c8644
[FOLD] Simplify Validations::add:
bachase Dec 15, 2017
9187516
[FOLD] Require increasing full validation sequence numbers:
bachase Dec 15, 2017
6d3c2e0
[FOLD] Use Ledgers in Validations_test:
bachase Dec 15, 2017
fa1a96e
[FOLD] Add RCLValidatedLedger:
bachase Dec 15, 2017
bd2b860
[FOLD] Don't save partial validations to database
bachase Dec 15, 2017
f0e6efd
[FOLD] Use LedgerTrie in generic validations:
bachase Dec 15, 2017
c614718
[FOLD] Use trie in generic Consensus:
bachase Dec 15, 2017
8315578
Use LedgerTrie for preferred ledger (RIPD-1551):
bachase Dec 15, 2017
b5f528a
[FOLD] Update docs
bachase Dec 15, 2017
fa576e5
[FOLD] Remove old validation code
bachase Dec 15, 2017
9501278
[FOLD] Fix misspellings
seelabs Dec 15, 2017
329ca88
[FOLD] Fix clang PeerGroup warnings
bachase Dec 18, 2017
ad35ee2
[FOLD] Address PR suggestions
bachase Dec 20, 2017
cff3191
[FOLD] Make a few memer functions const
HowardHinnant Dec 21, 2017
f9de5be
[FOLD] Change full validation sequence number invariant:
bachase Dec 21, 2017
fd58f28
[FOLD] Address PR comments
bachase Dec 27, 2017
2b2ba69
[FOLD] Explicitly initialize zero IDs
bachase Dec 28, 2017
0b58ac4
[FOLD] Address PR comments:
bachase Jan 8, 2018
7cdd0e7
[FOLD] Move support classes to namespace
bachase Jan 8, 2018
961747f
[FOLD] Fix assert
bachase Jan 8, 2018
37c583d
[FOLD] Enforce increasing sequence invariant on all validations:
bachase Jan 19, 2018
b6e19b7
[FOLD] Use uncommitted support in preferred branch:
bachase Jan 24, 2018
0ccf0a8
[FOLD] Reduce log warnings:
bachase Jan 25, 2018
277134b
[FOLD] Address PR comments
bachase Jan 26, 2018
83d5d55
[FOLD] Fix typos
bachase Jan 31, 2018
944527d
[FOLD] Only acquire ledgers we are not yet acquiring
bachase Jan 31, 2018
ea18089
[FOLD] Add SpanTip:
bachase Jan 31, 2018
2a97f83
[FOLD] Address PR comments
bachase Feb 2, 2018
5fd14de
[FOLD] Track seq and ID in acquiring ledgers
bachase Feb 3, 2018
f0bdfe1
[FOLD] Fallback to acquiring ledgers:
bachase Feb 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions src/ripple/consensus/LedgerTrie.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,46 @@

namespace ripple {

/** The tip of a span of ledger ancestry
*/
template <class Ledger>
class SpanTip
{
public:
using Seq = typename Ledger::Seq;
using ID = typename Ledger::ID;

SpanTip(Seq s, ID i, Ledger const lgr)
: seq{s}, id{i}, ledger{std::move(lgr)}
{
}

// The sequence number the tip ledger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> of the

Seq seq;
// The ID of the tip ledger
ID id;

/** Lookup the ID of an ancestor of the tip ledger

@param s The sequence number of the ancestor
@return The ID of the ancestor with that sequence number

@note s must be less than or equal to the sequence number of the
preferred ledger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should that say tip instead of preferred?

*/
ID
ancestor(Seq const& s) const
{
assert(s <= seq);
return ledger[s];
}

private:
Ledger const ledger;
};

namespace ledger_trie_detail {

// Represents a span of ancestry of a ledger
template <class Ledger>
class Span
Expand Down Expand Up @@ -101,12 +140,12 @@ class Span
return clamp(mismatch(ledger_, o));
}

// The Seq and ID of the end of the span
std::pair<Seq, ID>
// The tip of this span
SpanTip<Ledger>
tip() const
{
Seq tipSeq{end_ - Seq{1}};
return {tipSeq, ledger_[tipSeq]};
return SpanTip<Ledger>{tipSeq, ledger_[tipSeq], ledger_};
}

private:
Expand Down Expand Up @@ -137,7 +176,7 @@ class Span
friend std::ostream&
operator<<(std::ostream& o, Span const& s)
{
return o << s.tip().second << "[" << s.start_ << "," << s.end_ << ")";
return o << s.tip().id << "[" << s.start_ << "," << s.end_ << ")";
}

friend Span
Expand Down Expand Up @@ -203,8 +242,8 @@ struct Node
getJson() const
{
Json::Value res;
res["id"] = to_string(span.tip().second);
res["seq"] = static_cast<std::uint32_t>(span.tip().first);
res["id"] = to_string(span.tip().id);
res["seq"] = static_cast<std::uint32_t>(span.tip().seq);
res["tipSupport"] = tipSupport;
res["branchSupport"] = branchSupport;
if (!children.empty())
Expand Down Expand Up @@ -407,7 +446,6 @@ class LedgerTrie
boost::optional<Span> oldSuffix = loc->span.from(diffSeq);
boost::optional<Span> newSuffix = Span{ledger}.from(diffSeq);


if (oldSuffix)
{
// Have
Expand Down Expand Up @@ -623,7 +661,7 @@ class LedgerTrie
issued by this node.
@return Pair with the sequence number and ID of the preferred ledger
*/
std::pair<Seq,ID>
SpanTip<Ledger>
getPreferred(Seq const largestIssued) const
{
Node* curr = root.get();
Expand Down
49 changes: 18 additions & 31 deletions src/ripple/consensus/Validations.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,74 +637,61 @@ class Validations
and is *not* an ancestor of the current working ledger; otherwise it
remains the current working ledger.

@param currLedger The local node's current working ledger
@param curr The local node's current working ledger

@return The sequence and id of the preferred working ledger,
or Seq{0},ID{0} if no trusted validations are available to
determine the preferred ledger.
*/
std::pair<Seq, ID>
getPreferred(Ledger const& currLedger)
getPreferred(Ledger const& curr)
{
Seq preferredSeq{0};
ID preferredID{0};

ScopedLock lock{mutex_};
std::tie(preferredSeq, preferredID) =
SpanTip<Ledger> preferred =
withTrie(lock, [this](LedgerTrie<Ledger>& trie) {
return trie.getPreferred(localSeqEnforcer_.largest());
});

// No trusted validations to determine branch
if (preferredSeq == Seq{0})
return std::make_pair(preferredSeq, preferredID);

Seq currSeq = currLedger.seq();
ID currID = currLedger.id();
if (preferred.seq == Seq{0})
return std::make_pair(preferred.seq, preferred.id);

// If we are the parent of the preferred ledger, stick with our
// current ledger since we might be about to generate it
if (preferredSeq == currSeq + Seq{1})
{
for (auto const& it : lastLedger_)
{
Ledger const& ledger = it.second;
if (ledger.seq() == preferredSeq &&
ledger.id() == preferredID && ledger[currSeq] == currID)
return std::make_pair(currSeq, currID);
}
}
if (preferred.seq == curr.seq() + Seq{1} &&
preferred.ancestor(curr.seq()) == curr.id())
return std::make_pair(curr.seq(), curr.id());

// A ledger ahead of us is preferred regardless of whether it is
// a descendant of our working ledger or it is on a different chain
if (preferredSeq > currSeq)
return std::make_pair(preferredSeq, preferredID);
if (preferred.seq > curr.seq())
return std::make_pair(preferred.seq, preferred.id);

// Only switch to earlier or same sequence number
// if it is a different chain.
if (currLedger[preferredSeq] != preferredID)
return std::make_pair(preferredSeq, preferredID);
if (curr[preferred.seq] != preferred.id)
return std::make_pair(preferred.seq, preferred.id);

// Stick with current ledger
return std::make_pair(currSeq, currID);
return std::make_pair(curr.seq(), curr.id());
}

/** Get the ID of the preferred working ledger that exceeds a minimum valid
ledger sequence number

@param currLedger Current working ledger
@param curr Current working ledger
@param minValidSeq Minimum allowed sequence number

@return ID Of the preferred ledger, or currLedger if the preferred ledger
@return ID Of the preferred ledger, or curr if the preferred ledger
is not valid
*/
ID
getPreferred(Ledger const& currLedger, Seq minValidSeq)
getPreferred(Ledger const& curr, Seq minValidSeq)
{
std::pair<Seq, ID> preferred = getPreferred(currLedger);
std::pair<Seq, ID> preferred = getPreferred(curr);
if(preferred.first >= minValidSeq && preferred.second != ID{0})
return preferred.second;
return currLedger.id();
return curr.id();

}

Expand Down
Loading