Skip to content

Commit da02f50

Browse files
EIP-3651-warm-coinbase (#1961)
Signed-off-by: F Bojarski <[email protected]>
1 parent 8513fbd commit da02f50

File tree

11 files changed

+206
-92
lines changed

11 files changed

+206
-92
lines changed

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/AccountSnapshot.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public static AccountSnapshot canonical(Hub hub, Address address) {
6161
isAddressWarm(hub.messageFrame(), address));
6262
}
6363

64+
public static AccountSnapshot canonical(Hub hub, Address address, boolean warmth) {
65+
return canonical(hub, address).setWarmthTo(warmth);
66+
}
67+
6468
public static AccountSnapshot canonical(Hub hub, WorldView world, Address address) {
6569
return fromArguments(
6670
world,

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/Hub.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,6 @@ public List<Module> getTracelessModules() {
312312
/** reference table modules */
313313
private final List<Module> refTableModules;
314314

315-
public boolean coinbaseWarmthAtTransactionEnd = false;
316-
317315
/**
318316
* @return a list of all modules for which to generate traces
319317
*/
@@ -499,7 +497,7 @@ public void traceStartTransaction(final WorldView world, final Transaction tx) {
499497
new TxPreWarmingMacroSection(world, this);
500498
}
501499
state.processingPhase(TX_INIT);
502-
new TxInitializationSection(this, world);
500+
setInitializationSection(world);
503501
}
504502

505503
// Note: for deployment transactions the deployment number / status were updated during the
@@ -653,13 +651,12 @@ public void traceContextExit(MessageFrame frame) {
653651
.setPreFinalisationValues(
654652
leftOverGas,
655653
gasRefund,
656-
coinbaseWarmthAtTransactionEnd,
657-
txStack.getAccumulativeGasUsedInBlockBeforeTxStart());
654+
txStack.getAccumulativeGasUsedInBlockBeforeTxStart(),
655+
coinbaseWarmthAtTxEnd());
658656

659-
if (state.processingPhase() != TX_SKIP
660-
&& frame.getState() == MessageFrame.State.COMPLETED_SUCCESS) {
657+
if (state.processingPhase() != TX_SKIP) {
661658
state.processingPhase(TX_FINL);
662-
new TxFinalizationSection(this, frame.getWorldUpdater(), false);
659+
new TxFinalizationSection(this);
663660
}
664661
}
665662

@@ -721,18 +718,6 @@ public void tracePostExecution(MessageFrame frame, Operation.OperationResult ope
721718
if (isExceptional() || !opCode().isCallOrCreate()) {
722719
this.unlatchStack(frame, currentSection);
723720
}
724-
725-
if (frame.getDepth() == 0 && (isExceptional() || opCode().isHalt())) {
726-
state.processingPhase(TX_FINL);
727-
coinbaseWarmthAtTransactionEnd =
728-
isExceptional() || opCode() == REVERT
729-
? txStack.current().coinbaseWarmthAfterTxInit(this)
730-
: frame.isAddressWarm(coinbaseAddress());
731-
}
732-
733-
if (frame.getDepth() == 0 && (isExceptional() || opCode() == REVERT)) {
734-
new TxFinalizationSection(this, frame.getWorldUpdater(), true);
735-
}
736721
}
737722

738723
public boolean isUnexceptional() {
@@ -1081,4 +1066,12 @@ protected TransactionStack setTransactionStack() {
10811066
protected TxnData setTxnData() {
10821067
throw new IllegalStateException("must be implemented");
10831068
}
1069+
1070+
protected void setInitializationSection(WorldView world) {
1071+
throw new IllegalStateException("must be implemented");
1072+
}
1073+
1074+
protected boolean coinbaseWarmthAtTxEnd() {
1075+
throw new IllegalStateException("must be implemented");
1076+
}
10841077
}

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/LondonHub.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515

1616
package net.consensys.linea.zktracer.module.hub;
1717

18+
import static net.consensys.linea.zktracer.opcode.OpCode.REVERT;
19+
import static net.consensys.linea.zktracer.types.AddressUtils.isAddressWarm;
20+
1821
import net.consensys.linea.zktracer.ChainConfig;
22+
import net.consensys.linea.zktracer.module.hub.section.txInitializationSection.LondonInitializationSection;
1923
import net.consensys.linea.zktracer.module.hub.state.LondonTransactionStack;
2024
import net.consensys.linea.zktracer.module.hub.state.TransactionStack;
2125
import net.consensys.linea.zktracer.module.txndata.module.LondonTxnData;
2226
import net.consensys.linea.zktracer.module.txndata.module.TxnData;
2327
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
2428
import org.hyperledger.besu.evm.gascalculator.LondonGasCalculator;
29+
import org.hyperledger.besu.evm.worldstate.WorldView;
2530

2631
public class LondonHub extends Hub {
2732
public LondonHub(ChainConfig chain) {
@@ -42,4 +47,16 @@ protected TransactionStack setTransactionStack() {
4247
protected TxnData setTxnData() {
4348
return new LondonTxnData(this, wcp(), euc());
4449
}
50+
51+
@Override
52+
protected void setInitializationSection(WorldView world) {
53+
new LondonInitializationSection(this, world);
54+
}
55+
56+
@Override
57+
protected boolean coinbaseWarmthAtTxEnd() {
58+
return isExceptional() || opCode() == REVERT
59+
? txStack.current().isCoinbasePreWarmed()
60+
: isAddressWarm(messageFrame(), coinbaseAddress());
61+
}
4562
}

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/ShanghaiHub.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
package net.consensys.linea.zktracer.module.hub;
1717

1818
import net.consensys.linea.zktracer.ChainConfig;
19+
import net.consensys.linea.zktracer.module.hub.section.txInitializationSection.ShanghaiInitializationSection;
1920
import net.consensys.linea.zktracer.module.hub.state.ShanghaiTransactionStack;
2021
import net.consensys.linea.zktracer.module.hub.state.TransactionStack;
2122
import net.consensys.linea.zktracer.module.txndata.module.ShanghaiTxnData;
2223
import net.consensys.linea.zktracer.module.txndata.module.TxnData;
2324
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
2425
import org.hyperledger.besu.evm.gascalculator.ShanghaiGasCalculator;
26+
import org.hyperledger.besu.evm.worldstate.WorldView;
2527

2628
public class ShanghaiHub extends LondonHub {
2729
public ShanghaiHub(ChainConfig chain) {
@@ -42,4 +44,16 @@ protected TransactionStack setTransactionStack() {
4244
protected TxnData setTxnData() {
4345
return new ShanghaiTxnData(this, wcp(), euc());
4446
}
47+
48+
@Override
49+
protected void setInitializationSection(WorldView world) {
50+
new ShanghaiInitializationSection(this, world);
51+
}
52+
53+
@Override
54+
protected boolean coinbaseWarmthAtTxEnd() {
55+
// since EIP-3651 (Shanghai), the coinbase address is warm at the beginning of the transaction,
56+
// so obviously at the end.
57+
return true;
58+
}
4559
}

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/TxFinalizationSection.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class TxFinalizationSection extends TraceSection implements EndTransactio
4343
private AccountSnapshot coinbaseGasRefund;
4444
private AccountSnapshot coinbaseGasRefundNew;
4545

46-
public TxFinalizationSection(Hub hub, WorldView world, boolean exceptionOrRevert) {
46+
public TxFinalizationSection(Hub hub) {
4747
super(hub, (short) 4);
4848
hub.defers().scheduleForEndTransaction(this);
4949
txMetadata = hub.txStack().current();
@@ -55,7 +55,7 @@ public void resolveAtEndTransaction(
5555

5656
checkArgument(isSuccessful == txMetadata.statusCode());
5757

58-
DeploymentInfo deploymentInfo = hub.transients().conflation().deploymentInfo();
58+
final DeploymentInfo deploymentInfo = hub.transients().conflation().deploymentInfo();
5959
checkArgument(
6060
!deploymentInfo.getDeploymentStatus(txMetadata.getCoinbaseAddress()),
6161
"The coinbase may not be under deployment");
@@ -109,32 +109,18 @@ private void setSnapshots(Hub hub, WorldView world) {
109109
final Address senderAddress = txMetadata.getSender();
110110
final Address coinbaseAddress = txMetadata.getCoinbaseAddress();
111111

112-
if (senderIsCoinbase(hub)) {
113-
checkState(coinbaseWarmth());
114-
}
115-
116112
coinbaseGasRefundNew =
117113
AccountSnapshot.canonical(hub, world, coinbaseAddress)
118-
.setWarmthTo(coinbaseWarmth())
114+
.setWarmthTo(txMetadata.coinbaseWarmAtTransactionEnd())
119115
.setDeploymentInfo(hub);
120116
coinbaseGasRefund =
121117
coinbaseGasRefundNew.deepCopy().decrementBalanceBy(txMetadata.getCoinbaseReward());
122118

123119
senderGasRefundNew =
124-
senderIsCoinbase(hub)
120+
txMetadata.senderIsCoinbase()
125121
? coinbaseGasRefund.deepCopy()
126122
: AccountSnapshot.canonical(hub, world, senderAddress).turnOnWarmth();
127123
senderGasRefund =
128124
senderGasRefundNew.deepCopy().decrementBalanceBy(txMetadata.getGasRefundInWei());
129125
}
130-
131-
private boolean coinbaseWarmth() {
132-
return txMetadata.isCoinbaseWarmAtTransactionEnd();
133-
}
134-
135-
public static boolean senderIsCoinbase(Hub hub) {
136-
final TransactionProcessingMetadata tx = hub.txStack().current();
137-
final Address senderAddress = tx.getSender();
138-
return tx.getCoinbaseAddress().equals(senderAddress);
139-
}
140126
}

arithmetization/src/main/java/net/consensys/linea/zktracer/module/hub/section/TxPreWarmingMacroSection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public TxPreWarmingMacroSection(WorldView world, Hub hub) {
139139
final Address recipientAddress = effectiveToAddress(besuTx);
140140
currentTxMetadata.isSenderPreWarmed(seenAddresses.contains(senderAddress));
141141
currentTxMetadata.isRecipientPreWarmed(seenAddresses.contains(recipientAddress));
142+
currentTxMetadata.isCoinbasePreWarmed(
143+
seenAddresses.contains(hub.coinbaseAddress()));
142144
}
143145
});
144146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright ConsenSys Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.hub.section.txInitializationSection;
17+
18+
import net.consensys.linea.zktracer.module.hub.Hub;
19+
import org.hyperledger.besu.evm.worldstate.WorldView;
20+
21+
public class LondonInitializationSection extends TxInitializationSection {
22+
public LondonInitializationSection(Hub hub, WorldView world) {
23+
super(hub, world);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright ConsenSys Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*
13+
* SPDX-License-Identifier: Apache-2.0
14+
*/
15+
16+
package net.consensys.linea.zktracer.module.hub.section.txInitializationSection;
17+
18+
import static net.consensys.linea.zktracer.module.hub.AccountSnapshot.canonical;
19+
20+
import net.consensys.linea.zktracer.module.hub.AccountSnapshot;
21+
import net.consensys.linea.zktracer.module.hub.Hub;
22+
import net.consensys.linea.zktracer.module.hub.fragment.DomSubStampsSubFragment;
23+
import net.consensys.linea.zktracer.module.hub.fragment.account.AccountFragment;
24+
import net.consensys.linea.zktracer.types.TransactionProcessingMetadata;
25+
import org.hyperledger.besu.evm.worldstate.WorldView;
26+
27+
public class ShanghaiInitializationSection extends LondonInitializationSection {
28+
public ShanghaiInitializationSection(Hub hub, WorldView world) {
29+
super(hub, world);
30+
}
31+
32+
@Override
33+
protected void addCoinbaseWarmingFragment() {
34+
this.addFragment(coinbaseWarmingAccountFragment);
35+
}
36+
37+
@Override
38+
protected AccountFragment makeCoinbaseWarmingFragment(
39+
final Hub hub, final WorldView world, final TransactionProcessingMetadata tx) {
40+
final AccountSnapshot coinbase =
41+
canonical(hub, world, hub.coinbaseAddress(), tx.isCoinbasePreWarmed());
42+
return accountFragmentFactory.makeWithTrm(
43+
coinbase,
44+
coinbase.deepCopy().turnOnWarmth(),
45+
coinbase.address(),
46+
DomSubStampsSubFragment.standardDomSubStamps(getHubStamp(), domSubOffset()));
47+
}
48+
49+
@Override
50+
protected boolean senderWarmthAtGasPayment(final TransactionProcessingMetadata tx) {
51+
return tx.isSenderPreWarmed() || tx.senderIsCoinbase();
52+
}
53+
54+
@Override
55+
protected boolean recipientWarmthAtValueReception(TransactionProcessingMetadata tx) {
56+
return tx.isRecipientPreWarmed() || tx.recipientIsCoinbase();
57+
}
58+
}

0 commit comments

Comments
 (0)