diff --git a/src/blockindexworkcomparator.h b/src/blockindexworkcomparator.h new file mode 100644 --- /dev/null +++ b/src/blockindexworkcomparator.h @@ -0,0 +1,43 @@ +// Copyright (c) 2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_BLOCK_INDEX_WORK_COMPARATOR_H +#define BITCOIN_BLOCK_INDEX_WORK_COMPARATOR_H + +// TODO: Split chain.h apart and only include CBlockIndex +#include "chain.h" + +struct CBlockIndexWorkComparator { + bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const { + // First sort by most total work, ... + if (pa->nChainWork > pb->nChainWork) { + return false; + } + if (pa->nChainWork < pb->nChainWork) { + return true; + } + + // ... then by earliest time received, ... + if (pa->nSequenceId < pb->nSequenceId) { + return false; + } + if (pa->nSequenceId > pb->nSequenceId) { + return true; + } + + // Use pointer address as tie breaker (should only happen with blocks + // loaded from disk, as those all have id 0). + if (pa < pb) { + return false; + } + if (pa > pb) { + return true; + } + + // Identical blocks. + return false; + } +}; + +#endif // BITCOIN_BLOCK_INDEX_WORK_COMPARATOR_H diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -7,6 +7,7 @@ #include "validation.h" #include "arith_uint256.h" +#include "blockindexworkcomparator.h" #include "chainparams.h" #include "checkpoints.h" #include "checkqueue.h" @@ -94,38 +95,6 @@ // Internal stuff namespace { -struct CBlockIndexWorkComparator { - bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const { - // First sort by most total work, ... - if (pa->nChainWork > pb->nChainWork) { - return false; - } - if (pa->nChainWork < pb->nChainWork) { - return true; - } - - // ... then by earliest time received, ... - if (pa->nSequenceId < pb->nSequenceId) { - return false; - } - if (pa->nSequenceId > pb->nSequenceId) { - return true; - } - - // Use pointer address as tie breaker (should only happen with blocks - // loaded from disk, as those all have id 0). - if (pa < pb) { - return false; - } - if (pa > pb) { - return true; - } - - // Identical blocks. - return false; - } -}; - CBlockIndex *pindexBestInvalid; /**