degenbot-cli-core 0.6.0-alpha.19

Command semantics + the database command arms for the degenbot console (ADR-051 D1) - pyo3-free and clap-free.
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! The `aave` command arms (ADR-051 D1;).
//!
//! Ports `cli/aave.py`:
//!
//! - `aave activate` — [`activate_aave_market`] (the one-time market seed).
//! - `aave deactivate` — the `(chain, name)` market read + the
//!   [`deactivate_aave_market`] row flip.
//! - `aave update` — the active-market walk over
//!   [`run_aave_update`], with the post-run zero-balance cleanup and the
//!   opt-in completion backup.
//! - `aave position show` — the market/user scalar row reads ported onto the
//!   `degenbot-db::aave` read surface.
//! - `aave reset` — the market-scoped purge and the cold-boot substrate
//!   re-seed committed in ONE transaction through the market-reset module,
//!   so the NEXT update run takes the cold-boot path an empty database
//!   takes.
//! - `aave digest` — one market's completed-market record: per-table row
//!   counts + digests over the market tables, rendered as the manifest
//!   JSON a drive pins.

use std::sync::Arc;

use alloy::primitives::{address, Address};
use degenbot_aave::updater::verify::cleanup_zero_balance_positions_on_conn;
use degenbot_aave::{
    activate_aave_market, deactivate_aave_market, reset_market_to_cold_boot, run_aave_update,
    NoProgress, RunError, RunSpec, VerifyPolicy,
};
use degenbot_db::{
    aave_market_digest_for, ops, parse_completed_market_manifest, CompletedMarketDrive,
    CompletedMarketManifest, DbError, DegenbotDb,
};
use degenbot_rpc::provider::AlloyProvider;

use crate::block::{parse_to_block, resolve_to_block};
use crate::cancel::CancelHandle;
use crate::context::CliContext;
use crate::error::CliError;
use crate::prompt::{PromptPlan, Prompter};
use crate::report::{
    AavePositionLine, AaveReinit, AaveReport, AaveUpdateEntry, AaveUpdateOutcome, DeactivateOutcome,
};
use degenbot_db::AaveMarketPurgeCount;

/// The RPC retry budget the update arm's per-run transport build uses
/// (mirrors `crate::pool`'s budget).
const RPC_MAX_RETRIES: u32 = 5;

/// An Aave V3 deployment (the Python `aave/deployments.py` constants).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AaveDeployment {
    /// The chain id.
    pub chain_id: u64,
    /// The human chain label.
    pub chain_label: &'static str,
    /// The on-chain `getMarketId()` return — the name the auto-registration
    /// seam keys the bare inactive row on (and `aave activate` resolves via
    /// RPC; the two must match for the completion path to reuse the row).
    pub market_name: &'static str,
    /// The `PoolAddressProvider` contract.
    pub pool_address_provider: Address,
    /// The chain's GHO token.
    pub gho_token_address: Address,
}

/// The shipped Aave V3 deployments (Ethereum mainnet only, mirroring Python).
pub const AAVE_DEPLOYMENTS: &[AaveDeployment] = &[AaveDeployment {
    chain_id: 1,
    chain_label: "Ethereum",
    // The on-chain `getMarketId()` return — the static name the auto-
    // registration seam keys the bare inactive row on (the RPC-fetched name
    // `aave activate` resolves must match for the completion path to reuse
    // the row instead of creating a second one).
    market_name: "Aave Ethereum Market",
    pool_address_provider: address!("2f39d218133AFaB8F2B819B1066c7E434Ad94E9e"),
    gho_token_address: address!("40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f"),
}];

/// The `aave` command group.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AaveCommand {
    /// Activate (or re-activate) the chain's Aave V3 market.
    Activate {
        /// The chain id.
        chain_id: u64,
    },
    /// Deactivate a market by chain + name.
    Deactivate {
        /// The chain id.
        chain_id: u64,
        /// The `aave_v3_markets.name` to flip.
        market_name: String,
    },
    /// Emit one market's completed-market record: per-table row counts +
    /// digests over the market tables, rendered as the manifest JSON a
    /// drive pins.
    Digest {
        /// The chain id.
        chain_id: u64,
        /// The `aave_v3_markets.name` to digest; `None` resolves the
        /// chain's only registered market.
        market_name: Option<String>,
        /// A previous completed-market record whose `market.drive` block is
        /// carried into the output verbatim (the drive facts are the
        /// drive's own bookkeeping, not DB state).
        drive_manifest: Option<std::path::PathBuf>,
    },
    /// Update positions for every active market.
    Update {
        /// Max blocks per chunk before committing.
        chunk_size: u64,
        /// The widest block range a single `eth_getLogs` request may
        /// span (independent of `chunk_size`).
        max_block_range: u64,
        /// The raw `--to-block` identifier.
        to_block: String,
        /// The per-chunk touched-position verify.
        verify_chunk: bool,
        /// The market-wide interval + completion verify.
        verify_all: bool,
        /// The interval for the market-wide gate.
        verify_all_interval: u64,
        /// Stop after the first committed chunk.
        stop_after_one_chunk: bool,
        /// Preview only (skips the Rust call entirely).
        dry_run: bool,
        /// Back up the DB once per market at the end of the run.
        enable_backup: bool,
    },
    /// Purge one market's populated state, then take the cold-boot path a
    /// fresh empty database takes for it.
    Reset {
        /// The chain id.
        chain_id: u64,
        /// The `aave_v3_markets.name` to reset; `None` resolves the chain's
        /// only registered market.
        market_name: Option<String>,
        /// Preview the purge's per-relation counts and re-init without writing.
        dry_run: bool,
    },
    /// Display a user's collateral + debt positions.
    PositionShow {
        /// The user address.
        address: String,
        /// The market name.
        market: String,
        /// The chain id.
        chain_id: u64,
    },
}

impl AaveCommand {
    /// The arm's confirmation policy: no Python aave handler prompts.
    #[must_use]
    pub const fn prompt_plan(&self, _ctx: &CliContext<'_>) -> PromptPlan {
        PromptPlan::None
    }
}

/// Resolve the Aave V3 deployment for `chain_id`.
///
/// # Errors
///
/// [`CliError::UnknownDeployment`] when the chain has no shipped deployment.
pub fn resolve_aave_deployment(chain_id: u64) -> Result<&'static AaveDeployment, CliError> {
    AAVE_DEPLOYMENTS
        .iter()
        .find(|d| d.chain_id == chain_id)
        .ok_or_else(|| CliError::UnknownDeployment {
            chain_id,
            name: "ethereum_aave_v3".to_string(),
        })
}

/// Execute an `aave` command.
///
/// # Errors
///
/// [`CliError::UnknownDeployment`] / [`CliError::InvalidAddress`] /
/// [`CliError::InvalidArgument`] for bad inputs, [`CliError::Config`] for an
/// unresolved driver-domain value, [`CliError::NoActiveAaveMarkets`] when
/// `aave update` finds nothing, and [`CliError::AaveUpdate`] for a core
/// failure.
pub(crate) fn execute(
    command: &AaveCommand,
    ctx: &CliContext<'_>,
    _prompter: &dyn Prompter,
    cancel: &CancelHandle,
) -> Result<AaveReport, CliError> {
    match command {
        AaveCommand::Activate { chain_id } => activate(ctx, *chain_id),
        AaveCommand::Deactivate {
            chain_id,
            market_name,
        } => deactivate(ctx, *chain_id, market_name),
        AaveCommand::Digest {
            chain_id,
            market_name,
            drive_manifest,
        } => digest(
            ctx,
            *chain_id,
            market_name.as_deref(),
            drive_manifest.as_deref(),
        ),
        AaveCommand::Update {
            chunk_size,
            max_block_range,
            to_block,
            verify_chunk,
            verify_all,
            verify_all_interval,
            stop_after_one_chunk,
            dry_run,
            enable_backup,
        } => update(
            ctx,
            cancel,
            &UpdateArgs {
                chunk_size: *chunk_size,
                max_block_range: *max_block_range,
                to_block,
                verify_chunk: *verify_chunk,
                verify_all: *verify_all,
                verify_all_interval: *verify_all_interval,
                stop_after_one_chunk: *stop_after_one_chunk,
                dry_run: *dry_run,
                enable_backup: *enable_backup,
            },
        ),
        AaveCommand::Reset {
            chain_id,
            market_name,
            dry_run,
        } => reset(ctx, cancel, *chain_id, market_name.as_deref(), *dry_run),
        AaveCommand::PositionShow {
            address,
            market,
            chain_id,
        } => position_show(ctx, address, market, *chain_id),
    }
}

/// `aave activate`.
fn activate(ctx: &CliContext<'_>, chain_id: u64) -> Result<AaveReport, CliError> {
    let deployment = resolve_aave_deployment(chain_id)?;
    let database_path = ctx.database_path()?.value;
    let rpc_url = ctx.node_request_uri_for(chain_id)?.value;
    let chain = i64::try_from(chain_id)
        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
    // The thin live-provider wrapper (ADR-068 D5): one build per activation on
    // the shared runtime, and the core only injects it. Mirrors the update
    // arm's per-market build below.
    let result = match crate::pool::shared_runtime_block_on(async {
        AlloyProvider::new(&rpc_url, RPC_MAX_RETRIES).await
    }) {
        Ok(Ok(provider)) => activate_aave_market(
            &database_path,
            chain,
            &deployment.pool_address_provider.to_checksum(None),
            &deployment.gho_token_address.to_checksum(None),
            &provider,
        ),
        Ok(Err(err)) => Err(RunError::from(err)),
        Err(cli_err) => return Err(cli_err),
    }
    .map_err(CliError::AaveUpdate)?;
    tracing::info!(
        chain_id,
        market_id = result.market_id,
        "activated Aave V3 market"
    );
    Ok(AaveReport::Activated {
        chain_id,
        chain_label: deployment.chain_label,
        market_id: result.market_id,
        market_name: result.market_name,
        created: result.created,
    })
}

/// `aave deactivate`.
fn deactivate(
    ctx: &CliContext<'_>,
    chain_id: u64,
    market_name: &str,
) -> Result<AaveReport, CliError> {
    let database_path = ctx.database_path()?.value;
    let chain = i64::try_from(chain_id)
        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
    let market = {
        let db = (DegenbotDb::open(&database_path)?).0;
        db.fetch_aave_market_by_name(chain, market_name)?
    };
    let Some(market) = market else {
        return Ok(AaveReport::Deactivated {
            chain_id,
            market_id: None,
            outcome: DeactivateOutcome::NoEntry,
        });
    };
    if !market.active {
        return Ok(AaveReport::Deactivated {
            chain_id,
            market_id: Some(market.id),
            outcome: DeactivateOutcome::AlreadyDeactivated,
        });
    }
    deactivate_aave_market(&database_path, market.id).map_err(CliError::AaveUpdate)?;
    tracing::info!(
        chain_id,
        market_id = market.id,
        "deactivated Aave V3 market"
    );
    Ok(AaveReport::Deactivated {
        chain_id,
        market_id: Some(market.id),
        outcome: DeactivateOutcome::Deactivated,
    })
}

/// `aave digest`.
fn digest(
    ctx: &CliContext<'_>,
    chain_id: u64,
    market_name: Option<&str>,
    drive_manifest: Option<&std::path::Path>,
) -> Result<AaveReport, CliError> {
    let database_path = ctx.database_path()?.value;
    let chain = i64::try_from(chain_id)
        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
    let drive = drive_manifest.map(carried_drive).transpose()?;
    let digest = {
        // The read handle locks down at open (`query_only=on`): the digest
        // arm observes the DB, it never writes it.
        let db = (DegenbotDb::open(&database_path)?).0;
        let conn = db.lock();
        aave_market_digest_for(&conn, Some(chain), market_name)?
    };
    Ok(AaveReport::Digest {
        manifest: CompletedMarketManifest { digest, drive },
    })
}

/// Parse a previous completed-market record with the typed reader and carry
/// its `market.drive` block — the one content the record has that the DB
/// cannot yield.
///
/// The record must be one this writer emitted (the serialization stamp is
/// the version discriminant) and must carry a drive block; a record without
/// one has nothing to carry.
fn carried_drive(path: &std::path::Path) -> Result<CompletedMarketDrive, CliError> {
    let raw = std::fs::read_to_string(path).map_err(CliError::Io)?;
    let manifest = parse_completed_market_manifest(&raw)
        .map_err(|error| CliError::InvalidArgument(format!("{}: {error}", path.display())))?;
    manifest.drive.ok_or_else(|| {
        CliError::InvalidArgument(format!("{} carries no market.drive block", path.display()))
    })
}

/// The `aave update` flag bundle.
#[expect(clippy::struct_excessive_bools)]
struct UpdateArgs<'a> {
    chunk_size: u64,
    max_block_range: u64,
    to_block: &'a str,
    verify_chunk: bool,
    verify_all: bool,
    verify_all_interval: u64,
    stop_after_one_chunk: bool,
    dry_run: bool,
    enable_backup: bool,
}

/// The `aave update` flags' projection onto the run's [`VerifyPolicy`]: the
/// market-wide interval gate is live only when `--verify-all` is set.
impl From<&UpdateArgs<'_>> for VerifyPolicy {
    fn from(args: &UpdateArgs<'_>) -> Self {
        Self {
            verify_chunk: args.verify_chunk,
            verify_all_interval: args.verify_all.then_some(args.verify_all_interval),
            verify_all_at_completion: args.verify_all,
        }
    }
}

/// `aave update`.
#[expect(clippy::too_many_lines)]
fn update(
    ctx: &CliContext<'_>,
    cancel: &CancelHandle,
    args: &UpdateArgs<'_>,
) -> Result<AaveReport, CliError> {
    let database_path = ctx.database_path()?.value;
    // Self-serve registration: every supported Aave market not found in the
    // DB registers inactive (a bare row awaiting `aave activate`), so the
    // update never depends on prior CREATEs.
    crate::registrations::ensure_supported_registrations(&database_path)?;
    let markets = {
        let db = (DegenbotDb::open(&database_path)?).0;
        db.fetch_active_aave_markets()?
    };
    if markets.is_empty() {
        return Err(CliError::NoActiveAaveMarkets);
    }
    let spec = parse_to_block(args.to_block)?;
    let max_chunks = if args.stop_after_one_chunk {
        Some(1)
    } else {
        None
    };
    let mut entries: Vec<AaveUpdateEntry> = Vec::new();
    let mut chain_ids: Vec<i64> = Vec::new();
    for market in &markets {
        if !chain_ids.contains(&market.chain_id) {
            chain_ids.push(market.chain_id);
        }
    }
    for chain in chain_ids {
        if cancel.is_cancelled() {
            break;
        }
        let chain_unsigned = u64::try_from(chain).map_err(|_| {
            CliError::InvalidArgument(format!("market chain id {chain} is out of range"))
        })?;
        let rpc_url = ctx.node_request_uri_for(chain_unsigned)?.value;
        let resolved = resolve_to_block(spec, &rpc_url)?;
        for market in markets.iter().filter(|m| m.chain_id == chain) {
            if cancel.is_cancelled() {
                return Ok(AaveReport::Updated { entries });
            }
            let Some(last_update_block) = market.last_update_block else {
                entries.push(AaveUpdateEntry {
                    chain_id: chain,
                    market_id: market.id,
                    market_name: market.name.clone(),
                    outcome: AaveUpdateOutcome::NeedsBootstrap,
                });
                continue;
            };
            if args.dry_run {
                entries.push(AaveUpdateEntry {
                    chain_id: chain,
                    market_id: market.id,
                    market_name: market.name.clone(),
                    outcome: AaveUpdateOutcome::DryRun {
                        last_update_block,
                        to_block: resolved,
                    },
                });
                continue;
            }
            // The thin live-provider wrapper (ADR-068 D5): one build per
            // market run on the shared runtime — the same transport count the
            // core's old internal `AlloyProvider::new` site produced — and
            // the core only injects it. A build failure surfaces as the same
            // `CliError::AaveUpdate` the run's own failures use.
            let run = match crate::pool::shared_runtime_block_on(async {
                AlloyProvider::new(&rpc_url, RPC_MAX_RETRIES).await
            }) {
                Ok(Ok(provider)) => run_aave_update(
                    &database_path,
                    RunSpec {
                        chain_id: chain,
                        market_id: market.id,
                        to_block: resolved,
                        chunk_size: args.chunk_size,
                        max_block_range: args.max_block_range,
                        provider,
                        cancel: cancel.flag(),
                        progress: Arc::new(NoProgress),
                        verify: VerifyPolicy::from(args),
                        max_chunks,
                    },
                ),
                Ok(Err(err)) => Err(RunError::from(err)),
                Err(cli_err) => return Err(cli_err),
            };
            match run {
                Ok(report) => {
                    entries.push(AaveUpdateEntry {
                        chain_id: chain,
                        market_id: market.id,
                        market_name: market.name.clone(),
                        outcome: AaveUpdateOutcome::Advanced {
                            from_block: report.from_block,
                            to_block: report.to_block,
                            chunks_committed: report.chunks_committed,
                            total_events_applied: report.total_events_applied,
                        },
                    });
                    cleanup_zero_balance_positions(&database_path, market.id)?;
                    if args.enable_backup {
                        let backup = backup_for_block(&database_path, report.to_block)?;
                        tracing::info!(
                            chain_id = chain,
                            to_block = report.to_block,
                            backup = %backup.display(),
                            "created Aave database backup at block"
                        );
                    }
                }
                Err(RunError::Cancelled) => {
                    entries.push(AaveUpdateEntry {
                        chain_id: chain,
                        market_id: market.id,
                        market_name: market.name.clone(),
                        outcome: AaveUpdateOutcome::Cancelled,
                    });
                    return Ok(AaveReport::Updated { entries });
                }
                Err(err) => return Err(CliError::AaveUpdate(err)),
            }
        }
    }
    Ok(AaveReport::Updated { entries })
}

/// An exclusive, reset-scoped advisory lock over one database path.
///
/// The exclusion is an advisory `flock` (via [`std::fs::File::try_lock`]) on a
/// file beside the database, which also carries the holder's pid for
/// diagnostics. The kernel releases the lock when the holding process dies, so
/// a killed or crashed reset cannot wedge every later reset the way the
/// previous create-new pid file did: the surviving file carries no lock, and
/// the next reset takes it over. A live holder still blocks, and the refusal
/// still reports the path. It guards reset-against-reset on a shared database,
/// rather than racing a purge against an in-flight re-init. The updater arms
/// do not take this lock, so it does not by itself exclude a concurrent
/// `aave update`; the purge's own transaction is what keeps a racing writer
/// from observing a partially-purged market.
struct ResetLock {
    path: std::path::PathBuf,
    /// Holding the descriptor is what holds the `flock`: dropping it (after
    /// [`Drop for ResetLock`] unlinks the name) releases the lock. Never read;
    /// the lifetime IS the lock.
    _file: std::fs::File,
}

impl ResetLock {
    /// Take the lock, or report the holder's file path.
    fn acquire(database_path: &std::path::Path, market_id: i64) -> Result<Self, CliError> {
        use std::io::Write as _;

        let mut path = database_path.to_path_buf();
        let stem = database_path
            .file_name()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_default();
        path.set_file_name(format!("{stem}.aave-reset.lock"));
        // Open-or-create, never `create_new`: a file surviving its holder is
        // the expected crash case, and the flock — not the file's existence —
        // is the exclusion.
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)
            .map_err(CliError::Io)?;
        file.try_lock().map_err(|err| match err {
            std::fs::TryLockError::WouldBlock => CliError::AaveMarketMidUpdate {
                market_id,
                lock_path: path.display().to_string(),
            },
            std::fs::TryLockError::Error(err) => CliError::Io(err),
        })?;
        let mut file = file;
        file.set_len(0).map_err(CliError::Io)?;
        writeln!(file, "{}", std::process::id()).map_err(CliError::Io)?;
        Ok(Self { path, _file: file })
    }
}

impl Drop for ResetLock {
    fn drop(&mut self) {
        // Unlink while the flock is still held (the descriptor drops after
        // this body), so a waiter that opens the path cannot inherit a lock
        // on a file whose name is already gone.
        let _ = std::fs::remove_file(&self.path);
    }
}

/// `aave reset`.
///
/// Resolves the market the way `deactivate` does, reads and prints the purge's
/// per-relation counts as the plan, then — under the reset lock — purges the
/// market and re-seeds its cold-boot substrate in ONE transaction through
/// [`reset_market_to_cold_boot`]: the purge and the activate seam's completion
/// commit together, so nothing can land between them. The report renders the
/// facts that module read back from that transaction. No update run here: the
/// command returns in seconds, and the next `aave update` takes the cold-boot
/// path from the stamped cursor.
fn reset(
    ctx: &CliContext<'_>,
    _cancel: &CancelHandle,
    chain_id: u64,
    market_name: Option<&str>,
    dry_run: bool,
) -> Result<AaveReport, CliError> {
    let database_path = ctx.database_path()?.value;
    let chain = i64::try_from(chain_id)
        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
    // The re-init completes the purged row through the shipped deployment's
    // substrate (the address provider + GHO constants), so the chain must be
    // one the deployment table names — the same precondition `aave
    // activate` has.
    let deployment = resolve_aave_deployment(chain_id)?;
    let market = {
        let db = (DegenbotDb::open(&database_path)?).0;
        // No name given: the chain's shipped deployment names the market.
        let name = market_name.unwrap_or(deployment.market_name);
        db.fetch_aave_market_by_name(chain, name)?
    };
    let Some(market) = market else {
        let requested =
            market_name.map_or_else(|| deployment.market_name.to_string(), ToString::to_string);
        return Err(CliError::UnknownAaveMarket {
            chain_id,
            market_name: requested,
        });
    };

    // The plan is read first and reported, so the operator sees the blast radius
    // before the purge. The counts come off a read-only handle.
    let counts: Vec<AaveMarketPurgeCount> = {
        let db = (DegenbotDb::open(&database_path)?).0;
        db.count_aave_market_rows(market.id)?
    };
    for count in &counts {
        tracing::info!(
            chain_id,
            market_id = market.id,
            table = count.table,
            rows = count.rows,
            dry_run,
            "aave reset plan"
        );
    }
    if dry_run {
        return Ok(AaveReport::Reset {
            chain_id,
            market_id: market.id,
            market_name: market.name,
            counts,
            dry_run: true,
            reinit: None,
        });
    }

    // Taken BEFORE the module's transaction and held through it, so two
    // resets of this database cannot interleave a purge with a re-seed.
    let _lock = ResetLock::acquire(&database_path, market.id)?;

    // The module's activation RPC needs a live transport; the shipped
    // deployment constants supply the two addresses it reads (the on-chain
    // market name + the GHO token's metadata). The build is the same thin
    // provider wrapper the activate and update arms use.
    let rpc_url = ctx.node_request_uri_for(chain_id)?.value;
    let provider = match crate::pool::shared_runtime_block_on(async {
        AlloyProvider::new(&rpc_url, RPC_MAX_RETRIES).await
    }) {
        Ok(Ok(provider)) => provider,
        Ok(Err(err)) => return Err(CliError::AaveUpdate(RunError::from(err))),
        Err(cli_err) => return Err(cli_err),
    };

    // ONE open + ONE transaction: the module purges the market and re-seeds
    // its cold-boot substrate on this same transaction — the rewind block is
    // the shipped deployment's bootstrap block, where `aave activate` stamps a
    // fresh market — and returns the facts it read back from it. A single
    // commit is what removes the old kill-between-transactions window; a
    // failure drops the transaction and the database stays byte-identical. The
    // counts are read on this transaction ahead of the write, so they are the
    // rows the purge removes.
    let (removed, facts) = {
        let (db, _state) = DegenbotDb::open_for_writes(&database_path)?;
        let mut guard = db.lock();
        let tx = guard.transaction().map_err(DbError::from)?;
        let removed = DegenbotDb::count_aave_market_rows_on_conn(&tx, market.id)?;
        let facts = reset_market_to_cold_boot(
            &tx,
            market.id,
            &deployment.pool_address_provider.to_checksum(None),
            &deployment.gho_token_address.to_checksum(None),
            &provider,
        )
        .map_err(CliError::AaveUpdate)?;
        tx.commit().map_err(DbError::from)?;
        (removed, facts)
    };
    tracing::info!(
        chain_id,
        market_id = market.id,
        cursor = facts.last_update_block.unwrap_or_default(),
        "reset Aave V3 market and re-stamped the cold-boot substrate"
    );
    Ok(AaveReport::Reset {
        chain_id,
        market_id: market.id,
        market_name: market.name,
        counts: removed,
        dry_run: false,
        reinit: Some(AaveReinit {
            market_activated: facts.market_activated,
            contract_row_present: facts.contract_row_present,
            last_update_block: facts
                .last_update_block
                .map(|block| i64::try_from(block).unwrap_or(i64::MAX)),
        }),
    })
}

/// Delete the market's zero-balance collateral + debt rows under one transaction.
fn cleanup_zero_balance_positions(
    database_path: &std::path::Path,
    market_id: i64,
) -> Result<(), CliError> {
    let (db, _state) = DegenbotDb::open_for_writes(database_path)?;
    let mut guard = db.lock();
    let tx = guard.transaction().map_err(DbError::from)?;
    cleanup_zero_balance_positions_on_conn(&tx, market_id)?;
    tx.commit().map_err(DbError::from)?;
    Ok(())
}

/// The `<stem>-<to_block>.db.bak` sibling the Python completion backup writes.
fn backup_for_block(
    database_path: &std::path::Path,
    to_block: u64,
) -> Result<std::path::PathBuf, CliError> {
    let stem = database_path
        .file_stem()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_default();
    let mut backup = database_path.to_path_buf();
    backup.set_file_name(format!("{stem}-{to_block}.db.bak"));
    ops::backup_database(database_path, &backup)?;
    Ok(backup)
}

/// `aave position show`.
fn position_show(
    ctx: &CliContext<'_>,
    raw_address: &str,
    market: &str,
    chain_id: u64,
) -> Result<AaveReport, CliError> {
    let address: Address = raw_address
        .parse()
        .map_err(|_| CliError::InvalidAddress(raw_address.to_string()))?;
    let user_address = address.to_checksum(None);
    let chain = i64::try_from(chain_id)
        .map_err(|_| CliError::InvalidArgument(format!("chain id {chain_id} is out of range")))?;
    let database_path = ctx.database_path()?.value;
    let db = (DegenbotDb::open(&database_path)?).0;
    let Some(market_row) = db.fetch_aave_market_by_name(chain, market)? else {
        return Ok(AaveReport::PositionNoMarket {
            market: market.to_string(),
            chain_id,
        });
    };
    let Some(user) = db.fetch_aave_user_by_address(market_row.id, &user_address)? else {
        return Ok(AaveReport::PositionNoUser {
            user_address,
            market: market.to_string(),
            chain_id,
        });
    };
    let collateral = db
        .fetch_aave_collateral_positions(user.id)?
        .into_iter()
        .map(|p| AavePositionLine {
            symbol: p.underlying_symbol.unwrap_or_else(|| "Unknown".to_string()),
            balance: p.balance,
        })
        .collect();
    let debt = db
        .fetch_aave_debt_positions(user.id)?
        .into_iter()
        .map(|p| AavePositionLine {
            symbol: p.underlying_symbol.unwrap_or_else(|| "Unknown".to_string()),
            balance: p.balance,
        })
        .collect();
    Ok(AaveReport::Position {
        user_address,
        market: market.to_string(),
        chain_id,
        collateral,
        debt,
    })
}

#[cfg(test)]
mod tests {
    #![expect(clippy::unwrap_used)]

    use std::fs;
    use std::path::PathBuf;
    use std::time::{Duration, Instant};

    use super::ResetLock;
    use crate::error::CliError;

    /// Spawn the `#[ignore]` child test as a fake holder and wait until it
    /// reports the lock armed.
    fn spawn_holder(db: &PathBuf, dir: &std::path::Path) -> std::process::Child {
        let ready = dir.join("holder-ready");
        let child = std::process::Command::new(std::env::current_exe().unwrap())
            .args([
                "--exact",
                "aave::tests::reset_lock_holder_child",
                "--ignored",
                "--nocapture",
            ])
            .env("RESET_LOCK_TEST_DB", db)
            .env("RESET_LOCK_TEST_READY", &ready)
            .spawn()
            .unwrap();
        let deadline = Instant::now() + Duration::from_secs(30);
        while !ready.exists() {
            assert!(
                Instant::now() < deadline,
                "holder child never armed the reset lock"
            );
            std::thread::sleep(Duration::from_millis(20));
        }
        child
    }

    #[test]
    fn stale_lock_file_does_not_block_a_reset() {
        let dir = tempfile::tempdir().unwrap();
        let db = dir.path().join("degenbot.db");
        let lock_path = dir.path().join("degenbot.db.aave-reset.lock");
        // A lock file outliving its holder: dead-or-garbage pid, no flock.
        fs::write(&lock_path, "999999999 dead holder\n").unwrap();
        let lock = ResetLock::acquire(&db, 7).unwrap();
        // The takeover rewrites the pid file with the new holder's pid.
        assert_eq!(
            fs::read_to_string(&lock_path).unwrap().trim(),
            std::process::id().to_string()
        );
        drop(lock);
        assert!(!lock_path.exists());
    }

    #[test]
    fn a_live_holder_blocks_until_it_releases() {
        let dir = tempfile::tempdir().unwrap();
        let db = dir.path().join("degenbot.db");
        let holder = ResetLock::acquire(&db, 7).unwrap();
        // A second open file description conflicts with the held flock even
        // inside one process — the same refusal a concurrent reset sees.
        assert!(matches!(
            ResetLock::acquire(&db, 7),
            Err(CliError::AaveMarketMidUpdate { .. })
        ));
        drop(holder);
        ResetLock::acquire(&db, 7).unwrap();
    }

    #[test]
    fn a_killed_holder_releases_the_reset_lock() {
        let dir = tempfile::tempdir().unwrap();
        let db = dir.path().join("degenbot.db");
        let mut child = spawn_holder(&db, dir.path());
        // The live fake holder blocks.
        assert!(matches!(
            ResetLock::acquire(&db, 7),
            Err(CliError::AaveMarketMidUpdate { .. })
        ));
        // SIGKILL: the kernel drops the flock with the process, the pid file
        // survives, and the next reset proceeds.
        child.kill().unwrap();
        child.wait().unwrap();
        ResetLock::acquire(&db, 7).unwrap();
    }

    /// The fake holder: spawned as a child test process by
    /// `a_killed_holder_releases_the_reset_lock`, killed mid-hold.
    #[test]
    #[ignore = "spawned as the fake holder by a_killed_holder_releases_the_reset_lock"]
    fn reset_lock_holder_child() {
        let db = PathBuf::from(std::env::var("RESET_LOCK_TEST_DB").unwrap());
        let ready = PathBuf::from(std::env::var("RESET_LOCK_TEST_READY").unwrap());
        let _lock = ResetLock::acquire(&db, 7).unwrap();
        fs::write(&ready, b"armed").unwrap();
        // Hold until the parent kills us; the deadline only bounds a leak if
        // the parent dies before it can.
        let deadline = Instant::now() + Duration::from_secs(120);
        while Instant::now() < deadline {
            std::thread::sleep(Duration::from_millis(100));
        }
    }
}