brk_computer 0.3.0-beta.6

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
mod addr;
mod height;
mod resolution;
pub mod timestamp;
mod tx_heights;
mod tx_index;
mod txin_index;
mod txout_index;

use std::path::Path;

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

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

pub use addr::Vecs as AddrVecs;
pub use height::Vecs as HeightVecs;
pub use resolution::{DatedResolutionVecs, ResolutionVecs};
pub use timestamp::Timestamps;
pub use tx_heights::TxHeights;
pub use tx_index::Vecs as TxIndexVecs;
pub use txin_index::Vecs as TxInIndexVecs;
pub use txout_index::Vecs as TxOutIndexVecs;

pub type Minute10Vecs<M = Rw> = ResolutionVecs<Minute10, M>;
pub type Minute30Vecs<M = Rw> = ResolutionVecs<Minute30, M>;
pub type Hour1Vecs<M = Rw> = ResolutionVecs<Hour1, M>;
pub type Hour4Vecs<M = Rw> = ResolutionVecs<Hour4, M>;
pub type Hour12Vecs<M = Rw> = ResolutionVecs<Hour12, M>;
pub type Day1Vecs<M = Rw> = DatedResolutionVecs<Day1, M>;
pub type Day3Vecs<M = Rw> = DatedResolutionVecs<Day3, M>;
pub type EpochVecs<M = Rw> = ResolutionVecs<Epoch, M>;
pub type HalvingVecs<M = Rw> = ResolutionVecs<Halving, M>;
pub type Week1Vecs<M = Rw> = DatedResolutionVecs<Week1, M>;
pub type Month1Vecs<M = Rw> = DatedResolutionVecs<Month1, M>;
pub type Month3Vecs<M = Rw> = DatedResolutionVecs<Month3, M>;
pub type Month6Vecs<M = Rw> = DatedResolutionVecs<Month6, M>;
pub type Year1Vecs<M = Rw> = DatedResolutionVecs<Year1, M>;
pub type Year10Vecs<M = Rw> = DatedResolutionVecs<Year10, M>;

pub const DB_NAME: &str = "indexes";

#[derive(Traversable)]
pub struct Vecs<M: StorageMode = Rw> {
    db: Database,
    #[traversable(skip)]
    pub tx_heights: TxHeights,
    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 = ResolutionVecs::forced_import(&db, version)?;
        let halving = ResolutionVecs::forced_import(&db, version)?;
        let minute10 = ResolutionVecs::forced_import(&db, version)?;
        let minute30 = ResolutionVecs::forced_import(&db, version)?;
        let hour1 = ResolutionVecs::forced_import(&db, version)?;
        let hour4 = ResolutionVecs::forced_import(&db, version)?;
        let hour12 = ResolutionVecs::forced_import(&db, version)?;
        let day1 = Day1Vecs::forced_import(&db, version)?;
        let day3 = DatedResolutionVecs::forced_import(&db, version)?;
        let week1 = DatedResolutionVecs::forced_import(&db, version)?;
        let month1 = DatedResolutionVecs::forced_import(&db, version)?;
        let month3 = DatedResolutionVecs::forced_import(&db, version)?;
        let month6 = DatedResolutionVecs::forced_import(&db, version)?;
        let year1 = DatedResolutionVecs::forced_import(&db, version)?;
        let year10 = DatedResolutionVecs::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 timestamp = Timestamps::forced_import_from_locals(
            &db, version, &minute10, &minute30, &hour1, &hour4, &hour12, &day1, &day3, &week1,
            &month1, &month3, &month6, &year1, &year10,
        )?;

        let this = Self {
            tx_heights: TxHeights::init(indexer),
            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()?;

        self.tx_heights.update(indexer, starting_indexes.height);

        // 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(&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,
        )?;
        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, 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,
        exit: &Exit,
    ) -> Result<()> {
        self.height.epoch.compute_from_index(
            starting_indexes.height,
            &indexer.vecs.blocks.weight,
            exit,
        )?;
        self.epoch.first_height.inner.compute_first_per_index(
            starting_indexes.height,
            &self.height.epoch,
            exit,
        )?;

        self.height.halving.compute_from_index(
            starting_indexes.height,
            &indexer.vecs.blocks.weight,
            exit,
        )?;
        self.halving.first_height.inner.compute_first_per_index(
            starting_indexes.height,
            &self.height.halving,
            exit,
        )?;
        Ok(())
    }

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

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

        self.day1.first_height.inner.compute_first_per_index(
            starting_indexes.height,
            &self.height.day1,
            exit,
        )?;
        self.day1.date.compute_transform(
            starting_day1,
            &self.day1.first_height,
            |(di, ..)| (di, Date::from(di)),
            exit,
        )?;
        let ts = &self.timestamp.monotonic;

        macro_rules! dated_period {
            ($period:ident) => {{
                self.$period.first_height.inner.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.date.compute_transform(
                    start,
                    &self.$period.first_height,
                    |(idx, first_h, _)| (idx, Date::from(ts.collect_one(first_h).unwrap())),
                    exit,
                )?;
            }};
        }

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

        Ok(())
    }
}