Skip to content

Commit c256462

Browse files
authored
Merge branch 'main' into fix/missing-blobGasUsed-copy
2 parents 8111c5d + 278e576 commit c256462

File tree

7 files changed

+153
-62
lines changed

7 files changed

+153
-62
lines changed

evm/src/main/java/org/hyperledger/besu/evm/gascalculator/BerlinGasCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public long getSloadOperationGasCost() {
145145

146146
// Redefined costs from EIP-2929
147147
@Override
148-
public long callOperationGasCost(
148+
public long callOperationStaticGasCost(
149149
final MessageFrame frame,
150150
final long stipend,
151151
final long inputDataOffset,
@@ -156,7 +156,7 @@ public long callOperationGasCost(
156156
final Address recipientAddress,
157157
final boolean accountIsWarm) {
158158
final long baseCost =
159-
super.callOperationGasCost(
159+
super.callOperationStaticGasCost(
160160
frame,
161161
stipend,
162162
inputDataOffset,

evm/src/main/java/org/hyperledger/besu/evm/gascalculator/FrontierGasCalculator.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public long newAccountGasCost() {
285285
}
286286

287287
@Override
288-
public long callOperationGasCost(
288+
public long callOperationStaticGasCost(
289289
final MessageFrame frame,
290290
final long stipend,
291291
final long inputDataOffset,
@@ -308,6 +308,23 @@ public long callOperationGasCost(
308308
cost = clampedAdd(cost, callValueTransferGasCost());
309309
}
310310

311+
return cost;
312+
}
313+
314+
@Override
315+
public long callOperationGasCost(
316+
final MessageFrame frame,
317+
final long staticCallCost,
318+
final long stipend,
319+
final long inputDataOffset,
320+
final long inputDataLength,
321+
final long outputDataOffset,
322+
final long outputDataLength,
323+
final Wei transferValue,
324+
final Address recipientAddress,
325+
final boolean accountIsWarm) {
326+
long cost = staticCallCost;
327+
311328
final Account recipient = frame.getWorldUpdater().get(recipientAddress);
312329
if (recipient == null) {
313330
cost = clampedAdd(cost, newAccountGasCost());

evm/src/main/java/org/hyperledger/besu/evm/gascalculator/GasCalculator.java

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ public interface GasCalculator {
167167
long newAccountGasCost();
168168

169169
/**
170-
* Returns the gas cost for one of the various CALL operations.
170+
* Returns the total call cost given a pre-computed static call cost.
171171
*
172172
* @param frame The current frame
173+
* @param staticCallCost The static call cost
173174
* @param stipend The gas stipend being provided by the CALL caller
174175
* @param inputDataOffset The offset in memory to retrieve the CALL input data
175176
* @param inputDataLength The CALL input data length
@@ -181,15 +182,44 @@ public interface GasCalculator {
181182
* @return The gas cost for the CALL operation
182183
*/
183184
long callOperationGasCost(
184-
MessageFrame frame,
185-
long stipend,
186-
long inputDataOffset,
187-
long inputDataLength,
188-
long outputDataOffset,
189-
long outputDataLength,
190-
Wei transferValue,
191-
Address recipientAddress,
192-
boolean accountIsWarm);
185+
final MessageFrame frame,
186+
final long staticCallCost,
187+
final long stipend,
188+
final long inputDataOffset,
189+
final long inputDataLength,
190+
final long outputDataOffset,
191+
final long outputDataLength,
192+
final Wei transferValue,
193+
final Address recipientAddress,
194+
final boolean accountIsWarm);
195+
196+
/**
197+
* Returns the static gas cost to execute a call operation.
198+
*
199+
* <p>This method <strong>must not</strong> access or mutate world state and it must not mutate
200+
* {@code frame.getEip7928AccessList()}.
201+
*
202+
* @param frame The current frame
203+
* @param stipend The gas stipend being provided by the CALL caller
204+
* @param inputDataOffset The offset in memory to retrieve the CALL input data
205+
* @param inputDataLength The CALL input data length
206+
* @param outputDataOffset The offset in memory to place the CALL output data
207+
* @param outputDataLength The CALL output data length
208+
* @param transferValue The wei being transferred
209+
* @param recipientAddress The CALL recipient (may be null if self destructed or new) address
210+
* @param accountIsWarm The address of the contract is "warm" as per EIP-2929
211+
* @return The static gas cost for the CALL operation
212+
*/
213+
long callOperationStaticGasCost(
214+
final MessageFrame frame,
215+
final long stipend,
216+
final long inputDataOffset,
217+
final long inputDataLength,
218+
final long outputDataOffset,
219+
final long outputDataLength,
220+
final Wei transferValue,
221+
final Address recipientAddress,
222+
final boolean accountIsWarm);
193223

194224
/**
195225
* Gets additional call stipend.
@@ -377,15 +407,15 @@ long callOperationGasCost(
377407
* @return the cost for executing the self destruct operation
378408
*/
379409
default long selfDestructOperationGasCost(final Account recipient, final Wei inheritance) {
380-
return selfDestructOperationBaseGasCost();
410+
return selfDestructOperationStaticGasCost();
381411
}
382412

383413
/**
384-
* Returns the base cost for executing a {@link SelfDestructOperation}.
414+
* Returns the static cost for executing a {@link SelfDestructOperation}.
385415
*
386416
* @return the base cost for executing a {@link SelfDestructOperation}
387417
*/
388-
default long selfDestructOperationBaseGasCost() {
418+
default long selfDestructOperationStaticGasCost() {
389419
return 0L;
390420
}
391421

evm/src/main/java/org/hyperledger/besu/evm/gascalculator/SpuriousDragonGasCalculator.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class SpuriousDragonGasCalculator extends TangerineWhistleGasCalculator {
3030
public SpuriousDragonGasCalculator() {}
3131

3232
@Override
33-
public long callOperationGasCost(
33+
public long callOperationStaticGasCost(
3434
final MessageFrame frame,
3535
final long stipend,
3636
final long inputDataOffset,
@@ -49,16 +49,34 @@ public long callOperationGasCost(
4949

5050
long cost = clampedAdd(callOperationBaseGasCost(), memoryExpansionCost);
5151

52-
final boolean isTransferValueZero = transferValue.isZero();
53-
54-
if (!isTransferValueZero) {
52+
if (!transferValue.isZero()) {
5553
cost = clampedAdd(cost, callValueTransferGasCost());
54+
}
55+
56+
return cost;
57+
}
58+
59+
@Override
60+
public long callOperationGasCost(
61+
final MessageFrame frame,
62+
final long staticCallCost,
63+
final long stipend,
64+
final long inputDataOffset,
65+
final long inputDataLength,
66+
final long outputDataOffset,
67+
final long outputDataLength,
68+
final Wei transferValue,
69+
final Address recipientAddress,
70+
final boolean accountIsWarm) {
71+
if (transferValue.isZero()) {
72+
return staticCallCost;
73+
}
5674

57-
final Account recipient = frame.getWorldUpdater().get(recipientAddress);
58-
frame.getEip7928AccessList().ifPresent(t -> t.addTouchedAccount(recipientAddress));
59-
if (recipient == null || recipient.isEmpty()) {
60-
cost = clampedAdd(cost, newAccountGasCost());
61-
}
75+
long cost = staticCallCost;
76+
final Account recipient = frame.getWorldUpdater().get(recipientAddress);
77+
frame.getEip7928AccessList().ifPresent(t -> t.addTouchedAccount(recipientAddress));
78+
if (recipient == null || recipient.isEmpty()) {
79+
cost = clampedAdd(cost, newAccountGasCost());
6280
}
6381

6482
return cost;
@@ -74,7 +92,7 @@ public long selfDestructOperationGasCost(final Account recipient, final Wei inhe
7492
if ((recipient == null || recipient.isEmpty()) && !inheritance.isZero()) {
7593
return SELFDESTRUCT_OPERATION_CREATES_NEW_ACCOUNT;
7694
} else {
77-
return selfDestructOperationBaseGasCost();
95+
return selfDestructOperationStaticGasCost();
7896
}
7997
}
8098
}

evm/src/main/java/org/hyperledger/besu/evm/gascalculator/TangerineWhistleGasCalculator.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public long callOperationBaseGasCost() {
5858
}
5959

6060
@Override
61-
public long callOperationGasCost(
61+
public long callOperationStaticGasCost(
6262
final MessageFrame frame,
6363
final long stipend,
6464
final long inputDataOffset,
@@ -81,6 +81,23 @@ public long callOperationGasCost(
8181
cost = clampedAdd(cost, callValueTransferGasCost());
8282
}
8383

84+
return cost;
85+
}
86+
87+
@Override
88+
public long callOperationGasCost(
89+
final MessageFrame frame,
90+
final long staticCallCost,
91+
final long stipend,
92+
final long inputDataOffset,
93+
final long inputDataLength,
94+
final long outputDataOffset,
95+
final long outputDataLength,
96+
final Wei transferValue,
97+
final Address recipientAddress,
98+
final boolean accountIsWarm) {
99+
long cost = staticCallCost;
100+
84101
final Account recipient = frame.getWorldUpdater().get(recipientAddress);
85102
if (recipient == null) {
86103
cost = clampedAdd(cost, newAccountGasCost());
@@ -124,12 +141,12 @@ public long selfDestructOperationGasCost(final Account recipient, final Wei inhe
124141
if (recipient == null) {
125142
return SELFDESTRUCT_OPERATION_CREATES_NEW_ACCOUNT;
126143
} else {
127-
return selfDestructOperationBaseGasCost();
144+
return selfDestructOperationStaticGasCost();
128145
}
129146
}
130147

131148
@Override
132-
public long selfDestructOperationBaseGasCost() {
149+
public long selfDestructOperationStaticGasCost() {
133150
return SELFDESTRUCT_OPERATION_GAS_COST;
134151
}
135152

evm/src/main/java/org/hyperledger/besu/evm/operation/AbstractCallOperation.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,44 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
187187

188188
final Address to = to(frame);
189189
final boolean accountIsWarm = frame.warmUpAddress(to) || gasCalculator().isPrecompile(to);
190-
long cost = cost(frame, accountIsWarm);
190+
final long stipend = gas(frame);
191+
final long inputDataOffset = inputDataOffset(frame);
192+
final long inputDataLength = inputDataLength(frame);
193+
final long outputDataOffset = outputDataOffset(frame);
194+
final long outputDataLength = outputDataLength(frame);
195+
final Wei transferValue = value(frame);
196+
final Address recipientAddress = address(frame);
197+
198+
final long staticCost =
199+
gasCalculator()
200+
.callOperationStaticGasCost(
201+
frame,
202+
stipend,
203+
inputDataOffset,
204+
inputDataLength,
205+
outputDataOffset,
206+
outputDataLength,
207+
transferValue,
208+
recipientAddress,
209+
accountIsWarm);
210+
211+
if (frame.getRemainingGas() < staticCost) {
212+
return new OperationResult(staticCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
213+
}
214+
215+
long cost =
216+
gasCalculator()
217+
.callOperationGasCost(
218+
frame,
219+
staticCost,
220+
stipend,
221+
inputDataOffset,
222+
inputDataLength,
223+
outputDataOffset,
224+
outputDataLength,
225+
transferValue,
226+
recipientAddress,
227+
accountIsWarm);
191228
if (frame.getRemainingGas() < cost) {
192229
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS);
193230
}
@@ -255,34 +292,6 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
255292
return new OperationResult(cost, null, 0);
256293
}
257294

258-
/**
259-
* Calculates Cost.
260-
*
261-
* @param frame the frame
262-
* @param accountIsWarm whether the contract being called is "warm" as per EIP-2929.
263-
* @return the long
264-
*/
265-
public long cost(final MessageFrame frame, final boolean accountIsWarm) {
266-
final long stipend = gas(frame);
267-
final long inputDataOffset = inputDataOffset(frame);
268-
final long inputDataLength = inputDataLength(frame);
269-
final long outputDataOffset = outputDataOffset(frame);
270-
final long outputDataLength = outputDataLength(frame);
271-
final Address recipientAddress = address(frame);
272-
GasCalculator gasCalculator = gasCalculator();
273-
274-
return gasCalculator.callOperationGasCost(
275-
frame,
276-
stipend,
277-
inputDataOffset,
278-
inputDataLength,
279-
outputDataOffset,
280-
outputDataLength,
281-
value(frame),
282-
recipientAddress,
283-
accountIsWarm);
284-
}
285-
286295
/**
287296
* Complete.
288297
*

evm/src/main/java/org/hyperledger/besu/evm/operation/SelfDestructOperation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public OperationResult execute(final MessageFrame frame, final EVM evm) {
6565
frame.warmUpAddress(beneficiaryAddress) || gasCalculator().isPrecompile(beneficiaryAddress);
6666
final long beneficiaryAccessCost =
6767
beneficiaryIsWarm ? 0L : gasCalculator().getColdAccountAccessCost();
68-
final long baseCost =
69-
gasCalculator().selfDestructOperationBaseGasCost() + beneficiaryAccessCost;
68+
final long staticCost =
69+
gasCalculator().selfDestructOperationStaticGasCost() + beneficiaryAccessCost;
7070

71-
if (frame.getRemainingGas() < baseCost) {
72-
return new OperationResult(baseCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
71+
if (frame.getRemainingGas() < staticCost) {
72+
return new OperationResult(staticCost, ExceptionalHaltReason.INSUFFICIENT_GAS);
7373
}
7474

7575
final Account beneficiaryNullable = getAccount(beneficiaryAddress, frame);

0 commit comments

Comments
 (0)