-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle_asset_definition.rs
337 lines (320 loc) · 14.2 KB
/
toggle_asset_definition.rs
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
use cosmwasm_std::{DepsMut, MessageInfo, Response};
use result_extensions::ResultExtensions;
use crate::core::state::{load_asset_definition_by_type_v3, replace_asset_definition_v3};
use crate::{
core::{error::ContractError, msg::ExecuteMsg},
util::{
aliases::{AssetResult, EntryPointResponse},
contract_helpers::{check_admin_only, check_funds_are_empty},
event_attributes::{EventAttributes, EventType},
},
};
/// A transformation of [ExecuteMsg::ToggleAssetDefinition](crate::core::msg::ExecuteMsg::ToggleAssetDefinition)
/// for ease of use in the underlying [toggle_asset_definition](self::toggle_asset_definition) function.
///
/// # Parameters
///
/// * `asset_type` The unique identifier for the target [AssetDefinition](crate::core::types::asset_definition::AssetDefinitionV3),
/// keyed on its [asset_type](crate::core::types::asset_definition::AssetDefinitionV3::asset_type)
/// property.
/// * `expected_result` The value of [enabled](crate::core::types::asset_definition::AssetDefinitionV3::enabled)
/// after the toggle takes place. This value is required to ensure that multiple toggles executed
/// in succession (either by accident or by various unrelated callers) will only be honored if
/// the asset definition is in the intended state during the execution of the route.
#[derive(Clone, PartialEq, Eq)]
pub struct ToggleAssetDefinitionV1 {
pub asset_type: String,
pub expected_result: bool,
}
impl ToggleAssetDefinitionV1 {
/// Constructs a new instance of this struct.
///
/// # Parameters
///
/// * `asset_type` The unique identifier for the target [AssetDefinitionV3](crate::core::types::asset_definition::AssetDefinitionV3),
/// keyed on its [asset_type](crate::core::types::asset_definition::AssetDefinitionV3::asset_type)
/// property.
/// * `expected_result` The value of [enabled](crate::core::types::asset_definition::AssetDefinitionV3::enabled)
/// after the toggle takes place. This value is required to ensure that multiple toggles executed
/// in succession (either by accident or by various unrelated callers) will only be honored if
/// the asset definition is in the intended state during the execution of the route.
pub fn new<S: Into<String>>(asset_type: S, expected_result: bool) -> Self {
ToggleAssetDefinitionV1 {
asset_type: asset_type.into(),
expected_result,
}
}
/// Attempts to create an instance of this struct from a provided execute msg. If the provided
/// value is not of the [ToggleAssetDefinition](crate::core::msg::ExecuteMsg::ToggleAssetDefinition)
/// variant, then an [InvalidMessageType](crate::core::error::ContractError::InvalidMessageType)
/// error will be returned.
///
/// # Parameters
///
/// * `msg` An execute msg provided by the contract's [execute](crate::contract::execute) function.
pub fn from_execute_msg(msg: ExecuteMsg) -> AssetResult<ToggleAssetDefinitionV1> {
match msg {
ExecuteMsg::ToggleAssetDefinition {
asset_type,
expected_result,
} => ToggleAssetDefinitionV1::new(asset_type, expected_result).to_ok(),
_ => ContractError::InvalidMessageType {
expected_message_type: "ExecuteMsg::ToggleAssetDefinition".to_string(),
}
.to_err(),
}
}
}
/// The function used by [execute](crate::contract::execute) when an [ExecuteMsg::ToggleAssetDefinition](crate::core::msg::ExecuteMsg::ToggleAssetDefinition)
/// message is provided. Attempts to swap the [enabled](crate::core::types::asset_definition::AssetDefinitionV3::enabled)
/// property from true to false, or false to true.
///
/// # Parameters
///
/// * `deps` A dependencies object provided by the cosmwasm framework. Allows access to useful
/// resources like contract internal storage and a querier to retrieve blockchain objects.
/// * `info` A message information object provided by the cosmwasm framework. Describes the sender
/// of the instantiation message, as well as the funds provided as an amount during the transaction.
/// * `msg` An instance of the toggle asset definition v1 struct, provided by conversion from an
/// [ExecuteMsg](crate::core::msg::ExecuteMsg).
pub fn toggle_asset_definition(
deps: DepsMut,
info: MessageInfo,
msg: ToggleAssetDefinitionV1,
) -> EntryPointResponse {
check_admin_only(&deps.as_ref(), &info)?;
check_funds_are_empty(&info)?;
let mut asset_definition = load_asset_definition_by_type_v3(deps.storage, &msg.asset_type)?;
// Never toggle the state if the caller didn't expect the target result
// If current state == expected result, then the requestor wants to change TO the current state. So this is a no-op.
if asset_definition.enabled == msg.expected_result {
return ContractError::UnexpectedState {
explanation: format!(
"expected to toggle to [enabled = {}], but toggle would set value to [enabled = {}]",
msg.expected_result, !asset_definition.enabled
),
}
.to_err();
}
// Simply negate the current value in state to swap it
asset_definition.enabled = !asset_definition.enabled;
replace_asset_definition_v3(deps.storage, &asset_definition)?;
Response::new()
.add_attributes(
EventAttributes::new(EventType::ToggleAssetDefinition)
.set_asset_type(&msg.asset_type)
.set_new_value(&asset_definition.enabled),
)
.to_ok()
}
#[cfg(test)]
mod tests {
use cosmwasm_std::testing::{message_info, mock_env};
use cosmwasm_std::{Addr, Deps, DepsMut};
use provwasm_mocks::mock_provenance_dependencies;
use crate::core::state::load_asset_definition_by_type_v3;
use crate::{
contract::execute,
core::{error::ContractError, msg::ExecuteMsg},
testutil::{
test_constants::{DEFAULT_ADMIN_ADDRESS, DEFAULT_ASSET_TYPE},
test_utilities::{
empty_mock_info, mock_info_with_nhash, single_attribute_for_key,
test_instantiate_success, InstArgs,
},
},
util::{
constants::{ASSET_EVENT_TYPE_KEY, ASSET_TYPE_KEY, NEW_VALUE_KEY},
event_attributes::EventType,
},
};
use super::{toggle_asset_definition, ToggleAssetDefinitionV1};
#[test]
fn test_valid_toggle_asset_definition_via_execute() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let response = execute(
deps.as_mut(),
mock_env(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ExecuteMsg::ToggleAssetDefinition {
asset_type: DEFAULT_ASSET_TYPE.to_string(),
expected_result: false,
},
)
.expect("the toggle should work correctly");
assert!(
response.messages.is_empty(),
"toggling an asset definition should not require messages",
);
assert_eq!(
3,
response.attributes.len(),
"toggling an asset definition should produce the correct number of attributes",
);
assert_eq!(
EventType::ToggleAssetDefinition.event_name().as_str(),
single_attribute_for_key(&response, ASSET_EVENT_TYPE_KEY),
"the proper event type should be emitted",
);
assert_eq!(
DEFAULT_ASSET_TYPE,
single_attribute_for_key(&response, ASSET_TYPE_KEY),
"the proper asset type should be emitted",
);
assert_eq!(
"false",
single_attribute_for_key(&response, NEW_VALUE_KEY),
"the new value key should indicate that the asset definition has been set to enabled = false",
);
test_toggle_has_successfully_occurred(&deps.as_ref(), false);
}
#[test]
fn test_valid_toggle_asset_definition_via_internal() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
toggle_default_asset_definition(deps.as_mut(), false);
test_toggle_has_successfully_occurred(&deps.as_ref(), false);
}
#[test]
fn test_toggle_off_and_back_on() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
// First toggle should disable the automagically enabled default asset type
toggle_default_asset_definition(deps.as_mut(), false);
test_toggle_has_successfully_occurred(&deps.as_ref(), false);
// Second toggle should re-enable it
toggle_default_asset_definition(deps.as_mut(), true);
test_toggle_has_successfully_occurred(&deps.as_ref(), true);
}
#[test]
fn test_invalid_toggle_asset_definition_for_invalid_msg() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let error = execute(
deps.as_mut(),
mock_env(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ExecuteMsg::ToggleAssetDefinition {
asset_type: String::new(),
expected_result: false,
},
)
.unwrap_err();
assert!(
matches!(error, ContractError::InvalidMessageFields { .. }),
"expected the invalid message fields error to be returned when the message is malformatted, but got: {:?}",
error,
);
}
#[test]
fn test_invalid_toggle_asset_definition_for_invalid_sender() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let error = toggle_asset_definition(
deps.as_mut(),
message_info(&Addr::unchecked("not-the-admin"), &[]),
ToggleAssetDefinitionV1::new(DEFAULT_ASSET_TYPE, false),
)
.unwrap_err();
assert!(
matches!(error, ContractError::Unauthorized { .. }),
"expected the unauthorized error to be returned when the sender is not the admin, but got: {:?}",
error,
);
}
#[test]
fn test_invalid_toggle_asset_definition_for_provided_funds() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let error = toggle_asset_definition(
deps.as_mut(),
mock_info_with_nhash(DEFAULT_ADMIN_ADDRESS, 150),
ToggleAssetDefinitionV1::new(DEFAULT_ASSET_TYPE, false),
)
.unwrap_err();
assert!(
matches!(error, ContractError::InvalidFunds(_)),
"expected the invalid funds error to be returned when the sender provides funds, but got: {:?}",
error,
);
}
#[test]
fn test_invalid_toggle_asset_definition_for_invalid_asset_target() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let error = toggle_asset_definition(
deps.as_mut(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ToggleAssetDefinitionV1::new("no-u", false),
)
.unwrap_err();
assert!(
matches!(error, ContractError::RecordNotFound { .. }),
"expected the not found error to be returned, but got: {:?}",
error,
);
}
#[test]
fn test_toggle_to_incorrect_expected_state_fails() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
// The asset type should be enabled by default, so trying to toggle it to enabled again should fail
let enable_error = toggle_asset_definition(
deps.as_mut(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ToggleAssetDefinitionV1::new(DEFAULT_ASSET_TYPE, true),
)
.unwrap_err();
match enable_error {
ContractError::UnexpectedState { explanation } => {
assert_eq!(
"expected to toggle to [enabled = true], but toggle would set value to [enabled = false]",
explanation.as_str(),
"incorrect error message encountered on invalid toggle false -> true",
);
}
_ => panic!(
"unexpected error encountered on invalid toggle false -> true: {:?}",
enable_error
),
};
// Toggle off successfully to ensure the opposite attempt cannot be made either
toggle_default_asset_definition(deps.as_mut(), false);
let disable_error = toggle_asset_definition(
deps.as_mut(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ToggleAssetDefinitionV1::new(DEFAULT_ASSET_TYPE, false),
)
.unwrap_err();
match disable_error {
ContractError::UnexpectedState { explanation } => {
assert_eq!(
"expected to toggle to [enabled = false], but toggle would set value to [enabled = true]",
explanation.as_str(),
"incorrect error message encountered on invalid toggle true -> false",
);
}
_ => panic!(
"unexpected error encountered on invalid toggle true -> false: {:?}",
disable_error
),
}
}
fn test_toggle_has_successfully_occurred(deps: &Deps, expected_enabled_value: bool) {
let asset_def = load_asset_definition_by_type_v3(deps.storage, DEFAULT_ASSET_TYPE)
.expect("the default asset definition should exist in storage");
assert_eq!(
expected_enabled_value, asset_def.enabled,
"the asset definition enabled value was not toggled to the expected value",
);
}
fn toggle_default_asset_definition(deps: DepsMut, expected_result: bool) {
toggle_asset_definition(
deps,
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ToggleAssetDefinitionV1::new(DEFAULT_ASSET_TYPE, expected_result),
)
.expect("toggle should execute without fail");
}
}