Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: A0-A4 - LOG0-4 Opcode #322

Merged
merged 42 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
92561a1
mstore tests impl
Quentash Sep 6, 2023
b6804f5
made exec_mstore panic
Quentash Sep 6, 2023
80edce9
corrected test case error
Quentash Sep 6, 2023
a4f4597
mstore opcode impl
Quentash Sep 6, 2023
80264ac
review correction + add 2 tests
Quentash Sep 6, 2023
16cfa56
Merge branch 'feat/mstoretests' into feat/mstoreimpl
Quentash Sep 6, 2023
3b06efa
correction test case
Quentash Sep 6, 2023
e24598d
Merge branch 'feat/mstoretests' into feat/mstoreimpl
Quentash Sep 6, 2023
0224cfe
used pop_n instead
Quentash Sep 6, 2023
6042b2e
corrected #263 succeed into succeeded
Quentash Sep 6, 2023
ae6c067
Merge branch 'main' into feat/mstoreimpl
Quentash Sep 7, 2023
86cc9b1
reverted to single pop + removed tests
Quentash Sep 7, 2023
bab2bff
Merge branch 'main' into feat/mstoreimpl
Quentash Sep 7, 2023
34c0d3b
first commit
Quentash Sep 7, 2023
29015b2
Merge branch 'main' into feat/lognopcode
Quentash Sep 8, 2023
0a6cea7
removed print and used default for array
Quentash Sep 8, 2023
45f9e27
added readonly test & cleaned imports
Quentash Sep 8, 2023
c31e984
added internal get_datas
Quentash Sep 13, 2023
211719b
need to merge main
Quentash Sep 15, 2023
17e0f35
Merge branch 'main' into feat/lognopcode
Quentash Sep 15, 2023
c221d5c
removed ',' from merge
Quentash Sep 15, 2023
1f13db4
check load vs loadn
Quentash Sep 15, 2023
89b4872
switch data array from felt to u8
Quentash Sep 22, 2023
9d1c40c
Merge branch 'main' into feat/lognopcode
Quentash Sep 22, 2023
a800070
cleaned code/tests
Quentash Sep 22, 2023
f8bce7a
review correction
Quentash Sep 25, 2023
c54baa1
Merge branch 'main' into feat/lognopcode
Quentash Sep 25, 2023
b9512c4
compare all bytes in tests + inlined app_event
Quentash Sep 25, 2023
2e8de3c
stop useless slicing
Quentash Sep 25, 2023
717a6e4
added tests cases
Quentash Sep 25, 2023
a16352c
Merge branch 'main' into feat/lognopcode
Eikix Sep 27, 2023
e3e79dc
simplified expected array
Quentash Sep 27, 2023
68dabd6
Merge branch 'main' into feat/lognopcode
Quentash Sep 27, 2023
84e6771
Merge branch 'main' into feat/lognopcode
Quentash Oct 2, 2023
eec7854
merge correction
Quentash Oct 3, 2023
8be21f6
cleaned imports
Quentash Oct 3, 2023
9ef47fd
Merge branch 'main' into feat/lognopcode
Quentash Oct 3, 2023
2d7917c
scarb fmt did that
Quentash Oct 3, 2023
bffc21e
dupl app_event + setup_readonly
Quentash Oct 3, 2023
54cc27e
change error message + setup with readonly
Quentash Oct 4, 2023
6fc92f5
Merge branch 'main' into feat/lognopcode
Quentash Oct 4, 2023
e2844c0
correction rebase
Quentash Oct 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/evm/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ const TYPE_CONVERSION_ERROR: felt252 = 'Kakarot: type conversion error';
// RETURNDATA
const RETURNDATA_OUT_OF_BOUNDS_ERROR: felt252 = 'Kakarot: ReturnDataOutOfBounds';

// EVM STATE
const STATE_MODIFICATION_ERROR: felt252 = 'Kakarot: StateModificationError';
Quentash marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Drop, Copy, PartialEq)]
enum EVMError {
StackError: felt252,
InvalidProgramCounter: felt252,
TypeConversionError: felt252,
ReturnDataError: felt252
ReturnDataError: felt252,
StateModificationError: felt252
}


Expand All @@ -30,6 +34,7 @@ impl EVMErrorIntoU256 of Into<EVMError, u256> {
EVMError::InvalidProgramCounter(error_message) => error_message.into(),
EVMError::TypeConversionError(error_message) => error_message.into(),
EVMError::ReturnDataError(error_message) => error_message.into(),
EVMError::StateModificationError(error_message) => error_message.into(),
Quentash marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 1 addition & 0 deletions crates/evm/src/instructions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use environmental_information::EnvironmentInformationTrait;
mod exchange_operations;

mod logging_operations;
use logging_operations::LoggingOperationsTrait;

mod memory_operations;
use memory_operations::MemoryOperationTrait;
Expand Down
132 changes: 99 additions & 33 deletions crates/evm/src/instructions/logging_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,115 @@

// Internal imports
use evm::context::ExecutionContext;
use evm::context::ExecutionContextTrait;
use evm::errors::EVMError;

mod internal {
use evm::context::ExecutionContext;
use evm::context::ExecutionContextTrait;
use evm::context::{ExecutionContext, ExecutionContextTrait, BoxDynamicExecutionContextDestruct};
use evm::stack::StackTrait;
use evm::memory::MemoryTrait;
use evm::model::Event;
use evm::errors::{EVMError, STATE_MODIFICATION_ERROR};
use evm::helpers::U256IntoResultU32;
use utils::helpers::u256_to_bytes_array;

/// Generic logging operation.
/// Append log record with n topics.
fn exec_log_i(ref context: ExecutionContext, topics_len: u8) {}
}
fn exec_log_i(ref self: ExecutionContext, topics_len: u8) -> Result<(), EVMError> {
// Revert if the transaction is in a read only context
if self.read_only() {
return Result::Err(EVMError::StateModificationError(STATE_MODIFICATION_ERROR));
}

/// 0xA0 - LOG0 operation
/// Append log record with no topic.
/// # Specification: https://www.evm.codes/#a0?fork=shanghai
fn exec_log0(ref context: ExecutionContext) {
internal::exec_log_i(ref context, 0);
}
let popped = self.stack.pop_n(2 + topics_len.into())?;
let offset: u32 = Into::<u256, Result<u32, EVMError>>::into(*popped[0])?;
let size: u32 = Into::<u256, Result<u32, EVMError>>::into(*popped[1])?;

// Feed topics array for the new event
let mut topics: Array<u256> = Default::default();
let mut i = 0;
loop {
Quentash marked this conversation as resolved.
Show resolved Hide resolved
if i == topics_len {
break;
}
topics.append(*popped[2 + i.into()]);

/// 0xA1 - LOG1
/// Append log record with one topic.
/// # Specification: https://www.evm.codes/#a1?fork=shanghai
fn exec_log1(ref context: ExecutionContext) {
internal::exec_log_i(ref context, 1);
}
i += 1;
};

/// 0xA2 - LOG2
/// Append log record with two topics.
/// # Specification: https://www.evm.codes/#a2?fork=shanghai
fn exec_log2(ref context: ExecutionContext) {
internal::exec_log_i(ref context, 2);
}
// Feed data array for the new event
let mut datas: Array<felt252> = Default::default();
Quentash marked this conversation as resolved.
Show resolved Hide resolved
let mut i = 0;
Eikix marked this conversation as resolved.
Show resolved Hide resolved
loop {
if 31 + i > size {
if i != size {
let (mut loaded, _) = self.memory.load(offset + i);
let mut chunk: Array<u8> = u256_to_bytes_array(loaded);
Eikix marked this conversation as resolved.
Show resolved Hide resolved
let mut last_elem = 0;
let mut j = 0;
loop {
if j + i == size {
break;
}
last_elem *= 256;
last_elem += (*chunk[j]).into();
j += 1;
};
datas.append(last_elem);
}
break;
};
let (mut loaded, _) = self.memory.load(offset + i);
loaded /= 256;
datas.append(loaded.try_into().unwrap());
i += 31;
};

// Create and store the new event in the dynamic context
let event: Event = Event { keys: topics, data: datas };

let mut dyn_ctx = self.dynamic_context.unbox();
dyn_ctx.events.append(event);
Quentash marked this conversation as resolved.
Show resolved Hide resolved
self.dynamic_context = BoxTrait::new(dyn_ctx);

/// 0xA3 - LOG3
/// Append log record with three topics.
/// # Specification: https://www.evm.codes/#a3?fork=shanghai
fn exec_log3(ref context: ExecutionContext) {
internal::exec_log_i(ref context, 3);
Result::Ok(())
}
}

/// 0xA4 - LOG4
/// Append log record with 4 topics.
/// # Specification: https://www.evm.codes/#a4?fork=shanghai
fn exec_log4(ref context: ExecutionContext) {
internal::exec_log_i(ref context, 4);
#[generate_trait]
impl LoggingOperations of LoggingOperationsTrait {
/// 0xA0 - LOG0 operation
/// Append log record with no topic.
/// # Specification: https://www.evm.codes/#a0?fork=shanghai
fn exec_log0(ref self: ExecutionContext) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 0)
}


/// 0xA1 - LOG1
/// Append log record with one topic.
/// # Specification: https://www.evm.codes/#a1?fork=shanghai
fn exec_log1(ref self: ExecutionContext) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 1)
}

/// 0xA2 - LOG2
/// Append log record with two topics.
/// # Specification: https://www.evm.codes/#a2?fork=shanghai
fn exec_log2(ref self: ExecutionContext) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 2)
}

/// 0xA3 - LOG3
/// Append log record with three topics.
/// # Specification: https://www.evm.codes/#a3?fork=shanghai
fn exec_log3(ref self: ExecutionContext) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 3)
}

/// 0xA4 - LOG4
/// Append log record with 4 topics.
/// # Specification: https://www.evm.codes/#a4?fork=shanghai
fn exec_log4(ref self: ExecutionContext) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 4)
}
}
17 changes: 9 additions & 8 deletions crates/evm/src/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use utils::{helpers::u256_to_bytes_array};
use evm::errors::{EVMError, PC_OUT_OF_BOUNDS};
use evm::instructions::{
duplication_operations, environmental_information, exchange_operations, logging_operations,
memory_operations, sha3, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait,
system_operations, BlockInformationTrait, DuplicationOperationsTrait,
EnvironmentInformationTrait, PushOperationsTrait, MemoryOperationTrait
LoggingOperationsTrait, memory_operations, sha3, StopAndArithmeticOperationsTrait,
ComparisonAndBitwiseOperationsTrait, system_operations, BlockInformationTrait,
DuplicationOperationsTrait, EnvironmentInformationTrait, PushOperationsTrait,
MemoryOperationTrait
};
use result::ResultTrait;

Expand Down Expand Up @@ -603,23 +604,23 @@ impl EVMInterpreterImpl of EVMInterpreterTrait {
}
if opcode == 160 {
// LOG0
logging_operations::exec_log0(ref context);
return context.exec_log0();
}
if opcode == 161 {
// LOG1
logging_operations::exec_log1(ref context);
return context.exec_log1();
}
if opcode == 162 {
// LOG2
logging_operations::exec_log2(ref context);
return context.exec_log2();
}
if opcode == 163 {
// LOG3
logging_operations::exec_log3(ref context);
return context.exec_log3();
}
if opcode == 164 {
// LOG4
logging_operations::exec_log4(ref context);
return context.exec_log4();
}
if opcode == 240 {
// CREATE
Expand Down
1 change: 1 addition & 0 deletions crates/evm/src/tests/test_instructions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod test_block_information;
mod test_environment_information;
mod test_push_operations;
mod test_memory_operations;
mod test_logging_operations;
Loading
Loading