bitcoin-index 0.1.16-alpha.0

abstractions for working with indices of blockchain data
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
crate::ix!();

/**
  | Base class for indices of blockchain
  | data. This implements
  | 
  | CValidationInterface and ensures
  | blocks are indexed sequentially according
  | to their position in the active chain.
  |
  */
pub struct BaseIndex {

    /**
      | Whether the index is in sync with the main
      | chain. The flag is flipped from false to
      | true once, after which point this starts
      | processing ValidationInterface notifications
      | to stay in sync.
      */
    synced:           AtomicBool, // default = { false }

    /**
      | The last block in the chain that the index
      | is in sync with.
      */
    best_block_index: Atomic<*mut BlockIndex>, // default = { nullptr }

    thread_sync:      Thread,
    interrupt:        ThreadInterrupt,
    chainstate:       *mut dyn ChainStateInterface, // default = { nullptr }
}

impl ValidationInterface for BaseIndex { 

}

impl UpdatedBlockTip               for BaseIndex { }
impl TransactionAddedToMempool     for BaseIndex { }
impl TransactionRemovedFromMempool for BaseIndex { }
impl BlockConnected                for BaseIndex { }
impl BlockDisconnected             for BaseIndex { }
impl ChainStateFlushed             for BaseIndex { }
impl BlockChecked                  for BaseIndex { }
impl NewPoWValidBlock              for BaseIndex { }

pub trait BaseIndexInterface: 

    /*
       | Get the name of the index for display
       | in logs.
       |
       */
    GetName

    + Init
    + WriteBlock
    + CommitInternal
    + Rewind
    + GetDB {}

//-------------------------------------------[.cpp/bitcoin/src/index/base.cpp]

pub const DB_BEST_BLOCK:              char = 'B';
pub const SYNC_LOG_INTERVAL:           i64 = 30; // seconds
pub const SYNC_LOCATOR_WRITE_INTERVAL: i64 = 30; // seconds

pub fn fatal_error<Args>(
        fmt:  *const u8,
        args: &Args)  {

    todo!();
        /*
            std::string strMessage = tfm::format(fmt, args...);
        SetMiscWarning(Untranslated(strMessage));
        LogPrintf("*** %s\n", strMessage);
        AbortError("A fatal internal error occurred, see debug.log for details");
        StartShutdown();
        */
}

impl Drop for BaseIndex {

    /**
      | Destructor interrupts sync thread
      | if running and blocks until it exits.
      |
      */
    fn drop(&mut self) {
        todo!();
        /*
            Interrupt();
        Stop();
        */
    }
}

impl BaseIndex {

    pub fn current_index(&mut self) -> *const BlockIndex {
        
        todo!();
        /*
            return m_best_block_index.load(); }{
        */
    }
    
    pub fn init(&mut self) -> bool {
        
        todo!();
        /*
            CBlockLocator locator;
        if (!GetDB().ReadBestBlock(locator)) {
            locator.SetNull();
        }

        LOCK(cs_main);
        CChain& active_chain = m_chainstate->m_chain;
        if (locator.IsNull()) {
            m_best_block_index = nullptr;
        } else {
            m_best_block_index = m_chainstate->m_blockman.FindForkInGlobalIndex(active_chain, locator);
        }
        m_synced = m_best_block_index.load() == active_chain.Tip();
        if (!m_synced) {
            bool prune_violation = false;
            if (!m_best_block_index) {
                // index is not built yet
                // make sure we have all block data back to the genesis
                const CBlockIndex* block = active_chain.Tip();
                while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
                    block = block->pprev;
                }
                prune_violation = block != active_chain.Genesis();
            }
            // in case the index has a best block set and is not fully synced
            // check if we have the required blocks to continue building the index
            else {
                const CBlockIndex* block_to_test = m_best_block_index.load();
                if (!active_chain.Contains(block_to_test)) {
                    // if the bestblock is not part of the mainchain, find the fork
                    // and make sure we have all data down to the fork
                    block_to_test = active_chain.FindFork(block_to_test);
                }
                const CBlockIndex* block = active_chain.Tip();
                prune_violation = true;
                // check backwards from the tip if we have all block data until we reach the indexes bestblock
                while (block_to_test && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
                    if (block_to_test == block) {
                        prune_violation = false;
                        break;
                    }
                    block = block->pprev;
                }
            }
            if (prune_violation) {
                return InitError(strprintf(Untranslated("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)"), GetName()));
            }
        }
        return true;
        */
    }
    
    /**
      | Sync the index with the block index
      | starting from the current best block.
      |
      | Intended to be run in its own thread,
      | m_thread_sync, and can be interrupted with
      | m_interrupt. Once the index gets in sync,
      | the m_synced flag is set and the
      | BlockConnected ValidationInterface
      | callback takes over and the sync thread
      | exits.
      */
    pub fn thread_sync(&mut self)  {
        
        todo!();
        /*
            SetSyscallSandboxPolicy(SyscallSandboxPolicy::TX_INDEX);
        const CBlockIndex* pindex = m_best_block_index.load();
        if (!m_synced) {
            auto& consensus_params = Params().GetConsensus();

            int64_t last_log_time = 0;
            int64_t last_locator_write_time = 0;
            while (true) {
                if (m_interrupt) {
                    m_best_block_index = pindex;
                    // No need to handle errors in Commit. If it fails, the error will be already be
                    // logged. The best way to recover is to continue, as index cannot be corrupted by
                    // a missed commit to disk for an advanced index state.
                    Commit();
                    return;
                }

                {
                    LOCK(cs_main);
                    const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
                    if (!pindex_next) {
                        m_best_block_index = pindex;
                        m_synced = true;
                        // No need to handle errors in Commit. See rationale above.
                        Commit();
                        break;
                    }
                    if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
                        FatalError("%s: Failed to rewind index %s to a previous chain tip",
                                   __func__, GetName());
                        return;
                    }
                    pindex = pindex_next;
                }

                int64_t current_time = GetTime();
                if (last_log_time + SYNC_LOG_INTERVAL < current_time) {
                    LogPrintf("Syncing %s with block chain from height %d\n",
                              GetName(), pindex->nHeight);
                    last_log_time = current_time;
                }

                if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
                    m_best_block_index = pindex;
                    last_locator_write_time = current_time;
                    // No need to handle errors in Commit. See rationale above.
                    Commit();
                }

                CBlock block;
                if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
                    FatalError("%s: Failed to read block %s from disk",
                               __func__, pindex->GetBlockHash().ToString());
                    return;
                }
                if (!WriteBlock(block, pindex)) {
                    FatalError("%s: Failed to write block %s to index database",
                               __func__, pindex->GetBlockHash().ToString());
                    return;
                }
            }
        }

        if (pindex) {
            LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight);
        } else {
            LogPrintf("%s is enabled\n", GetName());
        }
        */
    }
    
    /**
      | Write the current index state (eg. chain
      | block locator and subclass-specific items)
      | to disk.
      |
      | Recommendations for error handling:
      |
      | If called on a successor of the previous
      | committed best block in the index, the
      | index can continue processing without risk
      | of corruption, though the index state will
      | need to catch up from further behind on
      | reboot. If the new state is not
      | a successor of the previous state (due to
      | a chain reorganization), the index must
      | halt until Commit succeeds or else it
      | could end up getting corrupted.
      */
    pub fn commit(&mut self) -> bool {
        
        todo!();
        /*
            CDBBatch batch(GetDB());
        if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) {
            return error("%s: Failed to commit latest %s state", __func__, GetName());
        }
        return true;
        */
    }
    
    pub fn commit_internal(&mut self, batch: &mut DBBatch) -> bool {
        
        todo!();
        /*
            LOCK(cs_main);
        GetDB().WriteBestBlock(batch, m_chainstate->m_chain.GetLocator(m_best_block_index));
        return true;
        */
    }
    
    pub fn rewind(&mut self, 
        current_tip: *const BlockIndex,
        new_tip:     *const BlockIndex) -> bool {
        
        todo!();
        /*
            assert(current_tip == m_best_block_index);
        assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);

        // In the case of a reorg, ensure persisted block locator is not stale.
        // Pruning has a minimum of 288 blocks-to-keep and getting the index
        // out of sync may be possible but a users fault.
        // In case we reorg beyond the pruned depth, ReadBlockFromDisk would
        // throw and lead to a graceful shutdown
        m_best_block_index = new_tip;
        if (!Commit()) {
            // If commit fails, revert the best block index to avoid corruption.
            m_best_block_index = current_tip;
            return false;
        }

        return true;
        */
    }
    
    pub fn block_connected(&mut self, 
        block:  &Arc<Block>,
        pindex: *const BlockIndex)  {
        
        todo!();
        /*
            if (!m_synced) {
            return;
        }

        const CBlockIndex* best_block_index = m_best_block_index.load();
        if (!best_block_index) {
            if (pindex->nHeight != 0) {
                FatalError("%s: First block connected is not the genesis block (height=%d)",
                           __func__, pindex->nHeight);
                return;
            }
        } else {
            // Ensure block connects to an ancestor of the current best block. This should be the case
            // most of the time, but may not be immediately after the sync thread catches up and sets
            // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are
            // in the ValidationInterface queue backlog even after the sync thread has caught up to the
            // new chain tip. In this unlikely event, log a warning and let the queue clear.
            if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) {
                LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " /* Continued */
                          "known best chain (tip=%s); not updating index\n",
                          __func__, pindex->GetBlockHash().ToString(),
                          best_block_index->GetBlockHash().ToString());
                return;
            }
            if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) {
                FatalError("%s: Failed to rewind index %s to a previous chain tip",
                           __func__, GetName());
                return;
            }
        }

        if (WriteBlock(*block, pindex)) {
            m_best_block_index = pindex;
        } else {
            FatalError("%s: Failed to write block %s to index",
                       __func__, pindex->GetBlockHash().ToString());
            return;
        }
        */
    }
    
    pub fn chain_state_flushed(&mut self, locator: &BlockLocator)  {
        
        todo!();
        /*
            if (!m_synced) {
            return;
        }

        const uint256& locator_tip_hash = locator.vHave.front();
        const CBlockIndex* locator_tip_index;
        {
            LOCK(cs_main);
            locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash);
        }

        if (!locator_tip_index) {
            FatalError("%s: First block (hash=%s) in locator was not found",
                       __func__, locator_tip_hash.ToString());
            return;
        }

        // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail
        // immediately after the sync thread catches up and sets m_synced. Consider the case where
        // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue
        // backlog even after the sync thread has caught up to the new chain tip. In this unlikely
        // event, log a warning and let the queue clear.
        const CBlockIndex* best_block_index = m_best_block_index.load();
        if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) {
            LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " /* Continued */
                      "chain (tip=%s); not writing index locator\n",
                      __func__, locator_tip_hash.ToString(),
                      best_block_index->GetBlockHash().ToString());
            return;
        }

        // No need to handle errors in Commit. If it fails, the error will be already be logged. The
        // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk
        // for an advanced index state.
        Commit();
        */
    }
    
    /**
      | Blocks the current thread until the index
      | is caught up to the current state of the
      | block chain. This only blocks if the index
      | has gotten in sync once and only needs to
      | process blocks in the ValidationInterface
      | queue. If the index is catching up from
      | far behind, this method does not block and
      | immediately returns false.
      */
    #[LOCKS_EXCLUDED(::cs_main)]
    pub fn block_until_synced_to_current_chain(&self) -> bool {
        
        todo!();

        /*
        AssertLockNotHeld(cs_main);

        if !synced {
            return false;
        }

        {
            //  Skip the queue-draining stuff if we know we're caught up with
            //  m_chain.Tip().
            LOCK(cs_main);

            let chain_tip: *mut BlockIndex = (*chainstate).chain.tip();

            let best_block_index: *mut BlockIndex = best_block_index.load();

            if (*best_block_index).get_ancestor((*chain_tip).n_height) == chain_tip {
                return true;
            }
        }

        log_printf("%s: %s is catching up on block notifications\n", func, get_name());

        sync_with_validation_interface_queue();

        true
        */
    }
    
    pub fn interrupt(&mut self)  {
        
        todo!();
        /*
            m_interrupt();
        */
    }
    
    /**
      | Start initializes the sync state and
      | registers the instance as
      | a ValidationInterface so that it stays in
      | sync with blockchain updates.
      */
    pub fn start(&mut self, active_chainstate: &mut dyn ChainStateInterface) -> bool {
        
        todo!();
        /*
            m_chainstate = &active_chainstate;
        // Need to register this ValidationInterface before running Init(), so that
        // callbacks are not missed if Init sets m_synced to true.
        RegisterValidationInterface(this);
        if (!Init()) {
            return false;
        }

        m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); });
        return true;
        */
    }
    
    /**
      | Stops the instance from staying in sync
      | with blockchain updates.
      |
      */
    pub fn stop(&mut self)  {
        
        todo!();
        /*
            UnregisterValidationInterface(this);

        if (m_thread_sync.joinable()) {
            m_thread_sync.join();
        }
        */
    }
    
    /**
      | Get a summary of the index and its state.
      |
      */
    pub fn get_summary(&self) -> IndexSummary {
        
        todo!();

        /*
        let summary: IndexSummary = IndexSummary::new();

        summary.name = get_name();

        summary.synced = synced;

        summary.best_block_height = match best_block_index {
            true   => (*best_block_index.load()).n_height,
            false  => 0
        };

        summary
        */
    }
}