Browse Source

Move initialisations to simplify cancelled checks

pull/145/head
Jack Grigg 8 years ago
parent
commit
1655db285d
  1. 9
      src/crypto/equihash.cpp
  2. 1
      src/crypto/equihash.h
  3. 24
      src/gtest/test_equihash.cpp

9
src/crypto/equihash.cpp

@ -357,6 +357,7 @@ template<unsigned int N, unsigned int K>
std::set<std::vector<eh_index>> Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state, const std::function<bool(EhSolverCancelCheck)> cancelled)
{
eh_index init_size { 1 << (CollisionBitLength + 1) };
eh_index recreate_size { UntruncateIndex(1, 0, CollisionBitLength + 1) };
// First run the algorithm with truncated indices
@ -364,6 +365,8 @@ std::set<std::vector<eh_index>> Equihash<N,K>::OptimisedSolve(const eh_HashState
// Each element of partialSolns is dynamically allocated in a call to
// GetTruncatedIndices(), and freed at the end of this function.
std::vector<eh_trunc*> partialSolns;
std::set<std::vector<eh_index>> solns;
int invalidCount = 0;
{
// 1) Generate first list
@ -458,7 +461,7 @@ std::set<std::vector<eh_index>> Equihash<N,K>::OptimisedSolve(const eh_HashState
}
i += j;
if (cancelled(FinalColliding)) break;
if (cancelled(FinalColliding)) goto cancelsolver;
}
} else
LogPrint("pow", "- List is empty\n");
@ -469,10 +472,6 @@ std::set<std::vector<eh_index>> Equihash<N,K>::OptimisedSolve(const eh_HashState
// Now for each solution run the algorithm again to recreate the indices
LogPrint("pow", "Culling solutions\n");
std::set<std::vector<eh_index>> solns;
eh_index recreate_size { UntruncateIndex(1, 0, CollisionBitLength + 1) };
int invalidCount = 0;
if (cancelled(StartCulling)) goto cancelsolver;
for (eh_trunc* partialSoln : partialSolns) {
size_t hashLen;
size_t lenIndices;

1
src/crypto/equihash.h

@ -118,7 +118,6 @@ enum EhSolverCancelCheck
RoundEnd,
FinalSorting,
FinalColliding,
StartCulling,
PartialGeneration,
PartialSorting,
PartialSubtreeEnd,

24
src/gtest/test_equihash.cpp

@ -51,12 +51,6 @@ TEST(equihash_tests, check_basic_solver_cancelled) {
}), EhSolverCancelledException);
}
{
ASSERT_NO_THROW(Eh48_5.BasicSolve(state, [](EhSolverCancelCheck pos) {
return pos == StartCulling;
}));
}
{
ASSERT_NO_THROW(Eh48_5.BasicSolve(state, [](EhSolverCancelCheck pos) {
return pos == PartialGeneration;
@ -130,25 +124,9 @@ TEST(equihash_tests, check_optimised_solver_cancelled) {
}), EhSolverCancelledException);
}
{
// More state required here, because in OptimisedSolve() the
// FinalColliding cancellation check can't throw because it will leak
// memory, and it can't goto because that steps over initialisations.
bool triggered = false;
ASSERT_THROW(Eh48_5.OptimisedSolve(state, [=](EhSolverCancelCheck pos) mutable {
if (triggered)
return pos == StartCulling;
if (pos == FinalColliding) {
triggered = true;
return true;
}
return false;
}), EhSolverCancelledException);
}
{
ASSERT_THROW(Eh48_5.OptimisedSolve(state, [](EhSolverCancelCheck pos) {
return pos == StartCulling;
return pos == FinalColliding;
}), EhSolverCancelledException);
}

Loading…
Cancel
Save