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: i64,
182    mux_id: u64,
183}
184
185impl Default for Generators {
186    fn default() -> Generators {
187        Generators {
188            address: 0,
189            nonce: 0,
190            mux_id: 0,
191        }
192    }
193}
194
195impl Generators {
196    // Read in a [`Generators`] from a reader.
197    pub fn read(r: impl std::io::Read) -> Result<Generators, std::io::Error> {
198        Ok(serde_json::from_reader::<_, Generators>(r)?)
199    }
200
201    // Read in a [`Generators`] from a file.
202    pub fn read_file(p: impl AsRef<std::path::Path>) -> Result<Generators, std::io::Error> {
203        Self::read(std::fs::File::open(p)?)
204    }
205
206    // Write a [`Generators`] to a writer.
207    pub fn write(&self, w: impl std::io::Write) -> Result<(), std::io::Error> {
208        Ok(serde_json::to_writer_pretty(w, self)?)
209    }
210
211    // Write a [`Generators`] to file.
212    pub fn write_file(&self, p: impl AsRef<std::path::Path>) -> Result<(), std::io::Error> {
213        let p = p.as_ref();
214        if let Some(dir) = p.parent() {
215            if !dir.exists() {
216                std::fs::create_dir_all(dir)?;
217            }
218        }
219        self.write(std::fs::File::create(p)?)
220    }
221}
222
223impl Generators {
224    pub fn address(&mut self) -> [u8; 32] {
225        self.address = self.address.checked_add(1).unwrap();
226        let b: [u8; 8] = self.address.to_be_bytes();
227        [
228            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],
229            b[2], b[3], b[4], b[5], b[6], b[7],
230        ]
231    }
232
233    pub fn nonce(&mut self) -> i64 {
234        self.nonce = self.nonce.checked_add(1).unwrap();
235        self.nonce
236    }
237
238    pub fn mux_id(&mut self) -> u64 {
239        self.mux_id = self.mux_id.checked_add(1).unwrap();
240        self.mux_id
241    }
242}
243
244#[doc(hidden)]
245pub type ContractFunctionF = dyn Send + Sync + Fn(Env, &[Val]) -> Val;
246#[doc(hidden)]
247pub trait ContractFunctionRegister {
248    fn register(name: &'static str, func: &'static ContractFunctionF);
249}
250#[doc(hidden)]
251pub trait ContractFunctionSet {
252    fn call(&self, func: &str, env: Env, args: &[Val]) -> Option<Val>;
253}
254
255#[doc(inline)]
256pub use crate::env::internal::LedgerInfo;
257
258/// Test utilities for [`Ledger`][crate::ledger::Ledger].
259pub trait Ledger {
260    /// Set ledger info.
261    fn set(&self, l: LedgerInfo);
262
263    /// Sets the protocol version.
264    fn set_protocol_version(&self, protocol_version: u32);
265
266    /// Sets the sequence number.
267    fn set_sequence_number(&self, sequence_number: u32);
268
269    /// Sets the timestamp.
270    fn set_timestamp(&self, timestamp: u64);
271
272    /// Sets the network ID.
273    fn set_network_id(&self, network_id: [u8; 32]);
274
275    /// Sets the base reserve.
276    fn set_base_reserve(&self, base_reserve: u32);
277
278    /// Sets the minimum temporary entry time-to-live.
279    fn set_min_temp_entry_ttl(&self, min_temp_entry_ttl: u32);
280
281    /// Sets the minimum persistent entry time-to-live.
282    fn set_min_persistent_entry_ttl(&self, min_persistent_entry_ttl: u32);
283
284    /// Sets the maximum entry time-to-live.
285    fn set_max_entry_ttl(&self, max_entry_ttl: u32);
286
287    /// Get ledger info.
288    fn get(&self) -> LedgerInfo;
289
290    /// Modify the ledger info.
291    fn with_mut<F>(&self, f: F)
292    where
293        F: FnMut(&mut LedgerInfo);
294}
295
296pub mod budget {
297    use core::fmt::{Debug, Display};
298
299    #[doc(inline)]
300    use crate::env::internal::budget::CostTracker;
301    #[doc(inline)]
302    pub use crate::xdr::ContractCostType;
303
304    /// Budget that tracks the resources consumed for the environment.
305    ///
306    /// The budget consistents of two cost dimensions:
307    ///  - CPU instructions
308    ///  - Memory
309    ///
310    /// Inputs feed into those cost dimensions.
311    ///
312    /// Note that all cost dimensions – CPU instructions, memory – and the VM
313    /// cost type inputs are likely to be underestimated when running Rust code
314    /// compared to running the WASM equivalent.
315    ///
316    /// ### Examples
317    ///
318    /// ```
319    /// use soroban_sdk::{Env, Symbol};
320    ///
321    /// # #[cfg(feature = "testutils")]
322    /// # fn main() {
323    /// #     let env = Env::default();
324    /// env.cost_estimate().budget().reset_default();
325    /// // ...
326    /// println!("{}", env.cost_estimate().budget());
327    /// # }
328    /// # #[cfg(not(feature = "testutils"))]
329    /// # fn main() { }
330    /// ```
331    pub struct Budget(pub(crate) crate::env::internal::budget::Budget);
332
333    impl Display for Budget {
334        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
335            writeln!(f, "{}", self.0)
336        }
337    }
338
339    impl Debug for Budget {
340        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
341            writeln!(f, "{:?}", self.0)
342        }
343    }
344
345    impl Budget {
346        pub(crate) fn new(b: crate::env::internal::budget::Budget) -> Self {
347            Self(b)
348        }
349
350        /// Reset the budget.
351        pub fn reset_default(&mut self) {
352            self.0.reset_default().unwrap();
353        }
354
355        pub fn reset_unlimited(&mut self) {
356            self.0.reset_unlimited().unwrap();
357        }
358
359        pub fn reset_limits(&mut self, cpu: u64, mem: u64) {
360            self.0.reset_limits(cpu, mem).unwrap();
361        }
362
363        pub fn reset_tracker(&mut self) {
364            self.0.reset_tracker().unwrap();
365        }
366
367        /// Returns the CPU instruction cost.
368        ///
369        /// Note that CPU instructions are likely to be underestimated when
370        /// running Rust code compared to running the WASM equivalent.
371        pub fn cpu_instruction_cost(&self) -> u64 {
372            self.0.get_cpu_insns_consumed().unwrap()
373        }
374
375        /// Returns the memory cost.
376        ///
377        /// Note that memory is likely to be underestimated when running Rust
378        /// code compared to running the WASM equivalent.
379        pub fn memory_bytes_cost(&self) -> u64 {
380            self.0.get_mem_bytes_consumed().unwrap()
381        }
382
383        /// Get the cost tracker associated with the cost type. The tracker
384        /// tracks the cumulative iterations and inputs and derived cpu and
385        /// memory. If the underlying model is a constant model, then inputs is
386        /// `None` and only iterations matter.
387        ///
388        /// Note that VM cost types are likely to be underestimated when running
389        /// natively as Rust code inside tests code compared to running the WASM
390        /// equivalent.
391        pub fn tracker(&self, cost_type: ContractCostType) -> CostTracker {
392            self.0.get_tracker(cost_type).unwrap()
393        }
394
395        /// Print the budget costs and inputs to stdout.
396        pub fn print(&self) {
397            println!("{}", self.0);
398        }
399    }
400}
401
402/// Test utilities for [`Events`][crate::events::Events].
403pub trait Events {
404    /// Returns all events that have been published by contracts.
405    ///
406    /// Returns a [`Vec`] of three element tuples containing:
407    /// - Contract ID
408    /// - Event Topics as a [`Vec<Val>`]
409    /// - Event Data as a [`Val`]
410    fn all(&self) -> Vec<(crate::Address, Vec<Val>, Val)>;
411}
412
413/// Test utilities for [`Logs`][crate::logs::Logs].
414pub trait Logs {
415    /// Returns all diagnostic events that have been logged.
416    fn all(&self) -> std::vec::Vec<String>;
417    /// Prints all diagnostic events to stdout.
418    fn print(&self);
419}
420
421/// Test utilities for [`BytesN`][crate::BytesN].
422pub trait BytesN<const N: usize> {
423    // Generate a BytesN filled with random bytes.
424    //
425    // The value filled is not cryptographically secure.
426    fn random(env: &Env) -> crate::BytesN<N>;
427}
428
429/// Generates an array of N random bytes.
430///
431/// The value returned is not cryptographically secure.
432pub(crate) fn random<const N: usize>() -> [u8; N] {
433    use rand::RngCore;
434    let mut arr = [0u8; N];
435    rand::thread_rng().fill_bytes(&mut arr);
436    arr
437}
438
439pub trait Address {
440    /// Generate a new Address.
441    ///
442    /// Implementation note: this always builds the contract addresses now. This
443    /// shouldn't normally matter though, as contracts should be agnostic to
444    /// the underlying Address value.
445    fn generate(env: &Env) -> crate::Address;
446}
447
448pub trait MuxedAddress {
449    /// Create a new MuxedAddress with arbitrary `Address` and id parts.
450    ///
451    /// Note, that since currently only accounts can be multiplexed, the
452    /// underlying `Address` will be an account (not contract) address.
453    fn generate(env: &Env) -> crate::MuxedAddress;
454
455    /// Returns a new `MuxedAddress` that has the same `Address` part as the
456    /// provided `address` and the provided multiplexing id.
457    ///
458    /// `address` can be either an `Address` or `MuxedAddress` and it has to
459    /// be an account (non-contract) address.
460    ///
461    /// Note on usage: the simplest way to test `MuxedAddress` is to generate
462    /// an arbitrary valid address with `MuxedAddress::generate`, then
463    /// `MuxedAddress::new` can be used to alter only the multiplexing id part
464    /// of that address.
465    fn new<T: Into<crate::MuxedAddress>>(address: T, id: u64) -> crate::MuxedAddress;
466}
467
468pub trait Deployer {
469    /// Gets the TTL of the given contract's instance.
470    ///
471    /// TTL is the number of ledgers left until the instance entry is considered
472    /// expired, excluding the current ledger.
473    ///
474    /// Panics if there is no instance corresponding to the provided address,
475    /// or if the instance has expired.
476    fn get_contract_instance_ttl(&self, contract: &crate::Address) -> u32;
477
478    /// Gets the TTL of the given contract's Wasm code entry.
479    ///
480    /// TTL is the number of ledgers left until the contract code entry
481    /// is considered expired, excluding the current ledger.
482    ///
483    /// Panics if there is no contract instance/code corresponding to
484    /// the provided address, or if the instance/code has expired.
485    fn get_contract_code_ttl(&self, contract: &crate::Address) -> u32;
486}
487
488pub use xdr::AccountFlags as IssuerFlags;
489
490#[derive(Clone)]
491pub struct StellarAssetIssuer {
492    env: Env,
493    account_id: xdr::AccountId,
494}
495
496impl StellarAssetIssuer {
497    pub(crate) fn new(env: Env, account_id: xdr::AccountId) -> Self {
498        Self { env, account_id }
499    }
500
501    /// Returns the flags for the issuer.
502    pub fn flags(&self) -> u32 {
503        let k = Rc::new(xdr::LedgerKey::Account(xdr::LedgerKeyAccount {
504            account_id: self.account_id.clone(),
505        }));
506
507        let (entry, _) = self.env.host().get_ledger_entry(&k).unwrap().unwrap();
508
509        match &entry.data {
510            xdr::LedgerEntryData::Account(e) => e.flags,
511            _ => panic!("expected account entry but got {:?}", entry.data),
512        }
513    }
514
515    /// Adds the flag specified to the existing issuer flags
516    pub fn set_flag(&self, flag: IssuerFlags) {
517        self.overwrite_issuer_flags(self.flags() | (flag as u32))
518    }
519
520    /// Clears the flag specified from the existing issuer flags
521    pub fn clear_flag(&self, flag: IssuerFlags) {
522        self.overwrite_issuer_flags(self.flags() & (!(flag as u32)))
523    }
524
525    pub fn address(&self) -> crate::Address {
526        xdr::ScAddress::Account(self.account_id.clone())
527            .try_into_val(&self.env.clone())
528            .unwrap()
529    }
530
531    /// Sets the issuer flags field.
532    /// Each flag is a bit with values corresponding to [xdr::AccountFlags]
533    ///
534    /// Use this to test interactions between trustlines/balances and the issuer flags.
535    fn overwrite_issuer_flags(&self, flags: u32) {
536        if u64::from(flags) > xdr::MASK_ACCOUNT_FLAGS_V17 {
537            panic!(
538                "issuer flags value must be at most {}",
539                xdr::MASK_ACCOUNT_FLAGS_V17
540            );
541        }
542
543        let k = Rc::new(xdr::LedgerKey::Account(xdr::LedgerKeyAccount {
544            account_id: self.account_id.clone(),
545        }));
546
547        let (entry, _) = self.env.host().get_ledger_entry(&k).unwrap().unwrap();
548        let mut entry = entry.as_ref().clone();
549
550        match entry.data {
551            xdr::LedgerEntryData::Account(ref mut e) => e.flags = flags,
552            _ => panic!("expected account entry but got {:?}", entry.data),
553        }
554
555        self.env
556            .host()
557            .add_ledger_entry(&k, &Rc::new(entry), None)
558            .unwrap();
559    }
560}
561
562pub struct StellarAssetContract {
563    address: crate::Address,
564    issuer: StellarAssetIssuer,
565    asset: xdr::Asset,
566}
567
568impl StellarAssetContract {
569    pub(crate) fn new(
570        address: crate::Address,
571        issuer: StellarAssetIssuer,
572        asset: xdr::Asset,
573    ) -> Self {
574        Self {
575            address,
576            issuer,
577            asset,
578        }
579    }
580
581    pub fn address(&self) -> crate::Address {
582        self.address.clone()
583    }
584
585    pub fn issuer(&self) -> StellarAssetIssuer {
586        self.issuer.clone()
587    }
588
589    #[doc(hidden)]
590    pub fn asset(&self) -> xdr::Asset {
591        self.asset.clone()
592    }
593}