Use lower quorum for smaller validator sets

This commit is contained in:
wilsonianb
2017-03-09 17:40:34 -08:00
committed by seelabs
parent 69bc58c5f6
commit ffc7cf8f6c
3 changed files with 97 additions and 4 deletions

View File

@@ -308,6 +308,9 @@ public:
for_each_listed (
std::function<void(PublicKey const&, bool)> func) const;
static std::size_t
calculateQuorum (std::size_t nTrustedKeys);
private:
/** Check response for trusted valid published list
@@ -412,11 +415,12 @@ ValidatorList::onConsensusStart (
auto size = rankedKeys.size();
// Use all eligible keys if there is only one trusted list.
if (publisherLists_.size() == 1)
// Use all eligible keys if there is less than 10 listed validators or
// only one trusted list
if (size < 10 || publisherLists_.size() == 1)
{
// Raise the quorum to 80% of the trusted set
std::size_t const targetQuorum = std::ceil (size * 0.8);
// Try to raise the quorum toward or above 80% of the trusted set
std::size_t const targetQuorum = ValidatorList::calculateQuorum (size);
if (targetQuorum > quorum)
quorum = targetQuorum;
}

View File

@@ -408,4 +408,16 @@ ValidatorList::for_each_listed (
func (v.first, trusted(v.first));
}
std::size_t
ValidatorList::calculateQuorum (std::size_t nTrustedKeys)
{
// Use 80% for large values of n, but have special cases for small numbers.
constexpr std::array<std::size_t, 10> quorum{{ 0, 1, 2, 2, 3, 3, 4, 5, 6, 7 }};
if (nTrustedKeys < quorum.size())
return quorum[nTrustedKeys];
return nTrustedKeys - nTrustedKeys / 5;
}
} // ripple

View File

@@ -124,6 +124,21 @@ private:
}
}
void
testCalculateQuorum ()
{
testcase ("Calculate Quorum");
for(std::size_t i = 1; i < 20; ++i)
{
auto const quorum = ValidatorList::calculateQuorum(i);
if (i < 10)
BEAST_EXPECT(quorum >= (i/2 + 1));
else
BEAST_EXPECT(quorum == std::ceil (i * 0.8));
}
}
void
testConfigLoad ()
{
@@ -733,6 +748,67 @@ private:
trustedKeys->onConsensusStart (activeValidators);
BEAST_EXPECT(! trustedKeys->trusted (list[0]));
}
{
// Test 1-9 configured validators
auto trustedKeys = std::make_unique <ValidatorList> (
manifests, manifests, env.timeKeeper(), beast::Journal ());
std::vector<std::string> cfgPublishers;
hash_set<PublicKey> activeValidators;
std::vector<std::string> cfgKeys;
cfgKeys.reserve(9);
while (cfgKeys.size() < cfgKeys.capacity())
{
auto const valKey = randomNode();
cfgKeys.push_back (toBase58(
TokenType::TOKEN_NODE_PUBLIC, valKey));
activeValidators.emplace (valKey);
BEAST_EXPECT(trustedKeys->load (
emptyLocalKey, cfgKeys, cfgPublishers));
trustedKeys->onConsensusStart (activeValidators);
BEAST_EXPECT(trustedKeys->quorum () ==
ValidatorList::calculateQuorum(cfgKeys.size()));
for (auto const& key : activeValidators)
BEAST_EXPECT(trustedKeys->trusted (key));
}
}
{
// Test 2-9 configured validators as validator
auto trustedKeys = std::make_unique <ValidatorList> (
manifests, manifests, env.timeKeeper(), beast::Journal ());
auto const localKey = randomNode();
std::vector<std::string> cfgPublishers;
hash_set<PublicKey> activeValidators;
std::vector<std::string> cfgKeys {
toBase58(TokenType::TOKEN_NODE_PUBLIC, localKey)};
cfgKeys.reserve(9);
while (cfgKeys.size() < cfgKeys.capacity())
{
auto const valKey = randomNode();
cfgKeys.push_back (toBase58(
TokenType::TOKEN_NODE_PUBLIC, valKey));
activeValidators.emplace (valKey);
BEAST_EXPECT(trustedKeys->load (
localKey, cfgKeys, cfgPublishers));
trustedKeys->onConsensusStart (activeValidators);
// When running as an unlisted validator,
// the quorum is incremented by 1 for 3 or 5 trusted validators.
auto expectedQuorum = ValidatorList::calculateQuorum(cfgKeys.size());
if (cfgKeys.size() == 3 || cfgKeys.size() == 5)
++expectedQuorum;
BEAST_EXPECT(trustedKeys->quorum () == expectedQuorum);
for (auto const& key : activeValidators)
BEAST_EXPECT(trustedKeys->trusted (key));
}
}
}
public:
@@ -740,6 +816,7 @@ public:
run() override
{
testGenesisQuorum ();
testCalculateQuorum ();
testConfigLoad ();
testApplyList ();
testUpdate ();