danicoin/src/CryptoNoteCore/BlockIndex.h

77 lines
2 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-08-13 10:51:37 +00:00
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include "crypto/hash.h"
2015-07-30 15:22:07 +00:00
#include <vector>
2014-08-13 10:51:37 +00:00
namespace CryptoNote
{
2015-07-30 15:22:07 +00:00
class ISerializer;
2014-08-13 10:51:37 +00:00
class BlockIndex {
public:
BlockIndex() :
m_index(m_container.get<1>()) {}
void pop() {
m_container.pop_back();
}
// returns true if new element was inserted, false if already exists
2015-07-30 15:22:07 +00:00
bool push(const Crypto::Hash& h) {
2014-08-13 10:51:37 +00:00
auto result = m_container.push_back(h);
return result.second;
}
2015-07-30 15:22:07 +00:00
bool hasBlock(const Crypto::Hash& h) const {
2014-08-13 10:51:37 +00:00
return m_index.find(h) != m_index.end();
}
2015-07-30 15:22:07 +00:00
bool getBlockHeight(const Crypto::Hash& h, uint32_t& height) const {
2014-08-13 10:51:37 +00:00
auto hi = m_index.find(h);
if (hi == m_index.end())
return false;
2015-07-30 15:22:07 +00:00
height = static_cast<uint32_t>(std::distance(m_container.begin(), m_container.project<0>(hi)));
2014-08-13 10:51:37 +00:00
return true;
}
2015-07-30 15:22:07 +00:00
uint32_t size() const {
return static_cast<uint32_t>(m_container.size());
2014-08-13 10:51:37 +00:00
}
void clear() {
m_container.clear();
}
2015-07-30 15:22:07 +00:00
Crypto::Hash getBlockId(uint32_t height) const;
std::vector<Crypto::Hash> getBlockIds(uint32_t startBlockIndex, uint32_t maxCount) const;
bool findSupplement(const std::vector<Crypto::Hash>& ids, uint32_t& offset) const;
std::vector<Crypto::Hash> buildSparseChain(const Crypto::Hash& startBlockId) const;
Crypto::Hash getTailId() const;
2014-08-13 10:51:37 +00:00
2015-07-30 15:22:07 +00:00
void serialize(ISerializer& s);
2014-08-13 10:51:37 +00:00
private:
typedef boost::multi_index_container <
2015-07-30 15:22:07 +00:00
Crypto::Hash,
2014-08-13 10:51:37 +00:00
boost::multi_index::indexed_by<
boost::multi_index::random_access<>,
2015-07-30 15:22:07 +00:00
boost::multi_index::hashed_unique<boost::multi_index::identity<Crypto::Hash>>
2014-08-13 10:51:37 +00:00
>
> ContainerT;
ContainerT m_container;
ContainerT::nth_index<1>::type& m_index;
};
}