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 095b849 commit 319a1be
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 49 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 := os.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 @@ -418,10 +418,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 @@ -85,11 +85,11 @@ func ReleaseCache(cache Cache) {
cache.lockfile.Close() // Also releases the file lock
}

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 @@ -100,7 +100,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
20 changes: 10 additions & 10 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestStoreCodeAndGetCode(t *testing.T) {
wasm, err := os.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 @@ -148,7 +148,7 @@ func TestRemoveCode(t *testing.T) {
wasm, err := os.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 @@ -165,7 +165,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 @@ -193,7 +193,7 @@ func TestPin(t *testing.T) {
wasm, err := os.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 TestUnpin(t *testing.T) {
wasm, err := os.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 @@ -280,7 +280,7 @@ func TestGetMetrics(t *testing.T) {
// Store contract
wasm, err := os.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 @@ -395,7 +395,7 @@ func TestGetPinnedMetrics(t *testing.T) {
// Store contract 1
wasm, err := os.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 All @@ -404,7 +404,7 @@ func TestGetPinnedMetrics(t *testing.T) {
// Store contract 2
cyberpunkWasm, err := os.ReadFile("../../testdata/cyberpunk.wasm")
require.NoError(t, err)
cyberpunkChecksum, err := StoreCode(cache, cyberpunkWasm)
cyberpunkChecksum, err := StoreCode(cache, cyberpunkWasm, true)
require.NoError(t, err)

err = Pin(cache, cyberpunkChecksum)
Expand Down Expand Up @@ -469,7 +469,7 @@ func TestInstantiate(t *testing.T) {
// create contract
wasm, err := os.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 @@ -1116,7 +1116,7 @@ func createFloaty2(t testing.TB, cache Cache) []byte {
func createContract(t testing.TB, cache Cache, wasmFile string) []byte {
wasm, err := os.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
21 changes: 17 additions & 4 deletions lib_libwasmvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@ func (vm *VM) StoreCode(code WasmCode, gasLimit uint64) (Checksum, uint64, error
return nil, gasCost, types.OutOfGasError{}
}

checksum, err := api.StoreCode(vm.cache, code)
checksum, err := api.StoreCode(vm.cache, code, true)
return checksum, gasCost, err
}

// 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, uint64, error) {
gasCost := compileCost(code)
if gasLimit < gasCost {
return nil, gasCost, types.OutOfGasError{}
}

checksum, err := api.StoreCode(vm.cache, code, false)
return checksum, gasCost, err
}

Expand Down Expand Up @@ -262,11 +275,11 @@ func (vm *VM) Migrate(
return &result, gasReport.UsedInternally, nil
}

// Sudo allows native Go modules to make priviledged (sudo) calls on the contract.
// Sudo allows native Go modules to make privileged (sudo) calls on the contract.
// The contract can expose entry points that cannot be triggered by any transaction, but only via
// native Go modules, and delegate the access control to the system.
//
// These work much like Migrate (same scenario) but allows custom apps to extend the priviledged entry points
// These work much like Migrate (same scenario) but allows custom apps to extend the privileged entry points
// without forking cosmwasm-vm.
func (vm *VM) Sudo(
checksum Checksum,
Expand Down Expand Up @@ -296,7 +309,7 @@ func (vm *VM) Sudo(
return &result, gasReport.UsedInternally, nil
}

// Reply allows the native Go wasm modules to make a priviledged call to return the result
// Reply allows the native Go wasm modules to make a privileged call to return the result
// of executing a SubMsg.
//
// These work much like Sudo (same scenario) but focuses on one specific case (and one message type)
Expand Down
9 changes: 5 additions & 4 deletions libwasmvm/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,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 @@ -86,35 +86,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 @@ -557,10 +557,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 @@ -586,10 +587,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 @@ -641,10 +643,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 @@ -682,10 +685,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 @@ -731,10 +735,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 @@ -789,21 +794,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 @@ -988,10 +995,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 @@ -29,7 +29,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 319a1be

Please sign in to comment.