Skip to content

Commit c820942

Browse files
authored
Merge pull request #669 from CosmWasm/change-argument-order
Use generic argument order A, S, Q consistently
2 parents 77d387f + a261403 commit c820942

File tree

11 files changed

+118
-83
lines changed

11 files changed

+118
-83
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
- Export method `cosmwasm_vm::Cache::stats` and response type `Stats`.
1717
- Remove `cosmwasm_vm::testing::MockApi::new`. Use `MockApi::default` instead.
1818
- Convert field `Instance::api` to a method.
19+
- Change order of generic arguments for consistency in `Instance`, `Cache` and
20+
`Backend` to always match `<A: Api, S: Storage, Q: Querier>`.
1921

2022
## 0.12.2 (2020-12-14)
2123

Diff for: contracts/queue/tests/integration.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use queue::contract::{
3131

3232
static WASM: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/release/queue.wasm");
3333

34-
fn create_contract() -> (Instance<MockStorage, MockApi, MockQuerier>, MessageInfo) {
34+
fn create_contract() -> (Instance<MockApi, MockStorage, MockQuerier>, MessageInfo) {
3535
let mut deps = mock_instance(WASM, &[]);
3636
let creator = HumanAddr(String::from("creator"));
3737
let info = mock_info(creator.as_str(), &[]);
@@ -40,13 +40,13 @@ fn create_contract() -> (Instance<MockStorage, MockApi, MockQuerier>, MessageInf
4040
(deps, info)
4141
}
4242

43-
fn get_count(deps: &mut Instance<MockStorage, MockApi, MockQuerier>) -> u32 {
43+
fn get_count(deps: &mut Instance<MockApi, MockStorage, MockQuerier>) -> u32 {
4444
let data = query(deps, mock_env(), QueryMsg::Count {}).unwrap();
4545
let res: CountResponse = from_binary(&data).unwrap();
4646
res.count
4747
}
4848

49-
fn get_sum(deps: &mut Instance<MockStorage, MockApi, MockQuerier>) -> i32 {
49+
fn get_sum(deps: &mut Instance<MockApi, MockStorage, MockQuerier>) -> i32 {
5050
let data = query(deps, mock_env(), QueryMsg::Sum {}).unwrap();
5151
let res: SumResponse = from_binary(&data).unwrap();
5252
res.sum

Diff for: contracts/reflect/tests/integration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ static WASM: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/release/re
4343
/// that supports SpecialQuery.
4444
pub fn mock_dependencies_with_custom_querier(
4545
contract_balance: &[Coin],
46-
) -> Backend<MockStorage, MockApi, MockQuerier<SpecialQuery>> {
46+
) -> Backend<MockApi, MockStorage, MockQuerier<SpecialQuery>> {
4747
let contract_addr = HumanAddr::from(MOCK_CONTRACT_ADDR);
4848
let custom_querier: MockQuerier<SpecialQuery> =
4949
MockQuerier::new(&[(&contract_addr, contract_balance)])
5050
.with_custom_handler(|query| SystemResult::Ok(custom_query_execute(query)));
5151

5252
Backend {
53-
storage: MockStorage::default(),
5453
api: MockApi::default(),
54+
storage: MockStorage::default(),
5555
querier: custom_querier,
5656
}
5757
}

Diff for: packages/vm/benches/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn bench_cache(c: &mut Criterion) {
8888
};
8989

9090
group.bench_function("save wasm", |b| {
91-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
91+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
9292
unsafe { Cache::new(options.clone()).unwrap() };
9393

9494
b.iter(|| {
@@ -103,7 +103,7 @@ fn bench_cache(c: &mut Criterion) {
103103
supported_features: features_from_csv("staking"),
104104
memory_cache_size: Size(0),
105105
};
106-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
106+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
107107
unsafe { Cache::new(non_memcache).unwrap() };
108108
let checksum = cache.save_wasm(CONTRACT).unwrap();
109109

@@ -119,7 +119,7 @@ fn bench_cache(c: &mut Criterion) {
119119

120120
group.bench_function("instantiate from memory", |b| {
121121
let checksum = Checksum::generate(CONTRACT);
122-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
122+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
123123
unsafe { Cache::new(options.clone()).unwrap() };
124124
// Load into memory
125125
cache

Diff for: packages/vm/src/backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ impl AddAssign for GasInfo {
5555
/// Designed to allow easy dependency injection at runtime.
5656
/// This cannot be copied or cloned since it would behave differently
5757
/// for mock storages and a bridge storage in the VM.
58-
pub struct Backend<S: Storage, A: Api, Q: Querier> {
59-
pub storage: S,
58+
pub struct Backend<A: Api, S: Storage, Q: Querier> {
6059
pub api: A,
60+
pub storage: S,
6161
pub querier: Q,
6262
}
6363

Diff for: packages/vm/src/cache.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ pub struct CacheOptions {
3030
pub memory_cache_size: Size,
3131
}
3232

33-
pub struct Cache<S: Storage, A: Api, Q: Querier> {
33+
pub struct Cache<A: Api, S: Storage, Q: Querier> {
3434
wasm_path: PathBuf,
3535
supported_features: HashSet<String>,
3636
memory_cache: InMemoryCache,
3737
fs_cache: FileSystemCache,
3838
stats: Stats,
3939
// Those two don't store data but only fix type information
40-
type_storage: PhantomData<S>,
4140
type_api: PhantomData<A>,
41+
type_storage: PhantomData<S>,
4242
type_querier: PhantomData<Q>,
4343
}
4444

45-
impl<S, A, Q> Cache<S, A, Q>
45+
impl<A, S, Q> Cache<A, S, Q>
4646
where
47-
S: Storage + 'static, // 'static is needed by `impl<…> Instance`
4847
A: Api + 'static, // 'static is needed by `impl<…> Instance`
48+
S: Storage + 'static, // 'static is needed by `impl<…> Instance`
4949
Q: Querier + 'static, // 'static is needed by `impl<…> Instance`
5050
{
5151
/// new stores the data for cache under base_dir
@@ -113,9 +113,9 @@ where
113113
pub fn get_instance(
114114
&mut self,
115115
checksum: &Checksum,
116-
backend: Backend<S, A, Q>,
116+
backend: Backend<A, S, Q>,
117117
options: InstanceOptions,
118-
) -> VmResult<Instance<S, A, Q>> {
118+
) -> VmResult<Instance<A, S, Q>> {
119119
let store = make_store_headless(Some(options.memory_limit));
120120
// Get module from memory cache
121121
if let Some(module) = self.memory_cache.load(checksum, &store)? {
@@ -218,15 +218,15 @@ mod tests {
218218

219219
#[test]
220220
fn save_wasm_works() {
221-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
221+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
222222
unsafe { Cache::new(make_testing_options()).unwrap() };
223223
cache.save_wasm(CONTRACT).unwrap();
224224
}
225225

226226
#[test]
227227
// This property is required when the same bytecode is uploaded multiple times
228228
fn save_wasm_allows_saving_multiple_times() {
229-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
229+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
230230
unsafe { Cache::new(make_testing_options()).unwrap() };
231231
cache.save_wasm(CONTRACT).unwrap();
232232
cache.save_wasm(CONTRACT).unwrap();
@@ -246,7 +246,7 @@ mod tests {
246246
)
247247
.unwrap();
248248

249-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
249+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
250250
unsafe { Cache::new(make_testing_options()).unwrap() };
251251
let save_result = cache.save_wasm(&wasm);
252252
match save_result.unwrap_err() {
@@ -276,7 +276,7 @@ mod tests {
276276

277277
#[test]
278278
fn load_wasm_works() {
279-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
279+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
280280
unsafe { Cache::new(make_testing_options()).unwrap() };
281281
let id = cache.save_wasm(CONTRACT).unwrap();
282282

@@ -295,7 +295,7 @@ mod tests {
295295
supported_features: default_features(),
296296
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
297297
};
298-
let mut cache1: Cache<MockStorage, MockApi, MockQuerier> =
298+
let mut cache1: Cache<MockApi, MockStorage, MockQuerier> =
299299
unsafe { Cache::new(options1).unwrap() };
300300
id = cache1.save_wasm(CONTRACT).unwrap();
301301
}
@@ -306,7 +306,7 @@ mod tests {
306306
supported_features: default_features(),
307307
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
308308
};
309-
let cache2: Cache<MockStorage, MockApi, MockQuerier> =
309+
let cache2: Cache<MockApi, MockStorage, MockQuerier> =
310310
unsafe { Cache::new(options2).unwrap() };
311311
let restored = cache2.load_wasm(&id).unwrap();
312312
assert_eq!(restored, CONTRACT);
@@ -315,7 +315,7 @@ mod tests {
315315

316316
#[test]
317317
fn load_wasm_errors_for_non_existent_id() {
318-
let cache: Cache<MockStorage, MockApi, MockQuerier> =
318+
let cache: Cache<MockApi, MockStorage, MockQuerier> =
319319
unsafe { Cache::new(make_testing_options()).unwrap() };
320320
let checksum = Checksum::from([
321321
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
@@ -339,7 +339,7 @@ mod tests {
339339
supported_features: default_features(),
340340
memory_cache_size: TESTING_MEMORY_CACHE_SIZE,
341341
};
342-
let mut cache: Cache<MockStorage, MockApi, MockQuerier> =
342+
let mut cache: Cache<MockApi, MockStorage, MockQuerier> =
343343
unsafe { Cache::new(options).unwrap() };
344344
let checksum = cache.save_wasm(CONTRACT).unwrap();
345345

Diff for: packages/vm/src/calls.rs

+57-27
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ const MAX_LENGTH_HANDLE: usize = 100_000;
1818
const MAX_LENGTH_MIGRATE: usize = 100_000;
1919
const MAX_LENGTH_QUERY: usize = 100_000;
2020

21-
pub fn call_init<S, A, Q, U>(
22-
instance: &mut Instance<S, A, Q>,
21+
pub fn call_init<A, S, Q, U>(
22+
instance: &mut Instance<A, S, Q>,
2323
env: &Env,
2424
info: &MessageInfo,
2525
msg: &[u8],
2626
) -> VmResult<ContractResult<InitResponse<U>>>
2727
where
28-
S: Storage + 'static,
2928
A: Api + 'static,
29+
S: Storage + 'static,
3030
Q: Querier + 'static,
3131
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
3232
{
@@ -37,15 +37,15 @@ where
3737
Ok(result)
3838
}
3939

40-
pub fn call_handle<S, A, Q, U>(
41-
instance: &mut Instance<S, A, Q>,
40+
pub fn call_handle<A, S, Q, U>(
41+
instance: &mut Instance<A, S, Q>,
4242
env: &Env,
4343
info: &MessageInfo,
4444
msg: &[u8],
4545
) -> VmResult<ContractResult<HandleResponse<U>>>
4646
where
47-
S: Storage + 'static,
4847
A: Api + 'static,
48+
S: Storage + 'static,
4949
Q: Querier + 'static,
5050
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
5151
{
@@ -56,15 +56,15 @@ where
5656
Ok(result)
5757
}
5858

59-
pub fn call_migrate<S, A, Q, U>(
60-
instance: &mut Instance<S, A, Q>,
59+
pub fn call_migrate<A, S, Q, U>(
60+
instance: &mut Instance<A, S, Q>,
6161
env: &Env,
6262
info: &MessageInfo,
6363
msg: &[u8],
6464
) -> VmResult<ContractResult<MigrateResponse<U>>>
6565
where
66-
S: Storage + 'static,
6766
A: Api + 'static,
67+
S: Storage + 'static,
6868
Q: Querier + 'static,
6969
U: DeserializeOwned + Clone + fmt::Debug + JsonSchema + PartialEq,
7070
{
@@ -75,11 +75,16 @@ where
7575
Ok(result)
7676
}
7777

78-
pub fn call_query<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
79-
instance: &mut Instance<S, A, Q>,
78+
pub fn call_query<A, S, Q>(
79+
instance: &mut Instance<A, S, Q>,
8080
env: &Env,
8181
msg: &[u8],
82-
) -> VmResult<ContractResult<QueryResponse>> {
82+
) -> VmResult<ContractResult<QueryResponse>>
83+
where
84+
A: Api + 'static,
85+
S: Storage + 'static,
86+
Q: Querier + 'static,
87+
{
8388
let env = to_vec(env)?;
8489
let data = call_query_raw(instance, &env, msg)?;
8590
let result: ContractResult<QueryResponse> = from_slice(&data)?;
@@ -96,57 +101,82 @@ pub fn call_query<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
96101

97102
/// Calls Wasm export "init" and returns raw data from the contract.
98103
/// The result is length limited to prevent abuse but otherwise unchecked.
99-
pub fn call_init_raw<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
100-
instance: &mut Instance<S, A, Q>,
104+
pub fn call_init_raw<A, S, Q>(
105+
instance: &mut Instance<A, S, Q>,
101106
env: &[u8],
102107
info: &[u8],
103108
msg: &[u8],
104-
) -> VmResult<Vec<u8>> {
109+
) -> VmResult<Vec<u8>>
110+
where
111+
A: Api + 'static,
112+
S: Storage + 'static,
113+
Q: Querier + 'static,
114+
{
105115
instance.set_storage_readonly(false);
106116
call_raw(instance, "init", &[env, info, msg], MAX_LENGTH_INIT)
107117
}
108118

109119
/// Calls Wasm export "handle" and returns raw data from the contract.
110120
/// The result is length limited to prevent abuse but otherwise unchecked.
111-
pub fn call_handle_raw<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
112-
instance: &mut Instance<S, A, Q>,
121+
pub fn call_handle_raw<A, S, Q>(
122+
instance: &mut Instance<A, S, Q>,
113123
env: &[u8],
114124
info: &[u8],
115125
msg: &[u8],
116-
) -> VmResult<Vec<u8>> {
126+
) -> VmResult<Vec<u8>>
127+
where
128+
A: Api + 'static,
129+
S: Storage + 'static,
130+
Q: Querier + 'static,
131+
{
117132
instance.set_storage_readonly(false);
118133
call_raw(instance, "handle", &[env, info, msg], MAX_LENGTH_HANDLE)
119134
}
120135

121136
/// Calls Wasm export "migrate" and returns raw data from the contract.
122137
/// The result is length limited to prevent abuse but otherwise unchecked.
123-
pub fn call_migrate_raw<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
124-
instance: &mut Instance<S, A, Q>,
138+
pub fn call_migrate_raw<A, S, Q>(
139+
instance: &mut Instance<A, S, Q>,
125140
env: &[u8],
126141
info: &[u8],
127142
msg: &[u8],
128-
) -> VmResult<Vec<u8>> {
143+
) -> VmResult<Vec<u8>>
144+
where
145+
A: Api + 'static,
146+
S: Storage + 'static,
147+
Q: Querier + 'static,
148+
{
129149
instance.set_storage_readonly(false);
130150
call_raw(instance, "migrate", &[env, info, msg], MAX_LENGTH_MIGRATE)
131151
}
132152

133153
/// Calls Wasm export "query" and returns raw data from the contract.
134154
/// The result is length limited to prevent abuse but otherwise unchecked.
135-
pub fn call_query_raw<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
136-
instance: &mut Instance<S, A, Q>,
155+
pub fn call_query_raw<A, S, Q>(
156+
instance: &mut Instance<A, S, Q>,
137157
env: &[u8],
138158
msg: &[u8],
139-
) -> VmResult<Vec<u8>> {
159+
) -> VmResult<Vec<u8>>
160+
where
161+
A: Api + 'static,
162+
S: Storage + 'static,
163+
Q: Querier + 'static,
164+
{
140165
instance.set_storage_readonly(true);
141166
call_raw(instance, "query", &[env, msg], MAX_LENGTH_QUERY)
142167
}
143168

144-
fn call_raw<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static>(
145-
instance: &mut Instance<S, A, Q>,
169+
fn call_raw<A, S, Q>(
170+
instance: &mut Instance<A, S, Q>,
146171
name: &str,
147172
args: &[&[u8]],
148173
result_max_length: usize,
149-
) -> VmResult<Vec<u8>> {
174+
) -> VmResult<Vec<u8>>
175+
where
176+
A: Api + 'static,
177+
S: Storage + 'static,
178+
Q: Querier + 'static,
179+
{
150180
let mut arg_region_ptrs = Vec::<Val>::with_capacity(args.len());
151181
for arg in args {
152182
let region_ptr = instance.allocate(arg.len())?;

0 commit comments

Comments
 (0)