Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b2bdc82

Browse files
authored
Merge pull request #1204 from ethereum/feature/create2-gas-cost-update
CREATE2 gas cost update
2 parents c27dc0f + fe10be2 commit b2bdc82

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

ethereumj-core/src/main/java/org/ethereum/vm/PrecompiledContracts.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.ethereum.util.BIUtil.isLessThan;
3131
import static org.ethereum.util.BIUtil.isZero;
3232
import static org.ethereum.util.ByteUtil.*;
33+
import static org.ethereum.vm.VMUtils.getSizeInWords;
3334

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

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

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

148149
@Override

ethereumj-core/src/main/java/org/ethereum/vm/VM.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
4242
import static org.ethereum.util.ByteUtil.toHexString;
4343
import static org.ethereum.vm.OpCode.*;
44+
import static org.ethereum.vm.VMUtils.getSizeInWords;
4445

4546
/**
4647
* The Ethereum Virtual Machine (EVM) is responsible for initialization
@@ -320,7 +321,7 @@ else if (!currentValue.isZero() && newValue.isZero()) {
320321
case SHA3:
321322
gasCost = gasCosts.getSHA3() + calcMemGas(gasCosts, oldMemSize, memNeeded(stack.peek(), stack.get(stack.size() - 2)), 0);
322323
DataWord size = stack.get(stack.size() - 2);
323-
long chunkUsed = (size.longValueSafe() + 31) / 32;
324+
long chunkUsed = getSizeInWords(size.longValueSafe());
324325
gasCost += chunkUsed * gasCosts.getSHA3_WORD();
325326
break;
326327
case CALLDATACOPY:
@@ -394,8 +395,10 @@ else if (!currentValue.isZero() && newValue.isZero()) {
394395
memNeeded(stack.get(stack.size() - 2), stack.get(stack.size() - 3)), 0);
395396
break;
396397
case CREATE2:
397-
gasCost = gasCosts.getCREATE() + calcMemGas(gasCosts, oldMemSize,
398-
memNeeded(stack.get(stack.size() - 2), stack.get(stack.size() - 3)), 0);
398+
DataWord codeSize = stack.get(stack.size() - 3);
399+
gasCost = gasCosts.getCREATE() +
400+
calcMemGas(gasCosts, oldMemSize, memNeeded(stack.get(stack.size() - 2), codeSize), 0) +
401+
getSizeInWords(codeSize.longValueSafe()) * gasCosts.getSHA3_WORD();
399402
break;
400403
case LOG0:
401404
case LOG1:

ethereumj-core/src/main/java/org/ethereum/vm/VMUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,11 @@ public static String unzipAndDecode(String content) {
153153
return content;
154154
}
155155
}
156+
157+
/**
158+
* Returns number of VM words required to hold data of size {@code size}
159+
*/
160+
public static long getSizeInWords(long size) {
161+
return size == 0 ? 0 : (size - 1) / 32 + 1;
162+
}
156163
}

0 commit comments

Comments
 (0)