mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-21 07:58:11 -07:00
global: opreturn part 3
This commit is contained in:
@@ -1,29 +1,4 @@
|
||||
import { brk } from "../../../utils/client.js";
|
||||
import {
|
||||
FILTERS,
|
||||
TYPE_BITS,
|
||||
TYPE_FILTER_MASK,
|
||||
getFilterBit,
|
||||
} from "./filters/model.js";
|
||||
|
||||
const VERSION_BITS = [
|
||||
0,
|
||||
getFilterBit("version:1"),
|
||||
getFilterBit("version:2"),
|
||||
getFilterBit("version:3"),
|
||||
];
|
||||
|
||||
const RBF_YES_BIT = getFilterBit("rbf:yes");
|
||||
const RBF_NO_BIT = getFilterBit("rbf:no");
|
||||
const INPUT_ONE_BIT = getFilterBit("input:one");
|
||||
const INPUT_MULTI_BIT = getFilterBit("input:multi");
|
||||
const OUTPUT_ONE_BIT = getFilterBit("output:one");
|
||||
const OUTPUT_MULTI_BIT = getFilterBit("output:multi");
|
||||
const FILTER_INDEX_BY_BIT = FILTERS.reduce((indexes, { bit, index }) => {
|
||||
indexes[bit] = index;
|
||||
|
||||
return indexes;
|
||||
}, /** @type {Record<number, number>} */ ({}));
|
||||
|
||||
/**
|
||||
* @template T
|
||||
@@ -76,13 +51,14 @@ export async function loadBlockPreview(block, signal) {
|
||||
const { start, end } = await loadBlockPreviewRange(block, signal);
|
||||
const tx = brk.series.transactions;
|
||||
const [weights, feeRates] = await Promise.all([
|
||||
fetchSeriesSlice(tx.size.weight.txIndex.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(tx.raw.weight.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(tx.fees.feeRate.by.tx_index, start, end, signal),
|
||||
]);
|
||||
|
||||
signal.throwIfAborted();
|
||||
|
||||
return {
|
||||
blockWeight: block.weight,
|
||||
range: { start, end },
|
||||
feeRates: /** @type {number[]} */ (feeRates.data),
|
||||
weights: /** @type {number[]} */ (weights.data),
|
||||
@@ -105,130 +81,6 @@ export async function loadBlockPreviewTxid(txIndex, signal) {
|
||||
return /** @type {string} */ (txid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number[]} starts
|
||||
* @param {number[]} counts
|
||||
*/
|
||||
function rangeEnd(starts, counts) {
|
||||
const last = starts.length - 1;
|
||||
|
||||
return starts[last] + counts[last];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint32Array} counts
|
||||
* @param {number} mask
|
||||
*/
|
||||
function countMask(counts, mask) {
|
||||
while (mask) {
|
||||
const bit = mask & -mask;
|
||||
|
||||
counts[FILTER_INDEX_BY_BIT[bit]] += 1;
|
||||
mask ^= bit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {readonly string[]} types
|
||||
* @param {number} start
|
||||
* @param {number} end
|
||||
*/
|
||||
function getTypesMask(types, start, end) {
|
||||
let mask = 0;
|
||||
|
||||
for (let index = start; index < end; index += 1) {
|
||||
mask |= TYPE_BITS[/** @type {keyof typeof TYPE_BITS} */ (types[index])] ?? 0;
|
||||
if (mask === TYPE_FILTER_MASK) return mask;
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {BlockPreviewRange} range
|
||||
* @param {AbortSignal} signal
|
||||
*/
|
||||
export async function loadBlockPreviewFilters(range, signal) {
|
||||
const { start, end } = range;
|
||||
const tx = brk.series.transactions;
|
||||
const indexes = brk.series.indexes.txIndex;
|
||||
const [
|
||||
versions,
|
||||
rbfs,
|
||||
inputCounts,
|
||||
outputCounts,
|
||||
firstInputs,
|
||||
firstOutputs,
|
||||
] = await Promise.all([
|
||||
fetchSeriesSlice(tx.raw.txVersion.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(tx.raw.isExplicitlyRbf.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(indexes.inputCount.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(indexes.outputCount.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(tx.raw.firstTxinIndex.by.tx_index, start, end, signal),
|
||||
fetchSeriesSlice(tx.raw.firstTxoutIndex.by.tx_index, start, end, signal),
|
||||
]);
|
||||
|
||||
signal.throwIfAborted();
|
||||
|
||||
const inputStart = /** @type {number} */ (firstInputs.data[0]);
|
||||
const outputStart = /** @type {number} */ (firstOutputs.data[0]);
|
||||
const [inputTypes, outputTypes] = await Promise.all([
|
||||
fetchSeriesSlice(
|
||||
brk.series.inputs.raw.outputType.by.txin_index,
|
||||
inputStart,
|
||||
rangeEnd(
|
||||
/** @type {number[]} */ (firstInputs.data),
|
||||
/** @type {number[]} */ (inputCounts.data),
|
||||
),
|
||||
signal,
|
||||
),
|
||||
fetchSeriesSlice(
|
||||
brk.series.outputs.raw.outputType.by.txout_index,
|
||||
outputStart,
|
||||
rangeEnd(
|
||||
/** @type {number[]} */ (firstOutputs.data),
|
||||
/** @type {number[]} */ (outputCounts.data),
|
||||
),
|
||||
signal,
|
||||
),
|
||||
]);
|
||||
|
||||
signal.throwIfAborted();
|
||||
|
||||
const masks = new Uint32Array(end - start);
|
||||
const counts = new Uint32Array(FILTERS.length);
|
||||
|
||||
for (let index = 0; index < versions.data.length; index += 1) {
|
||||
const version = /** @type {number} */ (versions.data[index]);
|
||||
const inputCount = /** @type {number} */ (inputCounts.data[index]);
|
||||
const outputCount = /** @type {number} */ (outputCounts.data[index]);
|
||||
const inputTypeStart = /** @type {number} */ (firstInputs.data[index]);
|
||||
const inputTypeEnd = inputTypeStart + inputCount;
|
||||
const outputTypeStart = /** @type {number} */ (firstOutputs.data[index]);
|
||||
const outputTypeEnd = outputTypeStart + outputCount;
|
||||
const mask =
|
||||
(VERSION_BITS[version] ?? 0) |
|
||||
(rbfs.data[index] ? RBF_YES_BIT : RBF_NO_BIT) |
|
||||
(inputCount === 1 ? INPUT_ONE_BIT : INPUT_MULTI_BIT) |
|
||||
(outputCount === 1 ? OUTPUT_ONE_BIT : OUTPUT_MULTI_BIT) |
|
||||
getTypesMask(
|
||||
/** @type {string[]} */ (inputTypes.data),
|
||||
inputTypeStart - inputStart,
|
||||
inputTypeEnd - inputStart,
|
||||
) |
|
||||
getTypesMask(
|
||||
/** @type {string[]} */ (outputTypes.data),
|
||||
outputTypeStart - outputStart,
|
||||
outputTypeEnd - outputStart,
|
||||
);
|
||||
|
||||
masks[index] = mask;
|
||||
countMask(counts, mask);
|
||||
}
|
||||
|
||||
return { counts, masks, start };
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {Object} BlockPreviewRange
|
||||
* @property {number} start
|
||||
@@ -237,6 +89,7 @@ export async function loadBlockPreviewFilters(range, signal) {
|
||||
|
||||
/**
|
||||
* @typedef {Object} BlockPreviewData
|
||||
* @property {number} blockWeight
|
||||
* @property {BlockPreviewRange} range
|
||||
* @property {number[]} weights
|
||||
* @property {number[]} feeRates
|
||||
@@ -248,10 +101,3 @@ export async function loadBlockPreviewFilters(range, signal) {
|
||||
* @property {number} weight
|
||||
* @property {number} feeRate
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} BlockPreviewFilterState
|
||||
* @property {number} start
|
||||
* @property {Uint32Array} masks
|
||||
* @property {Uint32Array} counts
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user