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 38 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
9 changes: 7 additions & 2 deletions crates/evm/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const RETURNDATA_OUT_OF_BOUNDS_ERROR: felt252 = 'KKT: ReturnDataOutOfBounds';
// JUMP
const INVALID_DESTINATION: felt252 = 'KKT: invalid JUMP destination';

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

#[derive(Drop, Copy, PartialEq)]
enum EVMError {
StackError: felt252,
Expand All @@ -22,7 +25,8 @@ enum EVMError {
ReturnDataError: felt252,
JumpError: felt252,
NotImplemented,
UnknownOpcode: u8
UnknownOpcode: u8,
StateModificationError: felt252
}


Expand All @@ -36,7 +40,8 @@ impl EVMErrorIntoU256 of Into<EVMError, u256> {
EVMError::JumpError(error_message) => error_message.into(),
EVMError::NotImplemented => 'NotImplemented'.into(),
// TODO: refactor with dynamic strings once supported
EVMError::UnknownOpcode => 'UnknownOpcode'.into()
EVMError::UnknownOpcode => 'UnknownOpcode'.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 @@ -17,6 +17,7 @@ use comparison_operations::ComparisonAndBitwiseOperationsTrait;
use duplication_operations::DuplicationOperationsTrait;
use environmental_information::EnvironmentInformationTrait;
use exchange_operations::ExchangeOperationsTrait;
use logging_operations::LoggingOperationsTrait;
use memory_operations::MemoryOperationTrait;
use push_operations::PushOperationsTrait;
use sha3::Sha3Trait;
Expand Down
97 changes: 63 additions & 34 deletions crates/evm/src/instructions/logging_operations.cairo
Original file line number Diff line number Diff line change
@@ -1,48 +1,77 @@
use evm::errors::EVMError;
//! Logging Operations.

// Internal imports
use evm::machine::Machine;

mod internal {
use evm::machine::Machine;
#[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: Machine) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 0)
}

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

/// 0xA0 - LOG0 operation
/// Append log record with no topic.
/// # Specification: https://www.evm.codes/#a0?fork=shanghai
fn exec_log0(ref machine: Machine) {
internal::exec_log_i(ref machine, 0);
}
/// 0xA2 - LOG2
/// Append log record with two topics.
/// # Specification: https://www.evm.codes/#a2?fork=shanghai
fn exec_log2(ref self: Machine) -> 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: Machine) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 3)
}

/// 0xA1 - LOG1
/// Append log record with one topic.
/// # Specification: https://www.evm.codes/#a1?fork=shanghai
fn exec_log1(ref machine: Machine) {
internal::exec_log_i(ref machine, 1);
/// 0xA4 - LOG4
/// Append log record with four topics.
/// # Specification: https://www.evm.codes/#a4?fork=shanghai
fn exec_log4(ref self: Machine) -> Result<(), EVMError> {
internal::exec_log_i(ref self, 4)
}
}

/// 0xA2 - LOG2
/// Append log record with two topics.
/// # Specification: https://www.evm.codes/#a2?fork=shanghai
fn exec_log2(ref machine: Machine) {
internal::exec_log_i(ref machine, 2);
}
mod internal {
use evm::errors::{EVMError, STATE_MODIFICATION_ERROR};
use evm::machine::{Machine, MachineCurrentContextTrait};
use evm::memory::MemoryTrait;
use evm::model::Event;
use evm::stack::StackTrait;

/// 0xA3 - LOG3
/// Append log record with three topics.
/// # Specification: https://www.evm.codes/#a3?fork=shanghai
fn exec_log3(ref machine: Machine) {
internal::exec_log_i(ref machine, 3);
}
/// Store a new event in the dynamic context using topics
/// popped from the stack and data from the memory.
///
/// # Arguments
///
/// * `self` - The context to which the event will be added
/// * `topics_len` - The amount of topics to pop from the stack
fn exec_log_i(ref self: Machine, 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));
}

let offset = self.stack.pop_usize()?;
let size = self.stack.pop_usize()?;
let topics: Array<u256> = self.stack.pop_n(topics_len.into())?;

let mut data: Array<u8> = Default::default();
self.memory.load_n(size, ref data, offset);

let event: Event = Event { keys: topics, data };
self.append_event(event);

/// 0xA4 - LOG4
/// Append log record with 4 topics.
/// # Specification: https://www.evm.codes/#a4?fork=shanghai
fn exec_log4(ref machine: Machine) {
internal::exec_log_i(ref machine, 4);
Result::Ok(())
}
}
18 changes: 10 additions & 8 deletions crates/evm/src/interpreter.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
use evm::context::{CallContextTrait,};
use evm::errors::{EVMError, PC_OUT_OF_BOUNDS};
use evm::instructions::{
ExchangeOperationsTrait, StopAndArithmeticOperationsTrait, ComparisonAndBitwiseOperationsTrait,
SystemOperationsTrait, BlockInformationTrait, DuplicationOperationsTrait,
EnvironmentInformationTrait, PushOperationsTrait, MemoryOperationTrait, logging_operations
duplication_operations, environmental_information, ExchangeOperationsTrait, logging_operations,
LoggingOperationsTrait, memory_operations, sha3, StopAndArithmeticOperationsTrait,
ComparisonAndBitwiseOperationsTrait, SystemOperationsTrait, BlockInformationTrait,
DuplicationOperationsTrait, EnvironmentInformationTrait, PushOperationsTrait,
MemoryOperationTrait
};
use evm::machine::{Machine, MachineCurrentContextTrait};
use utils::{helpers::u256_to_bytes_array};
Expand Down Expand Up @@ -586,23 +588,23 @@ impl EVMInterpreterImpl of EVMInterpreterTrait {
}
if opcode == 160 {
// LOG0
logging_operations::exec_log0(ref machine);
return machine.exec_log0();
}
if opcode == 161 {
// LOG1
logging_operations::exec_log1(ref machine);
return machine.exec_log1();
}
if opcode == 162 {
// LOG2
logging_operations::exec_log2(ref machine);
return machine.exec_log2();
}
if opcode == 163 {
// LOG3
logging_operations::exec_log3(ref machine);
return machine.exec_log3();
}
if opcode == 164 {
// LOG4
logging_operations::exec_log4(ref machine);
return machine.exec_log4();
}
if opcode == 240 {
// CREATE
Expand Down
16 changes: 16 additions & 0 deletions crates/evm/src/machine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ impl MachineCurrentContextImpl of MachineCurrentContextTrait {
current_call_ctx.read_only()
}

#[inline(always)]
fn set_read_only(ref self: Machine, value: bool) {
let mut current_call_ctx = self.call_context();
Quentash marked this conversation as resolved.
Show resolved Hide resolved
let mut current_execution_ctx = self.current_context.unbox();
current_call_ctx.read_only = value;
current_execution_ctx.call_context = BoxTrait::new(current_call_ctx);
self.current_context = BoxTrait::new(current_execution_ctx);
}

#[inline(always)]
fn append_event(ref self: Machine, event: Event) {
Quentash marked this conversation as resolved.
Show resolved Hide resolved
let mut current_execution_ctx = self.current_context.unbox();
current_execution_ctx.events.append(event);
self.current_context = BoxTrait::new(current_execution_ctx);
}

#[inline(always)]
fn gas_limit(ref self: Machine) -> u64 {
let current_call_ctx = self.call_context();
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[derive(Drop)]
struct Event {
keys: Array<u256>,
data: Array<felt252>,
data: Array<u8>,
}
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 @@ -3,6 +3,7 @@ mod test_comparison_operations;
mod test_duplication_operations;
mod test_environment_information;
mod test_exchange_operations;
mod test_logging_operations;
mod test_memory_operations;
mod test_push_operations;
mod test_sha3;
Expand Down
Loading