Repository: CosmWasm/cosmwasm
Pull request #995 (2021-07-06 22:29:05)

Canonical address length bumped to 54, max bumped to 64


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 03ee47b30..555683db5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,13 @@ and this project adheres to
   enabled by default.
 - cosmwasm-std: Make `MockApi::canonical_length` private.
 - cosmwasm-vm: Make `MockApi::canonical_length` private.
+- cosmwasm-std/cosmwasm-vm: Increase canonical address lengths up to 64 bytes.
+- cosmwasm-std/cosmwasm-vm: In `MockApi`, increase max length of supported human
+  addresses from 24 bytes to 54 bytes by using a longer canonical
+  representation. This allows you to insert typical bech32 addresses in tests.
+  ([#995])
+
+[#995]: https://github.com/CosmWasm/cosmwasm/pull/995
 
 ## [0.15.0] - 2021-06-24
 
diff --git a/contracts/hackatom/src/contract.rs b/contracts/hackatom/src/contract.rs
index eb1486ce2..141a324ac 100644
--- a/contracts/hackatom/src/contract.rs
+++ b/contracts/hackatom/src/contract.rs
@@ -174,7 +174,8 @@ fn do_user_errors_in_api_calls(api: &dyn Api) -> Result<Response, HackError> {
         }
     }
 
-    let invalid_bech32 = "bn93hg934hg08q340g8u4jcau3";
+    let invalid_bech32 =
+        "bn9hhssomeltvhzgvuqkwjkpwxojfuigltwedayzxljucefikuieillowaticksoistqoynmgcnj219a";
     match api.addr_canonicalize(invalid_bech32).unwrap_err() {
         StdError::GenericErr { .. } => {}
         err => {
diff --git a/packages/std/src/imports.rs b/packages/std/src/imports.rs
index d7a2f7cac..b4c983d1d 100644
--- a/packages/std/src/imports.rs
+++ b/packages/std/src/imports.rs
@@ -18,7 +18,7 @@ use crate::{
 };
 
 /// An upper bound for typical canonical address lengths (e.g. 20 in Cosmos SDK/Ethereum or 32 in Nano/Substrate)
-const CANONICAL_ADDRESS_BUFFER_LENGTH: usize = 32;
+const CANONICAL_ADDRESS_BUFFER_LENGTH: usize = 64;
 /// An upper bound for typical human readable address formats (e.g. 42 for Ethereum hex addresses or 90 for bech32)
 const HUMAN_ADDRESS_BUFFER_LENGTH: usize = 90;
 
diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs
index 2a34126b0..1fb58c66d 100644
--- a/packages/std/src/mock.rs
+++ b/packages/std/src/mock.rs
@@ -58,7 +58,7 @@ pub type MockStorage = MemoryStorage;
 /// Length of canonical addresses created with this API. Contracts should not make any assumtions
 /// what this value is.
 /// The value here must be restorable with `SHUFFLES_ENCODE` + `SHUFFLES_DECODE` in-shuffles.
-const CANONICAL_LENGTH: usize = 24;
+const CANONICAL_LENGTH: usize = 54;
 
 const SHUFFLES_ENCODE: usize = 18;
 const SHUFFLES_DECODE: usize = 2;
@@ -633,7 +633,8 @@ mod tests {
     #[should_panic(expected = "address too long")]
     fn addr_canonicalize_max_input_length() {
         let api = MockApi::default();
-        let human = String::from("some-extremely-long-address-not-supported-by-this-api");
+        let human =
+            String::from("some-extremely-long-address-not-supported-by-this-api-longer-than-54");
         let _ = api.addr_canonicalize(&human).unwrap();
     }
 
diff --git a/packages/vm/src/imports.rs b/packages/vm/src/imports.rs
index 84c0fbfa7..556b89f4c 100644
--- a/packages/vm/src/imports.rs
+++ b/packages/vm/src/imports.rs
@@ -34,8 +34,8 @@ const MI: usize = 1024 * 1024;
 const MAX_LENGTH_DB_KEY: usize = 64 * KI;
 /// Max key length for db_write (i.e. when VM reads from Wasm memory)
 const MAX_LENGTH_DB_VALUE: usize = 128 * KI;
-/// Typically 20 (Cosmos SDK, Ethereum) or 32 (Nano, Substrate)
-const MAX_LENGTH_CANONICAL_ADDRESS: usize = 32;
+/// Typically 20 (Cosmos SDK, Ethereum), 32 (Nano, Substrate) or 54 (MockApi)
+const MAX_LENGTH_CANONICAL_ADDRESS: usize = 64;
 /// The maximum allowed size for bech32 (https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32)
 const MAX_LENGTH_HUMAN_ADDRESS: usize = 90;
 const MAX_LENGTH_QUERY_CHAIN_REQUEST: usize = 64 * KI;
@@ -948,7 +948,7 @@ mod tests {
 
         let source_ptr1 = write_data(&env, b"fo\x80o"); // invalid UTF-8 (fo�o)
         let source_ptr2 = write_data(&env, b""); // empty
-        let source_ptr3 = write_data(&env, b"addressexceedingaddressspace"); // too long
+        let source_ptr3 = write_data(&env, b"addressexceedingaddressspacesuperlongreallylongiamensuringthatitislongerthaneverything"); // too long
 
         leave_default_data(&env);
 
@@ -1039,8 +1039,8 @@ mod tests {
 
         let source_ptr1 = write_data(&env, b"fo\x80o"); // invalid UTF-8 (fo�o)
         let source_ptr2 = write_data(&env, b""); // empty
-        let source_ptr3 = write_data(&env, b"addressexceedingaddressspace"); // too long
-        let dest_ptr = create_empty(&mut instance, 8);
+        let source_ptr3 = write_data(&env, b"addressexceedingaddressspacesuperlongreallylongiamensuringthatitislongerthaneverything"); // too long
+        let dest_ptr = create_empty(&mut instance, 70);
 
         leave_default_data(&env);
 
@@ -1139,7 +1139,7 @@ mod tests {
 
         let source_data = vec![0x22; api.canonical_length()];
         let source_ptr = write_data(&env, &source_data);
-        let dest_ptr = create_empty(&mut instance, 50);
+        let dest_ptr = create_empty(&mut instance, 70);
 
         leave_default_data(&env);
 
@@ -1154,7 +1154,7 @@ mod tests {
         let (env, mut instance) = make_instance(api);
 
         let source_ptr = write_data(&env, b"foo"); // too short
-        let dest_ptr = create_empty(&mut instance, 50);
+        let dest_ptr = create_empty(&mut instance, 70);
 
         leave_default_data(&env);
 
@@ -1170,7 +1170,7 @@ mod tests {
         let (env, mut instance) = make_instance(api);
 
         let source_ptr = write_data(&env, b"foo\0\0\0\0\0");
-        let dest_ptr = create_empty(&mut instance, 50);
+        let dest_ptr = create_empty(&mut instance, 70);
 
         leave_default_data(&env);
 
@@ -1189,8 +1189,8 @@ mod tests {
         let api = MockApi::default();
         let (env, mut instance) = make_instance(api);
 
-        let source_ptr = write_data(&env, &[61; 33]);
-        let dest_ptr = create_empty(&mut instance, 50);
+        let source_ptr = write_data(&env, &[61; 65]);
+        let dest_ptr = create_empty(&mut instance, 70);
 
         leave_default_data(&env);
 
@@ -1203,8 +1203,8 @@ mod tests {
                     },
                 ..
             } => {
-                assert_eq!(length, 33);
-                assert_eq!(max_length, 32);
+                assert_eq!(length, 65);
+                assert_eq!(max_length, 64);
             }
             err => panic!("Incorrect error returned: {:?}", err),
         }


Test patch:
diff --git a/packages/vm/src/testing/mock.rs b/packages/vm/src/testing/mock.rs
index 8966417f2..e71094df7 100644
--- a/packages/vm/src/testing/mock.rs
+++ b/packages/vm/src/testing/mock.rs
@@ -34,7 +34,7 @@ pub fn mock_backend_with_balances(
 /// Length of canonical addresses created with this API. Contracts should not make any assumtions
 /// what this value is.
 /// The value here must be restorable with `SHUFFLES_ENCODE` + `SHUFFLES_DECODE` in-shuffles.
-const CANONICAL_LENGTH: usize = 24;
+const CANONICAL_LENGTH: usize = 54;
 
 const SHUFFLES_ENCODE: usize = 18;
 const SHUFFLES_DECODE: usize = 2;
@@ -230,7 +230,7 @@ mod tests {
     #[test]
     fn canonical_address_max_input_length() {
         let api = MockApi::default();
-        let human = "longer-than-the-address-length-supported-by-this-api";
+        let human = "longer-than-the-address-length-supported-by-this-api-longer-than-54";
         match api.canonical_address(human).0.unwrap_err() {
             BackendError::UserErr { .. } => {}
             err => panic!("Unexpected error: {:?}", err),