Skip to content

Commit

Permalink
Add SimulateStoreCode function
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Dec 10, 2024
1 parent 3c3e2e5 commit 558ea9e
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 44 deletions.
2 changes: 1 addition & 1 deletion internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestValidateAddressFailure(t *testing.T) {
// create contract
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)
checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
Expand Down
9 changes: 5 additions & 4 deletions internal/api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,11 @@ struct cache_t *init_cache(struct ByteSliceView data_dir,
uint32_t instance_memory_limit,
struct UnmanagedVector *error_msg);

struct UnmanagedVector save_wasm(struct cache_t *cache,
struct ByteSliceView wasm,
bool unchecked,
struct UnmanagedVector *error_msg);
struct UnmanagedVector store_code(struct cache_t *cache,
struct ByteSliceView wasm,
bool checked,
bool persist,
struct UnmanagedVector *error_msg);

void remove_wasm(struct cache_t *cache,
struct ByteSliceView checksum,
Expand Down
6 changes: 3 additions & 3 deletions internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func ReleaseCache(cache Cache) {
C.release_cache(cache.ptr)
}

func StoreCode(cache Cache, wasm []byte) ([]byte, error) {
func StoreCode(cache Cache, wasm []byte, persist bool) ([]byte, error) {
w := makeView(wasm)
defer runtime.KeepAlive(wasm)
errmsg := uninitializedUnmanagedVector()
checksum, err := C.save_wasm(cache.ptr, w, cbool(false), &errmsg)
checksum, err := C.store_code(cache.ptr, w, cbool(true), cbool(persist), &errmsg)
if err != nil {
return nil, errorWithMessage(err, errmsg)
}
Expand All @@ -73,7 +73,7 @@ func StoreCodeUnchecked(cache Cache, wasm []byte) ([]byte, error) {
w := makeView(wasm)
defer runtime.KeepAlive(wasm)
errmsg := uninitializedUnmanagedVector()
checksum, err := C.save_wasm(cache.ptr, w, cbool(true), &errmsg)
checksum, err := C.store_code(cache.ptr, w, cbool(true), cbool(true), &errmsg)
if err != nil {
return nil, errorWithMessage(err, errmsg)
}
Expand Down
16 changes: 8 additions & 8 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestStoreCodeAndGetCode(t *testing.T) {
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)

checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)
expectedChecksum := sha256.Sum256(wasm)
require.Equal(t, expectedChecksum[:], checksum)
Expand All @@ -104,7 +104,7 @@ func TestRemoveCode(t *testing.T) {
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)

checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

// First removal works
Expand All @@ -121,7 +121,7 @@ func TestStoreCodeFailsWithBadData(t *testing.T) {
defer cleanup()

wasm := []byte("some invalid data")
_, err := StoreCode(cache, wasm)
_, err := StoreCode(cache, wasm, true)
require.Error(t, err)
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func TestPin(t *testing.T) {
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)

checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

err = Pin(cache, checksum)
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestUnpin(t *testing.T) {
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)

checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

err = Pin(cache, checksum)
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestGetMetrics(t *testing.T) {
// Store contract
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)
checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

// GetMetrics 2
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestInstantiate(t *testing.T) {
// create contract
wasm, err := ioutil.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)
checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
Expand Down Expand Up @@ -916,7 +916,7 @@ func createFloaty2(t *testing.T, cache Cache) []byte {
func createContract(t *testing.T, cache Cache, wasmFile string) []byte {
wasm, err := ioutil.ReadFile(wasmFile)
require.NoError(t, err)
checksum, err := StoreCode(cache, wasm)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)
return checksum
}
Expand Down
9 changes: 8 additions & 1 deletion lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ func (vm *VM) Create(code WasmCode) (Checksum, error) {
//
// TODO: return gas cost? Add gas limit??? there is no metering here...
func (vm *VM) StoreCode(code WasmCode) (Checksum, error) {
return api.StoreCode(vm.cache, code)
return api.StoreCode(vm.cache, code, true)
}

// SimulateStoreCode is the same as StoreCode but does not actually store the code.
// This is useful for simulating all the validations happening in StoreCode without actually
// writing anything to disk.
func (vm *VM) SimulateStoreCode(code WasmCode, gasLimit uint64) (Checksum, error) {
return api.StoreCode(vm.cache, code, false)
}

// StoreCodeUnchecked is the same as StoreCode but skips static validation checks.
Expand Down
9 changes: 5 additions & 4 deletions libwasmvm/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,11 @@ struct cache_t *init_cache(struct ByteSliceView data_dir,
uint32_t instance_memory_limit,
struct UnmanagedVector *error_msg);

struct UnmanagedVector save_wasm(struct cache_t *cache,
struct ByteSliceView wasm,
bool unchecked,
struct UnmanagedVector *error_msg);
struct UnmanagedVector store_code(struct cache_t *cache,
struct ByteSliceView wasm,
bool checked,
bool persist,
struct UnmanagedVector *error_msg);

void remove_wasm(struct cache_t *cache,
struct ByteSliceView checksum,
Expand Down
52 changes: 30 additions & 22 deletions libwasmvm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,35 @@ fn do_init_cache(
}

#[no_mangle]
pub extern "C" fn save_wasm(
pub extern "C" fn store_code(
cache: *mut cache_t,
wasm: ByteSliceView,
unchecked: bool,
checked: bool,
persist: bool,
error_msg: Option<&mut UnmanagedVector>,
) -> UnmanagedVector {
let r = match to_cache(cache) {
Some(c) => catch_unwind(AssertUnwindSafe(move || do_save_wasm(c, wasm, unchecked)))
.unwrap_or_else(|err| {
eprintln!("Panic in do_save_wasm: {err:?}");
Err(Error::panic())
}),
Some(c) => catch_unwind(AssertUnwindSafe(move || {
do_store_code(c, wasm, checked, persist)
}))
.unwrap_or_else(|err| {
eprintln!("Panic in do_save_wasm: {err:?}");
Err(Error::panic())
}),
None => Err(Error::unset_arg(CACHE_ARG)),
};
let checksum = handle_c_error_binary(r, error_msg);
UnmanagedVector::new(Some(checksum))
}

fn do_save_wasm(
fn do_store_code(
cache: &mut Cache<GoApi, GoStorage, GoQuerier>,
wasm: ByteSliceView,
unchecked: bool,
checked: bool,
persist: bool,
) -> Result<Checksum, Error> {
let wasm = wasm.read().ok_or_else(|| Error::unset_arg(WASM_ARG))?;
let checksum = if unchecked {
cache.save_wasm_unchecked(wasm)?
} else {
cache.save_wasm(wasm)?
};
let checksum = cache.store_code(wasm, checked, persist)?;
Ok(checksum)
}

Expand Down Expand Up @@ -454,10 +454,11 @@ mod tests {
let _ = error_msg.consume();

let mut error_msg = UnmanagedVector::default();
save_wasm(
store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand All @@ -483,10 +484,11 @@ mod tests {
let _ = error_msg.consume();

let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
let checksum = store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand Down Expand Up @@ -538,10 +540,11 @@ mod tests {
let _ = error_msg.consume();

let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
let checksum = store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand Down Expand Up @@ -579,10 +582,11 @@ mod tests {
let _ = error_msg.consume();

let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
let checksum = store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand Down Expand Up @@ -628,10 +632,11 @@ mod tests {
let _ = error_msg.consume();

let mut error_msg = UnmanagedVector::default();
let checksum = save_wasm(
let checksum = store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand Down Expand Up @@ -686,21 +691,23 @@ mod tests {
let _ = error_msg.consume();

let mut error_msg = UnmanagedVector::default();
let checksum_hackatom = save_wasm(
let checksum_hackatom = store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
let _ = error_msg.consume();
let checksum_hackatom = checksum_hackatom.consume().unwrap_or_default();

let mut error_msg = UnmanagedVector::default();
let checksum_ibc_reflect = save_wasm(
let checksum_ibc_reflect = store_code(
cache_ptr,
ByteSliceView::new(IBC_REFLECT),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand Down Expand Up @@ -791,10 +798,11 @@ mod tests {

// Save wasm
let mut error_msg = UnmanagedVector::default();
let checksum_hackatom = save_wasm(
let checksum_hackatom = store_code(
cache_ptr,
ByteSliceView::new(HACKATOM),
false,
true,
Some(&mut error_msg),
);
assert!(error_msg.is_none());
Expand Down
2 changes: 1 addition & 1 deletion libwasmvm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn handle_cpu_loop_with_cache() {
};

// store code
let checksum = cache.save_wasm(CYBERPUNK).unwrap();
let checksum = cache.store_code(CYBERPUNK, true, true).unwrap();

// instantiate
let env = mock_env();
Expand Down

0 comments on commit 558ea9e

Please sign in to comment.