Skip to content

Commit

Permalink
Merge pull request #1204 from ethereum/feature/create2-gas-cost-update
Browse files Browse the repository at this point in the history
CREATE2 gas cost update
  • Loading branch information
mkalinin committed Oct 5, 2018
2 parents c27dc0f + fe10be2 commit b2bdc82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
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;
}
}

0 comments on commit b2bdc82

Please sign in to comment.