|
| 1 | +#![cfg(feature = "unsafe-debug")] |
| 2 | + |
| 3 | +use super::*; |
| 4 | +use crate::unsafe_debug::{ExecutionObserver, ExportedFunction}; |
| 5 | +use frame_support::traits::Currency; |
| 6 | +use pallet_contracts_primitives::ExecReturnValue; |
| 7 | +use pretty_assertions::assert_eq; |
| 8 | +use std::cell::RefCell; |
| 9 | + |
| 10 | +#[derive(Clone, PartialEq, Eq, Debug)] |
| 11 | +struct DebugFrame { |
| 12 | + code_hash: CodeHash<Test>, |
| 13 | + call: ExportedFunction, |
| 14 | + input: Vec<u8>, |
| 15 | + result: Option<Vec<u8>>, |
| 16 | +} |
| 17 | + |
| 18 | +thread_local! { |
| 19 | + static DEBUG_EXECUTION_TRACE: RefCell<Vec<DebugFrame>> = RefCell::new(Vec::new()); |
| 20 | +} |
| 21 | + |
| 22 | +pub struct TestDebugger; |
| 23 | + |
| 24 | +impl ExecutionObserver<CodeHash<Test>> for TestDebugger { |
| 25 | + fn before_call(code_hash: &CodeHash<Test>, entry_point: ExportedFunction, input_data: &[u8]) { |
| 26 | + DEBUG_EXECUTION_TRACE.with(|d| { |
| 27 | + d.borrow_mut().push(DebugFrame { |
| 28 | + code_hash: code_hash.clone(), |
| 29 | + call: entry_point, |
| 30 | + input: input_data.to_vec(), |
| 31 | + result: None, |
| 32 | + }) |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + fn after_call( |
| 37 | + code_hash: &CodeHash<Test>, |
| 38 | + entry_point: ExportedFunction, |
| 39 | + input_data: Vec<u8>, |
| 40 | + output: &ExecReturnValue, |
| 41 | + ) { |
| 42 | + DEBUG_EXECUTION_TRACE.with(|d| { |
| 43 | + d.borrow_mut().push(DebugFrame { |
| 44 | + code_hash: code_hash.clone(), |
| 45 | + call: entry_point, |
| 46 | + input: input_data, |
| 47 | + result: Some(output.data.clone()), |
| 48 | + }) |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn unsafe_debugging_works() { |
| 55 | + let (wasm_caller, code_hash_caller) = compile_module::<Test>("call").unwrap(); |
| 56 | + let (wasm_callee, code_hash_callee) = compile_module::<Test>("store_call").unwrap(); |
| 57 | + |
| 58 | + fn current_stack() -> Vec<DebugFrame> { |
| 59 | + DEBUG_EXECUTION_TRACE.with(|stack| stack.borrow().clone()) |
| 60 | + } |
| 61 | + |
| 62 | + fn deploy(wasm: Vec<u8>) -> AccountId32 { |
| 63 | + Contracts::bare_instantiate( |
| 64 | + ALICE, |
| 65 | + 0, |
| 66 | + GAS_LIMIT, |
| 67 | + None, |
| 68 | + Code::Upload(wasm), |
| 69 | + vec![], |
| 70 | + vec![], |
| 71 | + DebugInfo::Skip, |
| 72 | + CollectEvents::Skip, |
| 73 | + ) |
| 74 | + .result |
| 75 | + .unwrap() |
| 76 | + .account_id |
| 77 | + } |
| 78 | + |
| 79 | + fn constructor_frame(hash: CodeHash<Test>, after: bool) -> DebugFrame { |
| 80 | + DebugFrame { |
| 81 | + code_hash: hash, |
| 82 | + call: ExportedFunction::Constructor, |
| 83 | + input: vec![], |
| 84 | + result: if after { Some(vec![]) } else { None }, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn call_frame(hash: CodeHash<Test>, args: Vec<u8>, after: bool) -> DebugFrame { |
| 89 | + DebugFrame { |
| 90 | + code_hash: hash, |
| 91 | + call: ExportedFunction::Call, |
| 92 | + input: args, |
| 93 | + result: if after { Some(vec![]) } else { None }, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { |
| 98 | + let _ = Balances::deposit_creating(&ALICE, 1_000_000); |
| 99 | + |
| 100 | + assert_eq!(current_stack(), vec![]); |
| 101 | + |
| 102 | + let addr_caller = deploy(wasm_caller); |
| 103 | + let addr_callee = deploy(wasm_callee); |
| 104 | + |
| 105 | + assert_eq!( |
| 106 | + current_stack(), |
| 107 | + vec![ |
| 108 | + constructor_frame(code_hash_caller, false), |
| 109 | + constructor_frame(code_hash_caller, true), |
| 110 | + constructor_frame(code_hash_callee, false), |
| 111 | + constructor_frame(code_hash_callee, true), |
| 112 | + ] |
| 113 | + ); |
| 114 | + |
| 115 | + let main_args = (100u32, &addr_callee).encode(); |
| 116 | + let inner_args = (100u32).encode(); |
| 117 | + |
| 118 | + assert_ok!(Contracts::call( |
| 119 | + RuntimeOrigin::signed(ALICE), |
| 120 | + addr_caller, |
| 121 | + 0, |
| 122 | + GAS_LIMIT, |
| 123 | + None, |
| 124 | + main_args.clone() |
| 125 | + )); |
| 126 | + |
| 127 | + let stack_top = current_stack()[4..].to_vec(); |
| 128 | + assert_eq!( |
| 129 | + stack_top, |
| 130 | + vec![ |
| 131 | + call_frame(code_hash_caller, main_args.clone(), false), |
| 132 | + call_frame(code_hash_callee, inner_args.clone(), false), |
| 133 | + call_frame(code_hash_callee, inner_args, true), |
| 134 | + call_frame(code_hash_caller, main_args, true), |
| 135 | + ] |
| 136 | + ); |
| 137 | + }); |
| 138 | +} |
0 commit comments