Skip to content

Commit 486ee78

Browse files
committed
Add Entrypoint enum
1 parent 7779079 commit 486ee78

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

Cargo.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/vm/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ allow_interface_version_7 = []
3535
bench = false
3636

3737
[dependencies]
38-
bytes = "1.4.0" # need a higher version than the one required by Wasmer for the Bytes -> Vec<u8> implementation
38+
bytes = "1.4.0" # need a higher version than the one required by Wasmer for the Bytes -> Vec<u8> implementation
3939
clru = "0.6.1"
4040
crc32fast = "1.3.2"
4141
# Uses the path when built locally; uses the given version from crates.io when published
@@ -50,12 +50,13 @@ sha2 = "0.10.3"
5050
thiserror = "1.0.26"
5151
wasmer = { version = "=4.2.2", default-features = false, features = ["cranelift", "singlepass"] }
5252
wasmer-middlewares = "=4.2.2"
53+
strum = { version = "0.25.0", default-features = false, features = ["derive"] }
5354

5455
# Dependencies that we do not use ourself. We add those entries
5556
# to bump the min version of them.
5657
bytecheck = "0.6.3" # With this version the simdutf8 dependency became optional
57-
enumset = "1.0.2" # Fixes https://github.com/Lymia/enumset/issues/17 (https://github.com/Lymia/enumset/commit/a430550cd6a3c9b1ef636d37f75dede7616f5b62)
58-
bitflags = "1.1.0" # https://github.com/CensoredUsername/dynasm-rs/pull/74
58+
enumset = "1.0.2" # Fixes https://github.com/Lymia/enumset/issues/17 (https://github.com/Lymia/enumset/commit/a430550cd6a3c9b1ef636d37f75dede7616f5b62)
59+
bitflags = "1.1.0" # https://github.com/CensoredUsername/dynasm-rs/pull/74
5960

6061
# Wasmer git/local (used for quick local debugging or patching)
6162
# wasmer = { git = "https://github.com/wasmerio/wasmer", rev = "877ce1f7c44fad853c", default-features = false, features = ["cranelift", "singlepass"] }
@@ -64,7 +65,7 @@ bitflags = "1.1.0" # https://github.com/CensoredUsername/dynasm-rs/pull/74
6465
# wasmer-middlewares = { path = "../../../wasmer/lib/middlewares" }
6566

6667
[dev-dependencies]
67-
criterion = { version = "0.4", features = [ "html_reports" ] }
68+
criterion = { version = "0.4", features = ["html_reports"] }
6869
glob = "0.3.1"
6970
hex-literal = "0.3.1"
7071
tempfile = "3.1.0"

packages/vm/src/cache.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::{self, File, OpenOptions};
33
use std::io::{Read, Write};
44
use std::marker::PhantomData;
55
use std::path::{Path, PathBuf};
6+
use std::str::FromStr;
67
use std::sync::Mutex;
78
use wasmer::{Engine, Store};
89

@@ -16,7 +17,7 @@ use crate::instance::{Instance, InstanceOptions};
1617
use crate::modules::{CachedModule, FileSystemCache, InMemoryCache, PinnedMemoryCache};
1718
use crate::parsed_wasm::ParsedWasm;
1819
use crate::size::Size;
19-
use crate::static_analysis::{ExportInfo, REQUIRED_IBC_EXPORTS};
20+
use crate::static_analysis::{Entrypoint, ExportInfo, REQUIRED_IBC_EXPORTS};
2021
use crate::wasm_backend::{compile, make_compiling_engine, make_runtime_engine};
2122

2223
const STATE_DIR: &str = "state";
@@ -120,12 +121,8 @@ pub struct AnalysisReport {
120121
/// `true` if and only if all [`REQUIRED_IBC_EXPORTS`] exist as exported functions.
121122
/// This does not guarantee they are functional or even have the correct signatures.
122123
pub has_ibc_entry_points: bool,
123-
/// `true` if the module has an `instantiate` export.
124-
/// This does not guarantee it is functional or even has the correct signature.
125-
pub has_instantiate_entry_point: bool,
126-
/// `true` if the module has a `migrate` export.
127-
/// This does not guarantee it is functional or even has the correct signature.
128-
pub has_migrate_entry_point: bool,
124+
/// A set of all entrypoints that are exported by the contract.
125+
pub entrypoints: HashSet<Entrypoint>,
129126
/// The set of capabilities the contract requires.
130127
pub required_capabilities: HashSet<String>,
131128
}
@@ -285,12 +282,16 @@ where
285282
let module = ParsedWasm::parse(&wasm)?;
286283
let exports = module.exported_function_names(None);
287284

285+
let entrypoints = exports
286+
.iter()
287+
.filter_map(|export| Entrypoint::from_str(export).ok())
288+
.collect();
289+
288290
Ok(AnalysisReport {
289291
has_ibc_entry_points: REQUIRED_IBC_EXPORTS
290292
.iter()
291293
.all(|required| exports.contains(*required)),
292-
has_instantiate_entry_point: exports.contains("instantiate"),
293-
has_migrate_entry_point: exports.contains("migrate"),
294+
entrypoints,
294295
required_capabilities: required_capabilities_from_module(&module),
295296
})
296297
}
@@ -1296,6 +1297,7 @@ mod tests {
12961297

12971298
#[test]
12981299
fn analyze_works() {
1300+
use Entrypoint as E;
12991301
let cache: Cache<MockApi, MockStorage, MockQuerier> =
13001302
unsafe { Cache::new(make_stargate_testing_options()).unwrap() };
13011303

@@ -1305,8 +1307,13 @@ mod tests {
13051307
report1,
13061308
AnalysisReport {
13071309
has_ibc_entry_points: false,
1308-
has_instantiate_entry_point: true,
1309-
has_migrate_entry_point: true,
1310+
entrypoints: HashSet::from([
1311+
E::Instantiate,
1312+
E::Migrate,
1313+
E::Sudo,
1314+
E::Execute,
1315+
E::Query
1316+
]),
13101317
required_capabilities: HashSet::new(),
13111318
}
13121319
);
@@ -1317,8 +1324,7 @@ mod tests {
13171324
report2,
13181325
AnalysisReport {
13191326
has_ibc_entry_points: true,
1320-
has_instantiate_entry_point: true,
1321-
has_migrate_entry_point: true,
1327+
entrypoints: HashSet::from([E::Instantiate, E::Reply, E::Query]),
13221328
required_capabilities: HashSet::from_iter([
13231329
"iterator".to_string(),
13241330
"stargate".to_string()
@@ -1332,8 +1338,7 @@ mod tests {
13321338
report3,
13331339
AnalysisReport {
13341340
has_ibc_entry_points: false,
1335-
has_instantiate_entry_point: false,
1336-
has_migrate_entry_point: false,
1341+
entrypoints: HashSet::new(),
13371342
required_capabilities: HashSet::from(["iterator".to_string()]),
13381343
}
13391344
);

packages/vm/src/static_analysis.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
use std::collections::HashSet;
22

3+
use strum::{Display, EnumString};
34
use wasmer::wasmparser::ExternalKind;
45

56
use crate::parsed_wasm::ParsedWasm;
67

8+
/// An enum containing all available contract entrypoints.
9+
/// This also provides conversions to and from strings.
10+
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash, EnumString, Display)]
11+
pub enum Entrypoint {
12+
#[strum(serialize = "instantiate")]
13+
Instantiate,
14+
#[strum(serialize = "execute")]
15+
Execute,
16+
#[strum(serialize = "migrate")]
17+
Migrate,
18+
#[strum(serialize = "sudo")]
19+
Sudo,
20+
#[strum(serialize = "reply")]
21+
Reply,
22+
#[strum(serialize = "query")]
23+
Query,
24+
#[strum(serialize = "ibc_channel_open")]
25+
IbcChannelOpen,
26+
#[strum(serialize = "ibc_channel_connect")]
27+
IbcChannelConnect,
28+
#[strum(serialize = "ibc_channel_close")]
29+
IbcChannelClose,
30+
#[strum(serialize = "ibc_packet_receive")]
31+
IbcPacketReceive,
32+
#[strum(serialize = "ibc_packet_ack")]
33+
IbcPacketAck,
34+
#[strum(serialize = "ibc_packet_timeout")]
35+
IbcPacketTimeout,
36+
}
37+
738
pub const REQUIRED_IBC_EXPORTS: &[&str] = &[
839
"ibc_channel_open",
940
"ibc_channel_connect",
@@ -62,6 +93,8 @@ impl ExportInfo for &wasmer::Module {
6293

6394
#[cfg(test)]
6495
mod tests {
96+
use std::str::FromStr;
97+
6598
use crate::VmError;
6699

67100
use super::*;
@@ -215,4 +248,22 @@ mod tests {
215248
HashSet::from_iter(vec!["bar".to_string(), "baz".to_string()])
216249
);
217250
}
251+
252+
#[test]
253+
fn entrypoint_from_string_works() {
254+
assert_eq!(
255+
Entrypoint::from_str("ibc_channel_open").unwrap(),
256+
Entrypoint::IbcChannelOpen
257+
);
258+
259+
assert!(Entrypoint::from_str("IbcChannelConnect").is_err());
260+
}
261+
262+
#[test]
263+
fn entrypoint_display_works() {
264+
assert_eq!(
265+
Entrypoint::IbcPacketTimeout.to_string(),
266+
"ibc_packet_timeout".to_string()
267+
);
268+
}
218269
}

0 commit comments

Comments
 (0)