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

Implement MCOPY (EIP-5656) #629

Merged
merged 2 commits into from
May 11, 2023
Merged

Implement MCOPY (EIP-5656) #629

merged 2 commits into from
May 11, 2023

Conversation

axic
Copy link
Member

@axic axic commented Apr 29, 2023

@@ -162,6 +162,7 @@ enum Opcode : uint8_t

OP_DUPN = 0xb5,
OP_SWAPN = 0xb6,
OP_MCOPY = 0xb7,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EIP has 0x5c, but that is taken by RJUMP.

@@ -900,6 +900,29 @@ inline code_iterator swapn(StackTop stack, ExecutionState& state, code_iterator
return pos + 2;
}

inline code_iterator mcopy(StackTop stack, ExecutionState& state, code_iterator pos) noexcept
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is based on calldatacopy, given the structure of this feature mirrors that.

The main difference is std::max(dst_index, src_index) ensures memory is expanded to the highest point, so copying can be sure it will copy 0s on padding. This matches the specification in the EIP.

@axic axic force-pushed the mcopy branch 2 times, most recently from 3cf1aa0 to e6f81b7 Compare April 29, 2023 23:15
@axic
Copy link
Member Author

axic commented Apr 29, 2023

Need to add some unit tests.

auto copy_size = static_cast<size_t>(size);

const auto copy_cost = num_words(copy_size) * 3;
if ((gas_left -= copy_cost) < 0)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could merge these two branches.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gumb0 here will be one more use of your helper 😅

@codecov
Copy link

codecov bot commented Apr 29, 2023

Codecov Report

Merging #629 (dfb6656) into master (21d91a3) will increase coverage by 0.03%.
The diff coverage is 97.82%.

❗ Current head dfb6656 differs from pull request most recent head c975564. Consider uploading reports for the commit c975564 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #629      +/-   ##
==========================================
+ Coverage   97.31%   97.34%   +0.03%     
==========================================
  Files          80       80              
  Lines        7677     7729      +52     
==========================================
+ Hits         7471     7524      +53     
+ Misses        206      205       -1     
Flag Coverage Δ
blockchaintests 64.67% <0.00%> (-0.18%) ⬇️
statetests 74.70% <0.00%> (-0.28%) ⬇️
unittests 94.85% <97.82%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
test/unittests/instructions_test.cpp 89.58% <ø> (ø)
lib/evmone/instructions.hpp 99.81% <93.33%> (-0.19%) ⬇️
test/unittests/evm_memory_test.cpp 99.24% <100.00%> (+0.20%) ⬆️
test/utils/bytecode.hpp 96.20% <100.00%> (+0.03%) ⬆️

... and 9 files with indirect coverage changes

@axic
Copy link
Member Author

axic commented May 3, 2023

@gumb0 are you interested taking this over for adding unit tests? Then imho we could merge it, and move on to state tests.

The only question mark is the opcode.

@axic axic marked this pull request as ready for review May 3, 2023 12:35
@axic
Copy link
Member Author

axic commented May 3, 2023

@gumb0 it seems the overlapping one is detected by asan.

lib/evmone/instructions.hpp Outdated Show resolved Hide resolved
@gumb0 gumb0 force-pushed the mcopy branch 2 times, most recently from 0be2974 to fa37f7c Compare May 8, 2023 19:40
@gumb0
Copy link
Member

gumb0 commented May 8, 2023

I've added an overlapping test with src > dst.

@axic
Copy link
Member Author

axic commented May 10, 2023

@chfast @rodiazet okay to merge?

@@ -900,6 +900,29 @@ inline code_iterator swapn(StackTop stack, ExecutionState& state, code_iterator
return pos + 2;
}

inline Result mcopy(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
{
const auto& dst_index = stack.pop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const auto& dst_index = stack.pop();
const auto dst_index = stack.pop();

The .pop() returns a copy of a value so name it like that (reference is technically valid C++ because of some special rule but unnecessary confusion IMHO).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it, but it's a reference in many other similar places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was wrong about it. I forgot the StackTop is just a view type and .pop() does not do much besides helps with counting arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I will change it back.

lib/evmone/instructions.hpp Outdated Show resolved Hide resolved
lib/evmone/instructions.hpp Show resolved Hide resolved
lib/evmone/instructions.hpp Outdated Show resolved Hide resolved
lib/evmone/instructions.hpp Outdated Show resolved Hide resolved
test/unittests/evm_memory_test.cpp Outdated Show resolved Hide resolved
test/unittests/evm_memory_test.cpp Show resolved Hide resolved
@gumb0 gumb0 force-pushed the mcopy branch 2 times, most recently from 823aec8 to ec658bb Compare May 11, 2023 11:03
@chfast chfast changed the title Implement MCOPY Implement MCOPY (EIP-5656) May 11, 2023
@gumb0 gumb0 merged commit ef05c31 into master May 11, 2023
@gumb0 gumb0 deleted the mcopy branch May 11, 2023 12:09
@yperbasis yperbasis added the Cancun Changes for Cancun Ethereum spec revision label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cancun Changes for Cancun Ethereum spec revision
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants