-
Notifications
You must be signed in to change notification settings - Fork 89
Add the responsible program's account index and inner instruction index to each InstructionError
#74
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
base: master
Are you sure you want to change the base?
Add the responsible program's account index and inner instruction index to each InstructionError
#74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
use serde_derive::{Deserialize, Serialize}; | ||
#[cfg(feature = "frozen-abi")] | ||
use solana_frozen_abi_macro::{AbiEnumVisitor, AbiExample}; | ||
use {core::fmt, solana_instruction::error::InstructionError, solana_sanitize::SanitizeError}; | ||
use { | ||
core::fmt, solana_instruction::error::InstructionError, solana_pubkey::Pubkey, | ||
solana_sanitize::SanitizeError, | ||
}; | ||
|
||
pub type TransactionResult<T> = Result<T, TransactionError>; | ||
|
||
|
@@ -42,9 +45,23 @@ pub enum TransactionError { | |
/// the `recent_blockhash` has been discarded. | ||
BlockhashNotFound, | ||
|
||
/// An error occurred while processing an instruction. The first element of the tuple | ||
/// indicates the instruction index in which the error occurred. | ||
InstructionError(u8, InstructionError), | ||
/// An error occurred while processing an instruction. | ||
InstructionError { | ||
err: InstructionError, | ||
/// The index of the inner instruction in which the error was thrown, starting from zero. | ||
/// This value will be `None` when the error was thrown from the outer instruction's | ||
/// program, and also for all errors stored using a version of this enum variant prior to | ||
/// `solana-transaction-error` 3.0.0. | ||
inner_instruction_index: Option<u8>, | ||
/// The index of the outer instruction in which the error was thrown, starting from zero. Do | ||
/// not infer the responsible program from the instruction at this index; the error might | ||
/// have been thrown from one of its inner instructions. See `responsible_program_address`. | ||
outer_instruction_index: u8, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we're making a breaking change, let's change this to a |
||
/// The address of the program that threw the error. Use this to decode the error (eg. to | ||
/// look up a custom error code in the program's IDL). This value will be `None` for errors | ||
/// stored using a version of this enum variant prior to `solana-transaction-error` 3.0.0. | ||
responsible_program_address: Option<Pubkey>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't store the program address here. It can be looked up via tx metadata. Let's record the stack height of the failure and the tx account index of the responsible program instead. If the responsible program was resolved from an ALT, the end user should resolve that themselves. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A central principle of this change is that the error alone should be scrutible; all questions you might have about an error should be encapsulated in the error itself. In any case, you might obtain this error from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think you're trying to maximize error context and I'm trying to minimize it. I was under the assumption that the main problem this change is trying to fix is providing a way to correlate the error with the program that returned it. The other problems you're trying to solve can be solved by dev infra tools that provide users with full instruction traces and stuff. |
||
}, | ||
|
||
/// Loader call chain is too deep | ||
CallChainTooDeep, | ||
|
@@ -163,7 +180,17 @@ impl fmt::Display for TransactionError { | |
=> f.write_str("This transaction has already been processed"), | ||
Self::BlockhashNotFound | ||
=> f.write_str("Blockhash not found"), | ||
Self::InstructionError(idx, err) => write!(f, "Error processing Instruction {idx}: {err}"), | ||
Self::InstructionError { | ||
err, | ||
outer_instruction_index, | ||
.. | ||
} | ||
// NOTE: We intentionally do not augment the error message in the event that the error | ||
// carries the address of the responsible program or the index of the inner | ||
// instruction. While it would add value to the log, to do so at this point would also | ||
// break any log parser that presumes a stable log format | ||
// (eg. https://tinyurl.com/3uuczr68). | ||
=> write!(f, "Error processing Instruction {outer_instruction_index}: {err}"), | ||
Self::CallChainTooDeep | ||
=> f.write_str("Loader call chain is too deep"), | ||
Self::MissingSignatureForFee | ||
|
@@ -415,3 +442,78 @@ impl TransportError { | |
|
||
#[cfg(not(target_os = "solana"))] | ||
pub type TransportResult<T> = std::result::Result<T, TransportError>; | ||
|
||
#[cfg(feature = "serde")] | ||
#[cfg(test)] | ||
mod tests { | ||
use { | ||
crate::TransactionError, | ||
serde_json::{from_value, json, to_value}, | ||
solana_instruction::error::InstructionError, | ||
solana_pubkey::Pubkey, | ||
test_case::test_case, | ||
}; | ||
|
||
#[cfg(feature = "serde")] | ||
#[test_case(InstructionError::Custom(42), 1, Some(Pubkey::new_unique()), None; "From top-level instruction")] | ||
#[test_case(InstructionError::Custom(42), 1, Some(Pubkey::new_unique()), Some(41); "From inner instruction")] | ||
#[test_case(InstructionError::Custom(42), 1, None, None; "Legacy instruction without inner/program")] | ||
fn test_serialize_instruction_error( | ||
err: InstructionError, | ||
outer_instruction_index: u8, | ||
responsible_program_address: Option<Pubkey>, | ||
inner_instruction_index: Option<u8>, | ||
) { | ||
let json = to_value(TransactionError::InstructionError { | ||
err: err.clone(), | ||
inner_instruction_index, | ||
outer_instruction_index, | ||
responsible_program_address, | ||
}) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
json, | ||
json!({ | ||
"InstructionError": { | ||
"err": err, | ||
"inner_instruction_index": inner_instruction_index, | ||
"outer_instruction_index": outer_instruction_index, | ||
"responsible_program_address": responsible_program_address, | ||
}, | ||
}), | ||
) | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
#[test_case(InstructionError::Custom(42), 1, Some(Pubkey::new_unique()), None; "From top-level instruction")] | ||
#[test_case(InstructionError::Custom(42), 1, Some(Pubkey::new_unique()), Some(41); "From inner instruction")] | ||
#[test_case(InstructionError::Custom(42), 1, None, None; "Legacy instruction without inner/program")] | ||
#[test_case(InstructionError::Custom(42), 1, None, Some(41); "Mixed instruction with inner index but no program address")] | ||
fn test_deserialize_instruction_error( | ||
err: InstructionError, | ||
outer_instruction_index: u8, | ||
responsible_program_address: Option<Pubkey>, | ||
inner_instruction_index: Option<u8>, | ||
) { | ||
let decoded_err: TransactionError = from_value(json!({ | ||
"InstructionError": { | ||
"err": err, | ||
"inner_instruction_index": inner_instruction_index, | ||
"outer_instruction_index": outer_instruction_index, | ||
"responsible_program_address": responsible_program_address, | ||
}, | ||
})) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
decoded_err, | ||
TransactionError::InstructionError { | ||
err, | ||
inner_instruction_index, | ||
outer_instruction_index, | ||
responsible_program_address, | ||
}, | ||
) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u16
because it's possible that an instruction makes more than 256 CPI's.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea behind this value was not to correlate it with the trace, but rather to give clients something they can use for display. For instance, explorers typically think of the first inner instruction of the second top-level instruction as being ‘Instruction 2.1.’ The indexes for such an instruction would be
{ outer_instruction_index: 1, inner_instruction_index: Some(0) }
.Adding this value also doesn't add any weight in blockstore, since I was able to compress it into the existing uint32 that currently stores the top-level (outer) instruction index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explorer feature you mentioned is fully reliant on the CPI trace. I'm not concerned with extra weight in the blockstore. I just want this to provide the minimal context needed to correlate the error.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I can't convert this to a
u16
.In blockstore, we don't store structured errors, we just dump bytes into a field.
This results in errors appearing like this in long-term storage:
I can't start writing a
u16
into the part of that serialization destined forouter_instruction_index
. Old versions of the validator won't be able to read that, and new versions of the validator will stop understanding old stored data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need a
u16
, since we only execute up to 64 instructions before we hit our instruction limit (which I think is a separate error). At that point it wouldn't be an instruction error...so while it's technically (now) possible to have an instruction index > 255...I don't think it's actually possible to get an index>255 here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, yeah not possible to get index > 255 right now so can skip the
u16
, sorry for the noise!