bvs-pauser 2.4.0

SatLayer Bitcoin Validated Service
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
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;

use crate::error::PauserError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::PAUSED;
use bvs_library::ownership;
use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;

const CONTRACT_NAME: &str = concat!("crates.io:", env!("CARGO_PKG_NAME"));
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    msg: InstantiateMsg,
) -> Result<Response, PauserError> {
    set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

    let owner = deps.api.addr_validate(&msg.owner)?;
    ownership::set_owner(deps.storage, &owner)?;

    PAUSED.save(deps.storage, &msg.initial_paused)?;

    Ok(Response::new()
        .add_attribute("method", "instantiate")
        .add_attribute("owner", msg.owner))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, PauserError> {
    match msg {
        ExecuteMsg::Pause { method, contract } => {
            let contract = deps.api.addr_validate(&contract)?;
            execute::pause(deps, env, info, contract, method)
        }
        ExecuteMsg::Unpause { method, contract } => {
            let contract = deps.api.addr_validate(&contract)?;
            execute::unpause(deps, info, contract, method)
        }
        ExecuteMsg::PauseGlobal {} => execute::pause_global(deps, info),
        ExecuteMsg::UnpauseGlobal {} => execute::unpause_global(deps, info),
        ExecuteMsg::TransferOwnership { new_owner } => {
            let new_owner = deps.api.addr_validate(&new_owner)?;
            ownership::transfer_ownership(deps.storage, info, new_owner)
                .map_err(PauserError::Ownership)
        }
    }
}

mod execute {
    use cosmwasm_std::Addr;

    use super::*;
    use crate::state::{PAUSED, PAUSED_CONTRACT_METHOD};

    pub fn pause(
        deps: DepsMut,
        env: Env,
        info: MessageInfo,
        contract: Addr,
        method: String,
    ) -> Result<Response, PauserError> {
        ownership::assert_owner(deps.storage, &info)?;

        // Check if the contract's func is already paused
        // Only mutate the state if it is not already paused
        if !PAUSED_CONTRACT_METHOD.has(deps.storage, (&contract, &method)) {
            PAUSED_CONTRACT_METHOD.save(deps.storage, (&contract, &method), &env.block.height)?;
        }

        Ok(Response::new()
            .add_attribute("method", "pause")
            .add_attribute("contract", contract)
            .add_attribute("paused_method", method)
            .add_attribute("sender", info.sender))
    }

    pub fn unpause(
        deps: DepsMut,
        info: MessageInfo,
        contract: Addr,
        method: String,
    ) -> Result<Response, PauserError> {
        ownership::assert_owner(deps.storage, &info)?;

        // Check if the contract is already unpaused
        // Only mutate the state if it is not already unpaused
        if PAUSED_CONTRACT_METHOD.has(deps.storage, (&contract, &method)) {
            PAUSED_CONTRACT_METHOD.remove(deps.storage, (&contract, &method));
        }

        Ok(Response::new()
            .add_attribute("method", "unpause")
            .add_attribute("contract", contract)
            .add_attribute("unpaused_method", method)
            .add_attribute("sender", info.sender))
    }

    pub fn pause_global(deps: DepsMut, info: MessageInfo) -> Result<Response, PauserError> {
        ownership::assert_owner(deps.storage, &info)?;

        PAUSED.save(deps.storage, &true)?;

        Ok(Response::new()
            .add_attribute("method", "pause_global")
            .add_attribute("sender", info.sender))
    }

    pub fn unpause_global(deps: DepsMut, info: MessageInfo) -> Result<Response, PauserError> {
        ownership::assert_owner(deps.storage, &info)?;

        PAUSED.save(deps.storage, &false)?;

        Ok(Response::new()
            .add_attribute("method", "unpause_global")
            .add_attribute("sender", info.sender))
    }
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
    match msg {
        QueryMsg::IsPaused { contract, method } => {
            let contract = deps.api.addr_validate(&contract)?;
            to_json_binary(&query::is_paused(deps, contract, method)?)
        }
        QueryMsg::CanExecute {
            contract,
            sender,
            method,
        } => {
            let contract = deps.api.addr_validate(&contract)?;
            let sender = deps.api.addr_validate(&sender)?;
            to_json_binary(&query::can_execute(deps, contract, sender, method)?)
        }
    }
}

mod query {
    use super::*;
    use crate::msg::{CanExecuteFlag, CanExecuteResponse, IsPausedResponse};
    use crate::state::{PAUSED, PAUSED_CONTRACT_METHOD};
    use cosmwasm_std::Addr;

    // This query checks if a contract's method is paused
    // Global pause takes precedence over contract and method pause
    pub fn is_paused(deps: Deps, contract: Addr, method: String) -> StdResult<IsPausedResponse> {
        let is_paused_globally = PAUSED.load(deps.storage)?;

        // technically we can do OR logic gate with the state below
        // but this early return approach only load the state until necessary
        if is_paused_globally {
            return Ok(IsPausedResponse::new(true));
        }

        let is_paused = PAUSED_CONTRACT_METHOD.has(deps.storage, (&contract, &method));

        Ok(IsPausedResponse::new(is_paused))
    }

    /// TODO(future): The _sender is currently not used.
    ///  Added for future compatibility, not yet utilized—current design pauses method and contract.
    ///  Global pause takes precedence over contract and method pause.
    pub fn can_execute(
        deps: Deps,
        contract: Addr,
        _sender: Addr,
        method: String,
    ) -> StdResult<CanExecuteResponse> {
        let is_paused_globally = PAUSED.load(deps.storage)?;

        if is_paused_globally {
            return Ok(CanExecuteFlag::Paused.into());
        }

        let is_paused = PAUSED_CONTRACT_METHOD.has(deps.storage, (&contract, &method));

        if is_paused {
            return Ok(CanExecuteFlag::Paused.into());
        }
        Ok(CanExecuteFlag::CanExecute.into())
    }
}

/// This can only be called by the contract ADMIN, enforced by `wasmd` separate from cosmwasm.
/// See https://github.com/CosmWasm/cosmwasm/issues/926#issuecomment-851259818
///
/// #### 2.0.0
/// - New [ExecuteMsg::Pause] and [ExecuteMsg::Unpause] for individual contract methods.
/// - No storage migration.
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
    cw2::ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
    Ok(Response::default())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::msg::{CanExecuteFlag, IsPausedResponse};
    use crate::state::PAUSED_CONTRACT_METHOD;
    use cosmwasm_std::testing::{message_info, mock_dependencies, mock_env};
    use cosmwasm_std::{coins, from_json};

    #[test]
    fn instantiate_msg() {
        let mut deps = mock_dependencies();

        let info = message_info(&deps.api.addr_make("sender"), &coins(1000, "rat"));
        let msg = InstantiateMsg {
            owner: deps.api.addr_make("owner").to_string(),
            initial_paused: false,
        };
        instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

        let res = query(
            deps.as_ref(),
            mock_env(),
            QueryMsg::IsPaused {
                contract: deps.api.addr_make("anyone").to_string(),
                method: "any_method".to_string(),
            },
        )
        .unwrap();
        let value: IsPausedResponse = from_json(&res).unwrap();
        assert!(!value.is_paused());
    }

    #[test]
    fn pause() {
        let mut deps = mock_dependencies();
        let env = mock_env();

        let owner = deps.api.addr_make("owner");

        ownership::set_owner(&mut deps.storage, &owner).unwrap();
        PAUSED.save(&mut deps.storage, &false).unwrap();

        let info = message_info(&owner, &[]);
        let contract = deps.api.addr_make("contract");
        let sender = deps.api.addr_make("sender");
        let method = "any_method".to_string();

        execute::pause(deps.as_mut(), env, info, contract.clone(), method.clone()).unwrap();

        let response = query::is_paused(deps.as_ref(), contract.clone(), method.clone()).unwrap();
        assert!(response.is_paused());

        let response = query::can_execute(deps.as_ref(), contract, sender, method).unwrap();
        let flag: CanExecuteFlag = response.into();
        assert_eq!(flag, CanExecuteFlag::Paused);
    }

    #[test]
    fn pause_unauthorized() {
        let mut deps = mock_dependencies();
        let env = mock_env();

        let owner = deps.api.addr_make("owner");

        ownership::set_owner(&mut deps.storage, &owner).unwrap();
        PAUSED.save(&mut deps.storage, &false).unwrap();

        let not_owner = deps.api.addr_make("not_owner");
        let info = message_info(&not_owner, &[]);
        let contract = deps.api.addr_make("contract");
        let sender = deps.api.addr_make("sender");
        let method = "any_method".to_string();
        execute::pause(deps.as_mut(), env, info, contract.clone(), method.clone())
            .expect_err("Unauthorized");

        {
            let res = query::is_paused(deps.as_ref(), contract.clone(), method.clone()).unwrap();
            assert!(!res.is_paused());

            let res = query::can_execute(deps.as_ref(), contract, sender, method).unwrap();
            let flag: CanExecuteFlag = res.into();
            assert_eq!(flag, CanExecuteFlag::CanExecute);
        }
    }

    #[test]
    fn unpause() {
        let mut deps = mock_dependencies();
        let env = mock_env();

        let owner = deps.api.addr_make("owner");

        let contract = deps.api.addr_make("anyone");
        let sender = deps.api.addr_make("sender");
        let method = "any_method".to_string();

        ownership::set_owner(&mut deps.storage, &owner).unwrap();
        PAUSED_CONTRACT_METHOD
            .save(&mut deps.storage, (&contract, &method), &env.block.height)
            .unwrap();
        PAUSED.save(&mut deps.storage, &false).unwrap();

        let info = message_info(&owner, &[]);
        execute::unpause(deps.as_mut(), info, contract.clone(), method.clone()).unwrap();

        {
            let res = query::is_paused(deps.as_ref(), contract.clone(), method.clone()).unwrap();
            assert!(!res.is_paused());

            let res = query::can_execute(deps.as_ref(), contract, sender, method).unwrap();
            let flag: CanExecuteFlag = res.into();
            assert_eq!(flag, CanExecuteFlag::CanExecute);
        }
    }

    #[test]
    fn unpause_unauthorized() {
        let mut deps = mock_dependencies();
        let env = mock_env();

        let owner = deps.api.addr_make("owner");

        let contract = deps.api.addr_make("anyone");
        let sender = deps.api.addr_make("sender");
        let method = "any_method".to_string();

        ownership::set_owner(&mut deps.storage, &owner).unwrap();
        PAUSED_CONTRACT_METHOD
            .save(&mut deps.storage, (&contract, &method), &env.block.height)
            .unwrap();
        PAUSED.save(&mut deps.storage, &false).unwrap();

        let not_owner = deps.api.addr_make("not_owner");
        let info = message_info(&not_owner, &[]);
        execute::unpause(deps.as_mut(), info, contract.clone(), method.clone())
            .expect_err("Unauthorized");

        {
            let res = query::is_paused(deps.as_ref(), contract.clone(), method.clone()).unwrap();
            assert!(res.is_paused());

            let res = query::can_execute(deps.as_ref(), contract, sender, method).unwrap();
            let flag: CanExecuteFlag = res.into();
            assert_eq!(flag, CanExecuteFlag::Paused);
        }
    }

    #[test]
    fn pause_pause() {
        let mut deps = mock_dependencies();
        let env = mock_env();

        let owner = deps.api.addr_make("owner");
        let info = message_info(&owner, &[]);
        let contract = deps.api.addr_make("anyone");
        let method = "any_method".to_string();

        ownership::set_owner(&mut deps.storage, &owner).unwrap();
        PAUSED.save(&mut deps.storage, &false).unwrap();

        execute::pause(
            deps.as_mut(),
            env.clone(),
            info.clone(),
            contract.clone(),
            method.clone(),
        )
        .unwrap();

        let res = query::is_paused(deps.as_ref(), contract.clone(), method.clone()).unwrap();
        assert!(res.is_paused());

        execute::pause(deps.as_mut(), env, info, contract.clone(), method.clone()).unwrap();

        let res = query::is_paused(deps.as_ref(), contract.clone(), method.clone()).unwrap();
        assert!(res.is_paused());

        let sender = deps.api.addr_make("sender");
        let res = query::can_execute(deps.as_ref(), contract, sender, method).unwrap();
        let flag: CanExecuteFlag = res.into();
        assert_eq!(flag, CanExecuteFlag::Paused);
    }

    #[test]
    fn test_global_pause() {
        let mut deps = mock_dependencies();
        let _env = mock_env();

        let owner = deps.api.addr_make("owner");
        let info = message_info(&owner, &[]);

        ownership::set_owner(&mut deps.storage, &owner).unwrap();
        PAUSED.save(&mut deps.storage, &false).unwrap();

        execute::pause_global(deps.as_mut(), info.clone()).unwrap();

        // this method is not paused atomically but globally pause take precedence
        // so it should be paused
        let res = query::is_paused(
            deps.as_ref(),
            deps.api.addr_make("anyone"),
            "any_method".to_string(),
        )
        .unwrap();
        assert!(res.is_paused());

        let res = query::can_execute(
            deps.as_ref(),
            deps.api.addr_make("anyone"),
            deps.api.addr_make("sender"),
            "any_method".to_string(),
        )
        .unwrap();
        let flag: CanExecuteFlag = res.into();

        assert_eq!(flag, CanExecuteFlag::Paused);

        // unauthorized global pause
        {
            let not_owner = deps.api.addr_make("not_owner");
            let info = message_info(&not_owner, &[]);

            execute::pause_global(deps.as_mut(), info).expect_err("Unauthorized");

            let res = query::is_paused(
                deps.as_ref(),
                deps.api.addr_make("anyone"),
                "any_method".to_string(),
            )
            .unwrap();
            assert!(res.is_paused());
        }

        // authorized global unpause
        {
            let info = message_info(&owner, &[]);

            execute::unpause_global(deps.as_mut(), info.clone()).unwrap();

            let res = query::is_paused(
                deps.as_ref(),
                deps.api.addr_make("anyone"),
                "any_method".to_string(),
            )
            .unwrap();
            assert!(!res.is_paused());

            let res = query::can_execute(
                deps.as_ref(),
                deps.api.addr_make("anyone"),
                deps.api.addr_make("sender"),
                "any_method".to_string(),
            )
            .unwrap();
            let flag: CanExecuteFlag = res.into();
            assert_eq!(flag, CanExecuteFlag::CanExecute);
        }
    }
}