Skip to content

Commit

Permalink
6 mem tets passing
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wickham committed Mar 26, 2024
1 parent 9adb1de commit 65519c1
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 15 deletions.
456 changes: 456 additions & 0 deletions output.test

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/evm_logic/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ impl EVMContext {
runtime.add_context();

let result = || -> bool {
self.gas_recorder
println!("Message data size : {}", self.message.data.bytes.len());
if self.message.data.bytes.len() != 0{
self.gas_recorder
.record_gas(call_data_gas_cost(&self.message.data.bytes));
}
if debug {
println!("Call Data Gas Cost: {}", self.gas_recorder.gas_usage);
}
Expand All @@ -118,7 +121,7 @@ impl EVMContext {
}
if debug {
println!(
"Gas : {:x}",
"Program Gas Usage : {:x}",
self.gas_input - self.gas_recorder.gas_usage as u64
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/evm_logic/evm/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ pub fn decode_instruction(evm: &mut EVMContext, runtime: &mut impl Runtime, debu
},

opcodes::MSIZE => {
evm.stack.push(U256::from(evm.memory.max_index as u64));
evm.stack.push(U256::from((evm.memory.max_index + 1) as u64));
evm.gas_recorder.record_gas(2);
},

Expand Down
18 changes: 13 additions & 5 deletions src/gas_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ impl GasRecorder {
}
let max_index = new_memory_size - 1;
let memory_size_word = (max_index / 4) as u64;
let old_cost = GasRecorder::memory_cost(new_memory_size);
let new_cost = GasRecorder::memory_cost(current_memory_size);
let old_cost = GasRecorder::memory_cost(current_memory_size);
let new_cost = GasRecorder::memory_cost(new_memory_size);
// println!("Old cost: {}, New cost: {}", old_cost, new_cost);
let len = new_memory_size - current_memory_size;
let memory_expansion_cost = 3 + 3 * (len as u64 + 31 / 32) as usize + (new_cost - old_cost);
// let memory_expansion_cost = 3 + 3 * (len as u64 + 31 / 32) as usize + (new_cost - old_cost);
let memory_expansion_cost = new_cost - old_cost;
self.gas_usage += memory_expansion_cost;
}

fn memory_cost(current_memory_size_bytes: usize) -> usize {
let memory_size_word = (current_memory_size_bytes - 1) / 4;
let memory_cost = usize::pow(memory_size_word, 2) / 512 + (3 * memory_size_word);
if current_memory_size_bytes == 0 {
return 0;
}
let memory_size_word = (current_memory_size_bytes + 31) / 32;
let memory_cost = (memory_size_word.pow(2)) / 512 + (3 * memory_size_word);
memory_cost
// let memory_size_word = (current_memory_size_bytes - 1) / 4;
// let memory_cost = usize::pow(memory_size_word, 2) / 512 + (3 * memory_size_word);
// memory_cost
}
}
4 changes: 4 additions & 0 deletions src/state/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ impl Memory {

#[inline]
fn expand(&mut self, new_max_address: usize, gas_recorder: &mut GasRecorder) {
if new_max_address == 0 {
return;
}
let new_max_address= new_max_address + 32;
self.max_index = new_max_address;
gas_recorder.record_memory_usage(self.bytes.len(), new_max_address);
self.bytes.resize(new_max_address, 0);
Expand Down
2 changes: 1 addition & 1 deletion test_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn generate_official_tests_from_folder(input: TokenStream) -> TokenStream {

for i in 0..num_tests {
let test_name = Ident::new(
format!("run_test_{}_{}",test_name, i).as_str(),
format!("run_test_{}_{}",test_name.replace("+", "pos").replace("-", "min"), i).as_str(),
proc_macro2::Span::call_site(),
);
tests.push(quote! {
Expand Down
10 changes: 7 additions & 3 deletions tests/official_tests/official_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn run_test(test: &TestState, debug: bool) {
current_context: None,
};
runtime.add_context();

println!("Message data size : {}", test.transaction.data.len());
// Execute the transaction
let gas_usage = EVMContext::execute_transaction(
&mut runtime,
Expand Down Expand Up @@ -125,13 +125,17 @@ pub fn run_test(test: &TestState, debug: bool) {
}

generate_official_tests_from_folder!(
"./tests/official_tests/tests/GeneralStateTests/VMTests/vmArithmeticTest"
"./tests/official_tests/tests/GeneralStateTests/stMemoryTest"
);

// generate_official_tests_from_folder!(
// "./tests/official_tests/tests/GeneralStateTests/stMemoryTest"
// );

// generate_official_tests_from_folder!(
// "./tests/official_tests/tests/GeneralStateTests/stRandom"
// );

// generate_official_tests_from_file!(
// "./tests/official_tests/tests/GeneralStateTests/VMTests/vmArithmeticTest/mul.json"
// "./tests/official_tests/tests/GeneralStateTests/stMemoryTest/mem32kb-1.json"
// );
3 changes: 0 additions & 3 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ pub fn opcode_map(input: TokenStream) -> TokenStream {
pub static ref OPCODE_MAP: phf::Map<u8, &'static str> = #map_tokens;
}
};

println!("{:?}", expanded.to_string());

// Convert the generated tokens back into a TokenStream
TokenStream::from(expanded)
}

0 comments on commit 65519c1

Please sign in to comment.