brk_computer 0.2.5

A Bitcoin dataset computer built on top of brk_indexer
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
mod addr;
mod cached_mappings;
mod day1;
mod day3;
mod epoch;
mod halving;
mod height;
mod hour1;
mod hour12;
mod hour4;
mod minute10;
mod minute30;
mod month1;
mod month3;
mod month6;
pub mod timestamp;
mod tx_index;
mod txin_index;
mod txout_index;
mod week1;
mod year1;
mod year10;

use std::path::Path;

use brk_error::Result;
use brk_indexer::Indexer;
use brk_traversable::Traversable;
use brk_types::{
    Date, Day1, Day3, Height, Hour1, Hour4, Hour12, Indexes, Minute10, Minute30, Month1, Month3,
    Month6, Version, Week1, Year1, Year10,
};
use vecdb::{CachedVec, Database, Exit, ReadableVec, Rw, StorageMode};

use crate::internal::db_utils::{finalize_db, open_db};

pub use addr::Vecs as AddrVecs;
pub use cached_mappings::CachedMappings;
pub use day1::Vecs as Day1Vecs;
pub use day3::Vecs as Day3Vecs;
pub use epoch::Vecs as EpochVecs;
pub use halving::Vecs as HalvingVecs;
pub use height::Vecs as HeightVecs;
pub use hour1::Vecs as Hour1Vecs;
pub use hour4::Vecs as Hour4Vecs;
pub use hour12::Vecs as Hour12Vecs;
pub use minute10::Vecs as Minute10Vecs;
pub use minute30::Vecs as Minute30Vecs;
pub use month1::Vecs as Month1Vecs;
pub use month3::Vecs as Month3Vecs;
pub use month6::Vecs as Month6Vecs;
pub use timestamp::Timestamps;
pub use tx_index::Vecs as TxIndexVecs;
pub use txin_index::Vecs as TxInIndexVecs;
pub use txout_index::Vecs as TxOutIndexVecs;
pub use week1::Vecs as Week1Vecs;
pub use year1::Vecs as Year1Vecs;
pub use year10::Vecs as Year10Vecs;

pub const DB_NAME: &str = "indexes";

#[derive(Traversable)]
pub struct Vecs<M: StorageMode = Rw> {
    db: Database,
    #[traversable(skip)]
    pub cached_mappings: CachedMappings,
    pub addr: AddrVecs,
    pub height: HeightVecs<M>,
    pub epoch: EpochVecs<M>,
    pub halving: HalvingVecs<M>,
    pub minute10: Minute10Vecs<M>,
    pub minute30: Minute30Vecs<M>,
    pub hour1: Hour1Vecs<M>,
    pub hour4: Hour4Vecs<M>,
    pub hour12: Hour12Vecs<M>,
    pub day1: Day1Vecs<M>,
    pub day3: Day3Vecs<M>,
    pub week1: Week1Vecs<M>,
    pub month1: Month1Vecs<M>,
    pub month3: Month3Vecs<M>,
    pub month6: Month6Vecs<M>,
    pub year1: Year1Vecs<M>,
    pub year10: Year10Vecs<M>,
    pub tx_index: TxIndexVecs<M>,
    pub txin_index: TxInIndexVecs,
    pub txout_index: TxOutIndexVecs,
    pub timestamp: Timestamps<M>,
}

impl Vecs {
    pub(crate) fn forced_import(
        parent: &Path,
        parent_version: Version,
        indexer: &Indexer,
    ) -> Result<Self> {
        let db = open_db(parent, DB_NAME, 1_000_000)?;

        let version = parent_version;

        let addr = AddrVecs::forced_import(version, indexer);
        let height = HeightVecs::forced_import(&db, version)?;
        let epoch = EpochVecs::forced_import(&db, version)?;
        let halving = HalvingVecs::forced_import(&db, version)?;
        let minute10 = Minute10Vecs::forced_import(&db, version)?;
        let minute30 = Minute30Vecs::forced_import(&db, version)?;
        let hour1 = Hour1Vecs::forced_import(&db, version)?;
        let hour4 = Hour4Vecs::forced_import(&db, version)?;
        let hour12 = Hour12Vecs::forced_import(&db, version)?;
        let day1 = Day1Vecs::forced_import(&db, version)?;
        let day3 = Day3Vecs::forced_import(&db, version)?;
        let week1 = Week1Vecs::forced_import(&db, version)?;
        let month1 = Month1Vecs::forced_import(&db, version)?;
        let month3 = Month3Vecs::forced_import(&db, version)?;
        let month6 = Month6Vecs::forced_import(&db, version)?;
        let year1 = Year1Vecs::forced_import(&db, version)?;
        let year10 = Year10Vecs::forced_import(&db, version)?;
        let tx_index = TxIndexVecs::forced_import(&db, version, indexer)?;
        let txin_index = TxInIndexVecs::forced_import(version, indexer);
        let txout_index = TxOutIndexVecs::forced_import(version, indexer);

        let cached_mappings = CachedMappings {
            minute10_first_height: CachedVec::new(&minute10.first_height),
            minute30_first_height: CachedVec::new(&minute30.first_height),
            hour1_first_height: CachedVec::new(&hour1.first_height),
            hour4_first_height: CachedVec::new(&hour4.first_height),
            hour12_first_height: CachedVec::new(&hour12.first_height),
            day1_first_height: CachedVec::new(&day1.first_height),
            day3_first_height: CachedVec::new(&day3.first_height),
            week1_first_height: CachedVec::new(&week1.first_height),
            month1_first_height: CachedVec::new(&month1.first_height),
            month3_first_height: CachedVec::new(&month3.first_height),
            month6_first_height: CachedVec::new(&month6.first_height),
            year1_first_height: CachedVec::new(&year1.first_height),
            year10_first_height: CachedVec::new(&year10.first_height),
            halving_identity: CachedVec::new(&halving.identity),
            epoch_identity: CachedVec::new(&epoch.identity),
        };

        let timestamp = Timestamps::forced_import_from_locals(
            &db, version, &minute10, &minute30, &hour1, &hour4, &hour12, &day1, &day3, &week1,
            &month1, &month3, &month6, &year1, &year10,
        )?;

        let this = Self {
            cached_mappings,
            addr,
            height,
            epoch,
            halving,
            minute10,
            minute30,
            hour1,
            hour4,
            hour12,
            day1,
            day3,
            week1,
            month1,
            month3,
            month6,
            year1,
            year10,
            tx_index,
            txin_index,
            txout_index,
            timestamp,
            db,
        };

        finalize_db(&this.db, &this)?;
        Ok(this)
    }

    pub(crate) fn compute(
        &mut self,
        indexer: &Indexer,
        starting_indexes: Indexes,
        exit: &Exit,
    ) -> Result<Indexes> {
        self.db.sync_bg_tasks()?;

        // timestamp_monotonic must be computed first — other mappings read it
        self.timestamp
            .compute_monotonic(indexer, starting_indexes.height, exit)?;

        self.compute_tx_indexes(indexer, &starting_indexes, exit)?;
        self.compute_height_indexes(indexer, &starting_indexes, exit)?;

        let prev_height = starting_indexes.height.decremented().unwrap_or_default();

        self.compute_timestamp_mappings(&starting_indexes, exit)?;

        let starting_day1 =
            self.compute_calendar_mappings(indexer, &starting_indexes, prev_height, exit)?;

        self.compute_period_vecs(indexer, &starting_indexes, prev_height, starting_day1, exit)?;

        self.timestamp.compute_per_resolution(
            indexer,
            &self.height,
            &self.halving,
            &self.epoch,
            &starting_indexes,
            exit,
        )?;

        let exit = exit.clone();
        self.db.run_bg(move |db| {
            let _lock = exit.lock();
            db.compact_deferred_default()
        });
        Ok(starting_indexes)
    }

    fn compute_tx_indexes(
        &mut self,
        indexer: &Indexer,
        starting_indexes: &Indexes,
        exit: &Exit,
    ) -> Result<()> {
        let (r1, r2) = rayon::join(
            || {
                self.tx_index.input_count.compute_count_from_indexes(
                    starting_indexes.tx_index,
                    &indexer.vecs.transactions.first_txin_index,
                    &indexer.vecs.inputs.outpoint,
                    exit,
                )
            },
            || {
                self.tx_index.output_count.compute_count_from_indexes(
                    starting_indexes.tx_index,
                    &indexer.vecs.transactions.first_txout_index,
                    &indexer.vecs.outputs.value,
                    exit,
                )
            },
        );
        r1?;
        r2?;
        Ok(())
    }

    fn compute_height_indexes(
        &mut self,
        indexer: &Indexer,
        starting_indexes: &Indexes,
        exit: &Exit,
    ) -> Result<()> {
        self.height.tx_index_count.compute_count_from_indexes(
            starting_indexes.height,
            &indexer.vecs.transactions.first_tx_index,
            &indexer.vecs.transactions.txid,
            exit,
        )?;
        self.height.identity.compute_from_index(
            starting_indexes.height,
            &indexer.vecs.blocks.weight,
            exit,
        )?;
        Ok(())
    }

    fn compute_timestamp_mappings(
        &mut self,
        starting_indexes: &Indexes,
        exit: &Exit,
    ) -> Result<()> {
        macro_rules! from_timestamp {
            ($field:ident, $period:ty) => {
                self.height.$field.compute_transform(
                    starting_indexes.height,
                    &self.timestamp.monotonic,
                    |(h, ts, _)| (h, <$period>::from_timestamp(ts)),
                    exit,
                )?;
            };
        }

        from_timestamp!(minute10, Minute10);
        from_timestamp!(minute30, Minute30);
        from_timestamp!(hour1, Hour1);
        from_timestamp!(hour4, Hour4);
        from_timestamp!(hour12, Hour12);
        from_timestamp!(day3, Day3);

        Ok(())
    }

    fn compute_calendar_mappings(
        &mut self,
        indexer: &Indexer,
        starting_indexes: &Indexes,
        prev_height: Height,
        exit: &Exit,
    ) -> Result<Day1> {
        let starting_day1 = self
            .height
            .day1
            .collect_one(prev_height)
            .unwrap_or_default();

        self.height.day1.compute_transform(
            starting_indexes.height,
            &self.timestamp.monotonic,
            |(h, ts, ..)| (h, Day1::try_from(Date::from(ts)).unwrap()),
            exit,
        )?;

        let starting_day1 = if let Some(day1) = self.height.day1.collect_one(prev_height) {
            starting_day1.min(day1)
        } else {
            starting_day1
        };

        self.compute_epoch(indexer, starting_indexes, prev_height, exit)?;

        self.height.week1.compute_transform(
            starting_indexes.height,
            &self.height.day1,
            |(h, di, _)| (h, Week1::from(di)),
            exit,
        )?;
        self.height.month1.compute_transform(
            starting_indexes.height,
            &self.height.day1,
            |(h, di, _)| (h, Month1::from(di)),
            exit,
        )?;
        self.height.month3.compute_transform(
            starting_indexes.height,
            &self.height.month1,
            |(h, mi, _)| (h, Month3::from(mi)),
            exit,
        )?;
        self.height.month6.compute_transform(
            starting_indexes.height,
            &self.height.month1,
            |(h, mi, _)| (h, Month6::from(mi)),
            exit,
        )?;
        self.height.year1.compute_transform(
            starting_indexes.height,
            &self.height.month1,
            |(h, mi, _)| (h, Year1::from(mi)),
            exit,
        )?;
        self.height.year10.compute_transform(
            starting_indexes.height,
            &self.height.year1,
            |(h, yi, _)| (h, Year10::from(yi)),
            exit,
        )?;

        Ok(starting_day1)
    }

    fn compute_epoch(
        &mut self,
        indexer: &Indexer,
        starting_indexes: &Indexes,
        prev_height: Height,
        exit: &Exit,
    ) -> Result<()> {
        let starting_difficulty = self
            .height
            .epoch
            .collect_one(prev_height)
            .unwrap_or_default();

        self.height.epoch.compute_from_index(
            starting_indexes.height,
            &indexer.vecs.blocks.weight,
            exit,
        )?;
        self.epoch.first_height.compute_first_per_index(
            starting_indexes.height,
            &self.height.epoch,
            exit,
        )?;
        self.epoch.identity.compute_from_index(
            starting_difficulty,
            &self.epoch.first_height,
            exit,
        )?;
        self.epoch.height_count.compute_count_from_indexes(
            starting_difficulty,
            &self.epoch.first_height,
            &self.timestamp.monotonic,
            exit,
        )?;

        let starting_halving = self
            .height
            .halving
            .collect_one(prev_height)
            .unwrap_or_default();

        self.height.halving.compute_from_index(
            starting_indexes.height,
            &indexer.vecs.blocks.weight,
            exit,
        )?;
        self.halving.first_height.compute_first_per_index(
            starting_indexes.height,
            &self.height.halving,
            exit,
        )?;
        self.halving.identity.compute_from_index(
            starting_halving,
            &self.halving.first_height,
            exit,
        )?;

        Ok(())
    }

    fn compute_period_vecs(
        &mut self,
        indexer: &Indexer,
        starting_indexes: &Indexes,
        prev_height: Height,
        starting_day1: Day1,
        exit: &Exit,
    ) -> Result<()> {
        macro_rules! basic_period {
            ($period:ident) => {
                self.$period.first_height.compute_first_per_index(
                    starting_indexes.height,
                    &self.height.$period,
                    exit,
                )?;
                self.$period.identity.compute_from_index(
                    self.height
                        .$period
                        .collect_one(prev_height)
                        .unwrap_or_default(),
                    &self.$period.first_height,
                    exit,
                )?;
            };
        }

        basic_period!(minute10);
        basic_period!(minute30);
        basic_period!(hour1);
        basic_period!(hour4);
        basic_period!(hour12);
        basic_period!(day3);

        self.day1.first_height.compute_first_per_index(
            starting_indexes.height,
            &self.height.day1,
            exit,
        )?;
        self.day1
            .identity
            .compute_from_index(starting_day1, &self.day1.first_height, exit)?;
        self.day1.date.compute_transform(
            starting_day1,
            &self.day1.identity,
            |(di, ..)| (di, Date::from(di)),
            exit,
        )?;
        self.day1.height_count.compute_count_from_indexes(
            starting_day1,
            &self.day1.first_height,
            &indexer.vecs.blocks.weight,
            exit,
        )?;

        let ts = &self.timestamp.monotonic;

        macro_rules! dated_period {
            ($period:ident) => {{
                self.$period.first_height.compute_first_per_index(
                    starting_indexes.height,
                    &self.height.$period,
                    exit,
                )?;
                let start = self
                    .height
                    .$period
                    .collect_one(prev_height)
                    .unwrap_or_default();
                self.$period.identity.compute_from_index(
                    start,
                    &self.$period.first_height,
                    exit,
                )?;
                self.$period.date.compute_transform(
                    start,
                    &self.$period.first_height,
                    |(idx, first_h, _)| (idx, Date::from(ts.collect_one(first_h).unwrap())),
                    exit,
                )?;
            }};
        }

        dated_period!(week1);
        dated_period!(month1);
        dated_period!(month3);
        dated_period!(month6);
        dated_period!(year1);
        dated_period!(year10);

        Ok(())
    }
}