soroban_sdk/
testutils.rs

1#![cfg(any(test, feature = "testutils"))]
2#![cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
3
4//! Utilities intended for use when testing.
5
6pub mod arbitrary;
7
8mod sign;
9use std::rc::Rc;
10
11pub use sign::ed25519;
12
13mod mock_auth;
14pub use mock_auth::{
15    AuthorizedFunction, AuthorizedInvocation, MockAuth, MockAuthContract, MockAuthInvoke,
16};
17use soroban_env_host::TryIntoVal;
18
19pub mod storage;
20
21pub mod cost_estimate;
22
23use crate::{xdr, ConstructorArgs, Env, Val, Vec};
24use soroban_ledger_snapshot::LedgerSnapshot;
25
26pub use crate::env::EnvTestConfig;
27
28pub trait Register {
29    fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address
30    where
31        I: Into<Option<&'i crate::Address>>,
32        A: ConstructorArgs;
33}
34
35impl<C> Register for C
36where
37    C: ContractFunctionSet + 'static,
38{
39    fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address
40    where
41        I: Into<Option<&'i crate::Address>>,
42        A: ConstructorArgs,
43    {
44        env.register_contract_with_constructor(id, self, args)
45    }
46}
47
48impl<'w> Register for &'w [u8] {
49    fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address
50    where
51        I: Into<Option<&'i crate::Address>>,
52        A: ConstructorArgs,
53    {
54        env.register_contract_wasm_with_constructor(id, self, args)
55    }
56}
57
58#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
59#[serde(rename_all = "snake_case")]
60pub struct Snapshot {
61    pub generators: Generators,
62    pub auth: AuthSnapshot,
63    pub ledger: LedgerSnapshot,
64    pub events: EventsSnapshot,
65}
66
67impl Snapshot {
68    // Read in a [`Snapshot`] from a reader.
69    pub fn read(r: impl std::io::Read) -> Result<Snapshot, std::io::Error> {
70        Ok(serde_json::from_reader::<_, Snapshot>(r)?)
71    }
72
73    // Read in a [`Snapshot`] from a file.
74    pub fn read_file(p: impl AsRef<std::path::Path>) -> Result<Snapshot, std::io::Error> {
75        Self::read(std::fs::File::open(p)?)
76    }
77
78    // Write a [`Snapshot`] to a writer.
79    pub fn write(&self, w: impl std::io::Write) -> Result<(), std::io::Error> {
80        Ok(serde_json::to_writer_pretty(w, self)?)
81    }
82
83    // Write a [`Snapshot`] to file.
84    pub fn write_file(&self, p: impl AsRef<std::path::Path>) -> Result<(), std::io::Error> {
85        let p = p.as_ref();
86        if let Some(dir) = p.parent() {
87            if !dir.exists() {
88                std::fs::create_dir_all(dir)?;
89            }
90        }
91        self.write(std::fs::File::create(p)?)
92    }
93}
94
95#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
96#[serde(rename_all = "snake_case")]
97pub struct EventsSnapshot(pub std::vec::Vec<EventSnapshot>);
98
99impl EventsSnapshot {
100    // Read in a [`EventsSnapshot`] from a reader.
101    pub fn read(r: impl std::io::Read) -> Result<EventsSnapshot, std::io::Error> {
102        Ok(serde_json::from_reader::<_, EventsSnapshot>(r)?)
103    }
104
105    // Read in a [`EventsSnapshot`] from a file.
106    pub fn read_file(p: impl AsRef<std::path::Path>) -> Result<EventsSnapshot, std::io::Error> {
107        Self::read(std::fs::File::open(p)?)
108    }
109
110    // Write a [`EventsSnapshot`] to a writer.
111    pub fn write(&self, w: impl std::io::Write) -> Result<(), std::io::Error> {
112        Ok(serde_json::to_writer_pretty(w, self)?)
113    }
114
115    // Write a [`EventsSnapshot`] to file.
116    pub fn write_file(&self, p: impl AsRef<std::path::Path>) -> Result<(), std::io::Error> {
117        let p = p.as_ref();
118        if let Some(dir) = p.parent() {
119            if !dir.exists() {
120                std::fs::create_dir_all(dir)?;
121            }
122        }
123        self.write(std::fs::File::create(p)?)
124    }
125}
126
127#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
128#[serde(rename_all = "snake_case")]
129pub struct EventSnapshot {
130    pub event: xdr::ContractEvent,
131    pub failed_call: bool,
132}
133
134impl From<crate::env::internal::events::HostEvent> for EventSnapshot {
135    fn from(v: crate::env::internal::events::HostEvent) -> Self {
136        Self {
137            event: v.event,
138            failed_call: v.failed_call,
139        }
140    }
141}
142
143#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
144#[serde(rename_all = "snake_case")]
145pub struct AuthSnapshot(
146    pub std::vec::Vec<std::vec::Vec<(xdr::ScAddress, xdr::SorobanAuthorizedInvocation)>>,
147);
148
149impl AuthSnapshot {
150    // Read in a [`AuthSnapshot`] from a reader.
151    pub fn read(r: impl std::io::Read) -> Result<AuthSnapshot, std::io::Error> {
152        Ok(serde_json::from_reader::<_, AuthSnapshot>(r)?)
153    }
154
155    // Read in a [`AuthSnapshot`] from a file.
156    pub fn read_file(p: impl AsRef<std::path::Path>) -> Result<AuthSnapshot, std::io::Error> {
157        Self::read(std::fs::File::open(p)?)
158    }
159
160    // Write a [`AuthSnapshot`] to a writer.
161    pub fn write(&self, w: impl std::io::Write) -> Result<(), std::io::Error> {
162        Ok(serde_json::to_writer_pretty(w, self)?)
163    }
164
165    // Write a [`AuthSnapshot`] to file.
166    pub fn write_file(&self, p: impl AsRef<std::path::Path>) -> Result<(), std::io::Error> {
167        let p = p.as_ref();
168        if let Some(dir) = p.parent() {
169            if !dir.exists() {
170                std::fs::create_dir_all(dir)?;
171            }
172        }
173        self.write(std::fs::File::create(p)?)
174    }
175}
176
177#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
178#[serde(rename_all = "snake_case")]
179pub struct Generators {
180    address: u64,
181    nonce: u64,
182}
183
184impl Default for Generators {
185    fn default() -> Generators {
186        Generators {
187            address: 0,
188            nonce: 0,
189        }
190    }
191}
192
193impl Generators {
194    // Read in a [`Generators`] from a reader.
195    pub fn read(r: impl std::io::Read) -> Result<Generators, std::io::Error> {
196        Ok(serde_json::from_reader::<_, Generators>(r)?)
197    }
198
199    // Read in a [`Generators`] from a file.
200    pub fn read_file(p: impl AsRef<std::path::Path>) -> Result<Generators, std::io::Error> {
201        Self::read(std::fs::File::open(p)?)
202    }
203
204    // Write a [`Generators`] to a writer.
205    pub fn write(&self, w: impl std::io::Write) -> Result<(), std::io::Error> {
206        Ok(serde_json::to_writer_pretty(w, self)?)
207    }
208
209    // Write a [`Generators`] to file.
210    pub fn write_file(&self, p: impl AsRef<std::path::Path>) -> Result<(), std::io::Error> {
211        let p = p.as_ref();
212        if let Some(dir) = p.parent() {
213            if !dir.exists() {
214                std::fs::create_dir_all(dir)?;
215            }
216        }
217        self.write(std::fs::File::create(p)?)
218    }
219}
220
221impl Generators {
222    pub fn address(&mut self) -> [u8; 32] {
223        self.address = self.address.checked_add(1).unwrap();
224        let b: [u8; 8] = self.address.to_be_bytes();
225        [
226            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b[0], b[1],
227            b[2], b[3], b[4], b[5], b[6], b[7],
228        ]
229    }
230
231    pub fn nonce(&mut self) -> i64 {
232        self.nonce = self.nonce.checked_add(1).unwrap();
233        self.nonce as i64
234    }
235}
236
237#[doc(hidden)]
238pub type ContractFunctionF = dyn Send + Sync + Fn(Env, &[Val]) -> Val;
239#[doc(hidden)]
240pub trait ContractFunctionRegister {
241    fn register(name: &'static str, func: &'static ContractFunctionF);
242}
243#[doc(hidden)]
244pub trait ContractFunctionSet {
245    fn call(&self, func: &str, env: Env, args: &[Val]) -> Option<Val>;
246}
247
248#[doc(inline)]
249pub use crate::env::internal::LedgerInfo;
250
251/// Test utilities for [`Ledger`][crate::ledger::Ledger].
252pub trait Ledger {
253    /// Set ledger info.
254    fn set(&self, l: LedgerInfo);
255
256    /// Sets the protocol version.
257    fn set_protocol_version(&self, protocol_version: u32);
258
259    /// Sets the sequence number.
260    fn set_sequence_number(&self, sequence_number: u32);
261
262    /// Sets the timestamp.
263    fn set_timestamp(&self, timestamp: u64);
264
265    /// Sets the network ID.
266    fn set_network_id(&self, network_id: [u8; 32]);
267
268    /// Sets the base reserve.
269    fn set_base_reserve(&self, base_reserve: u32);
270
271    /// Sets the minimum temporary entry time-to-live.
272    fn set_min_temp_entry_ttl(&self, min_temp_entry_ttl: u32);
273
274    /// Sets the minimum persistent entry time-to-live.
275    fn set_min_persistent_entry_ttl(&self, min_persistent_entry_ttl: u32);
276
277    /// Sets the maximum entry time-to-live.
278    fn set_max_entry_ttl(&self, max_entry_ttl: u32);
279
280    /// Get ledger info.
281    fn get(&self) -> LedgerInfo;
282
283    /// Modify the ledger info.
284    fn with_mut<F>(&self, f: F)
285    where
286        F: FnMut(&mut LedgerInfo);
287}
288
289pub mod budget {
290    use core::fmt::{Debug, Display};
291
292    #[doc(inline)]
293    use crate::env::internal::budget::CostTracker;
294    #[doc(inline)]
295    pub use crate::xdr::ContractCostType;
296
297    /// Budget that tracks the resources consumed for the environment.
298    ///
299    /// The budget consistents of two cost dimensions:
300    ///  - CPU instructions
301    ///  - Memory
302    ///
303    /// Inputs feed into those cost dimensions.
304    ///
305    /// Note that all cost dimensions – CPU instructions, memory – and the VM
306    /// cost type inputs are likely to be underestimated when running Rust code
307    /// compared to running the WASM equivalent.
308    ///
309    /// ### Examples
310    ///
311    /// ```
312    /// use soroban_sdk::{Env, Symbol};
313    ///
314    /// # #[cfg(feature = "testutils")]
315    /// # fn main() {
316    /// #     let env = Env::default();
317    /// env.cost_estimate().budget().reset_default();
318    /// // ...
319    /// println!("{}", env.cost_estimate().budget());
320    /// # }
321    /// # #[cfg(not(feature = "testutils"))]
322    /// # fn main() { }
323    /// ```
324    pub struct Budget(pub(crate) crate::env::internal::budget::Budget);
325
326    impl Display for Budget {
327        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
328            writeln!(f, "{}", self.0)
329        }
330    }
331
332    impl Debug for Budget {
333        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
334            writeln!(f, "{:?}", self.0)
335        }
336    }
337
338    impl Budget {
339        pub(crate) fn new(b: crate::env::internal::budget::Budget) -> Self {
340            Self(b)
341        }
342
343        /// Reset the budget.
344        pub fn reset_default(&mut self) {
345            self.0.reset_default().unwrap();
346        }
347
348        pub fn reset_unlimited(&mut self) {
349            self.0.reset_unlimited().unwrap();
350        }
351
352        pub fn reset_limits(&mut self, cpu: u64, mem: u64) {
353            self.0.reset_limits(cpu, mem).unwrap();
354        }
355
356        pub fn reset_tracker(&mut self) {
357            self.0.reset_tracker().unwrap();
358        }
359
360        /// Returns the CPU instruction cost.
361        ///
362        /// Note that CPU instructions are likely to be underestimated when
363        /// running Rust code compared to running the WASM equivalent.
364        pub fn cpu_instruction_cost(&self) -> u64 {
365            self.0.get_cpu_insns_consumed().unwrap()
366        }
367
368        /// Returns the memory cost.
369        ///
370        /// Note that memory is likely to be underestimated when running Rust
371        /// code compared to running the WASM equivalent.
372        pub fn memory_bytes_cost(&self) -> u64 {
373            self.0.get_mem_bytes_consumed().unwrap()
374        }
375
376        /// Get the cost tracker associated with the cost type. The tracker
377        /// tracks the cumulative iterations and inputs and derived cpu and
378        /// memory. If the underlying model is a constant model, then inputs is
379        /// `None` and only iterations matter.
380        ///
381        /// Note that VM cost types are likely to be underestimated when running
382        /// natively as Rust code inside tests code compared to running the WASM
383        /// equivalent.
384        pub fn tracker(&self, cost_type: ContractCostType) -> CostTracker {
385            self.0.get_tracker(cost_type).unwrap()
386        }
387
388        /// Print the budget costs and inputs to stdout.
389        pub fn print(&self) {
390            println!("{}", self.0);
391        }
392    }
393}
394
395/// Test utilities for [`Events`][crate::events::Events].
396pub trait Events {
397    /// Returns all events that have been published by contracts.
398    ///
399    /// Returns a [`Vec`] of three element tuples containing:
400    /// - Contract ID
401    /// - Event Topics as a [`Vec<Val>`]
402    /// - Event Data as a [`Val`]
403    fn all(&self) -> Vec<(crate::Address, Vec<Val>, Val)>;
404}
405
406/// Test utilities for [`Logs`][crate::logs::Logs].
407pub trait Logs {
408    /// Returns all diagnostic events that have been logged.
409    fn all(&self) -> std::vec::Vec<String>;
410    /// Prints all diagnostic events to stdout.
411    fn print(&self);
412}
413
414/// Test utilities for [`BytesN`][crate::BytesN].
415pub trait BytesN<const N: usize> {
416    // Generate a BytesN filled with random bytes.
417    //
418    // The value filled is not cryptographically secure.
419    fn random(env: &Env) -> crate::BytesN<N>;
420}
421
422/// Generates an array of N random bytes.
423///
424/// The value returned is not cryptographically secure.
425pub(crate) fn random<const N: usize>() -> [u8; N] {
426    use rand::RngCore;
427    let mut arr = [0u8; N];
428    rand::thread_rng().fill_bytes(&mut arr);
429    arr
430}
431
432pub trait Address {
433    /// Generate a new Address.
434    ///
435    /// Implementation note: this always builds the contract addresses now. This
436    /// shouldn't normally matter though, as contracts should be agnostic to
437    /// the underlying Address value.
438    fn generate(env: &Env) -> crate::Address;
439}
440
441pub trait Deployer {
442    /// Gets the TTL of the given contract's instance.
443    ///
444    /// TTL is the number of ledgers left until the instance entry is considered
445    /// expired, excluding the current ledger.
446    ///
447    /// Panics if there is no instance corresponding to the provided address,
448    /// or if the instance has expired.
449    fn get_contract_instance_ttl(&self, contract: &crate::Address) -> u32;
450
451    /// Gets the TTL of the given contract's Wasm code entry.
452    ///
453    /// TTL is the number of ledgers left until the contract code entry
454    /// is considered expired, excluding the current ledger.
455    ///
456    /// Panics if there is no contract instance/code corresponding to
457    /// the provided address, or if the instance/code has expired.
458    fn get_contract_code_ttl(&self, contract: &crate::Address) -> u32;
459}
460
461pub use xdr::AccountFlags as IssuerFlags;
462
463#[derive(Clone)]
464pub struct StellarAssetIssuer {
465    env: Env,
466    account_id: xdr::AccountId,
467}
468
469impl StellarAssetIssuer {
470    pub(crate) fn new(env: Env, account_id: xdr::AccountId) -> Self {
471        Self { env, account_id }
472    }
473
474    /// Returns the flags for the issuer.
475    pub fn flags(&self) -> u32 {
476        self.env
477            .host()
478            .with_mut_storage(|storage| {
479                let k = Rc::new(xdr::LedgerKey::Account(xdr::LedgerKeyAccount {
480                    account_id: self.account_id.clone(),
481                }));
482
483                let entry = storage.get(
484                    &k,
485                    soroban_env_host::budget::AsBudget::as_budget(self.env.host()),
486                )?;
487
488                match entry.data {
489                    xdr::LedgerEntryData::Account(ref e) => Ok(e.flags.clone()),
490                    _ => panic!("expected account entry but got {:?}", entry.data),
491                }
492            })
493            .unwrap()
494    }
495
496    /// Adds the flag specified to the existing issuer flags
497    pub fn set_flag(&self, flag: IssuerFlags) {
498        self.overwrite_issuer_flags(self.flags() | (flag as u32))
499    }
500
501    /// Clears the flag specified from the existing issuer flags
502    pub fn clear_flag(&self, flag: IssuerFlags) {
503        self.overwrite_issuer_flags(self.flags() & (!(flag as u32)))
504    }
505
506    pub fn address(&self) -> crate::Address {
507        xdr::ScAddress::Account(self.account_id.clone())
508            .try_into_val(&self.env.clone())
509            .unwrap()
510    }
511
512    /// Sets the issuer flags field.
513    /// Each flag is a bit with values corresponding to [xdr::AccountFlags]
514    ///
515    /// Use this to test interactions between trustlines/balances and the issuer flags.
516    fn overwrite_issuer_flags(&self, flags: u32) {
517        if u64::from(flags) > xdr::MASK_ACCOUNT_FLAGS_V17 {
518            panic!(
519                "issuer flags value must be at most {}",
520                xdr::MASK_ACCOUNT_FLAGS_V17
521            );
522        }
523
524        self.env
525            .host()
526            .with_mut_storage(|storage| {
527                let k = Rc::new(xdr::LedgerKey::Account(xdr::LedgerKeyAccount {
528                    account_id: self.account_id.clone(),
529                }));
530
531                let mut entry = storage
532                    .get(
533                        &k,
534                        soroban_env_host::budget::AsBudget::as_budget(self.env.host()),
535                    )?
536                    .as_ref()
537                    .clone();
538
539                match entry.data {
540                    xdr::LedgerEntryData::Account(ref mut e) => e.flags = flags,
541                    _ => panic!("expected account entry but got {:?}", entry.data),
542                }
543
544                storage.put(
545                    &k,
546                    &Rc::new(entry),
547                    None,
548                    soroban_env_host::budget::AsBudget::as_budget(self.env.host()),
549                )?;
550                Ok(())
551            })
552            .unwrap();
553    }
554}
555
556pub struct StellarAssetContract {
557    address: crate::Address,
558    issuer: StellarAssetIssuer,
559}
560
561impl StellarAssetContract {
562    pub(crate) fn new(address: crate::Address, issuer: StellarAssetIssuer) -> Self {
563        Self { address, issuer }
564    }
565
566    pub fn address(&self) -> crate::Address {
567        self.address.clone()
568    }
569
570    pub fn issuer(&self) -> StellarAssetIssuer {
571        self.issuer.clone()
572    }
573}