Implementation of PUSH0 (#4660)

Initial implementation of  PUSH0

Signed-off-by: lukelee-sl <luke.lee@swirldslabs.com>
Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
pull/4649/head
lukelee-sl 2 years ago committed by GitHub
parent e7074c0ecb
commit 148d01102b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 65
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/vm/operations/Push0OperationTest.java
  2. 4
      evm/src/main/java/org/hyperledger/besu/evm/MainnetEVMs.java
  3. 42
      evm/src/main/java/org/hyperledger/besu/evm/operation/Push0Operation.java

@ -0,0 +1,65 @@
/*
* Copyright contributors to Hyperledger Besu
*
* 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.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.evm.Code.EMPTY_CODE;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.BerlinGasCalculator;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.operation.Operation;
import org.hyperledger.besu.evm.operation.Operation.OperationResult;
import org.hyperledger.besu.evm.operation.Push0Operation;
import java.util.Optional;
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class Push0OperationTest {
private final GasCalculator gasCalculator = new BerlinGasCalculator();
@Test
public void shouldPush0OntoStack() {
final MessageFrame frame = createMessageFrame(100, Optional.of(Wei.of(5L)));
final Operation operation = new Push0Operation(gasCalculator);
final OperationResult result = operation.execute(frame, null);
Mockito.verify(frame).pushStackItem(Bytes.EMPTY);
assertThat(result.getGasCost()).isEqualTo(gasCalculator.getBaseTierGasCost());
assertSuccessResult(result);
}
private void assertSuccessResult(final OperationResult result) {
assertThat(result).isNotNull();
assertThat(result.getHaltReason()).isNull();
}
private MessageFrame createMessageFrame(final long initialGas, final Optional<Wei> baseFee) {
final MessageFrame frame = mock(MessageFrame.class);
when(frame.getRemainingGas()).thenReturn(initialGas);
final BlockHeader blockHeader = mock(BlockHeader.class);
when(blockHeader.getBaseFee()).thenReturn(baseFee);
when(frame.getBlockValues()).thenReturn(blockHeader);
when(frame.getCode()).thenReturn(EMPTY_CODE);
return frame;
}
}

@ -82,6 +82,7 @@ import org.hyperledger.besu.evm.operation.OriginOperation;
import org.hyperledger.besu.evm.operation.PCOperation;
import org.hyperledger.besu.evm.operation.PopOperation;
import org.hyperledger.besu.evm.operation.PrevRanDaoOperation;
import org.hyperledger.besu.evm.operation.Push0Operation;
import org.hyperledger.besu.evm.operation.PushOperation;
import org.hyperledger.besu.evm.operation.ReturnDataCopyOperation;
import org.hyperledger.besu.evm.operation.ReturnDataSizeOperation;
@ -425,6 +426,7 @@ public class MainnetEVMs {
final GasCalculator gasCalculator,
final BigInteger chainID) {
registerParisOperations(registry, gasCalculator, chainID);
// PUSH0
// Register the PUSH0 operation.
registry.put(new Push0Operation(gasCalculator));
}
}

@ -0,0 +1,42 @@
/*
* Copyright contributors to Hyperledger Besu
*
* 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.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.apache.tuweni.bytes.Bytes;
public class Push0Operation extends AbstractFixedCostOperation {
public static final int PUSH_BASE = 0x5F;
static final OperationResult push0Success = new OperationResult(2, null);
public Push0Operation(final GasCalculator gasCalculator) {
super(PUSH_BASE, "PUSH0", 0, 1, 1, gasCalculator, gasCalculator.getBaseTierGasCost());
}
@Override
public OperationResult executeFixedCostOperation(final MessageFrame frame, final EVM evm) {
return staticOperation(frame, frame.getPC());
}
public static OperationResult staticOperation(final MessageFrame frame, final int pc) {
frame.pushStackItem(Bytes.EMPTY);
frame.setPC(pc + 1);
return push0Success;
}
}
Loading…
Cancel
Save