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
use crate::{
    from_oura::{FromOura, OuraParseError},
    progress_tracker::ProgressTracker,
};
use anyhow::anyhow;
use itertools::Itertools;
use num_bigint::BigInt;
use oura::model as oura;
use plutus_ledger_api::v2::{
    address::Address,
    datum::{Datum, DatumHash, OutputDatum},
    redeemer::Redeemer,
    script::ScriptHash,
    transaction::{ScriptPurpose, TransactionHash, TransactionInput, TransactionOutput, TxInInfo},
    value::{CurrencySymbol, Value},
};
use serde_with::serde_as;
use std::fmt::Debug;
use std::{collections::HashMap, sync::atomic::Ordering};
use tracing::{event, Level};

/// Indication of when an event happened in the context of the chain.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ChainEventTime {
    pub block_number: u64,
    pub block_hash: String,
    pub slot: u64,
}

/// Chain events that the indexer is configured to produce.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum ChainEvent {
    /// A filtered transaction was confirmed
    TransactionEvent {
        time: ChainEventTime,
        transaction: TransactionEventRecord,
    },

    /// Rollback event occurred
    RollbackEvent { block_slot: u64, block_hash: String },

    /// Chain syncronisation progressed
    SyncProgressEvent {
        block_slot: u64,
        block_hash: String,
        percentage: f32,
    },
}

/// Details on an transaction event (excluding unnecessary information).
#[serde_as]
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TransactionEventRecord {
    pub hash: TransactionHash,
    pub fee: u64,
    pub size: u32,

    pub inputs: Vec<TransactionInput>,
    pub outputs: Vec<TxInInfo>,
    pub mint: Value,
    #[serde_as(as = "Vec<(_, _)>")]
    pub redeemers: HashMap<ScriptPurpose, Redeemer>,

    #[serde_as(as = "Vec<(_, _)>")]
    pub plutus_data: HashMap<DatumHash, Datum>,
    // TODO(chase): Which of these would be realistically be interested in?
    // pub vkey_witnesses: Option<Vec<VKeyWitnessRecord>>,
    // pub native_witnesses: Option<Vec<NativeWitnessRecord>>,
    // pub plutus_witnesses: Option<Vec<PlutusWitnessRecord>>,
    // pub plutus_redeemers: Option<Vec<PlutusRedeemerRecord>>,
}

pub fn parse_oura_event(
    ev: oura::Event,
    progress_tracker: &mut Option<ProgressTracker>,
) -> Result<Option<ChainEvent>, OuraParseError> {
    Ok(match ev.data {
        oura::EventData::Transaction(tx_rec) => {
            event!(Level::DEBUG, label="TransactionEvent", transaction_record=?tx_rec);

            Some(ChainEvent::TransactionEvent {
                time: ChainEventTime {
                    // These unwraps should not fail.
                    block_hash: ev.context.block_hash.unwrap(),
                    block_number: ev.context.block_number.unwrap(),
                    slot: ev.context.slot.unwrap(),
                },
                transaction: TransactionEventRecord::from_oura(tx_rec)?,
            })
        }
        oura::EventData::RollBack {
            block_slot,
            block_hash,
        } => {
            event!(Level::DEBUG, label="RollbackEvent", block_slot=?block_slot, block_hash=?block_hash);
            Some(ChainEvent::RollbackEvent {
                block_slot,
                block_hash,
            })
        }
        oura::EventData::Block(block_rec) => {
            event!(Level::DEBUG, label="BlockEvent", block_record=?block_rec);
            match progress_tracker {
                Some(progress_tracker) => {
                    let block_slot = block_rec.slot;
                    let block_hash = block_rec.hash;

                    let percentage = progress_tracker.get_percentage(block_slot)?;

                    let throttled_sync_progress = (percentage * 10.0) as usize;
                    let is_updated = progress_tracker
                        .sync_progress
                        .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |prev_status| {
                            if prev_status < throttled_sync_progress {
                                Some(throttled_sync_progress)
                            } else {
                                None
                            }
                        })
                        .is_ok();

                    if is_updated {
                        event!(
                            Level::INFO,
                            percentage = format!("{:.1}%", percentage),
                            ?block_slot,
                            ?block_hash,
                            label = "Chain synchronization progress"
                        );
                    }

                    Some(ChainEvent::SyncProgressEvent {
                        percentage,
                        block_slot,
                        block_hash,
                    })
                }

                None => Some(ChainEvent::SyncProgressEvent {
                    percentage: 100.0,
                    block_slot: block_rec.slot,
                    block_hash: block_rec.hash,
                }),
            }
        }
        _ => panic!("absurd: Indexer filter should only allow transaction event variant."),
    })
}

impl FromOura<oura::TransactionRecord> for TransactionEventRecord {
    fn from_oura(tx: oura::TransactionRecord) -> Result<TransactionEventRecord, OuraParseError> {
        Ok(TransactionEventRecord {
            hash: TransactionHash::from_oura(tx.hash.clone())?,
            fee: tx.fee,
            redeemers: tx
                .plutus_redeemers
                .unwrap()
                .into_iter()
                .map(|redeemer_record| {
                    // TODO(szg251): parse other redeemer tags
                    let script_purpose = match &redeemer_record.purpose[..] {
                        "spend" => {
                            let inputs = tx.inputs.as_ref().unwrap();
                            let input = inputs
                                .get(redeemer_record.input_idx as usize)
                                .ok_or(OuraParseError::ParseError(anyhow!(
                                    "No input found at redeemer index {}",
                                    redeemer_record.input_idx
                                )))?
                                .clone();

                            let transaction_input = TransactionInput {
                                transaction_id: TransactionHash::from_oura(input.tx_id)?,
                                index: BigInt::from(input.index),
                            };
                            ScriptPurpose::Spending(transaction_input)
                        }
                        "mint" => {
                            let policies = tx
                                .mint
                                .as_ref()
                                .unwrap()
                                .iter()
                                .map(|mint| mint.policy.clone())
                                .sorted()
                                .dedup()
                                .collect::<Vec<_>>();
                            let policy = policies
                                .get(redeemer_record.input_idx as usize)
                                .ok_or(OuraParseError::ParseError(anyhow!(
                                    "No mint found at redeemer index {}",
                                    redeemer_record.input_idx
                                )))?
                                .clone();

                            ScriptPurpose::Minting(CurrencySymbol::from_oura(policy)?)
                        }
                        // "cert" => ScriptPurpose::Certifying (),
                        // "reward" => ScriptPurpose::Rewarding(0)
                        _ => Err(OuraParseError::ParseError(anyhow!(
                            "Cannot parse redeemer tag variant: {}",
                            redeemer_record.purpose
                        )))?,
                    };

                    Ok((
                        script_purpose,
                        Redeemer::from_oura(redeemer_record.plutus_data)?,
                    ))
                })
                .collect::<Result<_, OuraParseError>>()?,
            size: tx.size,
            // All these unwraps should succeed since we enable `include_transaction_details`
            // in the mapper config.
            inputs: tx
                .inputs
                .unwrap()
                .into_iter()
                .map(|oura::TxInputRecord { tx_id, index }| {
                    Ok(TransactionInput {
                        transaction_id: TransactionHash::from_oura(tx_id)?,
                        index: BigInt::from(index),
                    })
                })
                .collect::<Result<_, OuraParseError>>()?,
            outputs: tx
                .outputs
                .unwrap()
                .into_iter()
                .enumerate()
                .map(
                    |(
                        index,
                        oura::TxOutputRecord {
                            address,
                            amount,
                            assets,
                            datum_hash,
                            inline_datum,
                            reference_script,
                        },
                    )| {
                        let reference = TransactionInput {
                            transaction_id: TransactionHash::from_oura(tx.hash.clone())?,
                            index: index.into(),
                        };
                        let output = TransactionOutput {
                            address: Address::from_oura(address)?,
                            datum: match (datum_hash, inline_datum) {
                                (None, None) => OutputDatum::None,
                                (_, Some(datm)) => {
                                    OutputDatum::InlineDatum(Datum::from_oura(datm.plutus_data)?)
                                }
                                (Some(dh), _) => OutputDatum::DatumHash(DatumHash::from_oura(dh)?),
                            },
                            // NOTE(chase): There is currently no way to know about reference scripts with Oura.
                            reference_script: reference_script
                                .map(ScriptHash::from_oura)
                                .transpose()?,
                            value: Value::ada_value(&BigInt::from(amount))
                                + Value::from_oura(assets.unwrap_or_default())?,
                        };

                        Ok(TxInInfo { reference, output })
                    },
                )
                .collect::<Result<_, OuraParseError>>()?,
            mint: tx.mint.map_or(Ok(Value::new()), Value::from_oura)?,
            plutus_data: tx
                .plutus_data
                .unwrap_or_default()
                .into_iter()
                .map(
                    |oura::PlutusDatumRecord {
                         plutus_data,
                         datum_hash,
                     }| {
                        Ok((
                            DatumHash::from_oura(datum_hash)?,
                            Datum::from_oura(plutus_data)?,
                        ))
                    },
                )
                .collect::<Result<_, OuraParseError>>()?,
        })
    }
}