veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
altintegration::MemPool Struct Reference

Data structure, that stores in memory all valid payloads (ATV, VTB, VbkBlock) submitted by other peers through p2p or by users through rpc. More...

Detailed Description

Invariant
All payloads in MemPool are at least statelessly valid.
Those payloads that are not contextually valid given current view may become valid or invalid later. Example: VTB's VBK containing block is 100, currently last known VBK block is 90. ATM we can't connect VTB to current VBK chain, so this VTB remains in MemPool until we explicitly remove it, or connect containing VBK block with this VTB.

Definition at line 41 of file mempool.hpp.

#include <mempool.hpp>

+ Collaboration diagram for altintegration::MemPool:

Classes

struct  SubmitResult
 Result of submission to mempool. More...
 

Public Types

enum  Status { VALID = 0 , FAILED_STATEFUL = 1 , FAILED_STATELESS = 2 }
 
using vbk_hash_t = typename VbkBlock::prev_hash_t
 
template<typename Payload >
using payload_map = std::unordered_map< typename Payload::id_t, std::shared_ptr< Payload > >
 
template<typename Payload >
using payload_value_sorted_map = ValueSortedMap< typename Payload::id_t, std::shared_ptr< Payload > >
 
using vbk_map_t = payload_map< VbkBlock >
 
using atv_map_t = payload_map< ATV >
 
using vtb_map_t = payload_map< VTB >
 
using relations_map_t = payload_map< VbkPayloadsRelations >
 
using vbk_value_sorted_map_t = payload_value_sorted_map< VbkBlock >
 
using vtb_value_sorted_map_t = payload_value_sorted_map< VTB >
 
using atv_value_sorted_map_t = payload_value_sorted_map< ATV >
 

Public Member Functions

 MemPool (AltBlockTree &tree)
 
template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN bool isKnown (const typename T::id_t &id, const bool onlyInMempool=false) const
 Use this method to determine if payload of type T with id id is already known to active chain. More...
 
template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN const T * get (const typename T::id_t &id) const
 getter for payloads stored in mempool More...
 
template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN SubmitResult submit (Slice< const uint8_t > bytes, bool doIsBlockOldCheck, ValidationState &state)
 Add new payload to mempool. More...
 
template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN SubmitResult submit (const T &pl, bool doIsBlockOldCheck, ValidationState &state)
 Add new payload to mempool. More...
 
template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN SubmitResult submit (const std::shared_ptr< T > &pl, bool doIsBlockOldCheck, ValidationState &state)
 Add new payload to mempool. More...
 
std::vector< BtcBlock::hash_tgetMissingBtcBlocks () const
 
VBK_CHECK_RETURN PopData generatePopData ()
 (POW) Miners should execute this to get POP content for "next block". More...
 
VBK_CHECK_RETURN PopData generatePopData (const std::function< void(const ATV &, const ValidationState &)> &onATV, const std::function< void(const VTB &, const ValidationState &)> &onVTB, const std::function< void(const VbkBlock &, const ValidationState &)> &onVBK)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void removeAll (const PopData &popData)
 Remove payloads from mempool by their IDs. More...
 
void cleanUp ()
 Remove paylaods that are statefully invalid anymore (duplicated, staled payloads)
 
void clear ()
 Clear mempool from all payloads.
 
template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
size_t onAccepted (std::function< void(const T &p)> f)
 Subscribe on "accepted" event - fires whenever new payload is added into mempool. More...
 
template<>
MemPool::SubmitResult submit (const std::shared_ptr< ATV > &atv, bool doIsBlockOldCheck, ValidationState &state)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<>
MemPool::SubmitResult submit (const std::shared_ptr< VTB > &vtb, bool doIsBlockOldCheck, ValidationState &state)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<>
MemPool::SubmitResult submit (const std::shared_ptr< VbkBlock > &block, bool doIsBlockOldCheck, ValidationState &state)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<>
const MemPool::payload_map< VbkBlock > & getMap () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<>
const MemPool::payload_value_sorted_map< VbkBlock > & getInFlightMap () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 

Public Attributes

signals::Signal< void(const ATV &atv)> on_atv_accepted
 fires when new valid ATV is accepted to mempool More...
 
signals::Signal< void(const VTB &atv)> on_vtb_accepted
 fires when new valid VTB is accepted to mempool More...
 
signals::Signal< void(const VbkBlock &atv)> on_vbkblock_accepted
 fires when new valid VbkBlock is accepted to mempool More...
 

Member Typedef Documentation

◆ atv_map_t

using altintegration::MemPool::atv_map_t = payload_map<ATV>

Definition at line 82 of file mempool.hpp.

◆ atv_value_sorted_map_t

using altintegration::MemPool::atv_value_sorted_map_t = payload_value_sorted_map<ATV>

Definition at line 88 of file mempool.hpp.

◆ payload_map

template<typename Payload >
using altintegration::MemPool::payload_map = std::unordered_map<typename Payload::id_t, std::shared_ptr<Payload> >

Definition at line 74 of file mempool.hpp.

◆ payload_value_sorted_map

template<typename Payload >
using altintegration::MemPool::payload_value_sorted_map = ValueSortedMap<typename Payload::id_t, std::shared_ptr<Payload> >

Definition at line 78 of file mempool.hpp.

◆ relations_map_t

using altintegration::MemPool::relations_map_t = payload_map<VbkPayloadsRelations>

Definition at line 84 of file mempool.hpp.

◆ vbk_hash_t

using altintegration::MemPool::vbk_hash_t = typename VbkBlock::prev_hash_t

Definition at line 71 of file mempool.hpp.

◆ vbk_map_t

using altintegration::MemPool::vbk_map_t = payload_map<VbkBlock>

Definition at line 81 of file mempool.hpp.

◆ vbk_value_sorted_map_t

using altintegration::MemPool::vbk_value_sorted_map_t = payload_value_sorted_map<VbkBlock>

Definition at line 86 of file mempool.hpp.

◆ vtb_map_t

using altintegration::MemPool::vtb_map_t = payload_map<VTB>

Definition at line 83 of file mempool.hpp.

◆ vtb_value_sorted_map_t

using altintegration::MemPool::vtb_value_sorted_map_t = payload_value_sorted_map<VTB>

Definition at line 87 of file mempool.hpp.

Member Enumeration Documentation

◆ Status

enum altintegration::MemPool::Status

Definition at line 42 of file mempool.hpp.

42{ VALID = 0, FAILED_STATEFUL = 1, FAILED_STATELESS = 2 };

Member Function Documentation

◆ generatePopData() [1/2]

VBK_CHECK_RETURN PopData altintegration::MemPool::generatePopData ( )
Warning
Expensive operation. It builds virtual VBK Block Tree with payloads stored in mempool, applies them to current AltBlockTree tip. All payloads that can not be connected will remain in mempool. As a result, this method returns altintegration::PopData which contains fully valid and connected payloads. This should be inserted into AltBlock as is.
Returns
statefully valid altintegration::PopData that can be connected to current tip.

◆ generatePopData() [2/2]

VBK_CHECK_RETURN PopData altintegration::MemPool::generatePopData ( const std::function< void(const ATV &, const ValidationState &)> &  onATV,
const std::function< void(const VTB &, const ValidationState &)> &  onVTB,
const std::function< void(const VbkBlock &, const ValidationState &)> &  onVBK 
)
Parameters
[in]onATVa callback that is executed when ATV have been considered
[in]onVTBa callback that is executed when VTB have been considered
[in]onVBKa callback that is executed when VbkBlock have been considered

◆ get()

template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN const T * altintegration::MemPool::get ( const typename T::id_t &  id) const
inline

Definition at line 155 of file mempool.hpp.

155 {
156 const auto& map = getMap<T>();
157 auto it = map.find(id);
158 if (it != map.end()) {
159 return it->second.get();
160 }
161
162 const auto& inflight = getInFlightMap<T>();
163 auto it2 = inflight.find(id);
164 if (it2 != inflight.end()) {
165 return it2->second.get();
166 }
167
168 return nullptr;
169 }

◆ isKnown()

template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN bool altintegration::MemPool::isKnown ( const typename T::id_t &  id,
const bool  onlyInMempool = false 
) const
inline
Template Parameters
TATV or VTB or VbkBlock
Parameters
[in]idpayload id
[in]onlyInMempoolif true, blockchain won't be searched for "known" payloads.
Returns
true if payload exists in mempool or active chain, false otherwise.

Definition at line 104 of file mempool.hpp.

105 {
106 // is `id` in mempool?
107 auto* inmempool = get<T>(id);
108 if (inmempool != nullptr) {
109 return true;
110 }
111
112 if (onlyInMempool) {
113 // we did not find in mempool, so exit early
114 return false;
115 }
116
117 const std::vector<uint8_t> v(id.begin(), id.end());
118
119 auto& tree = mempool_tree_.alt();
120 auto& fpl = tree.getFinalizedPayloadsIndex();
121 const auto* blockhash = fpl.find(v);
122 if (blockhash != nullptr) {
123 // finalized payloads index stores only payloads from finalized blocks.
124 // all finalized blocks are on active chain.
125 return true;
126 }
127
128 auto& pl = tree.getPayloadsIndex();
129 // is `id` in payloads index?
130 const auto& set = pl.find(v);
131 if (set.empty()) {
132 return false;
133 }
134
135 // check if any of candidates is on active chain
136 for (const auto& hash : set) {
137 const auto* candidate = tree.getBlockIndex(hash);
138 // all candidates must exist in a tree after split on
139 // PayloadsIndex+FinalizedPayloadsIndex. only finalized blocks may not
140 // exist in a tree.
141 VBK_ASSERT_MSG(candidate != nullptr, candidate->toPrettyString());
142 if (tree.getBestChain().contains(candidate)) {
143 // candidate is on main chain
144 return true;
145 }
146 }
147
148 // none of candidates are on main chain
149 return false;
150 }

◆ onAccepted()

template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
size_t altintegration::MemPool::onAccepted ( std::function< void(const T &p)>  f)
inline
Template Parameters
PopATV, VTB or VbkBlock
Parameters
fcallback
Returns
subscription id

Definition at line 329 of file mempool.hpp.

329 {
330 auto& sig = getSignal<T>();
331 return sig.connect(f);
332 }

◆ removeAll()

void altintegration::MemPool::removeAll ( const PopData popData)

Use it when new block arrives and it contains PopData. Doing this, mempool also executes clean() method.

Parameters
[in]popDataaltintegration::PopData

◆ submit() [1/3]

template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN SubmitResult altintegration::MemPool::submit ( const std::shared_ptr< T > &  pl,
bool  doIsBlockOldCheck,
ValidationState state 
)
inline

Does stateless validation, and conditionally contextual validation.

Note
When payloads are received by rpc, you can do contextual validation immediately. If payloads are added during reorg/p2p sync, you can disable contextual validation, as otherwise it may ban valid payloads from remote peers.
Template Parameters
shared_ptr<T>one of VTB, ATV, VbkBlock
Parameters
[in]plpayload
[in]doIsBlockOldCheckflag
[out]statevalidation state
Returns
true if payload is accepted to mempool, false otherwise

Definition at line 249 of file mempool.hpp.

251 {
252 (void)pl;
253 (void)state;
254 (void)doIsBlockOldCheck;
255 static_assert(sizeof(T) == 0, "Undefined type used in MemPool::submit");
256 return {};
257 }

◆ submit() [2/3]

template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN SubmitResult altintegration::MemPool::submit ( const T &  pl,
bool  doIsBlockOldCheck,
ValidationState state 
)
inline

Does stateless validation, and conditionally contextual validation.

Note
When payloads are received by rpc, you can do contextual validation immediately. If payloads are added during reorg/p2p sync, you can disable contextual validation, as otherwise it may ban valid payloads from remote peers.
Template Parameters
Tone of VTB, ATV, VbkBlock
Parameters
[in]plpayload
[in]doIsBlockOldCheckflag
[out]statevalidation state validation
Returns
true if payload is accepted to mempool, false otherwise

Definition at line 224 of file mempool.hpp.

226 {
227 return submit<T>(std::make_shared<T>(pl), doIsBlockOldCheck, state);
228 }

◆ submit() [3/3]

template<typename T , typename = typename std::enable_if<IsPopPayload<T>::value>::type>
VBK_CHECK_RETURN SubmitResult altintegration::MemPool::submit ( Slice< const uint8_t >  bytes,
bool  doIsBlockOldCheck,
ValidationState state 
)
inline

Does stateless validation, and conditionally contextual validation.

Note
When payloads are received by rpc, you can do contextual validation immediately. If payloads are added during reorg/p2p sync, you can disable contextual validation, as otherwise it may ban valid payloads from remote peers.
Template Parameters
Rawrepresantation one of VTB, ATV, VbkBlock
Parameters
[in]bytespayload`s bytes
[in]doIsBlockOldCheckflag
[out]statevalidation state validation
Returns
true if payload is accepted to mempool, false otherwise

Definition at line 191 of file mempool.hpp.

193 {
194 ReadStream stream(bytes);
195 T payload;
196 if (!DeserializeFromVbkEncoding(stream, payload, state)) {
197 return {FAILED_STATELESS,
198 state.Invalid("pop-mempool-submit-deserialize")};
199 }
200
201 return submit<T>(payload, doIsBlockOldCheck, state);
202 }
bool DeserializeFromVbkEncoding(ReadStream &stream, AltBlockAddon &out, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...

Member Data Documentation

◆ on_atv_accepted

signals::Signal<void(const ATV& atv)> altintegration::MemPool::on_atv_accepted

Definition at line 335 of file mempool.hpp.

◆ on_vbkblock_accepted

signals::Signal<void(const VbkBlock& atv)> altintegration::MemPool::on_vbkblock_accepted

Definition at line 339 of file mempool.hpp.

◆ on_vtb_accepted

signals::Signal<void(const VTB& atv)> altintegration::MemPool::on_vtb_accepted

Definition at line 337 of file mempool.hpp.


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