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
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::Path;

use serde::{Deserialize, Serialize};
use starknet_types::rpc::transactions::{
    BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction,
    BroadcastedInvokeTransaction, L1HandlerTransaction,
};

use super::{DumpOn, Starknet};
use crate::error::{DevnetResult, Error};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum DumpEvent {
    CreateBlock,
    SetTime(u64),
    IncreaseTime(u64),
    AddDeclareTransaction(BroadcastedDeclareTransaction),
    AddInvokeTransaction(BroadcastedInvokeTransaction),
    AddDeployAccountTransaction(BroadcastedDeployAccountTransaction),
    AddL1HandlerTransaction(L1HandlerTransaction),
}

impl Starknet {
    pub fn re_execute(&mut self, events: Vec<DumpEvent>) -> DevnetResult<()> {
        for event in events.into_iter() {
            match event {
                DumpEvent::AddDeclareTransaction(BroadcastedDeclareTransaction::V1(tx)) => {
                    self.add_declare_transaction_v1(*tx)?;
                }
                DumpEvent::AddDeclareTransaction(BroadcastedDeclareTransaction::V2(tx)) => {
                    self.add_declare_transaction_v2(*tx)?;
                }
                DumpEvent::AddDeclareTransaction(BroadcastedDeclareTransaction::V3(tx)) => {
                    self.add_declare_transaction_v3(*tx)?;
                }
                DumpEvent::AddDeployAccountTransaction(
                    BroadcastedDeployAccountTransaction::V1(tx),
                ) => {
                    self.add_deploy_account_transaction_v1(tx)?;
                }
                DumpEvent::AddDeployAccountTransaction(
                    BroadcastedDeployAccountTransaction::V3(tx),
                ) => {
                    self.add_deploy_account_transaction_v3(tx)?;
                }
                DumpEvent::AddInvokeTransaction(BroadcastedInvokeTransaction::V1(tx)) => {
                    self.add_invoke_transaction_v1(tx)?;
                }
                DumpEvent::AddInvokeTransaction(BroadcastedInvokeTransaction::V3(tx)) => {
                    self.add_invoke_transaction_v3(tx)?;
                }
                DumpEvent::AddL1HandlerTransaction(tx) => {
                    self.add_l1_handler_transaction(tx)?;
                }
                DumpEvent::CreateBlock => {
                    self.create_block_dump_event(None)?;
                }
                DumpEvent::SetTime(timestamp) => {
                    self.set_time(timestamp, false)?;
                }
                DumpEvent::IncreaseTime(time_shift) => {
                    self.increase_time(time_shift)?;
                }
            };
        }

        Ok(())
    }

    // add starknet dump event
    pub fn handle_dump_event(&mut self, event: DumpEvent) -> DevnetResult<()> {
        match self.config.dump_on {
            Some(DumpOn::Transaction) => self.dump_event(event),
            Some(DumpOn::Exit) => {
                self.dump_events.push(event);

                Ok(())
            }
            None => Ok(()),
        }
    }

    /// attach starknet event to end of existing file
    pub fn dump_event(&self, event: DumpEvent) -> DevnetResult<()> {
        match &self.config.dump_path {
            Some(path) => {
                let file_path = Path::new(path);
                if file_path.exists() {
                    // attach to file
                    let event_dump = serde_json::to_string(&event)
                        .map_err(|e| Error::SerializationError { origin: e.to_string() })?;
                    let mut file = OpenOptions::new()
                        .append(true)
                        .read(true)
                        .open(file_path)
                        .map_err(Error::IoError)?;
                    let mut buffer = [0; 1];
                    file.seek(SeekFrom::End(-1))?;
                    file.read_exact(&mut buffer)?;
                    if String::from_utf8_lossy(&buffer).into_owned() == "]" {
                        // if the last character is "]", remove it and add event at the end
                        let length = file.seek(SeekFrom::End(0)).map_err(Error::IoError)?;
                        file.set_len(length - 1).map_err(Error::IoError)?; // remove last "]" with set_len
                        file.write_all(format!(", {event_dump}]").as_bytes())
                            .map_err(Error::IoError)?;
                    } else {
                        // if the last character is not "]" it means that it's a wrongly formatted
                        // file
                        return Err(Error::FormatError);
                    }
                } else {
                    // create file
                    let events = vec![event];
                    let events_dump = serde_json::to_string(&events)
                        .map_err(|e| Error::SerializationError { origin: e.to_string() })?;
                    fs::write(Path::new(&path), events_dump)?;
                }

                Ok(())
            }
            None => Err(Error::FormatError),
        }
    }

    pub fn dump_events(&self) -> DevnetResult<()> {
        self.dump_events_custom_path(None)
    }

    /// save starknet events to file
    pub fn dump_events_custom_path(&self, custom_path: Option<String>) -> DevnetResult<()> {
        let dump_path = if custom_path.is_some() { &custom_path } else { &self.config.dump_path };
        match dump_path {
            Some(path) => {
                let events = &self.dump_events;

                // dump only if there are events to dump
                if !events.is_empty() {
                    let events_dump = serde_json::to_string(events)
                        .map_err(|e| Error::SerializationError { origin: e.to_string() })?;
                    fs::write(Path::new(&path), events_dump)?;
                }

                Ok(())
            }
            None => Err(Error::FormatError),
        }
    }

    pub fn load_events(&self) -> DevnetResult<Vec<DumpEvent>> {
        self.load_events_custom_path(None)
    }

    // load starknet events from file
    pub fn load_events_custom_path(
        &self,
        custom_path: Option<String>,
    ) -> DevnetResult<Vec<DumpEvent>> {
        let dump_path = if custom_path.is_some() { &custom_path } else { &self.config.dump_path };
        match dump_path {
            Some(path) => {
                let file_path = Path::new(path);

                // load only if the file exists, if config.dump_path is set but the file doesn't
                // exist it means that it's first execution and in that case return an empty vector,
                // in case of load from HTTP endpoint return FileNotFound error
                if file_path.exists() {
                    let file = File::open(file_path).map_err(Error::IoError)?;
                    let events: Vec<DumpEvent> = serde_json::from_reader(file)
                        .map_err(|e| Error::DeserializationError { origin: e.to_string() })?;

                    // to avoid doublets in transaction mode during load, we need to remove the file
                    // because they will be re-executed and saved again
                    if self.config.dump_on == Some(DumpOn::Transaction) {
                        fs::remove_file(file_path).map_err(Error::IoError)?;
                    }

                    Ok(events)
                } else {
                    Err(Error::FileNotFound)
                }
            }
            None => Err(Error::FormatError),
        }
    }
}