Skip to content

Commit 0fe8896

Browse files
authored
Merge pull request #472 from CosmWasm/avoid-statics
Avoid using static for constants
2 parents db70416 + a776765 commit 0fe8896

File tree

9 files changed

+33
-34
lines changed

9 files changed

+33
-34
lines changed

contracts/hackatom/src/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct VerifierResponse {
6666
pub verifier: HumanAddr,
6767
}
6868

69-
pub static CONFIG_KEY: &[u8] = b"config";
69+
pub const CONFIG_KEY: &[u8] = b"config";
7070

7171
pub fn init<S: Storage, A: Api, Q: Querier>(
7272
deps: &mut Extern<S, A, Q>,

contracts/reflect/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
44
use cosmwasm_std::{CanonicalAddr, Storage};
55
use cosmwasm_storage::{singleton, singleton_read, ReadonlySingleton, Singleton};
66

7-
static CONFIG_KEY: &[u8] = b"config";
7+
const CONFIG_KEY: &[u8] = b"config";
88

99
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
1010
pub struct State {

packages/vm/src/backends/cranelift.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wasmer_runtime_core::{
77

88
use crate::errors::VmResult;
99

10-
static FAKE_GAS_AVAILABLE: u64 = 1_000_000;
10+
const FAKE_GAS_AVAILABLE: u64 = 1_000_000;
1111

1212
pub fn compile(code: &[u8]) -> VmResult<Module> {
1313
let config = CompilerConfig {

packages/vm/src/cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::instance::Instance;
1212
use crate::modules::FileSystemCache;
1313
use crate::traits::{Api, Extern, Querier, Storage};
1414

15-
static WASM_DIR: &str = "wasm";
16-
static MODULES_DIR: &str = "modules";
15+
const WASM_DIR: &str = "wasm";
16+
const MODULES_DIR: &str = "modules";
1717

1818
#[derive(Debug, Default, Clone)]
1919
struct Stats {
@@ -162,7 +162,7 @@ mod test {
162162
use tempfile::TempDir;
163163
use wabt::wat2wasm;
164164

165-
static TESTING_GAS_LIMIT: u64 = 400_000;
165+
const TESTING_GAS_LIMIT: u64 = 400_000;
166166
static CONTRACT: &[u8] = include_bytes!("../testdata/contract.wasm");
167167

168168
fn default_features() -> HashSet<String> {

packages/vm/src/compatability.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::features::required_features_from_module;
88

99
/// Lists all imports we provide upon instantiating the instance in Instance::from_module()
1010
/// This should be updated when new imports are added
11-
static SUPPORTED_IMPORTS: &[&str] = &[
11+
const SUPPORTED_IMPORTS: &[&str] = &[
1212
"env.db_read",
1313
"env.db_write",
1414
"env.db_remove",
@@ -24,7 +24,7 @@ static SUPPORTED_IMPORTS: &[&str] = &[
2424
/// Lists all entry points we expect to be present when calling a contract.
2525
/// Basically, anything that is used in calls.rs
2626
/// This is unlikely to change much, must be frozen at 1.0 to avoid breaking existing contracts
27-
static REQUIRED_EXPORTS: &[&str] = &[
27+
const REQUIRED_EXPORTS: &[&str] = &[
2828
"cosmwasm_vm_version_2",
2929
"query",
3030
"init",
@@ -33,7 +33,7 @@ static REQUIRED_EXPORTS: &[&str] = &[
3333
"deallocate",
3434
];
3535

36-
static MEMORY_LIMIT: u32 = 512; // in pages
36+
const MEMORY_LIMIT: u32 = 512; // in pages
3737

3838
/// Checks if the data is valid wasm and compatibility with the CosmWasm API (imports and exports)
3939
pub fn check_wasm(wasm_code: &[u8], supported_features: &HashSet<String>) -> VmResult<()> {
@@ -295,7 +295,7 @@ mod test {
295295
#[test]
296296
fn test_check_wasm_exports() {
297297
// this is invalid, as it doesn't contain all required exports
298-
static WAT_MISSING_EXPORTS: &'static str = r#"
298+
const WAT_MISSING_EXPORTS: &'static str = r#"
299299
(module
300300
(type $t0 (func (param i32) (result i32)))
301301
(func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)

packages/vm/src/context.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ pub fn add_iterator<'a, S: Storage, Q: Querier>(
255255
.try_into()
256256
.expect("Found more iterator IDs than supported");
257257
let new_id = last_id + 1;
258-
static INT32_MAX_VALUE: u32 = 2_147_483_647;
259-
if new_id > INT32_MAX_VALUE {
260-
panic!("Iterator ID exceeded INT32_MAX_VALUE. This must not happen.");
258+
if new_id > (i32::MAX as u32) {
259+
panic!("Iterator ID exceeded i32::MAX. This must not happen.");
261260
}
262261
b.iterators.insert(new_id, iter);
263262
new_id
@@ -356,12 +355,12 @@ mod test {
356355
type MQ = MockQuerier;
357356

358357
// prepared data
359-
static INIT_KEY: &[u8] = b"foo";
360-
static INIT_VALUE: &[u8] = b"bar";
358+
const INIT_KEY: &[u8] = b"foo";
359+
const INIT_VALUE: &[u8] = b"bar";
361360
// this account has some coins
362-
static INIT_ADDR: &str = "someone";
363-
static INIT_AMOUNT: u128 = 500;
364-
static INIT_DENOM: &str = "TOKEN";
361+
const INIT_ADDR: &str = "someone";
362+
const INIT_AMOUNT: u128 = 500;
363+
const INIT_DENOM: &str = "TOKEN";
365364

366365
const GAS_LIMIT: u64 = 5_000_000;
367366

packages/vm/src/features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashSet;
33
use std::iter::FromIterator;
44
use wasmer_runtime_core::{export::Export, Instance as WasmerInstance};
55

6-
static REQUIRES_PREFIX: &str = "requires_";
6+
const REQUIRES_PREFIX: &str = "requires_";
77

88
/// Takes a comma-separated string, splits it by commas, removes empty elements and returns a set of features.
99
/// This can be used e.g. to initialize the cache.

packages/vm/src/imports.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ use crate::serde::to_vec;
2323
use crate::traits::{Api, Querier, Storage};
2424

2525
/// A kibi (kilo binary)
26-
static KI: usize = 1024;
26+
const KI: usize = 1024;
2727
/// Max key length for db_write (i.e. when VM reads from Wasm memory)
28-
static MAX_LENGTH_DB_KEY: usize = 64 * KI;
28+
const MAX_LENGTH_DB_KEY: usize = 64 * KI;
2929
/// Max key length for db_write (i.e. when VM reads from Wasm memory)
30-
static MAX_LENGTH_DB_VALUE: usize = 128 * KI;
30+
const MAX_LENGTH_DB_VALUE: usize = 128 * KI;
3131
/// Typically 20 (Cosmos SDK, Ethereum) or 32 (Nano, Substrate)
3232
const MAX_LENGTH_CANONICAL_ADDRESS: usize = 32;
3333
/// The maximum allowed size for bech32 (https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32)
3434
const MAX_LENGTH_HUMAN_ADDRESS: usize = 90;
35-
static MAX_LENGTH_QUERY_CHAIN_REQUEST: usize = 64 * KI;
35+
const MAX_LENGTH_QUERY_CHAIN_REQUEST: usize = 64 * KI;
3636

3737
/// Reads a storage entry from the VM's storage into Wasm memory
3838
pub fn do_read<S: Storage, Q: Querier>(ctx: &mut Ctx, key_ptr: u32) -> VmResult<u32> {
@@ -214,15 +214,15 @@ mod test {
214214
type MQ = MockQuerier;
215215

216216
// prepared data
217-
static KEY1: &[u8] = b"ant";
218-
static VALUE1: &[u8] = b"insect";
219-
static KEY2: &[u8] = b"tree";
220-
static VALUE2: &[u8] = b"plant";
217+
const KEY1: &[u8] = b"ant";
218+
const VALUE1: &[u8] = b"insect";
219+
const KEY2: &[u8] = b"tree";
220+
const VALUE2: &[u8] = b"plant";
221221

222222
// this account has some coins
223-
static INIT_ADDR: &str = "someone";
224-
static INIT_AMOUNT: u128 = 500;
225-
static INIT_DENOM: &str = "TOKEN";
223+
const INIT_ADDR: &str = "someone";
224+
const INIT_AMOUNT: u128 = 500;
225+
const INIT_DENOM: &str = "TOKEN";
226226

227227
const GAS_LIMIT: u64 = 5_000_000;
228228

packages/vm/src/instance.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::imports::{do_next, do_scan};
2727
use crate::memory::{get_memory_info, read_region, write_region};
2828
use crate::traits::{Api, Extern, Querier, Storage};
2929

30-
static WASM_PAGE_SIZE: u64 = 64 * 1024;
30+
const WASM_PAGE_SIZE: u64 = 64 * 1024;
3131

3232
pub struct Instance<S: Storage + 'static, A: Api + 'static, Q: Querier + 'static> {
3333
/// We put this instance in a box to maintain a constant memory address for the entire
@@ -255,10 +255,10 @@ mod test {
255255
};
256256
use wabt::wat2wasm;
257257

258-
static KIB: usize = 1024;
259-
static MIB: usize = 1024 * 1024;
258+
const KIB: usize = 1024;
259+
const MIB: usize = 1024 * 1024;
260+
const DEFAULT_GAS_LIMIT: u64 = 500_000;
260261
static CONTRACT: &[u8] = include_bytes!("../testdata/contract.wasm");
261-
static DEFAULT_GAS_LIMIT: u64 = 500_000;
262262

263263
// shorthands for function generics below
264264
type MS = MockStorage;

0 commit comments

Comments
 (0)