Skip to main content

soroban_cli/commands/contract/
restore.rs

1use std::{fmt::Debug, path::Path, str::FromStr};
2
3use crate::{
4    log::extract_events,
5    tx::sim_sign_and_send_tx,
6    xdr::{
7        Error as XdrError, ExtensionPoint, LedgerEntry, LedgerEntryChange, LedgerEntryData,
8        LedgerFootprint, Limits, Memo, Operation, OperationBody, Preconditions, RestoreFootprintOp,
9        SequenceNumber, SorobanResources, SorobanTransactionData, SorobanTransactionDataExt,
10        Transaction, TransactionExt, TransactionMeta, TransactionMetaV3, TransactionMetaV4,
11        TtlEntry, WriteXdr,
12    },
13};
14use clap::Parser;
15use stellar_strkey::DecodeError;
16
17use crate::commands::tx::fetch;
18use crate::{
19    commands::{
20        contract::extend,
21        global,
22        txn_result::{TxnEnvelopeResult, TxnResult},
23        HEADING_TRANSACTION,
24    },
25    config::{self, data, locator, network},
26    key, rpc, wasm, Pwd,
27};
28
29#[derive(Parser, Debug, Clone)]
30#[group(skip)]
31pub struct Cmd {
32    #[command(flatten)]
33    pub key: key::Args,
34
35    /// Number of ledgers to extend the entry
36    #[arg(long)]
37    pub ledgers_to_extend: Option<u32>,
38
39    /// Only print the new Time To Live ledger
40    #[arg(long)]
41    pub ttl_ledger_only: bool,
42
43    #[command(flatten)]
44    pub config: config::Args,
45
46    #[command(flatten)]
47    pub resources: crate::resources::Args,
48
49    /// Build the transaction and only write the base64 xdr to stdout
50    #[arg(long, help_heading = HEADING_TRANSACTION)]
51    pub build_only: bool,
52}
53
54impl FromStr for Cmd {
55    type Err = clap::error::Error;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        use clap::{CommandFactory, FromArgMatches};
59        Self::from_arg_matches_mut(&mut Self::command().get_matches_from(s.split_whitespace()))
60    }
61}
62
63impl Pwd for Cmd {
64    fn set_pwd(&mut self, pwd: &Path) {
65        self.config.set_pwd(pwd);
66    }
67}
68
69#[derive(thiserror::Error, Debug)]
70pub enum Error {
71    #[error("parsing key {key}: {error}")]
72    CannotParseKey {
73        key: String,
74        error: soroban_spec_tools::Error,
75    },
76
77    #[error("parsing XDR key {key}: {error}")]
78    CannotParseXdrKey { key: String, error: XdrError },
79
80    #[error("cannot parse contract ID {0}: {1}")]
81    CannotParseContractId(String, DecodeError),
82
83    #[error(transparent)]
84    Config(#[from] config::Error),
85
86    #[error("either `--key` or `--key-xdr` are required")]
87    KeyIsRequired,
88
89    #[error("xdr processing error: {0}")]
90    Xdr(#[from] XdrError),
91
92    #[error("Ledger entry not found")]
93    LedgerEntryNotFound,
94
95    #[error(transparent)]
96    Locator(#[from] locator::Error),
97
98    #[error("missing operation result")]
99    MissingOperationResult,
100
101    #[error(transparent)]
102    Rpc(#[from] rpc::Error),
103
104    #[error(transparent)]
105    Wasm(#[from] wasm::Error),
106
107    #[error(transparent)]
108    Key(#[from] key::Error),
109
110    #[error(transparent)]
111    Extend(#[from] extend::Error),
112
113    #[error(transparent)]
114    Data(#[from] data::Error),
115
116    #[error(transparent)]
117    Network(#[from] network::Error),
118
119    #[error(transparent)]
120    Fee(#[from] fetch::fee::Error),
121
122    #[error(transparent)]
123    Fetch(#[from] fetch::Error),
124}
125
126impl Cmd {
127    #[allow(clippy::too_many_lines)]
128    pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
129        let res = self
130            .execute(&self.config, global_args.quiet, global_args.no_cache)
131            .await?
132            .to_envelope();
133        let expiration_ledger_seq = match res {
134            TxnEnvelopeResult::TxnEnvelope(tx) => {
135                println!("{}", tx.to_xdr_base64(Limits::none())?);
136                return Ok(());
137            }
138            TxnEnvelopeResult::Res(res) => res,
139        };
140        if let Some(ledgers_to_extend) = self.ledgers_to_extend {
141            extend::Cmd {
142                key: self.key.clone(),
143                ledgers_to_extend,
144                config: self.config.clone(),
145                resources: self.resources.clone(),
146                ttl_ledger_only: false,
147                build_only: self.build_only,
148            }
149            .run(global_args)
150            .await?;
151        } else {
152            println!("New ttl ledger: {expiration_ledger_seq}");
153        }
154
155        Ok(())
156    }
157
158    pub async fn execute(
159        &self,
160        config: &config::Args,
161        quiet: bool,
162        no_cache: bool,
163    ) -> Result<TxnResult<u32>, Error> {
164        let print = crate::print::Print::new(quiet);
165        let network = config.get_network()?;
166        tracing::trace!(?network);
167        let entry_keys = self.key.parse_keys(&config.locator, &network)?;
168        let client = network.rpc_client()?;
169        client
170            .verify_network_passphrase(Some(&network.network_passphrase))
171            .await?;
172        let source_account = config.source_account()?;
173
174        // Get the account sequence number
175        let account_details = client
176            .get_account(&source_account.clone().to_string())
177            .await?;
178        let sequence: i64 = account_details.seq_num.into();
179
180        let tx = Box::new(Transaction {
181            source_account,
182            fee: config.get_inclusion_fee()?,
183            seq_num: SequenceNumber(sequence + 1),
184            cond: Preconditions::None,
185            memo: Memo::None,
186            operations: vec![Operation {
187                source_account: None,
188                body: OperationBody::RestoreFootprint(RestoreFootprintOp {
189                    ext: ExtensionPoint::V0,
190                }),
191            }]
192            .try_into()?,
193            ext: TransactionExt::V1(SorobanTransactionData {
194                ext: SorobanTransactionDataExt::V0,
195                resources: SorobanResources {
196                    footprint: LedgerFootprint {
197                        read_only: vec![].try_into()?,
198                        read_write: entry_keys.clone().try_into()?,
199                    },
200                    instructions: self.resources.instructions.unwrap_or_default(),
201                    disk_read_bytes: 0,
202                    write_bytes: 0,
203                },
204                resource_fee: 0,
205            }),
206        });
207        if self.build_only {
208            return Ok(TxnResult::Txn(tx));
209        }
210
211        let res = sim_sign_and_send_tx::<Error>(
212            &client,
213            &tx,
214            config,
215            &self.resources,
216            &[],
217            // Footprint restore is not an InvokeHostFunction op, so the RPC does
218            // not accept an auth mode.
219            None,
220            quiet,
221            no_cache,
222        )
223        .await?;
224
225        let meta = res
226            .result_meta
227            .as_ref()
228            .ok_or(Error::MissingOperationResult)?;
229
230        tracing::trace!(?meta);
231
232        let events = extract_events(meta);
233
234        crate::log::event::all(&events);
235        crate::log::event::contract(&events, &print);
236
237        // The transaction from core will succeed regardless of whether it actually found &
238        // restored the entry, so we have to inspect the result meta to tell if it worked or not.
239        let changes = match meta {
240            TransactionMeta::V4(TransactionMetaV4 { operations, .. }) => {
241                // Simply check if there is exactly one entry here. We only support restoring a single
242                // entry via this command (which we should fix separately, but).
243                if operations.is_empty() {
244                    return Err(Error::LedgerEntryNotFound);
245                }
246
247                operations[0].changes.clone()
248            }
249            TransactionMeta::V3(TransactionMetaV3 { operations, .. }) => {
250                // Simply check if there is exactly one entry here. We only support restoring a single
251                // entry via this command (which we should fix separately, but).
252                if operations.is_empty() {
253                    return Err(Error::LedgerEntryNotFound);
254                }
255
256                operations[0].changes.clone()
257            }
258            _ => return Err(Error::LedgerEntryNotFound),
259        };
260        tracing::debug!("Changes:\nlen:{}\n{changes:#?}", changes.len());
261
262        if changes.is_empty() {
263            print.infoln("No changes detected, transaction was a no-op.");
264            let entry = client.get_full_ledger_entries(&entry_keys).await?;
265            let extension = entry.entries[0].live_until_ledger_seq.unwrap_or_default();
266
267            return Ok(TxnResult::Res(extension));
268        }
269
270        Ok(TxnResult::Res(
271            parse_changes(&changes.to_vec()).ok_or(Error::LedgerEntryNotFound)?,
272        ))
273    }
274}
275
276fn parse_changes(changes: &[LedgerEntryChange]) -> Option<u32> {
277    changes
278        .iter()
279        .filter_map(|change| match change {
280            LedgerEntryChange::Restored(LedgerEntry {
281                data:
282                    LedgerEntryData::Ttl(TtlEntry {
283                        live_until_ledger_seq,
284                        ..
285                    }),
286                ..
287            })
288            | LedgerEntryChange::Updated(LedgerEntry {
289                data:
290                    LedgerEntryData::Ttl(TtlEntry {
291                        live_until_ledger_seq,
292                        ..
293                    }),
294                ..
295            })
296            | LedgerEntryChange::Created(LedgerEntry {
297                data:
298                    LedgerEntryData::Ttl(TtlEntry {
299                        live_until_ledger_seq,
300                        ..
301                    }),
302                ..
303            }) => Some(*live_until_ledger_seq),
304            _ => None,
305        })
306        .max()
307}
308
309#[cfg(test)]
310mod tests {
311    use super::*;
312    use crate::xdr::{
313        ContractDataDurability::Persistent, ContractDataEntry, ContractId, Hash, LedgerEntry,
314        LedgerEntryChange, LedgerEntryData, ScAddress, ScSymbol, ScVal, SequenceNumber, StringM,
315        TtlEntry,
316    };
317
318    #[test]
319    fn test_parse_changes_two_changes_restored() {
320        // Test the original expected format with 2 changes
321        let ttl_entry = TtlEntry {
322            live_until_ledger_seq: 12345,
323            key_hash: Hash([0; 32]),
324        };
325
326        let changes = vec![
327            LedgerEntryChange::State(LedgerEntry {
328                data: LedgerEntryData::Ttl(ttl_entry.clone()),
329                last_modified_ledger_seq: 0,
330                ext: crate::xdr::LedgerEntryExt::V0,
331            }),
332            LedgerEntryChange::Restored(LedgerEntry {
333                data: LedgerEntryData::Ttl(ttl_entry),
334                last_modified_ledger_seq: 0,
335                ext: crate::xdr::LedgerEntryExt::V0,
336            }),
337        ];
338
339        let result = parse_changes(&changes);
340        assert_eq!(result, Some(12345));
341    }
342
343    #[test]
344    fn test_parse_two_changes_that_had_expired() {
345        let ttl_entry = TtlEntry {
346            live_until_ledger_seq: 55555,
347            key_hash: Hash([0; 32]),
348        };
349
350        let counter = "COUNTER".parse::<StringM<32>>().unwrap();
351        let contract_data_entry = ContractDataEntry {
352            ext: ExtensionPoint::default(),
353            contract: ScAddress::Contract(ContractId(Hash([0; 32]))),
354            key: ScVal::Symbol(ScSymbol(counter)),
355            durability: Persistent,
356            val: ScVal::U32(1),
357        };
358
359        let changes = vec![
360            LedgerEntryChange::Restored(LedgerEntry {
361                data: LedgerEntryData::Ttl(ttl_entry.clone()),
362                last_modified_ledger_seq: 37429,
363                ext: crate::xdr::LedgerEntryExt::V0,
364            }),
365            LedgerEntryChange::Restored(LedgerEntry {
366                data: LedgerEntryData::ContractData(contract_data_entry.clone()),
367                last_modified_ledger_seq: 37429,
368                ext: crate::xdr::LedgerEntryExt::V0,
369            }),
370        ];
371
372        let result = parse_changes(&changes);
373        assert_eq!(result, Some(55555));
374    }
375
376    #[test]
377    fn test_parse_changes_two_changes_updated() {
378        // Test the original expected format with 2 changes, but second change is Updated
379        let ttl_entry = TtlEntry {
380            live_until_ledger_seq: 67890,
381            key_hash: Hash([0; 32]),
382        };
383
384        let changes = vec![
385            LedgerEntryChange::State(LedgerEntry {
386                data: LedgerEntryData::Ttl(ttl_entry.clone()),
387                last_modified_ledger_seq: 0,
388                ext: crate::xdr::LedgerEntryExt::V0,
389            }),
390            LedgerEntryChange::Updated(LedgerEntry {
391                data: LedgerEntryData::Ttl(ttl_entry),
392                last_modified_ledger_seq: 0,
393                ext: crate::xdr::LedgerEntryExt::V0,
394            }),
395        ];
396
397        let result = parse_changes(&changes);
398        assert_eq!(result, Some(67890));
399    }
400
401    #[test]
402    fn test_parse_changes_two_changes_created() {
403        // Test the original expected format with 2 changes, but second change is Created
404        let ttl_entry = TtlEntry {
405            live_until_ledger_seq: 11111,
406            key_hash: Hash([0; 32]),
407        };
408
409        let changes = vec![
410            LedgerEntryChange::State(LedgerEntry {
411                data: LedgerEntryData::Ttl(ttl_entry.clone()),
412                last_modified_ledger_seq: 0,
413                ext: crate::xdr::LedgerEntryExt::V0,
414            }),
415            LedgerEntryChange::Created(LedgerEntry {
416                data: LedgerEntryData::Ttl(ttl_entry),
417                last_modified_ledger_seq: 0,
418                ext: crate::xdr::LedgerEntryExt::V0,
419            }),
420        ];
421
422        let result = parse_changes(&changes);
423        assert_eq!(result, Some(11111));
424    }
425
426    #[test]
427    fn test_parse_changes_single_change_restored() {
428        // Test the new single change format with Restored type
429        let ttl_entry = TtlEntry {
430            live_until_ledger_seq: 22222,
431            key_hash: Hash([0; 32]),
432        };
433
434        let changes = vec![LedgerEntryChange::Restored(LedgerEntry {
435            data: LedgerEntryData::Ttl(ttl_entry),
436            last_modified_ledger_seq: 0,
437            ext: crate::xdr::LedgerEntryExt::V0,
438        })];
439
440        let result = parse_changes(&changes);
441        assert_eq!(result, Some(22222));
442    }
443
444    #[test]
445    fn test_parse_changes_single_change_updated() {
446        // Test the new single change format with Updated type
447        let ttl_entry = TtlEntry {
448            live_until_ledger_seq: 33333,
449            key_hash: Hash([0; 32]),
450        };
451
452        let changes = vec![LedgerEntryChange::Updated(LedgerEntry {
453            data: LedgerEntryData::Ttl(ttl_entry),
454            last_modified_ledger_seq: 0,
455            ext: crate::xdr::LedgerEntryExt::V0,
456        })];
457
458        let result = parse_changes(&changes);
459        assert_eq!(result, Some(33333));
460    }
461
462    #[test]
463    fn test_parse_changes_single_change_created() {
464        // Test the new single change format with Created type
465        let ttl_entry = TtlEntry {
466            live_until_ledger_seq: 44444,
467            key_hash: Hash([0; 32]),
468        };
469
470        let changes = vec![LedgerEntryChange::Created(LedgerEntry {
471            data: LedgerEntryData::Ttl(ttl_entry),
472            last_modified_ledger_seq: 0,
473            ext: crate::xdr::LedgerEntryExt::V0,
474        })];
475
476        let result = parse_changes(&changes);
477        assert_eq!(result, Some(44444));
478    }
479
480    #[test]
481    fn test_parse_changes_invalid_two_changes() {
482        // Test invalid 2-change format (not TTL data)
483        let not_ttl_change = LedgerEntryChange::Restored(LedgerEntry {
484            data: LedgerEntryData::Account(crate::xdr::AccountEntry {
485                account_id: crate::xdr::AccountId(crate::xdr::PublicKey::PublicKeyTypeEd25519(
486                    crate::xdr::Uint256([0; 32]),
487                )),
488                balance: 0,
489                seq_num: SequenceNumber(0),
490                num_sub_entries: 0,
491                inflation_dest: None,
492                flags: 0,
493                home_domain: crate::xdr::String32::default(),
494                thresholds: crate::xdr::Thresholds::default(),
495                signers: crate::xdr::VecM::default(),
496                ext: crate::xdr::AccountEntryExt::V0,
497            }),
498            last_modified_ledger_seq: 0,
499            ext: crate::xdr::LedgerEntryExt::V0,
500        });
501
502        let changes = vec![not_ttl_change.clone(), not_ttl_change];
503        let result = parse_changes(&changes);
504        assert_eq!(result, None);
505    }
506
507    #[test]
508    fn test_parse_changes_invalid_single_change() {
509        // Test invalid single change format (not TTL data)
510        let changes = vec![LedgerEntryChange::Restored(LedgerEntry {
511            data: LedgerEntryData::Account(crate::xdr::AccountEntry {
512                account_id: crate::xdr::AccountId(crate::xdr::PublicKey::PublicKeyTypeEd25519(
513                    crate::xdr::Uint256([0; 32]),
514                )),
515                balance: 0,
516                seq_num: SequenceNumber(0),
517                num_sub_entries: 0,
518                inflation_dest: None,
519                flags: 0,
520                home_domain: crate::xdr::String32::default(),
521                thresholds: crate::xdr::Thresholds::default(),
522                signers: crate::xdr::VecM::default(),
523                ext: crate::xdr::AccountEntryExt::V0,
524            }),
525            last_modified_ledger_seq: 0,
526            ext: crate::xdr::LedgerEntryExt::V0,
527        })];
528
529        let result = parse_changes(&changes);
530        assert_eq!(result, None);
531    }
532
533    #[test]
534    fn test_parse_changes_empty_changes() {
535        // Test empty changes array
536        let changes = vec![];
537
538        let result = parse_changes(&changes);
539        assert_eq!(result, None);
540    }
541}