forked from icon-project/xcall-multi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertion.rs
205 lines (189 loc) · 8.7 KB
/
assertion.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
use super::*;
use crate::{
error::ContractError,
state::{CwCallService, MAX_DATA_SIZE, MAX_ROLLBACK_SIZE},
types::{call_request::CallRequest, request::CSMessageRequest},
};
impl<'a> CwCallService<'a> {
/// This function checks if the caller is a contract and if the rollback option is null, and returns
/// an error if the rollback is not possible.
///
/// Arguments:
///
/// * `deps`: `deps` is an object that contains dependencies required by the contract to interact
/// with the blockchain. It is of type `Deps`, which is a struct that contains various modules such
/// as `storage`, `querier`, `api`, etc.
/// * `address`: The address of the caller that needs to be checked if it is a contract or not.
/// * `rollback`: `rollback` is an optional `Vec<u8>` parameter that represents the rollback data. If
/// it is `Some`, it means that a rollback is possible and the caller must be a contract. If it is
/// `None`, it means that a rollback is not possible and the caller can be any type
///
/// Returns:
///
/// a `Result` type with the `Ok` variant containing an empty tuple `()` and the `Err` variant
/// containing a `ContractError` if the condition in the `ensure!` macro is not met.
pub fn ensure_caller_is_contract_and_rollback_is_null(
&self,
deps: Deps,
address: &Addr,
rollback: &Option<Vec<u8>>,
) -> Result<(), ContractError> {
if rollback.is_some() {
ensure!(
is_contract(deps.querier, address),
ContractError::RollbackNotPossible
);
}
Ok(())
}
/// This function ensures that the length of the data is not greater than the maximum allowed size and
/// returns an error if it is.
///
/// Arguments:
///
/// * `data_len`: `data_len` is a variable of type `usize` that represents the length of some data. It
/// is used as a parameter in the `ensure_data_length` function to check if the length of the data is
/// within the maximum allowed size. If the length of the data exceeds the maximum size,
///
/// Returns:
///
/// The `ensure_data_length` function returns a `Result` type with the success case containing an empty
/// tuple `()` and the error case containing a `ContractError`.
pub fn ensure_data_length(&self, data_len: usize) -> Result<(), ContractError> {
ensure!(
data_len <= MAX_DATA_SIZE as usize,
ContractError::MaxDataSizeExceeded
);
Ok(())
}
/// This function ensures that the length of a given byte array (rollback) is not greater than a
/// specified maximum size.
///
/// Arguments:
///
/// * `rollback`: `rollback` is a slice of bytes (`&[u8]`) that represents the data to be rolled back in
/// a smart contract. The function `ensure_rollback_length` checks if the length of the `rollback` slice
/// is within the maximum allowed size (`MAX_ROLLBACK_SIZE`) and is not empty.
///
/// Returns:
///
/// a `Result` type with the `Ok` variant containing an empty tuple `()` and the `Err` variant
/// containing a `ContractError` if the condition in the `ensure!` macro is not met.
pub fn ensure_rollback_length(&self, rollback: &[u8]) -> Result<(), ContractError> {
ensure!(
rollback.is_empty() || rollback.len() <= MAX_ROLLBACK_SIZE as usize,
ContractError::MaxRollbackSizeExceeded
);
Ok(())
}
/// The function ensures that the request message is not null and returns an error if it is.
///
/// Arguments:
///
/// * `req_id`: A unique identifier for the request being made.
/// * `message`: `message` is a reference to a `CallServiceMessageRequest` struct. This struct likely
/// contains information about a request to call a service, such as the name of the service, the input
/// parameters, and any other relevant data. The function is checking to make sure that this message is
/// not null
///
/// Returns:
///
/// a `Result` with either an `Ok(())` if the `data` is not empty, or a
/// `ContractError::InvalidRequestId` with the given `req_id` if the `data` is empty.
pub fn ensure_request_not_null(
&self,
req_id: u128,
message: &CSMessageRequest,
) -> Result<(), ContractError> {
let data = to_binary(message).unwrap();
ensure!(
!(data.is_empty()),
ContractError::InvalidRequestId { id: req_id }
);
Ok(())
}
/// This function ensures that a call request message is not null and returns an error if it is.
///
/// Arguments:
///
/// * `sequence_no`: an unsigned 128-bit integer representing the sequence number of a call request.
/// * `message`: The `message` parameter is a reference to a `CallRequest` struct. It is used to ensure
/// that the `data` field of the `CallRequest` is not empty. If it is empty, it will return an error
/// indicating an invalid sequence ID.
///
/// Returns:
///
/// a `Result` enum with either an `Ok(())` value indicating that the call request is not null, or an
/// `Err(ContractError)` value indicating that the call request is null and providing an error message.
pub fn ensure_call_request_not_null(
&self,
sequence_no: u128,
message: &CallRequest,
) -> Result<(), ContractError> {
let data = to_binary(message).unwrap();
ensure!(
!(data.is_empty()),
ContractError::InvalidSequenceId { id: sequence_no }
);
Ok(())
}
/// This function checks if rollback is enabled and returns an error if it is not.
///
/// Arguments:
///
/// * `enabled`: A boolean value indicating whether rollback is enabled or not. If it is not enabled,
/// the function will return a `ContractError` with the message "RollbackNotEnabled".
///
/// Returns:
///
/// The function `ensure_rollback_enabled` is returning a `Result` type with the `Ok` variant containing
/// an empty tuple `()` if the `enabled` parameter is `true`, and a `ContractError` with the
/// `RollbackNotEnabled` variant if the `enabled` parameter is `false`.
pub fn ensure_rollback_enabled(&self, enabled: bool) -> Result<(), ContractError> {
ensure!(enabled, ContractError::RollbackNotEnabled);
Ok(())
}
/// The function ensures that the given address is the admin of the contract.
///
/// Arguments:
///
/// * `store`: `store` is a reference to a trait object of type `dyn Storage`. This is used to interact
/// with the contract's storage and retrieve data from it. The `ensure_admin` function uses `store` to
/// query the current admin address stored in the contract's storage.
/// * `address`: `address` is a variable of type `Addr` that represents the address of a user or
/// contract. It is used as an argument in the `ensure_admin` function to check if the address matches
/// the admin address stored in the contract's storage. If the addresses do not match, the function
/// returns
///
/// Returns:
///
/// a `Result` with either an `Ok(())` value indicating that the `address` parameter matches the stored
/// `admin` value, or a `ContractError` value with the message "OnlyAdmin" if the `address` parameter
/// does not match the stored `admin` value.
pub fn ensure_admin(&self, store: &dyn Storage, address: Addr) -> Result<(), ContractError> {
let admin = self.query_admin(store)?;
ensure_eq!(admin, address, ContractError::OnlyAdmin);
Ok(())
}
}
/// The function checks if a given address is a valid smart contract by querying its information using a
/// QuerierWrapper.
///
/// Arguments:
///
/// * `querier`: The `querier` parameter is an instance of the `QuerierWrapper` struct, which is used to
/// query information from the blockchain. It provides methods to query account balances, contract
/// state, and other information related to the blockchain.
/// * `address`: The `address` parameter is a variable of type `Addr` which represents the address of a
/// smart contract on the blockchain.
///
/// Returns:
///
/// The function `is_contract` returns a boolean value indicating whether the given address is a valid
/// smart contract on the blockchain or not. It does this by querying the blockchain through the
/// `querier` object to get information about the contract at the given `address`. If the query is
/// successful, it returns `true`, indicating that the address is a valid contract. If the query fails,
/// it returns false `
fn is_contract(querier: QuerierWrapper, address: &Addr) -> bool {
querier.query_wasm_contract_info(address).is_ok()
}