1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/chain.h]
//-------------------------------------------[.cpp/bitcoin/src/interfaces/chain.h]
/**
| An in-memory indexed chain of blocks.
|
*/
pub struct Chain {
chain: Vec<Option<Arc<BlockIndex>>>,
}
impl Index<i32> for Chain {
type Output = Option<Arc<BlockIndex>>;
/**
| Returns the index entry at a particular
| height in this chain, or nullptr if no
| such height exists.
|
*/
#[inline] fn index(&self, n_height: i32) -> &Self::Output {
todo!();
/*
if (nHeight < 0 || nHeight >= (int)vChain.size())
return nullptr;
return vChain[nHeight];
*/
}
}
//-------------------------------------------[.cpp/bitcoin/src/chain.cpp]
impl Tip for Chain {
fn tip(&self) -> Option<Arc<BlockIndex>> {
todo!();
/*
return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
*/
}
}
impl Contains<Arc<BlockIndex>> for Chain {
/**
| Efficiently check whether a block is
| present in this chain.
|
*/
fn contains(&self, pindex: Option<Arc<BlockIndex>>) -> bool {
todo!();
/*
return (*this)[pindex->nHeight] == pindex;
*/
}
}
impl GetLocator<Arc<BlockIndex>> for Chain {
type LocatorType = BlockLocator;
/**
| Return a CBlockLocator that refers
| to a block in this chain (by default the
| tip).
|
*/
fn get_locator(&self, pindex: Option<Arc<BlockIndex>>) -> Self::LocatorType {
todo!();
/*
int nStep = 1;
std::vector<uint256> vHave;
vHave.reserve(32);
if (!pindex)
pindex = Tip();
while (pindex) {
vHave.push_back(pindex->GetBlockHash());
// Stop when we have added the genesis block.
if (pindex->nHeight == 0)
break;
// Exponentially larger steps back, plus the genesis block.
int nHeight = std::max(pindex->nHeight - nStep, 0);
if (Contains(pindex)) {
// Use O(1) CChain index if possible.
pindex = (*this)[nHeight];
} else {
// Otherwise, use O(log n) skiplist.
pindex = pindex->GetAncestor(nHeight);
}
if (vHave.size() > 10)
nStep *= 2;
}
return CBlockLocator(vHave);
*/
}
}
impl ChainHeight for Chain {
/**
| Return the maximal height in the chain.
| Is equal to chain.Tip() ? chain.Tip()->nHeight
| : -1.
|
*/
fn height(&self) -> Option<usize> {
todo!();
/*
return vChain.size() - 1;
*/
}
}
impl ChainNext for Chain {
/**
| Find the successor of a block in this
| chain, or nullptr if the given index
| is not found or is the tip.
|
*/
fn next(&self, pindex: Option<Arc<BlockIndex>>) -> Option<Arc<BlockIndex>> {
todo!();
/*
if (Contains(pindex))
return (*this)[pindex->nHeight + 1];
else
return nullptr;
*/
}
}
impl Chain {
/**
| Returns the index entry for the genesis
| block of this chain, or nullptr if none.
|
*/
pub fn genesis(&self) -> Option<Arc<BlockIndex>> {
todo!();
/*
return vChain.size() > 0 ? vChain[0] : nullptr;
*/
}
/**
| Set/initialize a chain with a given
| tip.
|
*/
pub fn set_tip(&mut self, pindex: Option<Arc<BlockIndex>>) {
todo!();
/*
if (pindex == nullptr) {
vChain.clear();
return;
}
vChain.resize(pindex->nHeight + 1);
while (pindex && vChain[pindex->nHeight] != pindex) {
vChain[pindex->nHeight] = pindex;
pindex = pindex->pprev;
}
*/
}
/**
| Find the last common block between this
| chain and a block index entry.
|
*/
pub fn find_fork(&self, pindex: Option<Arc<BlockIndex>>) -> Option<Arc<BlockIndex>> {
todo!();
/*
if (pindex == nullptr) {
return nullptr;
}
if (pindex->nHeight > Height())
pindex = pindex->GetAncestor(Height());
while (pindex && !Contains(pindex))
pindex = pindex->pprev;
return pindex;
*/
}
/**
| Find the earliest block with timestamp
| equal or greater than the given time
| and height equal or greater than the
| given height.
|
*/
pub fn find_earliest_at_least(&self,
n_time: i64,
height: i32) -> Option<Arc<BlockIndex>> {
todo!();
/*
std::pair<int64_t, int> blockparams = std::make_pair(nTime, height);
std::vector<CBlockIndex*>::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), blockparams,
[](CBlockIndex* pBlock, const std::pair<int64_t, int>& blockparams) -> bool { return pBlock->GetBlockTimeMax() < blockparams.first || pBlock->nHeight < blockparams.second; });
return (lower == vChain.end() ? nullptr : *lower);
*/
}
}
#[EXCLUSIVE_LOCKS_REQUIRED(cs_main)]
pub fn next_sync_block(
pindex_prev: *const BlockIndex,
chain: &mut Chain) -> *const BlockIndex {
todo!();
/*
AssertLockHeld(cs_main);
if (!pindex_prev) {
return chain.Genesis();
}
const CBlockIndex* pindex = chain.Next(pindex_prev);
if (pindex) {
return pindex;
}
return chain.Next(chain.FindFork(pindex_prev));
*/
}
/**
| Return implementation of Chain interface.
|
*/
pub fn make_chain(context: &mut NodeContext) -> Box<Chain> {
todo!();
/*
return std::make_unique<ChainImpl>(context);
*/
}