Search in sources :

Example 21 with Gas

use of org.hyperledger.besu.evm.Gas in project besu by hyperledger.

the class FrontierGasCalculator method copyWordsToMemoryGasCost.

protected Gas copyWordsToMemoryGasCost(final MessageFrame frame, final Gas baseGasCost, final Gas wordGasCost, final long offset, final long length) {
    final long numWords = length / 32 + (length % 32 == 0 ? 0 : 1);
    final Gas copyCost = wordGasCost.times(Gas.of(numWords)).plus(baseGasCost);
    final Gas memoryCost = memoryExpansionGasCost(frame, offset, length);
    return copyCost.plus(memoryCost);
}
Also used : Gas(org.hyperledger.besu.evm.Gas)

Example 22 with Gas

use of org.hyperledger.besu.evm.Gas in project besu by hyperledger.

the class FrontierGasCalculator method callOperationGasCost.

@Override
public Gas callOperationGasCost(final MessageFrame frame, final Gas stipend, final long inputDataOffset, final long inputDataLength, final long outputDataOffset, final long outputDataLength, final Wei transferValue, final Account recipient, final Address to) {
    final Gas inputDataMemoryExpansionCost = memoryExpansionGasCost(frame, inputDataOffset, inputDataLength);
    final Gas outputDataMemoryExpansionCost = memoryExpansionGasCost(frame, outputDataOffset, outputDataLength);
    final Gas memoryExpansionCost = inputDataMemoryExpansionCost.max(outputDataMemoryExpansionCost);
    Gas cost = callOperationBaseGasCost().plus(stipend).plus(memoryExpansionCost);
    if (!transferValue.isZero()) {
        cost = cost.plus(callValueTransferGasCost());
    }
    if (recipient == null) {
        cost = cost.plus(newAccountGasCost());
    }
    return cost;
}
Also used : Gas(org.hyperledger.besu.evm.Gas)

Example 23 with Gas

use of org.hyperledger.besu.evm.Gas in project besu by hyperledger.

the class FrontierGasCalculator method transactionIntrinsicGasCost.

@Override
public Gas transactionIntrinsicGasCost(final Bytes payload, final boolean isContractCreate) {
    int zeros = 0;
    for (int i = 0; i < payload.size(); i++) {
        if (payload.get(i) == 0) {
            ++zeros;
        }
    }
    final int nonZeros = payload.size() - zeros;
    Gas cost = TX_BASE_COST.plus(TX_DATA_ZERO_COST.times(zeros)).plus(TX_DATA_NON_ZERO_COST.times(nonZeros));
    return isContractCreate ? cost.plus(txCreateExtraGasCost()) : cost;
}
Also used : Gas(org.hyperledger.besu.evm.Gas)

Example 24 with Gas

use of org.hyperledger.besu.evm.Gas in project besu by hyperledger.

the class FrontierGasCalculator method createOperationGasCost.

@Override
public Gas createOperationGasCost(final MessageFrame frame) {
    final long initCodeOffset = clampedToLong(frame.getStackItem(1));
    final long initCodeLength = clampedToLong(frame.getStackItem(2));
    final Gas memoryGasCost = memoryExpansionGasCost(frame, initCodeOffset, initCodeLength);
    return CREATE_OPERATION_GAS_COST.plus(memoryGasCost);
}
Also used : Gas(org.hyperledger.besu.evm.Gas)

Example 25 with Gas

use of org.hyperledger.besu.evm.Gas in project besu by hyperledger.

the class VMReferenceTest method runTest.

@Override
protected void runTest() {
    final MutableWorldState worldState = new DefaultMutableWorldState(spec.getInitialWorldState());
    final EnvironmentInformation execEnv = spec.getExec();
    final ProtocolSpec protocolSpec = MainnetProtocolSpecs.frontierDefinition(OptionalInt.empty(), OptionalInt.empty(), false, EvmConfiguration.DEFAULT).privacyParameters(PrivacyParameters.DEFAULT).privateTransactionValidatorBuilder(() -> new PrivateTransactionValidator(CHAIN_ID)).badBlocksManager(new BadBlockManager()).build(new MutableProtocolSchedule(CHAIN_ID));
    final ReferenceTestBlockchain blockchain = new ReferenceTestBlockchain(execEnv.getBlockHeader().getNumber());
    final MessageFrame frame = MessageFrame.builder().type(MessageFrame.Type.MESSAGE_CALL).messageFrameStack(new ArrayDeque<>()).worldUpdater(worldState.updater()).initialGas(spec.getExec().getGas()).contract(execEnv.getAccountAddress()).address(execEnv.getAccountAddress()).originator(execEnv.getOriginAddress()).gasPrice(execEnv.getGasPrice()).inputData(execEnv.getData()).sender(execEnv.getCallerAddress()).value(execEnv.getValue()).apparentValue(execEnv.getValue()).code(execEnv.getCode()).blockValues(execEnv.getBlockHeader()).depth(execEnv.getDepth()).completer(c -> {
    }).miningBeneficiary(execEnv.getBlockHeader().getCoinbase()).blockHashLookup(new BlockHashLookup(execEnv.getBlockHeader(), blockchain)).maxStackSize(MessageFrame.DEFAULT_MAX_STACK_SIZE).build();
    // This is normally set inside the containing message executing the code.
    frame.setState(MessageFrame.State.CODE_EXECUTING);
    protocolSpec.getEvm().runToHalt(frame, OperationTracer.NO_TRACING);
    if (spec.isExceptionHaltExpected()) {
        assertThat(frame.getState() == MessageFrame.State.EXCEPTIONAL_HALT).withFailMessage("VM should have exceptionally halted").isTrue();
    } else {
        // This is normally performed when the message processor executing the VM
        // executes to completion successfully.
        frame.getWorldUpdater().commit();
        assertThat(frame.getState() == MessageFrame.State.EXCEPTIONAL_HALT).withFailMessage("VM should not have exceptionally halted with " + frame.getExceptionalHaltReason()).isFalse();
        assertThat(frame.getOutputData()).withFailMessage("VM output differs").isEqualTo(spec.getOut());
        assertThat(worldState.rootHash()).withFailMessage("Final world state differs").isEqualTo(spec.getFinalWorldState().rootHash());
        final Gas actualGas = frame.getRemainingGas();
        final Gas expectedGas = spec.getFinalGas();
        final Gas difference = (expectedGas.compareTo(actualGas) > 0) ? expectedGas.minus(actualGas) : actualGas.minus(expectedGas);
        assertThat(actualGas).withFailMessage("Final gas does not match, with difference of %s", difference).isEqualTo(expectedGas);
    }
}
Also used : MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) MessageFrame(org.hyperledger.besu.evm.frame.MessageFrame) DefaultMutableWorldState(org.hyperledger.besu.ethereum.worldstate.DefaultMutableWorldState) Gas(org.hyperledger.besu.evm.Gas) EnvironmentInformation(org.hyperledger.besu.ethereum.referencetests.EnvironmentInformation) PrivateTransactionValidator(org.hyperledger.besu.ethereum.privacy.PrivateTransactionValidator) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) ReferenceTestBlockchain(org.hyperledger.besu.ethereum.referencetests.ReferenceTestBlockchain)

Aggregations

Gas (org.hyperledger.besu.evm.Gas)29 Address (org.hyperledger.besu.datatypes.Address)6 Bytes (org.apache.tuweni.bytes.Bytes)5 Wei (org.hyperledger.besu.datatypes.Wei)4 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)4 UInt256 (org.apache.tuweni.units.bigints.UInt256)3 MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)3 HederaWorldState (com.hedera.services.store.contracts.HederaWorldState)2 HederaWorldUpdater (com.hedera.services.store.contracts.HederaWorldUpdater)2 ImmutableList (com.google.common.collect.ImmutableList)1 InvalidTransactionException (com.hedera.services.exceptions.InvalidTransactionException)1 HederaStackedWorldStateUpdater (com.hedera.services.store.contracts.HederaStackedWorldStateUpdater)1 ArrayDeque (java.util.ArrayDeque)1 Map (java.util.Map)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 BadBlockManager (org.hyperledger.besu.ethereum.chain.BadBlockManager)1 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)1 MutableProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule)1 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)1 PrivateTransactionValidator (org.hyperledger.besu.ethereum.privacy.PrivateTransactionValidator)1