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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
use crate::wasm_emulation::api::RealApi;
use crate::wasm_emulation::input::ReplyArgs;
use crate::wasm_emulation::instance::instance_from_reused_module;
use crate::wasm_emulation::output::StorageChanges;
use crate::wasm_emulation::query::MockQuerier;
use crate::wasm_emulation::storage::DualStorage;
use cosmwasm_std::Addr;
use cosmwasm_std::Checksum;
use cosmwasm_std::CustomMsg;
use cosmwasm_std::StdError;
use cosmwasm_vm::{
    call_execute, call_instantiate, call_migrate, call_query, call_reply, call_sudo, Backend,
    BackendApi, Instance, InstanceOptions, Querier,
};
use cw_orch::daemon::queriers::CosmWasm;

use cosmwasm_std::Order;
use cosmwasm_std::Storage;

use serde::de::DeserializeOwned;
use wasmer::Engine;
use wasmer::Module;

use crate::wasm_emulation::input::InstanceArguments;
use crate::wasm_emulation::output::WasmRunnerOutput;

use cosmwasm_vm::internals::check_wasm;
use std::collections::HashSet;

use crate::Contract;

use cosmwasm_std::{Binary, CustomQuery, Deps, DepsMut, Env, MessageInfo, Reply, Response};

use anyhow::Result as AnyResult;

use super::channel::RemoteChannel;
use super::input::ExecuteArgs;
use super::input::InstantiateArgs;
use super::input::MigrateArgs;
use super::input::QueryArgs;
use super::input::SudoArgs;
use super::input::WasmFunction;
use super::instance::create_module;
use super::output::WasmOutput;
use super::query::mock_querier::ForkState;

fn apply_storage_changes<ExecC>(storage: &mut dyn Storage, output: &WasmRunnerOutput<ExecC>) {
    // We change all the values with the output
    for (key, value) in &output.storage.current_keys {
        storage.set(key, value);
    }

    // We remove all values that need to be removed from it
    for key in &output.storage.removed_keys {
        storage.remove(key);
    }
}

/// Taken from cosmwasm_vm::testing
/// This gas limit is used in integration tests and should be high enough to allow a reasonable
/// number of contract executions and queries on one instance. For this reason it is significatly
/// higher than the limit for a single execution that we have in the production setup.
const DEFAULT_GAS_LIMIT: u64 = 500_000_000_000_000; // ~0.5s

#[derive(Debug, Clone)]
pub struct DistantCodeId {
    pub code_id: u64,
    pub module: (Engine, Module),
}

#[derive(Clone)]
pub struct LocalWasmContract {
    pub code: Vec<u8>,
    pub module: (Engine, Module),
}

#[derive(Debug, Clone)]
pub enum WasmContract {
    Local(LocalWasmContract),
    DistantCodeId(DistantCodeId),
}

impl std::fmt::Debug for LocalWasmContract {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(
            f,
            "LocalContract {{ checksum: {} }}",
            Checksum::generate(&self.code),
        )
    }
}

impl WasmContract {
    pub fn new_local(code: Vec<u8>) -> Self {
        check_wasm(
            &code,
            &HashSet::from([
                "iterator".to_string(),
                "staking".to_string(),
                "stargate".to_string(),
            ]),
        )
        .unwrap();
        Self::Local(LocalWasmContract {
            code: code.clone(),
            module: create_module(&code).unwrap(),
        })
    }

    pub fn new_distant_code_id(code_id: u64, remote: RemoteChannel) -> Self {
        let code = {
            let wasm_querier = CosmWasm::new_sync(remote.channel.clone(), &remote.rt);

            let cache_key = format!("{}:{}", remote.chain_id, &code_id);

            let code = wasm_caching::maybe_cached_wasm(cache_key, || {
                remote
                    .rt
                    .block_on(wasm_querier._code_data(code_id))
                    .map_err(|e| e.into())
            })
            .unwrap();

            code
        };
        Self::DistantCodeId(DistantCodeId {
            code_id,
            module: create_module(&code).unwrap(),
        })
    }

    pub fn get_module(&self) -> (Engine, Module) {
        match self {
            WasmContract::Local(LocalWasmContract { module, .. }) => module.clone(),
            WasmContract::DistantCodeId(DistantCodeId { module, .. }) => module.clone(),
        }
    }

    pub fn run_contract<
        QueryC: CustomQuery + DeserializeOwned + 'static,
        ExecC: CustomMsg + DeserializeOwned,
    >(
        &self,
        args: InstanceArguments,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<WasmRunnerOutput<ExecC>> {
        let InstanceArguments {
            function,
            init_storage,
        } = args;
        let address = function.get_address();
        let module = self.get_module();

        let api = RealApi::new(&fork_state.remote.pub_address_prefix);

        // We create the backend here from outside information;
        let backend = Backend {
            api,
            storage: DualStorage::new(
                fork_state.remote.clone(),
                address.to_string(),
                Some(init_storage),
            )?,
            querier: MockQuerier::<ExecC, QueryC>::new(fork_state),
        };
        let options = InstanceOptions {
            gas_limit: DEFAULT_GAS_LIMIT,
        };

        // Then we create the instance

        let mut instance = instance_from_reused_module(module, backend, options)?;

        let gas_before = instance.get_gas_left();

        // Then we call the function that we wanted to call
        let result = execute_function(&mut instance, function)?;

        let gas_after = instance.get_gas_left();

        // We return the code response + any storage change (or the whole local storage object), with serializing
        let mut recycled_instance = instance.recycle().unwrap();

        let wasm_result = WasmRunnerOutput {
            storage: StorageChanges {
                current_keys: recycled_instance.storage.get_all_storage()?,
                removed_keys: recycled_instance.storage.removed_keys.into_iter().collect(),
            },
            gas_used: gas_before - gas_after,
            wasm: result,
        };

        Ok(wasm_result)
    }

    pub fn after_execution_callback<ExecC>(&self, output: &WasmRunnerOutput<ExecC>) {
        // We log the gas used
        let operation = match output.wasm {
            WasmOutput::Execute(_) => "execution",
            WasmOutput::Query(_) => "query",
            WasmOutput::Instantiate(_) => "instantiation",
            WasmOutput::Migrate(_) => "migration",
            WasmOutput::Sudo(_) => "sudo",
            WasmOutput::Reply(_) => "reply",
        };
        log::debug!(
            "Gas used {:?} for {:} on contract {:?}",
            output.gas_used,
            operation,
            self
        );
    }

    pub fn code_id(&self) -> u64 {
        match self {
            WasmContract::Local(_) => unimplemented!(),
            WasmContract::DistantCodeId(d) => d.code_id,
        }
    }
}

impl<ExecC, QueryC> Contract<ExecC, QueryC> for WasmContract
where
    ExecC: CustomMsg + DeserializeOwned,
    QueryC: CustomQuery + DeserializeOwned,
{
    fn execute(
        &self,
        deps: DepsMut<QueryC>,
        env: Env,
        info: MessageInfo,
        msg: Vec<u8>,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<Response<ExecC>> {
        // We start by building the dependencies we will pass through the wasm executer
        let execute_args = InstanceArguments {
            function: WasmFunction::Execute(ExecuteArgs { env, info, msg }),
            init_storage: deps.storage.range(None, None, Order::Ascending).collect(),
        };

        let decoded_result = self.run_contract(execute_args, fork_state)?;

        apply_storage_changes(deps.storage, &decoded_result);
        self.after_execution_callback(&decoded_result);

        match decoded_result.wasm {
            WasmOutput::Execute(x) => Ok(x),
            _ => panic!("Wrong kind of answer from wasm container"),
        }
    }

    fn instantiate(
        &self,
        deps: DepsMut<QueryC>,
        env: Env,
        info: MessageInfo,
        msg: Vec<u8>,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<Response<ExecC>> {
        // We start by building the dependencies we will pass through the wasm executer
        let instantiate_arguments = InstanceArguments {
            function: WasmFunction::Instantiate(InstantiateArgs { env, info, msg }),
            init_storage: deps.storage.range(None, None, Order::Ascending).collect(),
        };

        let decoded_result = self.run_contract(instantiate_arguments, fork_state)?;

        apply_storage_changes(deps.storage, &decoded_result);
        self.after_execution_callback(&decoded_result);

        match decoded_result.wasm {
            WasmOutput::Instantiate(x) => Ok(x),
            _ => panic!("Wrong kind of answer from wasm container"),
        }
    }

    fn query(
        &self,
        deps: Deps<QueryC>,
        env: Env,
        msg: Vec<u8>,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<Binary> {
        // We start by building the dependencies we will pass through the wasm executer
        let query_arguments = InstanceArguments {
            function: WasmFunction::Query(QueryArgs { env, msg }),
            init_storage: deps.storage.range(None, None, Order::Ascending).collect(),
        };

        let decoded_result: WasmRunnerOutput<ExecC> =
            self.run_contract(query_arguments, fork_state)?;

        self.after_execution_callback(&decoded_result);

        match decoded_result.wasm {
            WasmOutput::Query(x) => Ok(x),
            _ => panic!("Wrong kind of answer from wasm container"),
        }
    }

    // this returns an error if the contract doesn't implement sudo
    fn sudo(
        &self,
        deps: DepsMut<QueryC>,
        env: Env,
        msg: Vec<u8>,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<Response<ExecC>> {
        let sudo_args = InstanceArguments {
            function: WasmFunction::Sudo(SudoArgs { env, msg }),
            init_storage: deps.storage.range(None, None, Order::Ascending).collect(),
        };

        let decoded_result = self.run_contract(sudo_args, fork_state)?;

        apply_storage_changes(deps.storage, &decoded_result);
        self.after_execution_callback(&decoded_result);

        match decoded_result.wasm {
            WasmOutput::Sudo(x) => Ok(x),
            _ => panic!("Wrong kind of answer from wasm container"),
        }
    }

    // this returns an error if the contract doesn't implement reply
    fn reply(
        &self,
        deps: DepsMut<QueryC>,
        env: Env,
        reply: Reply,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<Response<ExecC>> {
        let reply_args = InstanceArguments {
            function: WasmFunction::Reply(ReplyArgs { env, reply }),
            init_storage: deps.storage.range(None, None, Order::Ascending).collect(),
        };

        let decoded_result = self.run_contract(reply_args, fork_state)?;

        apply_storage_changes(deps.storage, &decoded_result);
        self.after_execution_callback(&decoded_result);

        match decoded_result.wasm {
            WasmOutput::Reply(x) => Ok(x),
            _ => panic!("Wrong kind of answer from wasm container"),
        }
    }

    // this returns an error if the contract doesn't implement migrate
    fn migrate(
        &self,
        deps: DepsMut<QueryC>,
        env: Env,
        msg: Vec<u8>,
        fork_state: ForkState<ExecC, QueryC>,
    ) -> AnyResult<Response<ExecC>> {
        let migrate_args = InstanceArguments {
            function: WasmFunction::Migrate(MigrateArgs { env, msg }),
            init_storage: deps.storage.range(None, None, Order::Ascending).collect(),
        };

        let decoded_result = self.run_contract(migrate_args, fork_state)?;

        apply_storage_changes(deps.storage, &decoded_result);
        self.after_execution_callback(&decoded_result);

        match decoded_result.wasm {
            WasmOutput::Migrate(x) => Ok(x),
            _ => panic!("Wrong kind of answer from wasm container"),
        }
    }
}

pub fn execute_function<
    A: BackendApi + 'static,
    B: cosmwasm_vm::Storage + 'static,
    C: Querier + 'static,
    ExecC: CustomMsg + DeserializeOwned,
>(
    instance: &mut Instance<A, B, C>,
    function: WasmFunction,
) -> AnyResult<WasmOutput<ExecC>> {
    match function {
        WasmFunction::Execute(args) => {
            let result = call_execute(instance, &args.env, &args.info, &args.msg)?
                .into_result()
                .map_err(StdError::generic_err)?;
            Ok(WasmOutput::Execute(result))
        }
        WasmFunction::Query(args) => {
            let result = call_query(instance, &args.env, &args.msg)?
                .into_result()
                .map_err(StdError::generic_err)?;
            Ok(WasmOutput::Query(result))
        }
        WasmFunction::Instantiate(args) => {
            let result = call_instantiate(instance, &args.env, &args.info, &args.msg)?
                .into_result()
                .map_err(StdError::generic_err)?;
            Ok(WasmOutput::Instantiate(result))
        }
        WasmFunction::Reply(args) => {
            let result = call_reply(instance, &args.env, &args.reply)?
                .into_result()
                .map_err(StdError::generic_err)?;
            Ok(WasmOutput::Reply(result))
        }
        WasmFunction::Migrate(args) => {
            let result = call_migrate(instance, &args.env, &args.msg)?
                .into_result()
                .map_err(StdError::generic_err)?;
            Ok(WasmOutput::Migrate(result))
        }
        WasmFunction::Sudo(args) => {
            let result = call_sudo(instance, &args.env, &args.msg)?
                .into_result()
                .map_err(StdError::generic_err)?;
            Ok(WasmOutput::Sudo(result))
        }
    }
}

mod wasm_caching {
    use super::*;

    use std::{
        env, fs,
        io::{Read, Seek},
        os::unix::fs::FileExt,
        path::PathBuf,
    };

    use anyhow::{bail, Context};

    const WASM_CACHE_DIR: &str = "wasm_cache";
    const WASM_CACHE_ENV: &str = "WASM_CACHE";

    static CARGO_TARGET_DIR: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();

    pub(crate) fn cargo_target_dir() -> &'static PathBuf {
        CARGO_TARGET_DIR.get_or_init(|| {
            cargo_metadata::MetadataCommand::new()
                .no_deps()
                .exec()
                .unwrap()
                .target_directory
                .into()
        })
    }

    #[repr(u8)]
    enum WasmCachingStatus {
        /// Currently writing
        Writing,
        /// This wasm ready for use
        Ready,
        /// Writing to it have failed, it's not usable until valid cache written to it
        Corrupted,
    }

    impl From<u8> for WasmCachingStatus {
        fn from(value: u8) -> Self {
            match value {
                0 => WasmCachingStatus::Writing,
                1 => WasmCachingStatus::Ready,
                2 => WasmCachingStatus::Corrupted,
                _ => unimplemented!(),
            }
        }
    }

    impl WasmCachingStatus {
        pub fn set_status(self, file: &fs::File) {
            file.write_at(&[self as u8], 0)
                .expect("Failed to update wasm caching status");
        }

        pub fn status(file: &fs::File) -> Self {
            let buf = &mut [0];
            match file.read_at(buf, 0) {
                Ok(_) => buf[0].into(),
                Err(_) => WasmCachingStatus::Corrupted,
            }
        }
    }

    /// Returns wasm bytes for the contract
    ///
    /// Will get cached wasm stored in `CARGO_TARGET_DIR` (./target/ from project root by default)
    /// This feature can be disabled by setting wasm cache environment variable to `false`: `WASM_CACHE=false`
    ///
    /// # Arguments
    ///
    /// * `key` - A string key that represents unique id of the contract (usually chain-id:code_id)
    /// * `wasm_code_bytes` - Function that returns non-cached version of the contract
    ///
    /// # Scenarios
    ///
    /// - Wasm caching disabled: return result of the `wasm_code_bytes` function
    /// - Wasm file not found in cache location: return result of the `wasm_code_bytes` function, saving cache on on success
    /// - Wasm stored in cache: return bytes
    pub(crate) fn maybe_cached_wasm<F: Fn() -> AnyResult<Vec<u8>>>(
        key: String,
        wasm_code_bytes: F,
    ) -> AnyResult<Vec<u8>> {
        let wasm_cache_enabled = env::var(WASM_CACHE_ENV)
            .ok()
            .and_then(|wasm_cache| wasm_cache.parse().ok())
            .unwrap_or(true);

        // Wasm caching disabled, return function result
        if !wasm_cache_enabled {
            return wasm_code_bytes();
        }

        let wasm_cache_dir = cargo_target_dir().join(WASM_CACHE_DIR);
        // Prepare cache directory in `./target/`
        match fs::metadata(&wasm_cache_dir) {
            // Verify it's dir
            Ok(wasm_cache_metadata) => {
                if !wasm_cache_metadata.is_dir() {
                    bail!("{WASM_CACHE_DIR} supposed to be directory")
                }
            }
            // Error on checking cache dir, try to create it
            Err(_) => fs::create_dir(&wasm_cache_dir)
                .context("Wasm cache directory cannot be created, please check permissions")?,
        }

        let cached_wasm_file = wasm_cache_dir.join(key);
        let wasm_bytes = match fs::metadata(&cached_wasm_file) {
            // Cache file exists, try to read it
            Ok(_) => {
                let mut file =
                    fs::File::open(&cached_wasm_file).context("unable to open wasm cache file")?;
                // If someone is writing to it we need to wait, and then check again
                // TODO: decide what is the best way to wait for it
                let mut status = WasmCachingStatus::status(&file);
                if let WasmCachingStatus::Writing = status {
                    let options = file_lock::FileOptions::new().read(true);
                    // Blocking lock until writer unlocks it
                    let file_lock = file_lock::FileLock::lock(&cached_wasm_file, true, options)?;
                    status = WasmCachingStatus::status(&file_lock.file);
                    file_lock.unlock()?
                }
                match status {
                    WasmCachingStatus::Ready => {
                        let mut buf = vec![];
                        file.seek(std::io::SeekFrom::Start(1))?;
                        file.read_to_end(&mut buf)
                            .context("unable to open wasm cache file")?;
                        buf
                    }
                    // Ready for read
                    // Corrupted, need to write new wasm
                    WasmCachingStatus::Corrupted => {
                        store_new_wasm(wasm_code_bytes, &cached_wasm_file)?
                    }
                    // Someone dropped file lock with caching status Writing it means it's corrupted
                    WasmCachingStatus::Writing => {
                        store_new_wasm(wasm_code_bytes, &cached_wasm_file)?
                    }
                }
            }
            // Error on checking cache dir, get wasm bytes and try to cache it
            Err(_) => store_new_wasm(wasm_code_bytes, &cached_wasm_file)?,
        };
        Ok(wasm_bytes)
    }

    fn store_new_wasm<F: Fn() -> AnyResult<Vec<u8>>>(
        wasm_code_bytes: F,
        cached_wasm_file: &PathBuf,
    ) -> Result<Vec<u8>, anyhow::Error> {
        let options = file_lock::FileOptions::new().create(true).write(true);
        let file_lock_status = file_lock::FileLock::lock(cached_wasm_file, false, options);
        let wasm = wasm_code_bytes()?;
        if let Err(cache_save_err) = file_lock_status.and_then(|file_lock| {
            // Set writing status
            WasmCachingStatus::Writing.set_status(&file_lock.file);

            match file_lock.file.write_all_at(&wasm, 1) {
                // Done writing, set ready status
                Ok(()) => {
                    WasmCachingStatus::Ready.set_status(&file_lock.file);
                    Ok(())
                }
                // Failed to write, set corrupted status
                Err(e) => {
                    WasmCachingStatus::Corrupted.set_status(&file_lock.file);
                    Err(e)
                }
            }
        }) {
            // It's not critical if it fails, as we already have wasm bytes, so we just log it
            log::error!(target: "wasm_caching", "Failed to save wasm cache: {cache_save_err}")
        }
        Ok(wasm)
    }
}