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);
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations