2017-05-30 21:55:17 +02:00
|
|
|
// Copyright (c) 2018 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <interfaces/chain.h>
|
|
|
|
|
2017-07-27 16:08:31 +02:00
|
|
|
#include <chain.h>
|
2017-07-26 16:23:01 +02:00
|
|
|
#include <sync.h>
|
2017-07-27 16:08:31 +02:00
|
|
|
#include <uint256.h>
|
2017-05-30 21:55:17 +02:00
|
|
|
#include <util/system.h>
|
2017-07-26 16:23:01 +02:00
|
|
|
#include <validation.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2017-05-30 21:55:17 +02:00
|
|
|
|
|
|
|
namespace interfaces {
|
|
|
|
namespace {
|
|
|
|
|
2017-07-26 16:23:01 +02:00
|
|
|
class LockImpl : public Chain::Lock
|
|
|
|
{
|
2017-07-27 16:08:31 +02:00
|
|
|
Optional<int> getHeight() override
|
|
|
|
{
|
|
|
|
int height = ::chainActive.Height();
|
|
|
|
if (height >= 0) {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
Optional<int> getBlockHeight(const uint256& hash) override
|
|
|
|
{
|
|
|
|
CBlockIndex* block = LookupBlockIndex(hash);
|
|
|
|
if (block && ::chainActive.Contains(block)) {
|
|
|
|
return block->nHeight;
|
|
|
|
}
|
|
|
|
return nullopt;
|
|
|
|
}
|
|
|
|
int getBlockDepth(const uint256& hash) override
|
|
|
|
{
|
|
|
|
const Optional<int> tip_height = getHeight();
|
|
|
|
const Optional<int> height = getBlockHeight(hash);
|
|
|
|
return tip_height && height ? *tip_height - *height + 1 : 0;
|
|
|
|
}
|
|
|
|
uint256 getBlockHash(int height) override
|
|
|
|
{
|
|
|
|
CBlockIndex* block = ::chainActive[height];
|
|
|
|
assert(block != nullptr);
|
|
|
|
return block->GetBlockHash();
|
|
|
|
}
|
2017-07-26 16:23:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
|
|
|
|
{
|
|
|
|
using UniqueLock::UniqueLock;
|
|
|
|
};
|
|
|
|
|
2017-05-30 21:55:17 +02:00
|
|
|
class ChainImpl : public Chain
|
|
|
|
{
|
2017-07-26 16:23:01 +02:00
|
|
|
public:
|
|
|
|
std::unique_ptr<Chain::Lock> lock(bool try_lock) override
|
|
|
|
{
|
|
|
|
auto result = MakeUnique<LockingStateImpl>(::cs_main, "cs_main", __FILE__, __LINE__, try_lock);
|
|
|
|
if (try_lock && result && !*result) return {};
|
|
|
|
// std::move necessary on some compilers due to conversion from
|
|
|
|
// LockingStateImpl to Lock pointer
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
std::unique_ptr<Chain::Lock> assumeLocked() override { return MakeUnique<LockImpl>(); }
|
2017-05-30 21:55:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<Chain> MakeChain() { return MakeUnique<ChainImpl>(); }
|
|
|
|
|
|
|
|
} // namespace interfaces
|