Skip to content

Commit

Permalink
Merge branch 'main' into cw/fix-pr-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowhk committed Sep 12, 2023
2 parents ed35fa4 + 85e2864 commit f28a85f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/evm/src/instructions/memory_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ impl MemoryOperation of MemoryOperationTrait {
/// Get the value of memory size.
/// # Specification: https://www.evm.codes/#59?fork=shanghai
fn exec_msize(ref self: ExecutionContext) -> Result<(), EVMError> {
Result::Ok(())
let msize: u256 = self.memory.size().into();
self.stack.push(msize)
}

/// 0x56 - JUMP operation
Expand Down
7 changes: 7 additions & 0 deletions crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Memory {

trait MemoryTrait {
fn new() -> Memory;
fn size(ref self: Memory) -> usize;
fn store(ref self: Memory, element: u256, offset: usize);
fn store_n(ref self: Memory, elements: Span<u8>, offset: usize);
fn ensure_length(ref self: Memory, length: usize) -> usize;
Expand All @@ -31,6 +32,12 @@ impl MemoryImpl of MemoryTrait {
Memory { items: Default::default(), bytes_len: 0, }
}

/// Return size of the memory.
#[inline(always)]
fn size(ref self: Memory) -> usize {
self.bytes_len
}

/// Stores a 32-bytes element into the memory.
///
/// If the offset is aligned with the 16-bytes words in memory, the element is stored directly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use evm::errors::{EVMError, STACK_UNDERFLOW};
use evm::context::{
ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct, CallContextTrait,
};
use evm::helpers::U256IntoResultU32;
use integer::BoundedInt;


Expand Down Expand Up @@ -251,3 +252,51 @@ fn test_exec_mstore8_should_store_last_uint8_offset_63() {
let (stored, _) = ctx.memory.load(32);
assert(stored == 0xEF, 'mstore8 failed');
}


#[test]
#[available_gas(20000000)]
fn test_msize_initial() {
// Given
let mut ctx = setup_execution_context();

// When
let result = ctx.exec_msize();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.pop().unwrap() == 0, 'initial memory size should be 0');
}

#[test]
#[available_gas(20000000)]
fn test_exec_msize_store_max_offset_0() {
// Given
let mut ctx = setup_execution_context();
ctx.memory.store(BoundedInt::<u256>::max(), 0x00);

// When
let result = ctx.exec_msize();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.pop().unwrap() == 32, 'should 32 bytes after MSTORE');
}

#[test]
#[available_gas(20000000)]
fn test_exec_msize_store_max_offset_1() {
// Given
let mut ctx = setup_execution_context();
ctx.memory.store(BoundedInt::<u256>::max(), 0x01);

// When
let result = ctx.exec_msize();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(ctx.stack.len() == 1, 'stack should have one element');
assert(ctx.stack.pop().unwrap() == 64, 'should 64 bytes after MSTORE');
}

0 comments on commit f28a85f

Please sign in to comment.