veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
altintegration::Chain< BlockIndexT > Struct Template Reference

Fully in-memory chain representation. More...

Detailed Description

template<typename BlockIndexT>
struct altintegration::Chain< BlockIndexT >

Every chain has exactly one block at every height.

Template Parameters
Block

Definition at line 25 of file chain.hpp.

#include <chain.hpp>

+ Inheritance diagram for altintegration::Chain< BlockIndexT >:
+ Collaboration diagram for altintegration::Chain< BlockIndexT >:

Public Types

using index_t = BlockIndexT
 
using block_t = typename index_t::block_t
 
using hash_t = typename index_t::hash_t
 
using height_t = typename block_t::height_t
 
using storage_t = std::vector< index_t * >
 
using iterator_t = typename storage_t::iterator
 
using const_iterator_t = typename storage_t::const_iterator
 
using reverse_iterator_t = typename storage_t::reverse_iterator
 
using const_reverse_iterator_t = typename storage_t::const_reverse_iterator
 

Public Member Functions

 Chain (height_t startHeight)
 
 Chain (height_t startHeight, index_t *tip)
 
height_t getStartHeight () const
 
height_t firstHeight () const
 
bool contains (const index_t *index) const
 
index_t * operator[] (height_t height) const
 
index_t * next (const index_t *index) const
 
height_t chainHeight () const
 
bool empty () const
 
size_t size () const
 
size_t blocksCount () const
 
index_t * tip () const
 
index_t * first () const
 
reverse_iterator_t rbegin ()
 
const_reverse_iterator_t rbegin () const
 
reverse_iterator_t rend ()
 
const_reverse_iterator_t rend () const
 
iterator_t begin ()
 
const_iterator_t begin () const
 
iterator_t end ()
 
const_iterator_t end () const
 
void prependRoot (index_t *index)
 
void setTip (index_t *index)
 
void disconnectTip ()
 

Friends

bool operator== (const Chain &a, const Chain &b)
 
bool operator!= (const Chain &a, const Chain &b)
 

Member Typedef Documentation

◆ block_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::block_t = typename index_t::block_t

Definition at line 27 of file chain.hpp.

◆ const_iterator_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::const_iterator_t = typename storage_t::const_iterator

Definition at line 33 of file chain.hpp.

◆ const_reverse_iterator_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::const_reverse_iterator_t = typename storage_t::const_reverse_iterator

Definition at line 35 of file chain.hpp.

◆ hash_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::hash_t = typename index_t::hash_t

Definition at line 28 of file chain.hpp.

◆ height_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::height_t = typename block_t::height_t

Definition at line 29 of file chain.hpp.

◆ index_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::index_t = BlockIndexT

Definition at line 26 of file chain.hpp.

◆ iterator_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::iterator_t = typename storage_t::iterator

Definition at line 32 of file chain.hpp.

◆ reverse_iterator_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::reverse_iterator_t = typename storage_t::reverse_iterator

Definition at line 34 of file chain.hpp.

◆ storage_t

template<typename BlockIndexT >
using altintegration::Chain< BlockIndexT >::storage_t = std::vector<index_t*>

Definition at line 30 of file chain.hpp.

Constructor & Destructor Documentation

◆ Chain() [1/2]

template<typename BlockIndexT >
altintegration::Chain< BlockIndexT >::Chain ( height_t  startHeight)
inlineexplicit

Definition at line 39 of file chain.hpp.

39: startHeight_(startHeight) {}

◆ Chain() [2/2]

template<typename BlockIndexT >
altintegration::Chain< BlockIndexT >::Chain ( height_t  startHeight,
index_t *  tip 
)
inlineexplicit

Definition at line 41 of file chain.hpp.

42 : startHeight_(startHeight) {
43 setTip(tip);
44 }

Member Function Documentation

◆ begin() [1/2]

template<typename BlockIndexT >
iterator_t altintegration::Chain< BlockIndexT >::begin ( )
inline

Definition at line 89 of file chain.hpp.

89{ return chain.begin(); }

◆ begin() [2/2]

template<typename BlockIndexT >
const_iterator_t altintegration::Chain< BlockIndexT >::begin ( ) const
inline

Definition at line 90 of file chain.hpp.

90{ return chain.begin(); }

◆ blocksCount()

template<typename BlockIndexT >
size_t altintegration::Chain< BlockIndexT >::blocksCount ( ) const
inline

Definition at line 76 of file chain.hpp.

76{ return size(); }

◆ chainHeight()

template<typename BlockIndexT >
height_t altintegration::Chain< BlockIndexT >::chainHeight ( ) const
inline

Definition at line 68 of file chain.hpp.

68 {
69 return (height_t)chain.size() + startHeight_ - 1;
70 }

◆ contains()

template<typename BlockIndexT >
bool altintegration::Chain< BlockIndexT >::contains ( const index_t *  index) const
inline

Definition at line 50 of file chain.hpp.

50 {
51 return index != nullptr && this->operator[](index->getHeight()) == index;
52 }

◆ disconnectTip()

template<typename BlockIndexT >
void altintegration::Chain< BlockIndexT >::disconnectTip ( )
inline

Definition at line 119 of file chain.hpp.

119{ chain.pop_back(); }

◆ empty()

template<typename BlockIndexT >
bool altintegration::Chain< BlockIndexT >::empty ( ) const
inline

Definition at line 72 of file chain.hpp.

72{ return chain.empty(); }

◆ end() [1/2]

template<typename BlockIndexT >
iterator_t altintegration::Chain< BlockIndexT >::end ( )
inline

Definition at line 91 of file chain.hpp.

91{ return chain.end(); }

◆ end() [2/2]

template<typename BlockIndexT >
const_iterator_t altintegration::Chain< BlockIndexT >::end ( ) const
inline

Definition at line 92 of file chain.hpp.

92{ return chain.end(); }

◆ first()

template<typename BlockIndexT >
index_t * altintegration::Chain< BlockIndexT >::first ( ) const
inline

Definition at line 82 of file chain.hpp.

82{ return chain.empty() ? nullptr : chain[0]; }

◆ firstHeight()

template<typename BlockIndexT >
height_t altintegration::Chain< BlockIndexT >::firstHeight ( ) const
inline

Definition at line 48 of file chain.hpp.

48{ return startHeight_; }

◆ getStartHeight()

template<typename BlockIndexT >
height_t altintegration::Chain< BlockIndexT >::getStartHeight ( ) const
inline

Definition at line 47 of file chain.hpp.

47{ return firstHeight(); }

◆ next()

template<typename BlockIndexT >
index_t * altintegration::Chain< BlockIndexT >::next ( const index_t *  index) const
inline

Definition at line 64 of file chain.hpp.

64 {
65 return !contains(index) ? nullptr : (*this)[index->getHeight() + 1];
66 }

◆ operator[]()

template<typename BlockIndexT >
index_t * altintegration::Chain< BlockIndexT >::operator[] ( height_t  height) const
inline

Definition at line 54 of file chain.hpp.

54 {
55 if (height < startHeight_) return nullptr;
56
57 height_t innerHeight = toInnerHeight(height);
58 if (innerHeight >= height_t(chain.size())) {
59 return nullptr;
60 }
61 return chain[innerHeight];
62 }

◆ prependRoot()

template<typename BlockIndexT >
void altintegration::Chain< BlockIndexT >::prependRoot ( index_t *  index)
inline

Definition at line 94 of file chain.hpp.

94 {
95 // set a new bootstrap block which should be connected to the current one
96 VBK_ASSERT_MSG(!chain.empty() && index == chain[0]->pprev,
97 "new bootstrap block does not connect to the current one");
98 startHeight_ = index->getHeight();
99 chain.insert(chain.begin(), index);
100 }

◆ rbegin() [1/2]

template<typename BlockIndexT >
reverse_iterator_t altintegration::Chain< BlockIndexT >::rbegin ( )
inline

Definition at line 84 of file chain.hpp.

84{ return chain.rbegin(); }

◆ rbegin() [2/2]

template<typename BlockIndexT >
const_reverse_iterator_t altintegration::Chain< BlockIndexT >::rbegin ( ) const
inline

Definition at line 85 of file chain.hpp.

85{ return chain.rbegin(); }

◆ rend() [1/2]

template<typename BlockIndexT >
reverse_iterator_t altintegration::Chain< BlockIndexT >::rend ( )
inline

Definition at line 86 of file chain.hpp.

86{ return chain.rend(); }

◆ rend() [2/2]

template<typename BlockIndexT >
const_reverse_iterator_t altintegration::Chain< BlockIndexT >::rend ( ) const
inline

Definition at line 87 of file chain.hpp.

87{ return chain.rend(); }

◆ setTip()

template<typename BlockIndexT >
void altintegration::Chain< BlockIndexT >::setTip ( index_t *  index)
inline

Definition at line 102 of file chain.hpp.

102 {
103 if (index == nullptr || index->getHeight() < startHeight_) {
104 chain.clear();
105 return;
106 }
107
108 height_t innerHeight = toInnerHeight(index->getHeight());
109 chain.resize(innerHeight + 1);
110
111 while (index != nullptr && !contains(index) &&
112 index->getHeight() >= startHeight_) {
113 innerHeight = toInnerHeight(index->getHeight());
114 chain[innerHeight] = index;
115 index = index->getPrev();
116 }
117 }

◆ size()

template<typename BlockIndexT >
size_t altintegration::Chain< BlockIndexT >::size ( ) const
inline

Definition at line 74 of file chain.hpp.

74{ return chain.size(); }

◆ tip()

template<typename BlockIndexT >
index_t * altintegration::Chain< BlockIndexT >::tip ( ) const
inline

Definition at line 78 of file chain.hpp.

78 {
79 return chain.empty() ? nullptr : (*this)[chainHeight()];
80 }

Friends And Related Function Documentation

◆ operator!=

template<typename BlockIndexT >
bool operator!= ( const Chain< BlockIndexT > &  a,
const Chain< BlockIndexT > &  b 
)
friend

Definition at line 129 of file chain.hpp.

129{ return !(a == b); }

◆ operator==

template<typename BlockIndexT >
bool operator== ( const Chain< BlockIndexT > &  a,
const Chain< BlockIndexT > &  b 
)
friend

Definition at line 121 of file chain.hpp.

121 {
122 // sizes may vary, so compare tips
123 if (a.tip() == nullptr && b.tip() == nullptr) return true;
124 if (a.tip() == nullptr) return false;
125 if (b.tip() == nullptr) return false;
126 return a.tip()->getHash() == b.tip()->getHash();
127 }

The documentation for this struct was generated from the following file: