Skip to content

Commit

Permalink
debug: Add debugging to secret contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Dec 15, 2024
1 parent d9a2887 commit 26c7273
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
8 changes: 7 additions & 1 deletion _SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions packages/secret-contracts/nunya-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
73 changes: 71 additions & 2 deletions packages/secret-contracts/secret-gateway/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -86,6 +92,11 @@ pub fn instantiate(
///
#[entry_point]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
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),
Expand All @@ -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
Expand Down Expand Up @@ -151,6 +169,11 @@ fn create_gateway_keys(deps: DepsMut, env: Env) -> StdResult<Response> {
// 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(
Expand Down Expand Up @@ -191,6 +214,13 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult<Re
// load config
let config = CONFIG.load(deps.storage)?;

deps.set_debug_handler(move |msg, info| {
eprintln!("pre_execution");
eprintln!("config: {:#?}", config);
eprintln!("msg: {:#?}", msg);
eprintln!("state: {:#?}", state);
});

// Check if the payload matches the payload hash for Solana
let mut hasher = Keccak256::new();
hasher.update(msg.payload.as_slice());
Expand All @@ -211,6 +241,9 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult<Re
if msg.payload_hash.as_slice() != payload_hash_tmp_eth.as_slice()
&& msg.payload_hash.as_slice() != payload_hash_tmp_solana.as_slice()
{
deps.set_debug_handler(move |msg, info| {
eprintln!("Hashed Payload does not match payload hash");
});
return Err(StdError::generic_err(
"Hashed Payload does not match payload hash",
));
Expand All @@ -228,16 +261,31 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult<Re
Ok(_) => {
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
Expand All @@ -263,6 +311,11 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult<Re
task_id: msg.task_id,
};

deps.set_debug_handler(move |msg, info| {
eprintln!("pre_execution");
eprintln!("new_task: {:#?}", new_task);
});

// check if the task wasn't executed before already
let map_contains_task = TASK_MAP.contains(deps.storage, &new_task);

Expand Down Expand Up @@ -306,6 +359,11 @@ fn pre_execution(deps: DepsMut, _env: Env, msg: PreExecutionMsg) -> StdResult<Re
callback_gas_limit: payload.callback_gas_limit,
};

deps.set_debug_handler(move |msg, info| {
eprintln!("pre_execution");
eprintln!("task_info: {:#?}", task_info);
});

// map task to task info
TASK_MAP.insert(deps.storage, &new_task, &task_info)?;

Expand Down Expand Up @@ -346,6 +404,12 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
.get(deps.storage, &msg.task)
.ok_or_else(|| StdError::generic_err("task not found"))?;

deps.set_debug_handler(move |msg, info| {
eprintln!("post_execution");
eprintln!("msg: {:#?}", msg);
eprintln!("task_info: {:#?}", task_info);
});

// verify that input hash is correct one for Task
if msg.input_hash.as_slice() != task_info.input_hash.to_vec() {
return Err(StdError::generic_err("input hash does not match task"));
Expand Down Expand Up @@ -531,6 +595,11 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
callback_gas_limit: callback_gas_limit,
};

deps.set_debug_handler(move |msg, info| {
eprintln!("post_execution");
eprintln!("result_info: {:#?}", result_info);
});

// Store the result info in the result map
RESULT_MAP.insert(deps.storage, &msg.task, &result_info)?;

Expand Down

0 comments on commit 26c7273

Please sign in to comment.