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
use brontes_database::{libmdbx::Libmdbx, InitializedState, InitializedStateData, Tables};
use brontes_types::db::initialized_state::{
    CEX_QUOTES_FLAG, CEX_TRADES_FLAG, DEX_PRICE_FLAG, META_FLAG, TRACE_FLAG,
};
use clap::Parser;

#[derive(Debug, Parser)]
pub struct Clear {
    /// Tables to clear
    #[arg(
        long,
        short,
        value_delimiter = ',',
        default_value = "CexPrice,DexPrice,CexTrades,BlockInfo,InitializedState,MevBlocks,\
                         TokenDecimals,AddressToProtocolInfo,PoolCreationBlocks,Builder,\
                         AddressMeta,SearcherEOAs,SearcherContracts,SubGraphs,TxTraces"
    )]
    pub tables:                  Vec<Tables>,
    /// Mark metadata as uninitialized in the initialized state table
    #[arg(long, default_value = "false")]
    pub clear_metadata_flags:    bool,
    /// Mark cex quotes as uninitialized in the initialized state table
    #[arg(long, default_value = "false")]
    pub clear_cex_quotes_flags:  bool,
    /// Mark cex trades as uninitialized in the initialized state table
    #[arg(long, default_value = "false")]
    pub clear_cex_trades_flags:  bool,
    /// Mark tx traces as uninitialized in the initialized state table
    #[arg(long, default_value = "false")]
    pub clear_tx_traces_flags:   bool,
    /// Mark dex pricing as uninitialized in the initialized state table
    #[arg(long, default_value = "false")]
    pub clear_dex_pricing_flags: bool,
}

impl Clear {
    pub async fn execute(self, brontes_db_path: String) -> eyre::Result<()> {
        let db = Libmdbx::init_db(brontes_db_path, None)?;

        macro_rules! clear_table {
    ($table:expr, $($tables:ident),+) => {
        match $table {
            $(
                Tables::$tables => {
                            db
                            .clear_table::<brontes_database::libmdbx::tables::$tables>().unwrap()
                }
            )+
        }
    };
}

        self.tables.iter().for_each(|table| {
            clear_table!(
                table,
                CexPrice,
                CexTrades,
                InitializedState,
                BlockInfo,
                DexPrice,
                MevBlocks,
                TokenDecimals,
                AddressToProtocolInfo,
                PoolCreationBlocks,
                Builder,
                AddressMeta,
                SearcherEOAs,
                SearcherContracts,
                TxTraces
            )
        });

        if self.clear_cex_quotes_flags
            || self.clear_cex_trades_flags
            || self.clear_tx_traces_flags
            || self.clear_metadata_flags
            || self.clear_dex_pricing_flags
        {
            db.view_db(|tx| {
                let mut cur = tx.new_cursor::<InitializedState>()?;
                let walker = cur.walk_range(..)?;
                let mut updated_res = Vec::new();

                for item in walker.flatten() {
                    let mut key = item.1;
                    if self.clear_dex_pricing_flags {
                        key.apply_reset_key(DEX_PRICE_FLAG);
                    }
                    if self.clear_metadata_flags {
                        key.apply_reset_key(META_FLAG);
                    }
                    if self.clear_tx_traces_flags {
                        key.apply_reset_key(TRACE_FLAG);
                    }
                    if self.clear_cex_quotes_flags {
                        key.apply_reset_key(CEX_QUOTES_FLAG);
                    }
                    if self.clear_cex_trades_flags {
                        key.apply_reset_key(CEX_TRADES_FLAG);
                    }

                    updated_res.push(InitializedStateData::new(item.0, item.1));
                }
                db.write_table(&updated_res)?;
                Ok(())
            })?;
        }

        Ok(())
    }
}