Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner instruction lacks parent. #11

Open
spacecaterpillar opened this issue Feb 25, 2025 · 1 comment
Open

Inner instruction lacks parent. #11

spacecaterpillar opened this issue Feb 25, 2025 · 1 comment

Comments

@spacecaterpillar
Copy link

If you parse this transaction: https://explorer.solana.com/tx/599kTg8k8Mz7QJnAimAUmiYXtbynx7oczqFCXXFGXF32Sb8MzKuX42EX2Lsj2PEdk2TDsZTMKqsBUqhxHnBSqJAD

The last inner instruction 3.10, will have:

StructuredInstruction { instruction: Inner(InnerInstruction { program_id_index: 22, accounts: [4, 5, 12], data: [3, 222, 65, 212, 19, 0, 0, 0, 0], stack_height: Some(2) }), accounts: [Pubkey("Aq28kYyDAHxxAk5RSbfogegBDnsCivRJA56Nj4et1vhG"), Pubkey("4w63iDuwHidFuXxUN7BDA3Gh8tSjZjU9eKeYMA9Vb6Kk"), Pubkey("2MFoS3MPtvyQ4Wh4M9pdfPjz6UhVoNbFbGJAskCPCj3h")], program_id: Pubkey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), inner_instructions: RefCell { value: [] }, parent_instruction: RefCell { value: None }, logs: RefCell { value: Some([Invoke(InvokeLog { log: "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]" }), Program(ProgramLog { log: "Program log: Instruction: Transfer" }), Unknown(UnknownLog { log: "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 4645 of 1241316 compute units" }), Success(SuccessLog { log: "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success" })]) } }

You can see that it has stack_height = 2, but parent_instruction = None, which shouldn't be possible.

I believe the issue occurs because you assign the parent on stack pop? That might not happen for the last instruction, so we should assign on stack push? Perhaps something like:

diff --git a/src/utils/instruction.rs b/src/utils/instruction.rs
index 3094d73..2530594 100644
--- a/src/utils/instruction.rs
+++ b/src/utils/instruction.rs
@@ -180,6 +180,7 @@ where
     for instruction in flattened_instructions {
         let structured_instruction = Rc::new(StructuredInstruction::new(instruction, Vec::new().into(), &accounts));
 
+        // Pop instructions off stack and establish parent relationships
         while !instruction_stack.is_empty() && instruction_stack.last().unwrap().stack_height() >= structured_instruction.stack_height() {
             let popped_instruction = instruction_stack.pop().unwrap();
             *popped_instruction.logs.borrow_mut() = log_stack.close(logs, popped_instruction.program_id());
@@ -192,6 +193,12 @@ where
             }
         }
 
+        // Set parent relationship for current instruction if there's a parent on stack
+        if !instruction_stack.is_empty() && structured_instruction.stack_height().unwrap_or(1) > 1 {
+            *structured_instruction.parent_instruction.borrow_mut() =
+                Some(Rc::downgrade(instruction_stack.last().unwrap()));
+        }
+
         log_stack.open(logs, structured_instruction.program_id());
         instruction_stack.push(structured_instruction);
     }
@@ -201,6 +208,7 @@ where
         *popped_instruction.logs.borrow_mut() = log_stack.close(logs, popped_instruction.program_id());
 
         if !instruction_stack.is_empty() {
+            *popped_instruction.parent_instruction.borrow_mut() = Some(Rc::downgrade(instruction_stack.last().unwrap()));
             instruction_stack.last_mut().unwrap().inner_instructions.borrow_mut().push(popped_instruction);
         } else {
             structured_instructions.push(popped_instruction)
@0xpcut
Copy link
Owner

0xpcut commented Feb 25, 2025

Nice catch. I'll fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants