Skip to content

Commit b9963b2

Browse files
committed
remove cosmwasm-vm dependency
1 parent 989842b commit b9963b2

File tree

6 files changed

+95
-7
lines changed

6 files changed

+95
-7
lines changed

packages/vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ wasmer-middlewares = "2.3.0"
1717
# owasm-crypto = { path = "../crypto", version = "0.1.13" }
1818
tempfile = "3.1.0"
1919
clru = "0.2.0"
20-
cosmwasm-vm = "0.13.2"
20+
sha2 = "0.9.1"

packages/vm/src/cache.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::error::Error;
1+
use crate::{error::Error, checksum::Checksum};
22

33
use clru::CLruCache;
4-
use cosmwasm_vm::Checksum;
54
use wasmer::{Instance, Module, Store};
65

76
#[derive(Debug, Default, Clone, Copy, PartialEq)]

packages/vm/src/checksum.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::convert::TryFrom;
2+
3+
use sha2::{Digest, Sha256};
4+
5+
use crate::Error;
6+
7+
/// A SHA-256 checksum of a Wasm blob, used to identify a Wasm code.
8+
/// This must remain stable since this checksum is stored in the blockchain state.
9+
///
10+
/// This is often referred to as "code ID" in go-cosmwasm, even if code ID
11+
/// usually refers to an auto-incrementing number.
12+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13+
pub struct Checksum([u8; 32]);
14+
15+
impl Checksum {
16+
pub fn generate(wasm: &[u8]) -> Self {
17+
Checksum(Sha256::digest(wasm).into())
18+
}
19+
20+
/// Creates a lowercase hex encoded copy of this checksum
21+
pub fn to_hex(&self) -> String {
22+
hex::encode(self.0)
23+
}
24+
}
25+
26+
impl From<[u8; 32]> for Checksum {
27+
fn from(data: [u8; 32]) -> Self {
28+
Checksum(data)
29+
}
30+
}
31+
32+
impl TryFrom<&[u8]> for Checksum {
33+
type Error = Error;
34+
35+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
36+
if value.len() != 32 {
37+
return Err(Error::ChecksumLengthNotMatch);
38+
}
39+
let mut data = [0u8; 32];
40+
data.copy_from_slice(value);
41+
Ok(Checksum(data))
42+
}
43+
}
44+
45+
impl Into<Vec<u8>> for Checksum {
46+
fn into(self) -> Vec<u8> {
47+
// Rust 1.43+ also supports self.0.into()
48+
self.0.to_vec()
49+
}
50+
}
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
fn generate_works() {
58+
let wasm = vec![0x68, 0x69, 0x6a];
59+
let checksum = Checksum::generate(&wasm);
60+
61+
// echo -n "hij" | sha256sum
62+
let expected = [
63+
0x72, 0x2c, 0x8c, 0x99, 0x3f, 0xd7, 0x5a, 0x76, 0x27, 0xd6, 0x9e, 0xd9, 0x41, 0x34,
64+
0x4f, 0xe2, 0xa1, 0x42, 0x3a, 0x3e, 0x75, 0xef, 0xd3, 0xe6, 0x77, 0x8a, 0x14, 0x28,
65+
0x84, 0x22, 0x71, 0x04,
66+
];
67+
assert_eq!(checksum.0, expected);
68+
}
69+
70+
#[test]
71+
fn to_hex_works() {
72+
let wasm = vec![0x68, 0x69, 0x6a];
73+
let checksum = Checksum::generate(&wasm);
74+
// echo -n "hij" | sha256sum
75+
assert_eq!(
76+
checksum.to_hex(),
77+
"722c8c993fd75a7627d69ed941344fe2a1423a3e75efd3e6778a142884227104"
78+
);
79+
}
80+
81+
#[test]
82+
fn into_vec_works() {
83+
let checksum = Checksum::generate(&vec![12u8; 17]);
84+
let as_vec: Vec<u8> = checksum.into();
85+
assert_eq!(as_vec, checksum.0);
86+
}
87+
}

packages/vm/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub enum Error {
2121
OutOfGasError = 12, // Out-of-gas while executing the Wasm script.
2222
BadEntrySignatureError = 13, // Bad execution entry point signature.
2323
MemoryOutOfBoundError = 14, // Out-of-bound memory access while executing the wasm script
24+
UninitializedContextData = 15, // Error while getting uninitialized context data.
25+
ChecksumLengthNotMatch = 16, // Checksum not of intended length.
2426
// Host-generated errors while interacting with OEI.
2527
WrongPeriodActionError = 128, // OEI action to invoke is not available.
2628
TooManyExternalDataError = 129, // Too many external data requests.

packages/vm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod cache;
22
pub mod calls;
3+
mod checksum;
34
pub mod compile;
45
pub mod error;
56
mod imports;

packages/vm/src/vm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::borrow::Borrow;
33
use std::ptr::NonNull;
44
use std::sync::{Arc, Mutex, RwLock};
55

6-
use cosmwasm_vm::{VmError, VmResult};
76
use wasmer::{Instance, Memory, WasmerEnv};
87
use wasmer_middlewares::metering::{get_remaining_points, set_remaining_points, MeteringPoints};
98

@@ -109,16 +108,16 @@ where
109108
data.wasmer_instance = instance;
110109
}
111110

112-
pub fn with_wasmer_instance<C, R>(&self, callback: C) -> VmResult<R>
111+
pub fn with_wasmer_instance<C, R>(&self, callback: C) -> Result<R, Error>
113112
where
114-
C: FnOnce(&Instance) -> VmResult<R>,
113+
C: FnOnce(&Instance) -> Result<R, Error>,
115114
{
116115
self.with_context_data(|context_data| match context_data.wasmer_instance {
117116
Some(instance_ptr) => {
118117
let instance_ref = unsafe { instance_ptr.as_ref() };
119118
callback(instance_ref)
120119
}
121-
None => Err(VmError::UninitializedContextData { kind: "wasmer_instance".to_string() }),
120+
None => Err(Error::UninitializedContextData),
122121
})
123122
}
124123

0 commit comments

Comments
 (0)