@@ -10,8 +10,8 @@ pub use stack::{Stack, STACK_LIMIT};
10
10
11
11
use crate :: alloc:: borrow:: ToOwned ;
12
12
use crate :: {
13
- primitives:: Bytes , push, push_b256, return_ok, return_revert, CallInputs , CreateInputs ,
14
- CreateOutcome , Gas , Host , InstructionResult ,
13
+ primitives:: Bytes , push, push_b256, return_ok, return_revert, CallInputs , CallOutcome ,
14
+ CreateInputs , CreateOutcome , Gas , Host , InstructionResult ,
15
15
} ;
16
16
use alloc:: boxed:: Box ;
17
17
use core:: cmp:: min;
@@ -150,32 +150,51 @@ impl Interpreter {
150
150
}
151
151
}
152
152
153
- /// When sub call returns we can insert output of that call into this interpreter .
153
+ /// Inserts the outcome of a call into the virtual machine's state .
154
154
///
155
- /// Note that shared memory is required as a input field.
156
- /// As SharedMemory inside Interpreter is taken and replaced with empty (not valid) memory.
157
- pub fn insert_call_output (
155
+ /// This function takes the result of a call, represented by `CallOutcome`,
156
+ /// and updates the virtual machine's state accordingly. It involves updating
157
+ /// the return data buffer, handling gas accounting, and setting the memory
158
+ /// in shared storage based on the outcome of the call.
159
+ ///
160
+ /// # Arguments
161
+ ///
162
+ /// * `shared_memory` - A mutable reference to the shared memory used by the virtual machine.
163
+ /// * `call_outcome` - The outcome of the call to be processed, containing details such as
164
+ /// instruction result, gas information, and output data.
165
+ ///
166
+ /// # Behavior
167
+ ///
168
+ /// The function first copies the output data from the call outcome to the virtual machine's
169
+ /// return data buffer. It then checks the instruction result from the call outcome:
170
+ ///
171
+ /// - `return_ok!()`: Processes successful execution, refunds gas, and updates shared memory.
172
+ /// - `return_revert!()`: Handles a revert by only updating the gas usage and shared memory.
173
+ /// - `InstructionResult::FatalExternalError`: Sets the instruction result to a fatal external error.
174
+ /// - Any other result: No specific action is taken.
175
+ pub fn insert_call_outcome (
158
176
& mut self ,
159
177
shared_memory : & mut SharedMemory ,
160
- result : InterpreterResult ,
161
- memory_return_offset : Range < usize > ,
178
+ call_outcome : CallOutcome ,
162
179
) {
163
- let out_offset = memory_return_offset . start ;
164
- let out_len = memory_return_offset . len ( ) ;
180
+ let out_offset = call_outcome . memory_start ( ) ;
181
+ let out_len = call_outcome . memory_length ( ) ;
165
182
166
- self . return_data_buffer = result . output ;
183
+ self . return_data_buffer = call_outcome . output ( ) . to_owned ( ) ;
167
184
let target_len = min ( out_len, self . return_data_buffer . len ( ) ) ;
168
185
169
- match result . result {
186
+ match call_outcome . instruction_result ( ) {
170
187
return_ok ! ( ) => {
171
188
// return unspend gas.
172
- self . gas . erase_cost ( result. gas . remaining ( ) ) ;
173
- self . gas . record_refund ( result. gas . refunded ( ) ) ;
189
+ let remaining = call_outcome. gas ( ) . remaining ( ) ;
190
+ let refunded = call_outcome. gas ( ) . refunded ( ) ;
191
+ self . gas . erase_cost ( remaining) ;
192
+ self . gas . record_refund ( refunded) ;
174
193
shared_memory. set ( out_offset, & self . return_data_buffer [ ..target_len] ) ;
175
194
push ! ( self , U256 :: from( 1 ) ) ;
176
195
}
177
196
return_revert ! ( ) => {
178
- self . gas . erase_cost ( result . gas . remaining ( ) ) ;
197
+ self . gas . erase_cost ( call_outcome . gas ( ) . remaining ( ) ) ;
179
198
shared_memory. set ( out_offset, & self . return_data_buffer [ ..target_len] ) ;
180
199
push ! ( self , U256 :: ZERO ) ;
181
200
}
0 commit comments