Skip to content

Commit 47d48d0

Browse files
authored
Merge pull request #46 from osmosis-labs/wasm-sudo
Enable calling sudo on contract directly
2 parents 7ac0268 + 65d2ac9 commit 47d48d0

27 files changed

Lines changed: 855 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
33
"packages/*",
4+
"test_contracts/*",
45
]
56
resolver = "2"

packages/osmosis-test-tube/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ serde_json = "1.0.85"
2020
test-tube = {version = "0.4.0", path = "../test-tube", features = ["module-wrappers"]}
2121
thiserror = "1.0.34"
2222

23+
[features]
24+
default = []
25+
wasm-sudo = ["test-tube/wasm-sudo"]
26+
2327
[build-dependencies]
2428
bindgen = "0.69.1"
2529

2630
[dev-dependencies]
2731
cw1-whitelist = "1.1.1"
2832
rayon = "1.5.3"
33+
simple-sudo = {path = "../../test_contracts/simple-sudo"}

packages/osmosis-test-tube/libosmosistesttube/go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/osmosis-labs/test-tube/osmosis-test-tube
22

3-
go 1.19
3+
go 1.21
4+
5+
toolchain go1.21.6
46

57
require (
68
cosmossdk.io/errors v1.0.0

packages/osmosis-test-tube/libosmosistesttube/go.sum

Lines changed: 50 additions & 0 deletions
Large diffs are not rendered by default.

packages/osmosis-test-tube/libosmosistesttube/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ func EndBlock(envId uint64) {
160160
envRegister.Store(envId, env)
161161
}
162162

163+
//export WasmSudo
164+
func WasmSudo(envId uint64, bech32Address, msgJson string) *C.char {
165+
env := loadEnv(envId)
166+
// Temp fix for concurrency issue
167+
mu.Lock()
168+
defer mu.Unlock()
169+
170+
accAddr, err := sdk.AccAddressFromBech32(bech32Address)
171+
if err != nil {
172+
panic(err)
173+
}
174+
175+
msgBytes := []byte(msgJson)
176+
177+
res, err := env.App.WasmKeeper.Sudo(env.Ctx, accAddr, msgBytes)
178+
if err != nil {
179+
return encodeErrToResultBytes(result.ExecuteError, err)
180+
}
181+
182+
envRegister.Store(envId, env)
183+
184+
return encodeBytesResultBytes(res)
185+
}
186+
163187
//export Execute
164188
func Execute(envId uint64, base64ReqDeliverTx string) *C.char {
165189
env := loadEnv(envId)

packages/osmosis-test-tube/src/runner/app.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ impl OsmosisTestApp {
109109
) -> RunnerResult<P> {
110110
self.inner.get_param_set(subspace, type_url)
111111
}
112+
113+
/// Directly trigger sudo entrypoint on a given contract.
114+
///
115+
/// # Caution
116+
///
117+
/// This function bypasses standard state changes and processes within the chain logic that might occur in normal situation,
118+
/// It is primarily intended for internal system logic where necessary state adjustments are handled.
119+
/// Use only with full understanding of the function's impact on system state and testing validity.
120+
/// Improper use may result in misleading test outcomes, including false positives or negatives.
121+
#[cfg(feature = "wasm-sudo")]
122+
pub fn wasm_sudo<M: serde::Serialize>(
123+
&self,
124+
contract_address: &str,
125+
sudo_msg: M,
126+
) -> RunnerResult<Vec<u8>> {
127+
self.inner.wasm_sudo(contract_address, sudo_msg)
128+
}
112129
}
113130

114131
impl<'a> Runner<'a> for OsmosisTestApp {
@@ -570,4 +587,57 @@ mod tests {
570587
// should succeed
571588
assert!(res.data.success);
572589
}
590+
591+
#[cfg(feature = "wasm-sudo")]
592+
#[test]
593+
fn test_wasm_sudo() {
594+
let app = OsmosisTestApp::default();
595+
let wasm = Wasm::new(&app);
596+
597+
let wasm_byte_code = std::fs::read("./test_artifacts/simple_sudo.wasm").unwrap();
598+
let alice = app
599+
.init_account(&coins(1_000_000_000_000, "uosmo"))
600+
.unwrap();
601+
602+
let code_id = wasm
603+
.store_code(&wasm_byte_code, None, &alice)
604+
.unwrap()
605+
.data
606+
.code_id;
607+
608+
let contract_addr = wasm
609+
.instantiate(
610+
code_id,
611+
&simple_sudo::msg::InstantiateMsg {},
612+
None,
613+
Some("simple_sudo"),
614+
&[],
615+
&alice,
616+
)
617+
.unwrap()
618+
.data
619+
.address;
620+
621+
let res = app
622+
.wasm_sudo(
623+
&contract_addr,
624+
simple_sudo::msg::SudoMsg::SetRandomData {
625+
key: "x".to_string(),
626+
value: "1".to_string(),
627+
},
628+
)
629+
.unwrap();
630+
631+
assert_eq!(String::from_utf8(res).unwrap(), "x=1");
632+
633+
let res: simple_sudo::msg::RandomDataResponse = wasm
634+
.query(
635+
&contract_addr,
636+
&simple_sudo::msg::QueryMsg::GetRandomData {
637+
key: "x".to_string(),
638+
},
639+
)
640+
.unwrap();
641+
assert_eq!(res.value, "1");
642+
}
573643
}

packages/osmosis-test-tube/src/runner/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod tests {
88

99
use super::app::OsmosisTestApp;
1010
use base64::Engine;
11-
use cosmwasm_std::{to_binary, BankMsg, Coin, CosmosMsg, Empty, Event, WasmMsg};
11+
use cosmwasm_std::{to_json_binary, BankMsg, Coin, CosmosMsg, Empty, Event, WasmMsg};
1212
use cw1_whitelist::msg::{ExecuteMsg, InstantiateMsg};
1313
use osmosis_std::types::cosmos::bank::v1beta1::{MsgSendResponse, QueryBalanceRequest};
1414
use osmosis_std::types::cosmwasm::wasm::v1::{
@@ -162,7 +162,7 @@ mod tests {
162162
// Wasm::Instantiate
163163
let instantiate_msg: CosmosMsg = CosmosMsg::Wasm(WasmMsg::Instantiate {
164164
code_id,
165-
msg: to_binary(&InstantiateMsg {
165+
msg: to_json_binary(&InstantiateMsg {
166166
admins: vec![signer.address()],
167167
mutable: true,
168168
})
@@ -180,7 +180,7 @@ mod tests {
180180
// Wasm::Execute
181181
let execute_msg: CosmosMsg = CosmosMsg::Wasm(WasmMsg::Execute {
182182
contract_addr: contract_address.clone(),
183-
msg: to_binary(&ExecuteMsg::<Empty>::Freeze {}).unwrap(),
183+
msg: to_json_binary(&ExecuteMsg::<Empty>::Freeze {}).unwrap(),
184184
funds: vec![],
185185
});
186186
let execute_res = app
180 KB
Binary file not shown.

packages/test-tube/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ rayon = "1.5.3"
2424

2525
[features]
2626
default = []
27-
2827
module-wrappers = ["bank", "wasm"]
28+
wasm-sudo = []
2929

3030
bank = ["osmosis-std"]
3131
wasm = ["osmosis-std"]

packages/test-tube/src/bindings.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ extern "C" {
220220
extern "C" {
221221
pub fn EndBlock(envId: GoUint64);
222222
}
223+
extern "C" {
224+
pub fn WasmSudo(
225+
envId: GoUint64,
226+
bech32Address: GoString,
227+
msgJson: GoString,
228+
) -> *mut ::std::os::raw::c_char;
229+
}
223230
extern "C" {
224231
pub fn IncreaseTime(envId: GoUint64, seconds: GoInt64);
225232
}

0 commit comments

Comments
 (0)