Skip to content

Commit

Permalink
fix'selfdestruct): immutable map (#1337)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Francois Bojarski <francois.bojarski@consensys.net>
Co-authored-by: Lorenzo Gentile <lorenzogentile404@gmail.com>
  • Loading branch information
1 parent f36621b commit 4c9141d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.consensys.linea.zktracer.module.hub.defer.PostTransactionDefer;
import net.consensys.linea.zktracer.module.hub.fragment.DomSubStampsSubFragment;
import net.consensys.linea.zktracer.module.hub.fragment.TraceFragment;
import net.consensys.linea.zktracer.module.hub.section.halt.EphemeralAccount;
import net.consensys.linea.zktracer.module.romlex.ContractMetadata;
import net.consensys.linea.zktracer.types.EWord;
import net.consensys.linea.zktracer.types.TransactionProcessingMetadata;
Expand Down Expand Up @@ -171,11 +172,10 @@ public Trace trace(Trace trace) {
@Override
public void resolvePostTransaction(
Hub hub, WorldView state, Transaction tx, boolean isSuccessful) {
final Map<TransactionProcessingMetadata.EphemeralAccount, Integer> effectiveSelfDestructMap =
final Map<EphemeralAccount, Integer> effectiveSelfDestructMap =
transactionProcessingMetadata.getEffectiveSelfDestructMap();
final TransactionProcessingMetadata.EphemeralAccount ephemeralAccount =
new TransactionProcessingMetadata.EphemeralAccount(
oldState.address(), oldState.deploymentNumber());
final EphemeralAccount ephemeralAccount =
new EphemeralAccount(oldState.address(), oldState.deploymentNumber());
if (effectiveSelfDestructMap.containsKey(ephemeralAccount)) {
final int selfDestructTime = effectiveSelfDestructMap.get(ephemeralAccount);
markedForSelfDestruct = hubStamp > selfDestructTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright ConsenSys Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package net.consensys.linea.zktracer.module.hub.section.halt;

import net.consensys.linea.zktracer.runtime.callstack.CallFrame;

public record AttemptedSelfDestruct(int hubStamp, CallFrame callFrame) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright ConsenSys Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package net.consensys.linea.zktracer.module.hub.section.halt;

import org.hyperledger.besu.datatypes.Address;

/**
* Ephemeral accounts are both accounts that have been deployed on-chain and accounts that live for
* a limited time
*/
public record EphemeralAccount(Address address, int deploymentNumber) {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.base.Preconditions.*;
import static net.consensys.linea.zktracer.module.hub.signals.Exceptions.OUT_OF_GAS_EXCEPTION;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -33,8 +34,6 @@
import net.consensys.linea.zktracer.module.hub.signals.Exceptions;
import net.consensys.linea.zktracer.runtime.callstack.CallFrame;
import net.consensys.linea.zktracer.types.TransactionProcessingMetadata;
import net.consensys.linea.zktracer.types.TransactionProcessingMetadata.AttemptedSelfDestruct;
import net.consensys.linea.zktracer.types.TransactionProcessingMetadata.EphemeralAccount;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.hyperledger.besu.datatypes.Address;
Expand Down Expand Up @@ -137,23 +136,22 @@ public SelfdestructSection(Hub hub) {
}

// Unexceptional case
Map<EphemeralAccount, List<AttemptedSelfDestruct>> unexceptionalSelfDestructMap =
final Map<EphemeralAccount, List<AttemptedSelfDestruct>> unexceptionalSelfDestructMap =
hub.txStack().current().getUnexceptionalSelfDestructMap();

EphemeralAccount ephemeralAccount =
final EphemeralAccount ephemeralAccount =
new EphemeralAccount(
addressWhichMaySelfDestruct, selfdestructorAccountBefore.deploymentNumber());

if (unexceptionalSelfDestructMap.containsKey(ephemeralAccount)) {
List<AttemptedSelfDestruct> attemptedSelfDestructs =
unexceptionalSelfDestructMap.get(ephemeralAccount);
attemptedSelfDestructs.add(new AttemptedSelfDestruct(hubStamp, hub.currentFrame()));
// We do not need to put again the list in the map, as it is a reference
unexceptionalSelfDestructMap
.get(ephemeralAccount)
.add(new AttemptedSelfDestruct(hubStamp, hub.currentFrame()));
} else {
unexceptionalSelfDestructMap.put(
new EphemeralAccount(
addressWhichMaySelfDestruct, selfdestructorAccountBefore.deploymentNumber()),
List.of(new AttemptedSelfDestruct(hubStamp, hub.currentFrame())));
new ArrayList<>(List.of(new AttemptedSelfDestruct(hubStamp, hub.currentFrame()))));
}

hub.defers().scheduleForPostRollback(this, hub.currentFrame());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import net.consensys.linea.zktracer.ZkTracer;
import net.consensys.linea.zktracer.module.hub.AccountSnapshot;
import net.consensys.linea.zktracer.module.hub.Hub;
import net.consensys.linea.zktracer.module.hub.section.halt.AttemptedSelfDestruct;
import net.consensys.linea.zktracer.module.hub.section.halt.EphemeralAccount;
import net.consensys.linea.zktracer.module.hub.transients.Block;
import net.consensys.linea.zktracer.module.hub.transients.StorageInitialValues;
import net.consensys.linea.zktracer.runtime.callstack.CallFrame;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Transaction;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -121,12 +122,6 @@ public class TransactionProcessingMetadata {

@Getter final Map<EphemeralAccount, Integer> effectiveSelfDestructMap = new HashMap<>();

// Ephermeral accounts are both accounts that have been deployed on-chain
// and accounts that live for a limited time
public record EphemeralAccount(Address address, int deploymentNumber) {}

public record AttemptedSelfDestruct(int hubStamp, CallFrame callFrame) {}

public TransactionProcessingMetadata(
final WorldView world,
final Transaction transaction,
Expand Down Expand Up @@ -168,14 +163,14 @@ public void setPreFinalisationValues(
final boolean coinbaseIsWarmAtFinalisation,
final int accumulatedGasUsedInBlockAtStartTx) {

this.isCoinbaseWarmAtTransactionEnd(coinbaseIsWarmAtFinalisation);
isCoinbaseWarmAtTransactionEnd(coinbaseIsWarmAtFinalisation);
this.refundCounterMax = refundCounterMax;
this.setLeftoverGas(leftOverGas);
this.gasUsed = computeGasUsed();
this.refundEffective = computeRefundEffective();
this.gasRefunded = computeRefunded();
this.totalGasUsed = computeTotalGasUsed();
this.accumulatedGasUsedInBlock = (int) (accumulatedGasUsedInBlockAtStartTx + totalGasUsed);
setLeftoverGas(leftOverGas);
gasUsed = computeGasUsed();
refundEffective = computeRefundEffective();
gasRefunded = computeRefunded();
totalGasUsed = computeTotalGasUsed();
accumulatedGasUsedInBlock = (int) (accumulatedGasUsedInBlockAtStartTx + totalGasUsed);
}

public void completeLineaTransaction(
Expand Down Expand Up @@ -208,7 +203,7 @@ private boolean computeRequiresEvmExecution(WorldView world) {
.orElse(false);
}

return !this.besuTransaction.getInit().get().isEmpty();
return !besuTransaction.getInit().get().isEmpty();
}

private BigInteger getInitialBalance(WorldView world) {
Expand All @@ -228,7 +223,7 @@ public long getInitiallyAvailableGas() {
}

private long computeRefundEffective() {
final long maxRefundableAmount = this.getGasUsed() / MAX_REFUND_QUOTIENT;
final long maxRefundableAmount = getGasUsed() / MAX_REFUND_QUOTIENT;
return Math.min(maxRefundableAmount, refundCounterMax);
}

Expand Down Expand Up @@ -268,7 +263,7 @@ public long computeGasUsed() {

/* g* in the EYP */
public long computeRefunded() {
return leftoverGas + this.refundEffective;
return leftoverGas + refundEffective;
}

/* Tg - g* in the EYP */
Expand All @@ -294,14 +289,14 @@ public Wei getGasRefundInWei() {
}

public int numberWarmedAddress() {
return this.besuTransaction.getAccessList().isPresent()
? this.besuTransaction.getAccessList().get().size()
return besuTransaction.getAccessList().isPresent()
? besuTransaction.getAccessList().get().size()
: 0;
}

public int numberWarmedKey() {
return this.besuTransaction.getAccessList().isPresent()
? this.besuTransaction.getAccessList().get().stream()
return besuTransaction.getAccessList().isPresent()
? besuTransaction.getAccessList().get().stream()
.mapToInt(accessListEntry -> accessListEntry.storageKeys().size())
.sum()
: 0;
Expand Down

0 comments on commit 4c9141d

Please sign in to comment.