1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/// # Execution Result
///
/// This module contains the structures used to interpret the program execution results, either
/// normal programs or starknet contracts.
use crate::{error::Error, native_panic, utils::decode_error_message, values::Value};
use starknet_types_core::felt::Felt;
#[derive(
Debug,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
)]
pub struct BuiltinStats {
pub bitwise: usize,
pub ec_op: usize,
pub range_check: usize,
pub pedersen: usize,
pub poseidon: usize,
pub segment_arena: usize,
pub range_check_96: usize,
pub circuit_add: usize,
pub circuit_mul: usize,
}
/// The result of the JIT execution.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExecutionResult {
pub remaining_gas: Option<u64>,
pub return_value: Value,
pub builtin_stats: BuiltinStats,
}
/// Starknet contract execution result.
#[derive(
Debug,
Default,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
serde::Serialize,
serde::Deserialize,
)]
pub struct ContractExecutionResult {
pub remaining_gas: u64,
pub failure_flag: bool,
pub return_values: Vec<Felt>,
pub error_msg: Option<String>,
}
impl ContractExecutionResult {
/// Convert an [`ExecutionResult`] into a [`ContractExecutionResult`]
pub fn from_execution_result(result: ExecutionResult) -> Result<Self, Error> {
let mut error_msg = None;
let failure_flag;
let return_values = match &result.return_value {
Value::Enum { tag, value, .. } => {
failure_flag = *tag != 0;
if !failure_flag {
if let Value::Struct { fields, .. } = &**value {
if let Value::Struct { fields, .. } = &fields[0] {
if let Value::Array(data) = &fields[0] {
let felt_vec: Vec<_> = data
.iter()
.map(|x| {
if let Value::Felt252(f) = x {
Ok(*f)
} else {
native_panic!("should always be a felt")
}
})
.collect::<Result<_, _>>()?;
felt_vec
} else {
Err(Error::UnexpectedValue(format!(
"wrong type, expected: Struct {{ Struct {{ Array<felt252> }} }}, value: {:?}",
value
)))?
}
} else {
Err(Error::UnexpectedValue(format!(
"wrong type, expected: Struct {{ Struct {{ Array<felt252> }} }}, value: {:?}",
value
)))?
}
} else {
Err(Error::UnexpectedValue(format!(
"wrong type, expected: Struct {{ Struct {{ Array<felt252> }} }}, value: {:?}",
value
)))?
}
} else if let Value::Struct { fields, .. } = &**value {
if fields.len() < 2 {
Err(Error::UnexpectedValue(format!(
"wrong type, expect: struct.fields.len() >= 2, value: {:?}",
fields
)))?
}
if let Value::Array(data) = &fields[1] {
let felt_vec: Vec<_> = data
.iter()
.map(|x| {
if let Value::Felt252(f) = x {
Ok(*f)
} else {
native_panic!("should always be a felt")
}
})
.collect::<Result<_, _>>()?;
let bytes_err: Vec<_> = felt_vec
.iter()
.flat_map(|felt| felt.to_bytes_be().to_vec())
// remove null chars
.filter(|b| *b != 0)
.collect();
let str_error = decode_error_message(&bytes_err);
error_msg = Some(str_error);
felt_vec
} else {
Err(Error::UnexpectedValue(format!(
"wrong type, expected: Struct {{ [X, Array<felt252>] }}, value: {:?}",
value
)))?
}
} else {
Err(Error::UnexpectedValue(format!(
"wrong type, expected: Struct {{ [X, Array<felt252>] }}, value: {:?}",
value
)))?
}
}
_ => {
failure_flag = true;
Err(Error::UnexpectedValue(
"wrong return value type expected a enum".to_string(),
))?
}
};
Ok(Self {
remaining_gas: result.remaining_gas.unwrap_or(0),
return_values,
failure_flag,
error_msg,
})
}
}