sui_indexer_alt/handlers/
kv_epoch_ends.rs

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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::ops::Range;
use std::sync::Arc;

use anyhow::{bail, Context, Result};
use diesel::{ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use sui_indexer_alt_framework::{
    db::{Connection, Db},
    pipeline::{concurrent::Handler, Processor},
    types::{
        event::SystemEpochInfoEvent,
        full_checkpoint_content::CheckpointData,
        transaction::{TransactionDataAPI, TransactionKind},
    },
};
use sui_indexer_alt_schema::{epochs::StoredEpochEnd, schema::kv_epoch_ends};

use crate::handlers::cp_sequence_numbers::epoch_interval;

pub(crate) struct KvEpochEnds;

impl Processor for KvEpochEnds {
    const NAME: &'static str = "kv_epoch_ends";

    type Value = StoredEpochEnd;

    fn process(&self, checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> {
        let CheckpointData {
            checkpoint_summary,
            transactions,
            ..
        } = checkpoint.as_ref();

        let Some(end_of_epoch) = checkpoint_summary.end_of_epoch_data.as_ref() else {
            return Ok(vec![]);
        };

        let Some(transaction) = transactions.iter().find(|tx| {
            matches!(
                tx.transaction.intent_message().value.kind(),
                TransactionKind::ChangeEpoch(_) | TransactionKind::EndOfEpochTransaction(_)
            )
        }) else {
            bail!(
                "Failed to get end of epoch transaction in checkpoint {} with EndOfEpochData",
                checkpoint_summary.sequence_number,
            );
        };

        if let Some(SystemEpochInfoEvent {
            total_stake,
            storage_fund_reinvestment,
            storage_charge,
            storage_rebate,
            storage_fund_balance,
            stake_subsidy_amount,
            total_gas_fees,
            total_stake_rewards_distributed,
            leftover_storage_fund_inflow,
            ..
        }) = transaction
            .events
            .iter()
            .flat_map(|events| &events.data)
            .find_map(|event| {
                event
                    .is_system_epoch_info_event()
                    .then(|| bcs::from_bytes(&event.contents))
            })
            .transpose()
            .context("Failed to deserialize SystemEpochInfoEvent")?
        {
            Ok(vec![StoredEpochEnd {
                epoch: checkpoint_summary.epoch as i64,
                cp_hi: checkpoint_summary.sequence_number as i64 + 1,
                tx_hi: checkpoint_summary.network_total_transactions as i64,
                end_timestamp_ms: checkpoint_summary.timestamp_ms as i64,

                safe_mode: false,

                total_stake: Some(total_stake as i64),
                storage_fund_balance: Some(storage_fund_balance as i64),
                storage_fund_reinvestment: Some(storage_fund_reinvestment as i64),
                storage_charge: Some(storage_charge as i64),
                storage_rebate: Some(storage_rebate as i64),
                stake_subsidy_amount: Some(stake_subsidy_amount as i64),
                total_gas_fees: Some(total_gas_fees as i64),
                total_stake_rewards_distributed: Some(total_stake_rewards_distributed as i64),
                leftover_storage_fund_inflow: Some(leftover_storage_fund_inflow as i64),

                epoch_commitments: bcs::to_bytes(&end_of_epoch.epoch_commitments)
                    .context("Failed to serialize EpochCommitment-s")?,
            }])
        } else {
            Ok(vec![StoredEpochEnd {
                epoch: checkpoint_summary.epoch as i64,
                cp_hi: checkpoint_summary.sequence_number as i64 + 1,
                tx_hi: checkpoint_summary.network_total_transactions as i64,
                end_timestamp_ms: checkpoint_summary.timestamp_ms as i64,

                safe_mode: true,

                total_stake: None,
                storage_fund_balance: None,
                storage_fund_reinvestment: None,
                storage_charge: None,
                storage_rebate: None,
                stake_subsidy_amount: None,
                total_gas_fees: None,
                total_stake_rewards_distributed: None,
                leftover_storage_fund_inflow: None,

                epoch_commitments: bcs::to_bytes(&end_of_epoch.epoch_commitments)
                    .context("Failed to serialize EpochCommitment-s")?,
            }])
        }
    }
}

#[async_trait::async_trait]
impl Handler for KvEpochEnds {
    type Store = Db;

    const MIN_EAGER_ROWS: usize = 1;

    async fn commit<'a>(values: &[Self::Value], conn: &mut Connection<'a>) -> Result<usize> {
        Ok(diesel::insert_into(kv_epoch_ends::table)
            .values(values)
            .on_conflict_do_nothing()
            .execute(conn)
            .await?)
    }

    async fn prune<'a>(
        &self,
        from: u64,
        to_exclusive: u64,
        conn: &mut Connection<'a>,
    ) -> Result<usize> {
        let Range {
            start: from_epoch,
            end: to_epoch,
        } = epoch_interval(conn, from..to_exclusive).await?;
        if from_epoch < to_epoch {
            let filter = kv_epoch_ends::table
                .filter(kv_epoch_ends::epoch.between(from_epoch as i64, to_epoch as i64 - 1));
            Ok(diesel::delete(filter).execute(conn).await?)
        } else {
            Ok(0)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use sui_indexer_alt_framework::{
        types::test_checkpoint_data_builder::TestCheckpointDataBuilder, Indexer,
    };
    use sui_indexer_alt_schema::MIGRATIONS;

    use crate::handlers::cp_sequence_numbers::CpSequenceNumbers;

    async fn get_all_kv_epoch_ends(conn: &mut Connection<'_>) -> Result<Vec<StoredEpochEnd>> {
        let result = kv_epoch_ends::table
            .order_by(kv_epoch_ends::epoch.asc())
            .load(conn)
            .await?;
        Ok(result)
    }

    async fn get_epoch_num_of_all_kv_epoch_ends(conn: &mut Connection<'_>) -> Result<Vec<i64>> {
        let epochs = get_all_kv_epoch_ends(conn).await?;
        Ok(epochs.iter().map(|e| e.epoch).collect())
    }

    #[tokio::test]
    pub async fn test_kv_epoch_ends_safe_mode() {
        let (indexer, _db) = Indexer::new_for_testing(&MIGRATIONS).await;
        let mut conn = indexer.db().connect().await.unwrap();

        let mut builder = TestCheckpointDataBuilder::new(0);
        let checkpoint = Arc::new(builder.advance_epoch(true));
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();

        let epochs = get_all_kv_epoch_ends(&mut conn).await.unwrap();
        assert_eq!(epochs.len(), 1);
        assert!(epochs[0].safe_mode);
        assert_eq!(epochs[0].total_gas_fees, None);

        let checkpoint = Arc::new(builder.advance_epoch(false));
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();

        let epochs = get_all_kv_epoch_ends(&mut conn).await.unwrap();
        assert_eq!(epochs.len(), 2);
        assert!(!epochs[1].safe_mode);
        assert_eq!(epochs[1].total_gas_fees, Some(0));
    }

    #[tokio::test]
    pub async fn test_kv_epoch_ends_same_epoch() {
        let (indexer, _db) = Indexer::new_for_testing(&MIGRATIONS).await;
        let mut conn = indexer.db().connect().await.unwrap();

        // Test that there is nothing to commit while we haven't reached epoch end.
        let mut builder = TestCheckpointDataBuilder::new(0);
        let checkpoint = Arc::new(builder.build_checkpoint());
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        assert_eq!(values.len(), 0);
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        let checkpoint = Arc::new(builder.build_checkpoint());
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        assert_eq!(values.len(), 0);
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        // When the advance epoch tx is detected, there should be an entry to commit.
        let checkpoint = Arc::new(builder.advance_epoch(false));
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        assert_eq!(values.len(), 1);
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        // Afterwards, kv_epoch_ends should not have anything to commit until the next advance epoch
        // tx.
        let checkpoint = Arc::new(builder.build_checkpoint());
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        assert_eq!(values.len(), 0);
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        let checkpoint = Arc::new(builder.build_checkpoint());
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        assert_eq!(values.len(), 0);
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        let epochs = get_epoch_num_of_all_kv_epoch_ends(&mut conn).await.unwrap();
        assert_eq!(epochs, vec![0]);

        let rows_pruned = KvEpochEnds.prune(0, 4, &mut conn).await.unwrap();
        let epochs = get_epoch_num_of_all_kv_epoch_ends(&mut conn).await.unwrap();
        assert_eq!(epochs.len(), 0);
        assert_eq!(rows_pruned, 1);
    }

    #[tokio::test]
    pub async fn test_kv_epoch_ends_advance_multiple_epochs() {
        let (indexer, _db) = Indexer::new_for_testing(&MIGRATIONS).await;
        let mut conn = indexer.db().connect().await.unwrap();

        // Advance epoch three times, 0, 1, 2
        let mut builder = TestCheckpointDataBuilder::new(0);
        let checkpoint = Arc::new(builder.advance_epoch(false));
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        let checkpoint = Arc::new(builder.advance_epoch(false));
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        let checkpoint = Arc::new(builder.advance_epoch(false));
        let values = KvEpochEnds.process(&checkpoint).unwrap();
        KvEpochEnds::commit(&values, &mut conn).await.unwrap();
        let values = CpSequenceNumbers.process(&checkpoint).unwrap();
        CpSequenceNumbers::commit(&values, &mut conn).await.unwrap();

        let epochs = get_epoch_num_of_all_kv_epoch_ends(&mut conn).await.unwrap();
        assert_eq!(epochs, vec![0, 1, 2]);

        let rows_pruned = KvEpochEnds.prune(0, 2, &mut conn).await.unwrap();
        let epochs = get_epoch_num_of_all_kv_epoch_ends(&mut conn).await.unwrap();
        // Only epoch 2 remains, after pruning 0 and 1.
        assert_eq!(epochs, vec![2]);
        assert_eq!(rows_pruned, 2);
    }
}