Skip to content

Commit

Permalink
Merge branch 'main' into contract_executor_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana authored Sep 18, 2024
2 parents 6ae655c + 8faf10f commit 10f48a4
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 43 deletions.
43 changes: 9 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ starknet-types-core = { version = "0.1.5", default-features = false, features =
tempfile = "3.6"
thiserror = "1.0.59"
tracing = "0.1"
utf8_iter = "1.0.4"


# CLI dependencies
Expand All @@ -107,7 +108,7 @@ colored = { version = "2.1.0", optional = true }
keccak = "0.1.5"
k256 = "0.13.3"
p256 = "0.13.2"
sha2 = "0.10.8" # needed for the syscall handler stub
sha2 = "0.10.8" # needed for the syscall handler stub
scarb-metadata = { git = "https://github.com/software-mansion/scarb.git", rev = "v2.8.2", optional = true }
scarb-ui = { git = "https://github.com/software-mansion/scarb.git", rev = "v2.8.2", optional = true }
sec1 = "0.7.3"
Expand Down
3 changes: 1 addition & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ crate-type = ["rlib", "cdylib", "staticlib"]

[dependencies]
starknet-types-core = { version = "0.1.5", default-features = false, features = [
"std", "serde",
"std", "serde", "hash"
] }
cairo-lang-sierra-gas = "2.8.2"
libc = "0.2.158"
starknet-crypto = "0.7.1"
starknet-curve = "0.5.0"
lazy_static = "1.5.0"
rand = "0.8.5"
5 changes: 3 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starknet_curve::curve_params::BETA;
use starknet_types_core::{
curve::{AffinePoint, ProjectivePoint},
felt::Felt,
hash::StarkHash,
};
use std::ops::Mul;
use std::{collections::HashMap, fs::File, io::Write, os::fd::FromRawFd, ptr::NonNull, slice};
Expand Down Expand Up @@ -110,7 +111,7 @@ pub unsafe extern "C" fn cairo_native__libfunc__pedersen(
let rhs = Felt::from_bytes_le_slice(rhs);

// Compute pedersen hash and copy the result into `dst`.
let res = starknet_crypto::pedersen_hash(&lhs, &rhs);
let res = starknet_types_core::hash::Pedersen::hash(&lhs, &rhs);
dst.copy_from_slice(&res.to_bytes_le());
}

Expand Down Expand Up @@ -145,7 +146,7 @@ pub unsafe extern "C" fn cairo_native__libfunc__hades_permutation(
];

// Compute Poseidon permutation.
starknet_crypto::poseidon_permute_comp(&mut state);
starknet_types_core::hash::Poseidon::hades_permutation(&mut state);

// Write back the results.
op0.copy_from_slice(&state[0].to_bytes_le());
Expand Down
4 changes: 2 additions & 2 deletions src/execution_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
///
/// This module contains the structures used to interpret the program execution results, either
/// normal programs or starknet contracts.
use crate::{error::Error, values::JitValue};
use crate::{error::Error, utils::decode_error_message, values::JitValue};
use starknet_types_core::felt::Felt;

#[derive(
Expand Down Expand Up @@ -126,7 +126,7 @@ impl ContractExecutionResult {
// remove null chars
.filter(|b| *b != 0)
.collect();
let str_error = String::from_utf8(bytes_err).unwrap().to_owned();
let str_error = decode_error_message(&bytes_err);

error_msg = Some(str_error);
felt_vec
Expand Down
4 changes: 2 additions & 2 deletions src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use crate::{
module::NativeModule,
starknet::{handler::StarknetSyscallHandlerCallbacks, StarknetSyscallHandler},
types::TypeBuilder,
utils::{generate_function_name, get_integer_layout},
utils::{decode_error_message, generate_function_name, get_integer_layout},
OptLevel,
};

Expand Down Expand Up @@ -401,7 +401,7 @@ impl ContractExecutor {
// remove null chars
.filter(|b| *b != 0)
.collect();
let str_error = String::from_utf8(bytes_err).unwrap().to_owned();
let str_error = decode_error_message(&bytes_err);

error_msg = Some(str_error);
}
Expand Down
59 changes: 59 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ pub fn generate_function_name(
}
}

/// Decode an UTF-8 error message replacing invalid bytes with their hexadecimal representation, as
/// done by Python's `x.decode('utf-8', errors='backslashreplace')`.
pub fn decode_error_message(data: &[u8]) -> String {
let mut pos = 0;
utf8_iter::ErrorReportingUtf8Chars::new(data).fold(String::new(), |mut acc, ch| {
match ch {
Ok(ch) => {
acc.push(ch);
pos += ch.len_utf8();
}
Err(_) => {
acc.push_str(&format!("\\x{:02x}", data[pos]));
pos += 1;
}
};

acc
})
}

/// Return the layout for an integer of arbitrary width.
///
/// This assumes the platform's maximum (effective) alignment is 16 bytes, and that every integer
Expand Down Expand Up @@ -866,6 +886,45 @@ pub mod test {
assert_eq!(entry_point.unwrap().id.id, 15);
}

#[test]
fn decode_error_message() {
// Checkout [issue 795](https://github.com/lambdaclass/cairo_native/issues/795) for context.
assert_eq!(
super::decode_error_message(&[
97, 114, 103, 101, 110, 116, 47, 109, 117, 108, 116, 105, 99, 97, 108, 108, 45,
102, 97, 105, 108, 101, 100, 3, 232, 78, 97, 116, 105, 118, 101, 32, 101, 120, 101,
99, 117, 116, 105, 111, 110, 32, 101, 114, 114, 111, 114, 58, 32, 69, 114, 114,
111, 114, 32, 97, 116, 32, 112, 99, 61, 48, 58, 49, 48, 52, 58, 10, 71, 111, 116,
32, 97, 110, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 32, 119, 104, 105,
108, 101, 32, 101, 120, 101, 99, 117, 116, 105, 110, 103, 32, 97, 32, 104, 105,
110, 116, 58, 32, 69, 114, 114, 111, 114, 32, 97, 116, 32, 112, 99, 61, 48, 58, 49,
56, 52, 58, 10, 71, 111, 116, 32, 97, 110, 32, 101, 120, 99, 101, 112, 116, 105,
111, 110, 32, 119, 104, 105, 108, 101, 32, 101, 120, 101, 99, 117, 116, 105, 110,
103, 32, 97, 32, 104, 105, 110, 116, 58, 32, 69, 120, 99, 101, 101, 100, 101, 100,
32, 116, 104, 101, 32, 109, 97, 120, 105, 109, 117, 109, 32, 110, 117, 109, 98,
101, 114, 32, 111, 102, 32, 101, 118, 101, 110, 116, 115, 44, 32, 110, 117, 109,
98, 101, 114, 32, 101, 118, 101, 110, 116, 115, 58, 32, 49, 48, 48, 49, 44, 32,
109, 97, 120, 32, 110, 117, 109, 98, 101, 114, 32, 101, 118, 101, 110, 116, 115,
58, 32, 49, 48, 48, 48, 46, 10, 67, 97, 105, 114, 111, 32, 116, 114, 97, 99, 101,
98, 97, 99, 107, 32, 40, 109, 111, 115, 116, 32, 114, 101, 99, 101, 110, 116, 32,
99, 97, 108, 108, 32, 108, 97, 115, 116, 41, 58, 10, 85, 110, 107, 110, 111, 119,
110, 32, 108, 111, 99, 97, 116, 105, 111, 110, 32, 40, 112, 99, 61, 48, 58, 49, 52,
51, 52, 41, 10, 85, 110, 107, 110, 111, 119, 110, 32, 108, 111, 99, 97, 116, 105,
111, 110, 32, 40, 112, 99, 61, 48, 58, 49, 51, 57, 53, 41, 10, 85, 110, 107, 110,
111, 119, 110, 32, 108, 111, 99, 97, 116, 105, 111, 110, 32, 40, 112, 99, 61, 48,
58, 57, 53, 51, 41, 10, 85, 110, 107, 110, 111, 119, 110, 32, 108, 111, 99, 97,
116, 105, 111, 110, 32, 40, 112, 99, 61, 48, 58, 51, 51, 57, 41, 10, 10, 67, 97,
105, 114, 111, 32, 116, 114, 97, 99, 101, 98, 97, 99, 107, 32, 40, 109, 111, 115,
116, 32, 114, 101, 99, 101, 110, 116, 32, 99, 97, 108, 108, 32, 108, 97, 115, 116,
41, 58, 10, 85, 110, 107, 110, 111, 119, 110, 32, 108, 111, 99, 97, 116, 105, 111,
110, 32, 40, 112, 99, 61, 48, 58, 49, 54, 55, 56, 41, 10, 85, 110, 107, 110, 111,
119, 110, 32, 108, 111, 99, 97, 116, 105, 111, 110, 32, 40, 112, 99, 61, 48, 58,
49, 54, 54, 52, 41, 10
]),
"argent/multicall-failed\x03\\xe8Native execution error: Error at pc=0:104:\nGot an exception while executing a hint: Error at pc=0:184:\nGot an exception while executing a hint: Exceeded the maximum number of events, number events: 1001, max number events: 1000.\nCairo traceback (most recent call last):\nUnknown location (pc=0:1434)\nUnknown location (pc=0:1395)\nUnknown location (pc=0:953)\nUnknown location (pc=0:339)\n\nCairo traceback (most recent call last):\nUnknown location (pc=0:1678)\nUnknown location (pc=0:1664)\n",
);
}

// ==============================
// == TESTS: felt252_str
// ==============================
Expand Down

0 comments on commit 10f48a4

Please sign in to comment.