fix: fix panic in event parsing due to empty stack #2856
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fixes #2855 - please see linked issue for more details
Issue
When subscribing to logs via Program::on -> Program::on_internal -> parse_logs_response the thread panics.
The issue is due to calling
Execution::Program
inparse_logs_response
which asserts thatSelf::stack
isn't empty.However, the current logic empties the stack upon the return of the first outer instruction matching the subscribed Program ID, and so when there are multiple outer instructions within the logs, a panic occurs.
Fix
This PR introduces a few changes to
parse_logs_response
andhandle_system_log
handle_system_log
"cpi"
is only pushed as anew_program
when the invoke instruction isn't invoking at a depth of [1]. We'll handle these outer instruction invocations inparse_logs_response
parse_logs_response
The for loop is now a peekable iterator which is manually managed via next().
If the current instruction returned
did_pop = true
(and hence was a program success log) then we peek at the next log (if one exists).If this ends with
"invoke [1]
then it means the current instruction was the end of an outer instruction, and a new outer instruction will begin on the next iteration.At this stage the stack is empty due to the pop, so we capture the program ID of the next instruction and push this to the stack.
Tests
Added a minimal test case to confirm the new logic now allows this to run without any panics. The logs used in the test are from a real transaction on mainnet, but with dummy program IDs and event names.
Copying the test case to the current commit will replicate the panic behavior.