From 26c727315e0c387aec7fb163ad2e3b5b0742c1c6 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Sun, 15 Dec 2024 09:43:28 +0900 Subject: [PATCH] debug: Add debugging to secret contracts --- _SETUP.md | 8 +- .../nunya-contract/src/contract.rs | 19 +++++ .../secret-gateway/src/contract.rs | 73 ++++++++++++++++++- 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/_SETUP.md b/_SETUP.md index eb1a4f6..99e4a4d 100644 --- a/_SETUP.md +++ b/_SETUP.md @@ -282,7 +282,13 @@ git submodule update --init --recursive make build ``` * Note: Use `make build-mainnet-reproducible` to deploy to Testnet - * TODO: The default Makefile uses `--features="debug-print"` but running that gives error `the package secret_gateway depends on secret-cosmwasm-std, with features: debug-print but secret-cosmwasm-std does not have these features.` + * Note: The default Makefile uses `--features="debug-print"` but running that gives error `the package secret_gateway depends on secret-cosmwasm-std, with features: debug-print but secret-cosmwasm-std does not have these features.`. The reason why it was removed is mentioned here: + * https://github.com/CosmWasm/cosmwasm/issues/1841 + * https://github.com/CosmWasm/wasmvm/pull/453 + * https://github.com/CosmWasm/cosmwasm/pull/1667 + * https://github.com/CosmWasm/cosmwasm/pull/1953 + * Solution: + * https://github.com/CosmWasm/cosmwasm/blob/main/contracts/cyberpunk/tests/integration.rs#L126 * Note: Use existing localsecret Docker container that is running already. diff --git a/packages/secret-contracts/nunya-contract/src/contract.rs b/packages/secret-contracts/nunya-contract/src/contract.rs index 5d5f47d..3e48b1c 100644 --- a/packages/secret-contracts/nunya-contract/src/contract.rs +++ b/packages/secret-contracts/nunya-contract/src/contract.rs @@ -44,6 +44,13 @@ pub fn instantiate( owner: info.sender.clone(), }; + deps.set_debug_handler(move |msg, info| { + eprintln!("instantiate"); + eprintln!("msg: {:#?}", msg); + eprintln!("info: {:#?}", info); + eprintln!("state: {:#?}", state); + }); + deps.api .debug(format!("Contract was initialized by {}", info.sender).as_str()); @@ -91,6 +98,12 @@ fn try_handle( // verify signature with stored gateway public key let config = CONFIG.load(deps.storage)?; + deps.set_debug_handler(move |msg, info| { + eprintln!("try_handle"); + eprintln!("msg: {:#?}", msg); + eprintln!("info: {:#?}", info); + }); + // Security // // reference: evm-kv-store-demo @@ -118,6 +131,12 @@ fn try_handle( // determine which function to call based on the included handle let handle = msg.handle.as_str(); + + deps.set_debug_handler(move |msg, info| { + eprintln!("try_handle"); + eprintln!("handle: {:#?}", handle); + }); + match handle { // TODO: change all below to snake_case if required for Gateway contract to be able to call. // but first test that `request_value` works. diff --git a/packages/secret-contracts/secret-gateway/src/contract.rs b/packages/secret-contracts/secret-gateway/src/contract.rs index 1a7922f..f2b710e 100644 --- a/packages/secret-contracts/secret-gateway/src/contract.rs +++ b/packages/secret-contracts/secret-gateway/src/contract.rs @@ -67,6 +67,12 @@ pub fn instantiate( signing_keys: KeyPair::default(), }; + deps.set_debug_handler(move |msg, info| { + eprintln!("instantiate"); + eprintln!("msg: {:#?}", msg); + eprintln!("info: {:#?}", info); + }); + CONFIG.save(deps.storage, &state)?; let _result = create_gateway_keys(deps, env); @@ -86,6 +92,11 @@ pub fn instantiate( /// #[entry_point] pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult { + deps.set_debug_handler(move |msg, info| { + eprintln!("execute"); + eprintln!("msg: {:#?}", msg); + eprintln!("info: {:#?}", info); + }); let response = match msg { ExecuteMsg::Input { inputs } => pre_execution(deps, env, inputs), ExecuteMsg::Output { outputs } => post_execution(deps, env, outputs), @@ -107,6 +118,13 @@ fn rotate_gateway_keys(deps: DepsMut, env: Env, info: MessageInfo) -> StdResult< let caller_raw = deps.api.addr_canonicalize(info.sender.as_str())?; + deps.set_debug_handler(move |msg, info| { + eprintln!("rotate_gateway_keys"); + eprintln!("caller: {:#?}", caller); + eprintln!("state: {:#?}", state); + eprintln!("info: {:#?}", info); + }); + // check if the keys have already been created if state.keyed { //if keys were have already been created, check if admin is calling this @@ -151,6 +169,11 @@ fn create_gateway_keys(deps: DepsMut, env: Env) -> StdResult { // load config let state = CONFIG.load(deps.storage)?; + deps.set_debug_handler(move |msg, info| { + eprintln!("create_gateway_keys"); + eprintln!("state: {:#?}", state); + }); + // check if the keys have already been created if state.keyed { return Err(StdError::generic_err( @@ -191,6 +214,13 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult StdResult StdResult { unsafe_payload = false; // Both decryption and verification succeeded + + deps.set_debug_handler(move |msg, info| { + eprintln!("pre_execution"); + eprintln!("Both decryption and verification succeeded"); + eprintln!("decrypted_payload: {:#?}", decrypted_payload); + }); + decrypted_payload } - Err(_err) => { + Err(err) => { + deps.set_debug_handler(move |msg, info| { + eprintln!("pre_execution"); + eprintln!("Verification failed: {:#?}", err); + }); //return Err(StdError::generic_err(format!("Verification failed: {}", err))); // Continue with the decrypted payload if only verification fails decrypted_payload } } } - Err(_err) => { + Err(err) => { + deps.set_debug_handler(move |msg, info| { + eprintln!("pre_execution"); + eprintln!("Decryption failed: {:#?}", err); + }); //return Err(StdError::generic_err(format!("Decryption failed: {}", err))); // If decryption fails, continue with the original, encrypted payload // We are not verifying the payload in this case as it's already deemed unsafe @@ -263,6 +311,11 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult StdResult StdResult StdResult