// 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. #include #include #include #include "crypto/hash.h" #include namespace CryptoNote { class ISerializer; 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 bool push(const Crypto::Hash& h) { auto result = m_container.push_back(h); return result.second; } bool hasBlock(const Crypto::Hash& h) const { return m_index.find(h) != m_index.end(); } bool getBlockHeight(const Crypto::Hash& h, uint32_t& height) const { auto hi = m_index.find(h); if (hi == m_index.end()) return false; height = static_cast(std::distance(m_container.begin(), m_container.project<0>(hi))); return true; } uint32_t size() const { return static_cast(m_container.size()); } void clear() { m_container.clear(); } Crypto::Hash getBlockId(uint32_t height) const; std::vector getBlockIds(uint32_t startBlockIndex, uint32_t maxCount) const; bool findSupplement(const std::vector& ids, uint32_t& offset) const; std::vector buildSparseChain(const Crypto::Hash& startBlockId) const; Crypto::Hash getTailId() const; void serialize(ISerializer& s); private: typedef boost::multi_index_container < Crypto::Hash, boost::multi_index::indexed_by< boost::multi_index::random_access<>, boost::multi_index::hashed_unique> > > ContainerT; ContainerT m_container; ContainerT::nth_index<1>::type& m_index; }; }