From de10c45ec35f54416cc1aabe626a9743136190cb Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Sat, 15 Jul 2023 23:12:08 +0200 Subject: [PATCH] runtime-sdk/modules/evm: Make custom ABI-encoded error decoding easier --- runtime-sdk/modules/evm/src/lib.rs | 16 +++------------- runtime-sdk/modules/evm/src/test.rs | 6 +++--- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/runtime-sdk/modules/evm/src/lib.rs b/runtime-sdk/modules/evm/src/lib.rs index 69defaa894..ac6bfdc78b 100644 --- a/runtime-sdk/modules/evm/src/lib.rs +++ b/runtime-sdk/modules/evm/src/lib.rs @@ -232,26 +232,16 @@ fn process_evm_result(exit_reason: evm::ExitReason, data: Vec) -> Result MAX_REASON_SIZE { - MAX_REASON_SIZE - } else { - data.len() - }; + let max_raw_len = data.len().clamp(0, MAX_REASON_SIZE); if data.len() < MIN_SIZE || !data.starts_with(ERROR_STRING_SELECTOR) { - return Err(Error::Reverted(format!( - "invalid reason prefix: '{}'", - base64::encode(&data[..max_raw_len]) - ))); + return Err(Error::Reverted(base64::encode(&data[..max_raw_len]))); } // Decode and validate length. let mut length = primitive_types::U256::from(&data[FIELD_LENGTH_START..FIELD_LENGTH_START + 32]) .low_u32() as usize; if FIELD_REASON_START + length > data.len() { - return Err(Error::Reverted(format!( - "invalid reason length: '{}'", - base64::encode(&data[..max_raw_len]) - ))); + return Err(Error::Reverted(base64::encode(&data[..max_raw_len]))); } // Make sure that this doesn't ever return huge reason values as this is at least // somewhat contract-controlled. diff --git a/runtime-sdk/modules/evm/src/test.rs b/runtime-sdk/modules/evm/src/test.rs index 253dd4d529..d08f77faac 100644 --- a/runtime-sdk/modules/evm/src/test.rs +++ b/runtime-sdk/modules/evm/src/test.rs @@ -837,7 +837,7 @@ fn test_revert_reason_decoding() { // Malformed output, incorrect selector and bad length. ( "BADBADBADBADBADBAD", - "invalid reason prefix: 'utututututut'", + "utututututut", ), // Malformed output, bad selector. ( @@ -845,7 +845,7 @@ fn test_revert_reason_decoding() { 0000000000000000000000000000000000000000000000000000000000000020\ 0000000000000000000000000000000000000000000000000000000000000018\ 4461692f696e73756666696369656e742d62616c616e63650000000000000000", - "invalid reason prefix: 'uqqqrQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABhEYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA=='", + "uqqqrQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABhEYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA==", ), // Malformed output, corrupted length. ( @@ -853,7 +853,7 @@ fn test_revert_reason_decoding() { 0000000000000000000000000000000000000000000000000000000000000020\ 00000000000000000000000000000000000000000000000000000000FFFFFFFF\ 4461692f696e73756666696369656e742d62616c616e63650000000000000000", - "invalid reason length: 'CMN5oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////9EYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA=='", + "CMN5oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP////9EYWkvaW5zdWZmaWNpZW50LWJhbGFuY2UAAAAAAAAAAA==", ), ];