Backend indexer for Riften Labs BCH DeFi contracts (Cauldron, Moria). Mirror of gitlab.com/riftenlabs/riftenlabs-indexer (AGPL-3.0). Author: Riften Labs.
Find a file
jakobsn e733bad6ad Fix regressions introduced by the Stage 2 per-leg pricing phases
Review of today's Phase 1-4 commits found six defects, all introduced by those
phases and verified against 283c0c3. Each fix carries a regression test that was
confirmed to fail against the old code.

Reference is now derived from reserves, not folded from an arbitrary start
    Policy was a stateful fold instantiated in three places with three different
    lifetimes: per API request, per 24h rebuild batch, and per seed backscan. A
    fold whose result depends on where you began reading is not a function of the
    chain, which produced three symptoms: rebuild_range reset the policy at every
    batch boundary (~1125 times per token over full history, each reset waving the
    first leg of that day through unjudged via "no reference yet"); candlesticks()
    built a virgin policy per request, so the same hour rendered differently at 1W
    and 1M; and the fast path handed the raw tail a policy that had never seen the
    legs behind the ohlcv_1h buckets preceding it.

    Policy::seeded() now primes reserves from a snapshot query, so every caller
    starts from the same chain state. The d_i = min(S_i, T_i * R) circularity is
    resolved by seeding the weighted median with the unweighted one and reweighting
    to a fixed point, rather than by carrying the previous leg's R forward.

f64::MIN/MAX no longer reach the database
    A bucket whose legs were all muted or unpriceable was inserted with its high/low
    accumulators still at their sentinels, putting +/-1.8e308 into ohlcv_1h and from
    there onto the chart. The SQL this replaced dropped such buckets via an inner
    join; restored that behaviour.

Credit off-by-one
    apply() inserted new pools with sequence: leg.sequence, then gated the credit
    update behind leg.sequence > state.sequence -- false on that very insert. Pools
    needed two accepted prints to earn any credit, weakening tier-2 qualification.

Seed lookback restored to unbounded
    Phase 4 capped the backscan at 24h; the pre-Stage-2 query had no horizon. Tokens
    trading less often than daily lost their carry-forward price entirely.

Hot path and edge cases
    Dropped the per-leg format! allocation in favour of a Copy verdict enum; reused
    a scratch buffer across reference recomputation; fixed the weighted median
    biasing low on integer division and collapsing to the smallest ratio when all
    weights are zero; replaced copy_from_slice with a checked conversion so a
    malformed blob errors instead of panicking. Legs moving tokens for zero sats are
    now unpriceable rather than printing 0.0.

Tests
    test_multipool_arb_priced_by_gross_volume_not_net and
    test_single_direction_multileg_price_matches_net_ratio had their assertions
    rewritten during Phase 4 to match whatever the code produced, leaving names that
    contradicted what they checked. Renamed and rewritten to assert the durable
    invariant: a printed price must be one a leg actually executed at.
    test_credit_seeding_on_accepted_print asserted is_some() on a struct that always
    exists and passed with the credit bug fully present; replaced.

Still open (unbuilt plan phases, not regressions): withdrawal-event synthesis so
withdrawn pools stop voting in the median, credit seeding at pool creation, tier-2
summing credit across a tx's swap legs, swap vs liquidity-event distinction, and
config threading of GuardParams.

OHLCV_VERSION 3 -> 4: version 3 buckets were written by the buggy code and
INSERT OR IGNORE never corrects rows in place.

Tests: 238 passing

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 17:29:16 +02:00
.vscode Convert hash columns from TEXT to BLOB storage 2026-01-22 08:30:17 +01:00
contrib Delphi v2 support 2026-05-04 15:03:40 +02:00
internal Expose authchain head + optional local IPFS gateway for BCMR downloads 2026-06-24 11:07:31 +00:00
linters Fix potential API hangs from write lock contention 2026-02-02 21:01:50 +01:00
src Fix regressions introduced by the Stage 2 per-leg pricing phases 2026-07-29 17:29:16 +02:00
.gitignore Add Chipnet support 2026-01-09 13:46:58 +01:00
.gitlab-ci.yml ci: Use lockfile as cache key 2026-02-02 11:52:09 +01:00
build.rs missing build file 2024-10-25 15:28:40 +02:00
Cargo.lock ido: replace num-bigint with malachite 2026-06-22 10:41:39 +00:00
Cargo.toml ido: replace num-bigint with malachite 2026-06-22 10:41:39 +00:00
LICENSE use AGPL 2024-02-05 16:32:27 +01:00
README.md README: contributors license 2024-03-14 10:54:44 +01:00

Riftenlabs Indexer

RiftenLabs indexer is for indexing DeFi contracts by Riften Labs. It allows for querying historical on-chain data on Bitcoin Cash.

Contributing

All contributors are required to sign a Contributor License Agreement (CLA) before their contributions can be merged into the project.

Contributions are generally welcome. If you intend to make larger changes please discuss them in an issue before MRing them to avoid duplicate work and architectural mismatches.

Installing Rust

Rust can be installed using your package manager of choice or rustup.rs. The former way is considered more secure since it typically doesn't involve trust in the CA system. But you should be aware that the version of Rust shipped by your distribution might be out of date.

Building

The library can be built and tested using cargo:

git clone https://gitlab.com/riftenlabs/riftenlabs-indexer.git
cd riftenlabs-indexer
cargo build

You can run tests with:

cargo test