Represents simplified view on Altchain's block tree, maintains VBK tree and BTC tree.
More...
Helps Altchain to compare Altchain blocks by POP score in POP Fork Resolution.
- Invariant
- If POP score of two chains are equal, it expects Altchain to resolve conflict using their native fork resolution algorithm.
AltBlockTree is initialized with Altchain, Veriblock and Bitcoin parameters, as well as with PayloadsStorage implementation.
AltChainParamsMain altp;
PayloadsProviderImpl provider;
BlockReaderImpl blockProvider
AltBlockTree tree(altp, vbkp, btcp, provider, blockProvider);
Represents simplified view on Altchain's block tree, maintains VBK tree and BTC tree.
mainnet network params in Bitcoin chain.
After initialization AltBlockTree does not contain any blocks. Users MUST bootstrap AltBlockTree - add initial/genesis/root block of Altchain.
We will refer to this block as bootstrap block. Bootstrap block is a first block in Altchain that can be endorsed, but can not contain POP body (PopData).
If bootstrap()
fails, your block provided in struct AltChainParams::getBootstrapBlock() is invalid.
ValidationState will contain detailed info about failure reason, if any.
bool ret = tree.bootstrap(state);
assert(ret && "bootstrap is unsuccessful");
Class that is used for storing validation state.
- Invariant
- bootstrap block is immediately finalized - it can not be reorganized.
Whenever any new full block (block with header, block body and PopData) is accepted, users must first add block header to AltBlockTree, then add PopData to this block.
return false;
}
void acceptBlock(const hash_t &block, const PopData &payloads)
Attach "block body" - PopData to block header, which already exists in AltBlockTree.
VBK_CHECK_RETURN bool acceptBlockHeader(const AltBlock &block, ValidationState &state)
Validate and add ALT block header to AltBlockTree.
AltBlockTree::acceptBlockHeader() connects block immediately if all previous blocks are connected, or just adds block body to AltBlock when one of previous blocks is not connected.
After that, users can check if this block is connected:
assert(blockindex && "we added this block to the tree, so it must exist");
if(candidates.empty()) {
return true;
}
for(const auto* candidate : candidates) {
VBK_CHECK_RETURN int comparePopScore(const AltBlock::hash_t &A, const AltBlock::hash_t &B)
Efficiently compares current tip (A) and any other block (B).
std::vector< const index_t * > getConnectedTipsAfter(const index_t &index) const
Get all connected tips after given block.
const Chain< index_t > & getBestChain() const
Getter for currently Active Chain.
index_t * getBlockIndex(const T &hash)
Get BlockIndex by block hash.
- Note
- after AltBlockTree::comparePopScore AltBlockTree always corresponds to a state, as if winner chain have been applied.
if(result < 0) {
UpdateTip(candidate->getHash());
tip = candidate;
return true;
} else if (result == 0) {
if(tip->nChainWork < candidate->nChainWork) {
UpdateTip(candidate->getHash());
}
return true;
} else {
return true;
}
}
}
- Invariant
- AltBlockTree::comparePopScore always compares current AltBlockTree tip to other block. To avoid confusion, you must specify tip explicitly as first arg. If incorrect tip is passed, function dies on assert.
-
AltBlockTree::comparePopScore always leaves AltBlockTree switched to winner (by POP Score) chain.
-
Current active chain of AltBlockTree always corresponds to an empty tree with all applied blocks from first bootstrap block to current tip, i.e. currently applied active chain and this state MUST be always valid.
When tip is changed, Altchain MUST change state of AltBlockTree:
- Note
- use AltBlockTree::setState() to switch from current best chain to new block. It can very expensive operation if there's large reorg. If altchain is already at this state, this is a no-op.
bool ret = tree.
setState(bestHash, state);
assert(ret && "this block won FR, so it must be valid");
}
VBK_CHECK_RETURN bool setState(index_t &to, ValidationState &state) override
Switch AltBlockTree from the current tip to different block, while doing all validations of intermedi...
- Invariant
- Current tip of your Altchain tree MUST correspond to
tree.getBestChain().tip()
, so calling AltBlockTree::setState() ensures they are in sync.
- See also
- PayloadsStorage
-
AltChainParams
-
VbkChainParams
-
BtcChainParams
Definition at line 178 of file alt_block_tree.hpp.
|
| AltBlockTree (const alt_config_t &alt_config, const vbk_config_t &vbk_config, const btc_config_t &btc_config, PayloadsStorage &payloadsProvider, BlockReader &blockProvider) |
|
void | bootstrap () |
| Set the very first (bootstrap) altchain block with POP enabled. More...
|
|
VBK_CHECK_RETURN bool | acceptBlockHeader (const AltBlock &block, ValidationState &state) |
| Validate and add ALT block header to AltBlockTree. More...
|
|
void | acceptBlock (const hash_t &block, const PopData &payloads) |
| Attach "block body" - PopData to block header, which already exists in AltBlockTree. More...
|
|
void | acceptBlock (index_t &index, const PopData &payloads, ValidationState &state) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
|
void | acceptBlock (index_t &index, const PopData &payloads) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
|
BlockPayloadMutator | makeConnectedLeafPayloadMutator (index_t &block) |
| Create a session object that allows incremental payload list modification in the given block. More...
|
|
std::vector< const index_t * > | getConnectedTipsAfter (const index_t &index) const |
| Get all connected tips after given block. More...
|
|
VBK_CHECK_RETURN bool | loadBlockForward (const stored_index_t &index, bool fast_load, ValidationState &state) override |
| Efficiently connect block loaded from disk as a leaf. More...
|
|
VBK_CHECK_RETURN bool | loadTip (const hash_t &hash, ValidationState &state) override |
| After all blocks loaded, efficiently set current tip. More...
|
|
VBK_CHECK_RETURN int | comparePopScore (const AltBlock::hash_t &A, const AltBlock::hash_t &B) |
| Efficiently compares current tip (A) and any other block (B). More...
|
|
VBK_CHECK_RETURN bool | setState (index_t &to, ValidationState &state) override |
| Switch AltBlockTree from the current tip to different block, while doing all validations of intermediate blocks. More...
|
|
const VbkBlockTree & | vbk () const |
| Accessor for VBK tree. More...
|
|
const VbkBlockTree::BtcTree & | btc () const |
| Accessor for BTC tree. More...
|
|
const AltChainParams & | getParams () const |
| Accessor for Network Parameters stored in this tree. More...
|
|
const std::unordered_set< index_t * > & | getTips () const |
|
std::vector< index_t * > | getBlocks () const |
|
std::vector< index_t * > | getAllBlocks () const |
|
const BlockReader & | getBlockProvider () const |
|
| BaseBlockTree (const BlockReader &blockProvider) |
|
| BaseBlockTree (const BaseBlockTree &)=delete |
|
| BaseBlockTree (BaseBlockTree &&)=default |
|
BaseBlockTree & | operator= (const BaseBlockTree &)=delete |
|
BaseBlockTree & | operator= (BaseBlockTree &&)=default |
|
const Chain< index_t > & | getBestChain () const |
| Getter for currently Active Chain. More...
|
|
prev_block_hash_t | makePrevHash (const T &h) const |
|
BaseBlockTree< VbkBlock >::prev_block_hash_t | makePrevHash (const hash_t &h) const |
| HACK: getBlockIndex accepts either hash_t or prev_block_hash_t then, depending on what it received, it should do trim LE on full hash to receive short hash, which is stored inside a map. More...
|
|
index_t * | getBlockIndex (const T &hash) |
| Get BlockIndex by block hash. More...
|
|
const index_t * | getBlockIndex (const T &hash) const |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
|
|
index_t * | findBlockIndex (const T &hash) |
| Get BlockIndex by block hash. More...
|
|
const index_t * | findBlockIndex (const T &hash) const |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
|
|
virtual bool | loadTip (const hash_t &hash, ValidationState &state) |
|
virtual bool | loadBlockForward (const stored_index_t &index, bool fast_load, ValidationState &state) |
| Efficiently connects BlockIndex to this tree as a leaf, when it is loaded from disk. More...
|
|
void | removeSubtree (index_t &toRemove) |
| Removes block and all its successors. More...
|
|
void | removeSubtree (const hash_t &toRemove) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
|
|
void | removeLeaf (index_t &toRemove) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
|
|
void | invalidateSubtree (index_t &toBeInvalidated, enum BlockValidityStatus reason, bool shouldDetermineBestChain=true) |
| Mark given block as invalid. More...
|
|
void | invalidateSubtree (const hash_t &toBeInvalidated, enum BlockValidityStatus reason, bool shouldDetermineBestChain=true) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
|
|
void | revalidateSubtree (const hash_t &hash, enum BlockValidityStatus reason, bool shouldDetermineBestChain=true) |
|
void | revalidateSubtree (index_t &toBeValidated, enum BlockValidityStatus reason, bool shouldDetermineBestChain=true) |
|
bool | isBootstrapped () const |
| Check if the blockchain is bootstrapped. More...
|
|
virtual bool | setState (const hash_t &block, ValidationState &state) |
|
virtual bool | setState (index_t &index, ValidationState &) |
|
virtual void | overrideTip (index_t &to) |
|
index_t & | getRoot () const |
|
const FinalizedPayloadsIndex< index_t > & | getFinalizedPayloadsIndex () const |
|
const PayloadsIndex< index_t > & | getPayloadsIndex () const |
|
DeferForkResolutionGuard | deferForkResolutionGuard () |
|