Skip to content

Commit 2f5b15e

Browse files
committed
Remove the ability to change canonical length in VM's MockApi
1 parent 0db5945 commit 2f5b15e

File tree

5 files changed

+56
-56
lines changed

5 files changed

+56
-56
lines changed

contracts/staking/tests/integration.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn sample_validator<U: Into<HumanAddr>>(addr: U) -> Validator {
4343

4444
#[test]
4545
fn initialization_with_missing_validator() {
46-
let mut ext = mock_dependencies(20, &[]);
46+
let mut ext = mock_dependencies(&[]);
4747
ext.querier
4848
.update_staking("ustake", &[sample_validator("john")], &[]);
4949
let mut deps = Instance::from_code(WASM, ext, 500_000, false).unwrap();
@@ -71,7 +71,7 @@ fn initialization_with_missing_validator() {
7171
#[test]
7272
fn proper_initialization() {
7373
// we need to use the verbose approach here to customize the querier with staking info
74-
let mut ext = mock_dependencies(20, &[]);
74+
let mut ext = mock_dependencies(&[]);
7575
ext.querier.update_staking(
7676
"ustake",
7777
&[

packages/vm/src/imports.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,17 @@ mod test {
571571
#[test]
572572
fn do_canonicalize_address_works() {
573573
let mut instance = make_instance();
574+
let api = MockApi::default();
574575

575576
let source_ptr = write_data(&mut instance, b"foo");
576-
let dest_ptr = create_empty(&mut instance, 8);
577+
let dest_ptr = create_empty(&mut instance, api.canonical_length as u32);
577578

578579
let ctx = instance.context_mut();
579580
leave_default_data(ctx);
580581

581-
let api = MockApi::new(8);
582582
do_canonicalize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr).unwrap();
583-
assert_eq!(force_read(ctx, dest_ptr), b"foo\0\0\0\0\0");
583+
let data = force_read(ctx, dest_ptr);
584+
assert_eq!(data.len(), api.canonical_length);
584585
}
585586

586587
#[test]
@@ -594,7 +595,7 @@ mod test {
594595

595596
let ctx = instance.context_mut();
596597
leave_default_data(ctx);
597-
let api = MockApi::new(8);
598+
let api = MockApi::default();
598599

599600
let res = do_canonicalize_address::<MA, MS, MQ>(api, ctx, source_ptr1, dest_ptr).unwrap();
600601
assert_ne!(res, 0);
@@ -622,7 +623,7 @@ mod test {
622623
let ctx = instance.context_mut();
623624
leave_default_data(ctx);
624625

625-
let api = MockApi::new_failing(8, "Temporarily unavailable");
626+
let api = MockApi::new_failing("Temporarily unavailable");
626627
let result = do_canonicalize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr);
627628
match result.unwrap_err() {
628629
VmError::FfiErr {
@@ -644,7 +645,7 @@ mod test {
644645
let ctx = instance.context_mut();
645646
leave_default_data(ctx);
646647

647-
let api = MockApi::new(8);
648+
let api = MockApi::default();
648649
let result = do_canonicalize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr);
649650
match result.unwrap_err() {
650651
VmError::CommunicationErr {
@@ -670,14 +671,14 @@ mod test {
670671
let ctx = instance.context_mut();
671672
leave_default_data(ctx);
672673

673-
let api = MockApi::new(8);
674+
let api = MockApi::default();
674675
let result = do_canonicalize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr);
675676
match result.unwrap_err() {
676677
VmError::CommunicationErr {
677678
source: CommunicationError::RegionTooSmall { size, required, .. },
678679
} => {
679680
assert_eq!(size, 7);
680-
assert_eq!(required, 8);
681+
assert_eq!(required, api.canonical_length);
681682
}
682683
err => panic!("Incorrect error returned: {:?}", err),
683684
}
@@ -686,17 +687,18 @@ mod test {
686687
#[test]
687688
fn do_humanize_address_works() {
688689
let mut instance = make_instance();
690+
let api = MockApi::default();
689691

690-
let source_ptr = write_data(&mut instance, b"foo\0\0\0\0\0");
692+
let source_data = vec![0x22; api.canonical_length];
693+
let source_ptr = write_data(&mut instance, &source_data);
691694
let dest_ptr = create_empty(&mut instance, 50);
692695

693696
let ctx = instance.context_mut();
694697
leave_default_data(ctx);
695698

696-
let api = MockApi::new(8);
697699
let error_ptr = do_humanize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr).unwrap();
698700
assert_eq!(error_ptr, 0);
699-
assert_eq!(force_read(ctx, dest_ptr), b"foo");
701+
assert_eq!(force_read(ctx, dest_ptr), source_data);
700702
}
701703

702704
#[test]
@@ -709,7 +711,7 @@ mod test {
709711
let ctx = instance.context_mut();
710712
leave_default_data(ctx);
711713

712-
let api = MockApi::new(8);
714+
let api = MockApi::default();
713715
let res = do_humanize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr).unwrap();
714716
assert_ne!(res, 0);
715717
let err = String::from_utf8(force_read(ctx, res)).unwrap();
@@ -726,7 +728,7 @@ mod test {
726728
let ctx = instance.context_mut();
727729
leave_default_data(ctx);
728730

729-
let api = MockApi::new_failing(8, "Temporarily unavailable");
731+
let api = MockApi::new_failing("Temporarily unavailable");
730732
let result = do_humanize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr);
731733
match result.unwrap_err() {
732734
VmError::FfiErr {
@@ -746,7 +748,7 @@ mod test {
746748
let ctx = instance.context_mut();
747749
leave_default_data(ctx);
748750

749-
let api = MockApi::new(8);
751+
let api = MockApi::default();
750752
let result = do_humanize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr);
751753
match result.unwrap_err() {
752754
VmError::CommunicationErr {
@@ -765,21 +767,22 @@ mod test {
765767
#[test]
766768
fn do_humanize_address_fails_for_destination_region_too_small() {
767769
let mut instance = make_instance();
770+
let api = MockApi::default();
768771

769-
let source_ptr = write_data(&mut instance, b"foo\0\0\0\0\0");
772+
let source_data = vec![0x22; api.canonical_length];
773+
let source_ptr = write_data(&mut instance, &source_data);
770774
let dest_ptr = create_empty(&mut instance, 2);
771775

772776
let ctx = instance.context_mut();
773777
leave_default_data(ctx);
774778

775-
let api = MockApi::new(8);
776779
let result = do_humanize_address::<MA, MS, MQ>(api, ctx, source_ptr, dest_ptr);
777780
match result.unwrap_err() {
778781
VmError::CommunicationErr {
779782
source: CommunicationError::RegionTooSmall { size, required, .. },
780783
} => {
781784
assert_eq!(size, 2);
782-
assert_eq!(required, 3);
785+
assert_eq!(required, api.canonical_length);
783786
}
784787
err => panic!("Incorrect error returned: {:?}", err),
785788
}

packages/vm/src/instance.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ mod test {
310310

311311
#[test]
312312
fn required_features_works() {
313-
let deps = mock_dependencies(20, &[]);
313+
let deps = mock_dependencies(&[]);
314314
let instance = Instance::from_code(CONTRACT, deps, DEFAULT_GAS_LIMIT, false).unwrap();
315315
assert_eq!(instance.required_features.len(), 0);
316316
}
@@ -331,7 +331,7 @@ mod test {
331331
)
332332
.unwrap();
333333

334-
let deps = mock_dependencies(20, &[]);
334+
let deps = mock_dependencies(&[]);
335335
let instance = Instance::from_code(&wasm, deps, DEFAULT_GAS_LIMIT, false).unwrap();
336336
assert_eq!(instance.required_features.len(), 3);
337337
assert!(instance.required_features.contains("nutrients"));

packages/vm/src/testing/instance.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub fn mock_instance_with_gas_limit(
7070
#[derive(Debug)]
7171
pub struct MockInstanceOptions<'a> {
7272
// dependencies
73-
pub canonical_address_length: usize,
7473
pub balances: &'a [(&'a HumanAddr, &'a [Coin])],
7574
/// This option is merged into balances and might override an existing value
7675
pub contract_balance: Option<&'a [Coin]>,
@@ -87,7 +86,6 @@ impl Default for MockInstanceOptions<'_> {
8786
fn default() -> Self {
8887
Self {
8988
// dependencies
90-
canonical_address_length: 20,
9189
balances: Default::default(),
9290
contract_balance: Default::default(),
9391
backend_error: None,
@@ -118,9 +116,9 @@ pub fn mock_instance_with_options(
118116
}
119117

120118
let api = if let Some(backend_error) = options.backend_error {
121-
MockApi::new_failing(options.canonical_address_length, backend_error)
119+
MockApi::new_failing(backend_error)
122120
} else {
123-
MockApi::new(options.canonical_address_length)
121+
MockApi::default()
124122
};
125123

126124
let deps = Extern {

packages/vm/src/testing/mock.rs

+30-31
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,23 @@ const GAS_COST_CANONICALIZE: u64 = 55;
1212

1313
/// All external requirements that can be injected for unit tests.
1414
/// It sets the given balance for the contract itself, nothing else
15-
pub fn mock_dependencies(
16-
canonical_length: usize,
17-
contract_balance: &[Coin],
18-
) -> Extern<MockStorage, MockApi, MockQuerier> {
15+
pub fn mock_dependencies(contract_balance: &[Coin]) -> Extern<MockStorage, MockApi, MockQuerier> {
1916
let contract_addr = HumanAddr::from(MOCK_CONTRACT_ADDR);
2017
Extern {
2118
storage: MockStorage::default(),
22-
api: MockApi::new(canonical_length),
19+
api: MockApi::default(),
2320
querier: MockQuerier::new(&[(&contract_addr, contract_balance)]),
2421
}
2522
}
2623

2724
/// Initializes the querier along with the mock_dependencies.
2825
/// Sets all balances provided (yoy must explicitly set contract balance if desired)
2926
pub fn mock_dependencies_with_balances(
30-
canonical_length: usize,
3127
balances: &[(&HumanAddr, &[Coin])],
3228
) -> Extern<MockStorage, MockApi, MockQuerier> {
3329
Extern {
3430
storage: MockStorage::default(),
35-
api: MockApi::new(canonical_length),
31+
api: MockApi::default(),
3632
querier: MockQuerier::new(balances),
3733
}
3834
}
@@ -42,30 +38,36 @@ pub fn mock_dependencies_with_balances(
4238
/// This is not really smart, but allows us to see a difference (and consistent length for canonical adddresses).
4339
#[derive(Copy, Clone)]
4440
pub struct MockApi {
45-
canonical_length: usize,
41+
/// Length of canonical addresses created with this API. Contracts should not make any assumtions
42+
/// what this value is.
43+
pub canonical_length: usize,
4644
/// When set, all calls to the API fail with FfiError::Unknown containing this message
4745
backend_error: Option<&'static str>,
4846
}
4947

5048
impl MockApi {
51-
pub fn new(canonical_length: usize) -> Self {
52-
MockApi {
53-
canonical_length,
54-
backend_error: None,
55-
}
49+
#[deprecated(
50+
since = "0.11.0",
51+
note = "The canonical length argument is unused. Use MockApi::default() instead."
52+
)]
53+
pub fn new(_canonical_length: usize) -> Self {
54+
MockApi::default()
5655
}
5756

58-
pub fn new_failing(canonical_length: usize, backend_error: &'static str) -> Self {
57+
pub fn new_failing(backend_error: &'static str) -> Self {
5958
MockApi {
60-
canonical_length,
6159
backend_error: Some(backend_error),
60+
..MockApi::default()
6261
}
6362
}
6463
}
6564

6665
impl Default for MockApi {
6766
fn default() -> Self {
68-
Self::new(20)
67+
MockApi {
68+
canonical_length: 24,
69+
backend_error: None,
70+
}
6971
}
7072
}
7173

@@ -174,21 +176,18 @@ mod test {
174176
}
175177

176178
#[test]
177-
fn flip_addresses() {
178-
let api = MockApi::new(20);
179-
let human = HumanAddr("shorty".to_string());
180-
let canon = api.canonical_address(&human).0.unwrap();
181-
assert_eq!(canon.len(), 20);
182-
assert_eq!(&canon.as_slice()[0..6], human.as_str().as_bytes());
183-
assert_eq!(&canon.as_slice()[6..], &[0u8; 14]);
184-
185-
let (recovered, _gas_cost) = api.human_address(&canon);
186-
assert_eq!(recovered.unwrap(), human);
179+
fn canonicalize_and_humanize_restores_original() {
180+
let api = MockApi::default();
181+
182+
let original = HumanAddr::from("shorty");
183+
let canonical = api.canonical_address(&original).0.unwrap();
184+
let (recovered, _gas_cost) = api.human_address(&canonical);
185+
assert_eq!(recovered.unwrap(), original);
187186
}
188187

189188
#[test]
190189
fn human_address_input_length() {
191-
let api = MockApi::new(10);
190+
let api = MockApi::default();
192191
let input = CanonicalAddr(Binary(vec![61; 11]));
193192
let (result, _gas_info) = api.human_address(&input);
194193
match result.unwrap_err() {
@@ -199,8 +198,8 @@ mod test {
199198

200199
#[test]
201200
fn canonical_address_min_input_length() {
202-
let api = MockApi::new(10);
203-
let human = HumanAddr("1".to_string());
201+
let api = MockApi::default();
202+
let human = HumanAddr::from("1");
204203
match api.canonical_address(&human).0.unwrap_err() {
205204
FfiError::UserErr { .. } => {}
206205
err => panic!("Unexpected error: {:?}", err),
@@ -209,8 +208,8 @@ mod test {
209208

210209
#[test]
211210
fn canonical_address_max_input_length() {
212-
let api = MockApi::new(10);
213-
let human = HumanAddr("longer-than-10".to_string());
211+
let api = MockApi::default();
212+
let human = HumanAddr::from("longer-than-the-address-length-supported-by-this-api");
214213
match api.canonical_address(&human).0.unwrap_err() {
215214
FfiError::UserErr { .. } => {}
216215
err => panic!("Unexpected error: {:?}", err),

0 commit comments

Comments
 (0)