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 all 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
9 changes: 6 additions & 3 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 @@ -320,7 +321,7 @@ else if (!currentValue.isZero() && newValue.isZero()) {
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 chunkUsed = getSizeInWords(size.longValueSafe());
gasCost += chunkUsed * gasCosts.getSHA3_WORD();
break;
case CALLDATACOPY:
Expand Down Expand Up @@ -394,8 +395,10 @@ else if (!currentValue.isZero() && newValue.isZero()) {
memNeeded(stack.get(stack.size() - 2), stack.get(stack.size() - 3)), 0);
break;
case CREATE2:
gasCost = gasCosts.getCREATE() + calcMemGas(gasCosts, oldMemSize,
memNeeded(stack.get(stack.size() - 2), stack.get(stack.size() - 3)), 0);
DataWord codeSize = stack.get(stack.size() - 3);
gasCost = gasCosts.getCREATE() +
calcMemGas(gasCosts, oldMemSize, memNeeded(stack.get(stack.size() - 2), codeSize), 0) +
getSizeInWords(codeSize.longValueSafe()) * 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;
}
}