zephyr_vm/
snapshot.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
//! Snapshot utilites required to correctly perform tx simulation
//! calculations.

use std::rc::Rc;

use rusqlite::{params, Connection};
use snapshot_utils::get_ttl;
use soroban_env_host::storage::{EntryWithLiveUntil, SnapshotSource};
use soroban_env_host::xdr::{
    AccountEntry, LedgerEntry, LedgerEntryExt, LedgerKey, Limits, PublicKey, ReadXdr,
    SequenceNumber, Thresholds, WriteXdr,
};
use soroban_env_host::HostError;
use soroban_simulation::SnapshotSourceWithArchive;
use stellar_xdr::next::Uint256;

pub struct DynamicSnapshot {}

pub mod snapshot_utils {
    use rusqlite::{params, Connection};
    use sha2::{Digest, Sha256};
    use soroban_env_host::xdr::{
        Hash, LedgerEntry, LedgerEntryData, LedgerKey, Limits, ReadXdr, WriteXdr,
    };

    pub fn get_current_ledger_sequence() -> (i32, i64) {
        let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
        let query_string = format!(
            "SELECT ledgerseq, closetime FROM ledgerheaders ORDER BY ledgerseq DESC LIMIT 1"
        );

        let mut stmt = conn.prepare(&query_string).unwrap();
        let mut entries = stmt.query(params![]).unwrap();

        let row = entries.next().unwrap();

        if row.is_none() {
            // Unrecoverable: no ledger is running
            return (0, 0);
        }

        (
            row.unwrap().get(0).unwrap_or(0),
            row.unwrap().get(1).unwrap_or(0),
        )
    }

    pub fn get_ttl(key: LedgerKey) -> u32 {
        let mut hasher = Sha256::new();
        hasher.update(key.to_xdr(Limits::none()).unwrap());
        let result = {
            let hashed = hasher.finalize().as_slice().try_into().unwrap();
            Hash(hashed).to_xdr_base64(Limits::none()).unwrap()
        };

        let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
        let query_string = format!("SELECT ledgerentry FROM ttl WHERE keyhash = ?1");

        let mut stmt = conn.prepare(&query_string).unwrap();
        let mut entries = stmt.query(params![result]).unwrap();

        let row = entries.next().unwrap();

        if row.is_none() {
            // TODO: error log
            return 0;
        }

        let entry = {
            let string: String = row.unwrap().get(0).unwrap();
            LedgerEntry::from_xdr_base64(&string, Limits::none()).unwrap()
        };

        let LedgerEntryData::Ttl(ttl) = entry.data else {
            return 0;
        };
        ttl.live_until_ledger_seq
    }
}

impl SnapshotSourceWithArchive for DynamicSnapshot {
    fn get_including_archived(
        &self,
        key: &Rc<LedgerKey>,
    ) -> std::result::Result<Option<EntryWithLiveUntil>, soroban_env_host::HostError> {
        let LedgerKey::ConfigSetting(setting) = key.as_ref() else {
            return Err(HostError::from(
                soroban_env_host::Error::from_contract_error(0),
            ));
        };

        let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
        let query_string =
            format!("SELECT ledgerentry FROM configsettings WHERE configsettingid = ?1");

        let mut stmt = conn.prepare(&query_string).unwrap();
        let mut entries = stmt
            .query(params![setting.config_setting_id as i32])
            .unwrap();

        let row = entries.next().unwrap();

        if row.is_none() {
            // TODO: error log
            return Err(HostError::from(
                soroban_env_host::Error::from_contract_error(0),
            ));
        }

        let entry = {
            let string: String = row.unwrap().get(0).unwrap();
            LedgerEntry::from_xdr_base64(&string, Limits::none()).unwrap()
        };

        Ok(Some((Rc::new(entry), Some(u32::MAX))))
    }
}

pub fn snapshot_get_universal(
    //key: &std::rc::Rc<soroban_env_host::xdr::LedgerKey>,
    key: Vec<u8>,
) -> Result<Option<(Vec<u8>, Option<u32>)>, soroban_env_host::HostError> {
    let key = LedgerKey::from_xdr(key, Limits::none())
        .map_err(|_| soroban_env_host::xdr::Error::Invalid)?;

    let entry: Option<EntryWithLiveUntil> = match key {
        LedgerKey::Trustline(trustline) => {
            let PublicKey::PublicKeyTypeEd25519(Uint256(bytes)) = trustline.account_id.0;
            let account_id = stellar_strkey::ed25519::PublicKey(bytes).to_string();
            let asset_xdr = trustline.asset.to_xdr_base64(Limits::none()).unwrap();

            let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
            let query_string =
                format!("SELECT ledgerentry FROM trustlines where accountid = ?1 AND asset = ?2");

            let mut stmt = conn.prepare(&query_string).unwrap();
            let mut entries = stmt.query(params![account_id, asset_xdr]).unwrap();

            let row = entries.next().unwrap();

            if row.is_none() {
                return Ok(None);
            }
            let row = row.unwrap();

            let xdr_entry: String = row.get(0).unwrap();
            let xdr_entry = LedgerEntry::from_xdr_base64(xdr_entry, Limits::none()).unwrap();

            Some((Rc::new(xdr_entry), None))
        }

        LedgerKey::Account(key) => {
            let PublicKey::PublicKeyTypeEd25519(ed25519) = key.account_id.0.clone();
            let id = stellar_strkey::ed25519::PublicKey(ed25519.0).to_string();

            let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
            let query_string = format!("SELECT balance FROM accounts where accountid = ?1");

            let mut stmt = conn.prepare(&query_string).unwrap();
            let mut entries = stmt.query(params![id]).unwrap();

            let row = entries.next().unwrap();

            if row.is_none() {
                return Ok(None);
            }
            let row = row.unwrap();

            let entry = LedgerEntry {
                last_modified_ledger_seq: 0,
                ext: LedgerEntryExt::V0,
                data: soroban_env_host::xdr::LedgerEntryData::Account(AccountEntry {
                    account_id: key.account_id.clone(),
                    balance: row.get(0).unwrap(),
                    seq_num: SequenceNumber(0),
                    num_sub_entries: 0,
                    inflation_dest: None,
                    flags: 0,
                    home_domain: Default::default(),
                    thresholds: Thresholds([0; 4]),
                    signers: vec![].try_into().unwrap(),
                    ext: soroban_env_host::xdr::AccountEntryExt::V0,
                }),
            };

            Some((Rc::new(entry), None))
        }

        LedgerKey::ContractCode(key) => {
            let hash = key.hash.clone();
            let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
            let query_string = format!("SELECT ledgerentry FROM contractcode where hash = ?1");

            let mut stmt = conn.prepare(&query_string).unwrap();
            let mut entries = stmt
                .query(params![hash.to_xdr_base64(Limits::none()).unwrap()])
                .unwrap();

            let row = entries.next().unwrap();

            if row.is_none() {
                return Ok(None);
            }
            let row = row.unwrap();

            let xdr_entry: String = row.get(0).unwrap();
            let xdr_entry = LedgerEntry::from_xdr_base64(xdr_entry, Limits::none()).unwrap();

            Some((
                Rc::new(xdr_entry),
                Some(get_ttl(LedgerKey::ContractCode(key.clone()))),
            ))
        }

        LedgerKey::ContractData(key) => {
            let contract = key.contract.clone();
            let scval = key.key.clone();

            let conn = Connection::open("/tmp/rs_ingestion_temp/stellar.db").unwrap();
            let query_string =
                format!("SELECT ledgerentry FROM contractdata where contractid = ?1 AND key = ?2");

            let mut stmt = conn.prepare(&query_string).unwrap();
            let mut entries = stmt
                .query(params![
                    contract.to_xdr_base64(Limits::none()).unwrap(),
                    scval.to_xdr_base64(Limits::none()).unwrap()
                ])
                .unwrap();
            let row = entries.next().unwrap();

            if row.is_none() {
                return Ok(None);
            }
            let row = row.unwrap();

            let xdr_entry: String = row.get(0).unwrap();
            let xdr_entry = LedgerEntry::from_xdr_base64(xdr_entry, Limits::none()).unwrap();

            Some((
                Rc::new(xdr_entry),
                Some(get_ttl(LedgerKey::ContractData(key.clone()))),
            ))
        }

        _ => None,
    };

    if let Some(key) = entry {
        Ok(Some((key.0.to_xdr(Limits::none())?, key.1)))
    } else {
        Ok(None)
    }
}

impl SnapshotSource for DynamicSnapshot {
    fn get(
        &self,
        key: &std::rc::Rc<soroban_env_host::xdr::LedgerKey>,
    ) -> Result<Option<soroban_env_host::storage::EntryWithLiveUntil>, soroban_env_host::HostError>
    {
        let xdred = snapshot_get_universal(key.as_ref().to_xdr(Limits::none()).unwrap())?;
        if let Some(xdr_key) = xdred {
            Ok(Some((
                Rc::new(LedgerEntry::from_xdr(xdr_key.0, Limits::none())?),
                xdr_key.1,
            )))
        } else {
            Ok(None)
        }
    }
}