forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EIP-1153: Transient storage opcodes (hyperledger#4118)
* Implement EIP-1153: Transient storage opcodes * Added a new ByteCodeBuilder class to make it easier to construct the E2E test cases. * currently targeting cancun Signed-off-by: Cody Born <codyborn@outlook.com> Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net> Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com> Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net> Signed-off-by: Karim TAAM <karim.t2am@gmail.com> Signed-off-by: Diego López León <dieguitoll@gmail.com> Signed-off-by: Antony Denyer <git@antonydenyer.co.uk> Signed-off-by: Simon Dudley <simon.dudley@consensys.net> Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com> Co-authored-by: Fabio Di Fabio <fabio.difabio@consensys.net> Co-authored-by: Jiri Peinlich <jiri.peinlich@gmail.com> Co-authored-by: Daniel Lehrner <daniel.lehrner@consensys.net> Co-authored-by: Justin Florentine <justin+github@florentine.us> Co-authored-by: garyschulte <garyschulte@gmail.com> Co-authored-by: Gabriel Trintinalia <gabriel.trintinalia@consensys.net> Co-authored-by: Sally MacFarlane <sally.macfarlane@consensys.net> Co-authored-by: matkt <karim.t2am@gmail.com> Co-authored-by: Diego López León <dieguitoll@gmail.com> Co-authored-by: Antony Denyer <git@antonydenyer.co.uk> Co-authored-by: Miguel Rojo <miguelangel.rojofernandez@mastercard.com> Co-authored-by: Miguel Angel Rojo <freemanz1486@gmail.com> Co-authored-by: mark-terry <36909937+mark-terry@users.noreply.github.com> Co-authored-by: Simon Dudley <simon.dudley@consensys.net> Co-authored-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
- Loading branch information
1 parent
98f68ea
commit 53e11e3
Showing
12 changed files
with
1,239 additions
and
4 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
.../java/org/hyperledger/besu/ethereum/vm/operations/TransientStorageOperationBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors. | ||
* | ||
* 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 org.hyperledger.besu.ethereum.vm.operations; | ||
|
||
import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import org.hyperledger.besu.datatypes.Address; | ||
import org.hyperledger.besu.datatypes.Wei; | ||
import org.hyperledger.besu.ethereum.chain.Blockchain; | ||
import org.hyperledger.besu.ethereum.core.BlockHeader; | ||
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; | ||
import org.hyperledger.besu.ethereum.core.MessageFrameTestFixture; | ||
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; | ||
import org.hyperledger.besu.evm.frame.MessageFrame; | ||
import org.hyperledger.besu.evm.gascalculator.CancunGasCalculator; | ||
import org.hyperledger.besu.evm.operation.TLoadOperation; | ||
import org.hyperledger.besu.evm.operation.TStoreOperation; | ||
import org.hyperledger.besu.evm.worldstate.WorldUpdater; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
import org.apache.tuweni.units.bigints.UInt256; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
|
||
@State(Scope.Thread) | ||
public class TransientStorageOperationBenchmark { | ||
|
||
private OperationBenchmarkHelper operationBenchmarkHelper; | ||
private TStoreOperation tstore; | ||
private TLoadOperation tload; | ||
private MessageFrame frame; | ||
|
||
private MessageFrame createMessageFrame(final Address address) { | ||
final Blockchain blockchain = mock(Blockchain.class); | ||
|
||
final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive(); | ||
final WorldUpdater worldStateUpdater = worldStateArchive.getMutable().updater(); | ||
final BlockHeader blockHeader = new BlockHeaderTestFixture().buildHeader(); | ||
final MessageFrame benchmarkFrame = | ||
new MessageFrameTestFixture() | ||
.address(address) | ||
.worldUpdater(worldStateUpdater) | ||
.blockHeader(blockHeader) | ||
.blockchain(blockchain) | ||
.build(); | ||
worldStateUpdater.getOrCreate(address).getMutable().setBalance(Wei.of(1)); | ||
worldStateUpdater.commit(); | ||
|
||
return benchmarkFrame; | ||
} | ||
|
||
@Setup | ||
public void prepare() throws Exception { | ||
operationBenchmarkHelper = OperationBenchmarkHelper.create(); | ||
CancunGasCalculator gasCalculator = new CancunGasCalculator(); | ||
tstore = new TStoreOperation(gasCalculator); | ||
tload = new TLoadOperation(gasCalculator); | ||
frame = createMessageFrame(Address.fromHexString("0x18675309")); | ||
} | ||
|
||
@TearDown | ||
public void cleanUp() throws Exception { | ||
operationBenchmarkHelper.cleanUp(); | ||
} | ||
|
||
@Benchmark | ||
public Bytes executeOperation() { | ||
frame.pushStackItem(UInt256.ONE); | ||
frame.pushStackItem(UInt256.fromHexString("0x01")); | ||
tstore.execute(frame, null); | ||
frame.pushStackItem(UInt256.fromHexString("0x01")); | ||
tload.execute(frame, null); | ||
return frame.popStackItem(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
evm/src/main/java/org/hyperledger/besu/evm/gascalculator/CancunGasCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors. | ||
* | ||
* 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 org.hyperledger.besu.evm.gascalculator; | ||
|
||
/** | ||
* Gas Calculator for Cancun | ||
* | ||
* <UL> | ||
* <LI>Gas costs for TSTORE/TLOAD | ||
* </UL> | ||
*/ | ||
public class CancunGasCalculator extends LondonGasCalculator { | ||
|
||
private static final long TLOAD_GAS = WARM_STORAGE_READ_COST; | ||
private static final long TSTORE_GAS = WARM_STORAGE_READ_COST; | ||
|
||
// EIP-1153 | ||
@Override | ||
public long getTransientLoadOperationGasCost() { | ||
return TLOAD_GAS; | ||
} | ||
|
||
@Override | ||
public long getTransientStoreOperationGasCost() { | ||
return TSTORE_GAS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
evm/src/main/java/org/hyperledger/besu/evm/operation/TLoadOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors. | ||
* | ||
* 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 org.hyperledger.besu.evm.operation; | ||
|
||
import org.hyperledger.besu.evm.EVM; | ||
import org.hyperledger.besu.evm.frame.ExceptionalHaltReason; | ||
import org.hyperledger.besu.evm.frame.MessageFrame; | ||
import org.hyperledger.besu.evm.gascalculator.GasCalculator; | ||
import org.hyperledger.besu.evm.internal.FixedStack.OverflowException; | ||
import org.hyperledger.besu.evm.internal.FixedStack.UnderflowException; | ||
|
||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.apache.tuweni.units.bigints.UInt256; | ||
|
||
/** Implements the TLOAD operation defined in EIP-1153 */ | ||
public class TLoadOperation extends AbstractOperation { | ||
|
||
/** | ||
* TLoad operation | ||
* | ||
* @param gasCalculator gas calculator for costing | ||
*/ | ||
public TLoadOperation(final GasCalculator gasCalculator) { | ||
super(0xb3, "TLOAD", 1, 1, gasCalculator); | ||
} | ||
|
||
@Override | ||
public OperationResult execute(final MessageFrame frame, final EVM evm) { | ||
final long cost = gasCalculator().getTransientLoadOperationGasCost(); | ||
try { | ||
final Bytes32 slot = UInt256.fromBytes(frame.popStackItem()); | ||
if (frame.getRemainingGas() < cost) { | ||
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_GAS); | ||
} else { | ||
frame.pushStackItem(frame.getTransientStorageValue(frame.getRecipientAddress(), slot)); | ||
|
||
return new OperationResult(cost, null); | ||
} | ||
} catch (final UnderflowException ufe) { | ||
return new OperationResult(cost, ExceptionalHaltReason.INSUFFICIENT_STACK_ITEMS); | ||
} catch (final OverflowException ofe) { | ||
return new OperationResult(cost, ExceptionalHaltReason.TOO_MANY_STACK_ITEMS); | ||
} | ||
} | ||
} |
Oops, something went wrong.