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

CREATE2 gas cost update #1204

Merged
merged 3 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.ethereum.util.BIUtil.isLessThan;
import static org.ethereum.util.BIUtil.isZero;
import static org.ethereum.util.ByteUtil.*;
import static org.ethereum.vm.VMUtils.getSizeInWords;

/**
* @author Roman Mandeleil
Expand Down Expand Up @@ -102,7 +103,7 @@ public long getGasForData(byte[] data) {
// gas charge for the execution:
// minimum 1 and additional 1 for each 32 bytes word (round up)
if (data == null) return 15;
return 15 + (data.length + 31) / 32 * 3;
return 15 + getSizeInWords(data.length) * 3;
}

@Override
Expand All @@ -120,7 +121,7 @@ public long getGasForData(byte[] data) {
// gas charge for the execution:
// minimum 50 and additional 50 for each 32 bytes word (round up)
if (data == null) return 60;
return 60 + (data.length + 31) / 32 * 12;
return 60 + getSizeInWords(data.length) * 12;
}

@Override
Expand All @@ -142,7 +143,7 @@ public long getGasForData(byte[] data) {
// gas charge for the execution:
// minimum 50 and additional 50 for each 32 bytes word (round up)
if (data == null) return 600;
return 600 + (data.length + 31) / 32 * 120;
return 600 + getSizeInWords(data.length) * 120;
}

@Override
Expand Down
14 changes: 12 additions & 2 deletions ethereumj-core/src/main/java/org/ethereum/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
import static org.ethereum.util.ByteUtil.toHexString;
import static org.ethereum.vm.OpCode.*;
import static org.ethereum.vm.VMUtils.getSizeInWords;

/**
* The Ethereum Virtual Machine (EVM) is responsible for initialization
Expand Down Expand Up @@ -319,8 +320,11 @@ else if (!currentValue.isZero() && newValue.isZero()) {
break;
case SHA3:
gasCost = gasCosts.getSHA3() + calcMemGas(gasCosts, oldMemSize, memNeeded(stack.peek(), stack.get(stack.size() - 2)), 0);
DataWord size = stack.get(stack.size() - 2);
long chunkUsed = (size.longValueSafe() + 31) / 32;
long size = stack.get(stack.size() - 2).longValueSafe();
if (size == Long.MAX_VALUE) {
Copy link
Member

Choose a reason for hiding this comment

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

What is this check for?

Copy link
Contributor Author

@mkalinin mkalinin Oct 4, 2018

Choose a reason for hiding this comment

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

This check is similar to check in mem gas calculation code. And looks like it's already done in a call to calcMemGas prior to this check. I'll remove it in favor of code readability.

throw Program.Exception.gasOverflow(BigInteger.valueOf(size), BigInteger.valueOf(Long.MAX_VALUE));
}
long chunkUsed = getSizeInWords(size);
gasCost += chunkUsed * gasCosts.getSHA3_WORD();
break;
case CALLDATACOPY:
Expand Down Expand Up @@ -396,6 +400,12 @@ else if (!currentValue.isZero() && newValue.isZero()) {
case CREATE2:
gasCost = gasCosts.getCREATE() + calcMemGas(gasCosts, oldMemSize,
memNeeded(stack.get(stack.size() - 2), stack.get(stack.size() - 3)), 0);
long codeSize = stack.get(stack.size() - 3).longValueSafe();
if (codeSize == Long.MAX_VALUE) {
Copy link
Member

Choose a reason for hiding this comment

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

What is this check for?

throw Program.Exception.gasOverflow(BigInteger.valueOf(codeSize), BigInteger.valueOf(Long.MAX_VALUE));
}
gasCost += getSizeInWords(codeSize) * gasCosts.getSHA3_WORD();

break;
case LOG0:
case LOG1:
Expand Down
7 changes: 7 additions & 0 deletions ethereumj-core/src/main/java/org/ethereum/vm/VMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,11 @@ public static String unzipAndDecode(String content) {
return content;
}
}

/**
* Returns number of VM words required to hold data of size {@code size}
*/
public static long getSizeInWords(long size) {
return size == 0 ? 0 : (size - 1) / 32 + 1;
}
}