Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIP-584: Fix too low estimate values for auto account creation and contract create #6633

Merged
merged 10 commits into from
Aug 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hedera.mirror.web3.evm.properties.MirrorNodeEvmProperties;
import com.hedera.mirror.web3.evm.store.contract.EntityAddressSequencer;
import com.hedera.node.app.service.evm.contracts.execution.EvmProperties;
import com.hedera.services.contracts.execution.LivePricesSource;
import com.hedera.services.contracts.gascalculator.GasCalculatorHederaV22;
import com.hedera.services.fees.BasicHbarCentExchange;
import com.hedera.services.fees.FeeCalculator;
Expand Down Expand Up @@ -193,6 +194,11 @@ UsageBasedFeeCalculator usageBasedFeeCalculator(
txnUsageEstimators);
}

@Bean
LivePricesSource livePricesSource(HbarCentExchange hbarCentExchange, UsagePricesProvider usagePricesProvider) {
return new LivePricesSource(hbarCentExchange, usagePricesProvider);
}

@Bean
ExpandHandleSpanMapAccessor expandHandleSpanMapAccessor() {
return new ExpandHandleSpanMapAccessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.tuweni.bytes.Bytes;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.evm.code.CodeFactory;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.gascalculator.GasCalculator;
import org.hyperledger.besu.evm.processor.ContractCreationProcessor;
Expand All @@ -43,6 +44,7 @@ public class MirrorEvmTxProcessor extends HederaEvmTxProcessor {

private final AbstractCodeCache codeCache;
private final MirrorEvmContractAliases aliasManager;
private final boolean isCreate;

@SuppressWarnings("java:S107")
public MirrorEvmTxProcessor(
Expand All @@ -54,11 +56,13 @@ public MirrorEvmTxProcessor(
final Map<String, Provider<ContractCreationProcessor>> ccps,
final BlockMetaSource blockMetaSource,
final MirrorEvmContractAliases aliasManager,
final AbstractCodeCache codeCache) {
final AbstractCodeCache codeCache,
final boolean isCreate) {
super(worldState, pricesAndFeesProvider, dynamicProperties, gasCalculator, mcps, ccps, blockMetaSource);

this.aliasManager = aliasManager;
this.codeCache = codeCache;
this.isCreate = isCreate;
}

public HederaEvmTransactionProcessingResult execute(
Expand All @@ -69,7 +73,7 @@ public HederaEvmTransactionProcessingResult execute(
final Bytes callData,
final Instant consensusTime,
final boolean isStatic) {
final long gasPrice = gasPriceTinyBarsGiven(consensusTime, false);
final long gasPrice = gasPriceTinyBarsGiven(consensusTime, true);
// in cases where the receiver is the zero address, we know it's a contract create scenario
super.setupFields(receiver.equals(Address.ZERO));

Expand All @@ -86,7 +90,7 @@ public HederaEvmTransactionProcessingResult execute(

@Override
protected HederaFunctionality getFunctionType() {
return HederaFunctionality.ContractCall;
return isCreate ? HederaFunctionality.ContractCreate : HederaFunctionality.ContractCall;
}

@Override
Expand All @@ -99,12 +103,23 @@ protected MessageFrame buildInitialFrame(
ResponseCodeEnum.INVALID_TRANSACTION, StringUtils.EMPTY, StringUtils.EMPTY);
}

return baseInitialFrame
.type(MessageFrame.Type.MESSAGE_CALL)
.address(to)
.contract(to)
.inputData(payload)
.code(code)
.build();
if (isCreate) {
return baseInitialFrame
.type(MessageFrame.Type.CONTRACT_CREATION)
.address(to)
.contract(to)
.inputData(payload)
.inputData(Bytes.EMPTY)
.code(CodeFactory.createCode(payload, 0, false))
.build();
} else {
return baseInitialFrame
.type(MessageFrame.Type.MESSAGE_CALL)
.address(to)
.contract(to)
.inputData(payload)
.code(code)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.hedera.node.app.service.evm.contracts.execution.HederaEvmTransactionProcessingResult;
import com.hedera.node.app.service.evm.store.contracts.AbstractCodeCache;
import com.hedera.node.app.service.evm.store.models.HederaEvmAccount;
import com.hedera.services.contracts.execution.LivePricesSource;
import com.hedera.services.contracts.gascalculator.GasCalculatorHederaV22;
import com.hedera.services.store.contracts.precompile.PrecompileMapper;
import com.hedera.services.txns.crypto.AbstractAutoCreationLogic;
Expand All @@ -52,7 +53,7 @@ public class MirrorEvmTxProcessorFacadeImpl implements MirrorEvmTxProcessorFacad
private final AbstractAutoCreationLogic autoCreationLogic;
private final MirrorNodeEvmProperties evmProperties;
private final StaticBlockMetaSource blockMetaSource;
private final PricesAndFeesImpl pricesAndFees;
private final LivePricesSource pricesAndFees;
private final GasCalculatorHederaV22 gasCalculator;
private final PrecompileMapper precompileMapper;
private final EntityAddressSequencer entityAddressSequencer;
Expand All @@ -67,7 +68,7 @@ public MirrorEvmTxProcessorFacadeImpl(
final MirrorNodeEvmProperties evmProperties,
final TraceProperties traceProperties,
final StaticBlockMetaSource blockMetaSource,
final PricesAndFeesImpl pricesAndFees,
final LivePricesSource pricesAndFees,
final GasCalculatorHederaV22 gasCalculator,
final EntityAddressSequencer entityAddressSequencer,
final ContractRepository contractRepository,
Expand Down Expand Up @@ -131,7 +132,8 @@ public HederaEvmTransactionProcessingResult execute(
ccps(gasCalculator, evmProperties),
blockMetaSource,
mirrorEvmContractAliases,
codeCache);
codeCache,
Address.ZERO.equals(receiver));

processor.setOperationTracer(mirrorOperationTracer);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2019-2023 Hedera Hashgraph, LLC
*
* 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.
*/

package com.hedera.services.contracts.execution;

import static com.hedera.services.hapi.utils.fees.FeeBuilder.getTinybarsFromTinyCents;

import com.hedera.node.app.service.evm.contracts.execution.PricesAndFeesProvider;
import com.hedera.services.fees.HbarCentExchange;
import com.hedera.services.fees.calculation.UsagePricesProvider;
import com.hederahashgraph.api.proto.java.FeeComponents;
import com.hederahashgraph.api.proto.java.HederaFunctionality;
import com.hederahashgraph.api.proto.java.Timestamp;
import java.time.Instant;
import java.util.function.ToLongFunction;

/**
* This class is a modified copy of LivePricesSource from hedera-services repo.
*
* Differences with the original:
* 1. Hardcoded multiplier
*/
public class LivePricesSource implements PricesAndFeesProvider {

private final HbarCentExchange exchange;
private final UsagePricesProvider usagePrices;

public LivePricesSource(final HbarCentExchange exchange, final UsagePricesProvider usagePrices) {
this.exchange = exchange;
this.usagePrices = usagePrices;
}

@Override
public long currentGasPrice(final Instant now, final HederaFunctionality function) {
return currentPrice(now, function, FeeComponents::getGas);
}

private long currentPrice(
final Instant now,
final HederaFunctionality function,
final ToLongFunction<FeeComponents> resourcePriceFn) {
final var timestamp =
Timestamp.newBuilder().setSeconds(now.getEpochSecond()).build();
long feeInTinyCents = currentFeeInTinycents(now, function, resourcePriceFn);
long feeInTinyBars = getTinybarsFromTinyCents(exchange.rate(timestamp), feeInTinyCents);
final var unscaledPrice = Math.max(1L, feeInTinyBars);

final var maxMultiplier = Long.MAX_VALUE / feeInTinyBars;

// Hardcoded to 1, since we don't have congestion control in the Archive Node
final var curMultiplier = 1L;
if (curMultiplier > maxMultiplier) {
return Long.MAX_VALUE;

Check warning on line 66 in hedera-mirror-web3/src/main/java/com/hedera/services/contracts/execution/LivePricesSource.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/services/contracts/execution/LivePricesSource.java#L66

Added line #L66 was not covered by tests
} else {
return unscaledPrice * curMultiplier;
}
}

private long currentFeeInTinycents(
final Instant now,
final HederaFunctionality function,
final ToLongFunction<FeeComponents> resourcePriceFn) {
final var timestamp =
Timestamp.newBuilder().setSeconds(now.getEpochSecond()).build();
final var prices = usagePrices.defaultPricesGiven(function, timestamp);

/* Fee schedule prices are set in thousandths of a tinycent */
return resourcePriceFn.applyAsLong(prices.getServicedata()) / 1000;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* 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.
*/

package com.hedera.services.contracts.gascalculator;

import com.hedera.services.fees.HbarCentExchange;
import com.hedera.services.fees.calculation.UsagePricesProvider;
import com.hedera.services.hapi.utils.fees.FeeBuilder;
import com.hederahashgraph.api.proto.java.FeeData;
import com.hederahashgraph.api.proto.java.HederaFunctionality;
import com.hederahashgraph.api.proto.java.Timestamp;
import org.hyperledger.besu.evm.frame.MessageFrame;

/**
* Direct copy from hedera-services
*
* Utility methods used by Hedera adapted {@link
* org.hyperledger.besu.evm.gascalculator.GasCalculator}
*/
public final class GasCalculatorHederaUtil {
private static final int LOG_CONTRACT_ID_SIZE = 24;
private static final int LOG_TOPIC_SIZE = 32;
private static final int LOG_BLOOM_SIZE = 256;

private GasCalculatorHederaUtil() {
throw new UnsupportedOperationException("Utility Class");

Check warning on line 39 in hedera-mirror-web3/src/main/java/com/hedera/services/contracts/gascalculator/GasCalculatorHederaUtil.java

View check run for this annotation

Codecov / codecov/patch

hedera-mirror-web3/src/main/java/com/hedera/services/contracts/gascalculator/GasCalculatorHederaUtil.java#L38-L39

Added lines #L38 - L39 were not covered by tests
}

public static long ramByteHoursTinyBarsGiven(
final UsagePricesProvider usagePrices,
final HbarCentExchange exchange,
long consensusTime,
HederaFunctionality functionType) {
final var timestamp = Timestamp.newBuilder().setSeconds(consensusTime).build();
FeeData prices = usagePrices.defaultPricesGiven(functionType, timestamp);
long feeInTinyCents = prices.getServicedata().getRbh() / 1000;
long feeInTinyBars = FeeBuilder.getTinybarsFromTinyCents(exchange.rate(timestamp), feeInTinyCents);
return Math.max(1L, feeInTinyBars);
}

public static long calculateLogSize(int numberOfTopics, long dataSize) {
return LOG_CONTRACT_ID_SIZE + LOG_BLOOM_SIZE + LOG_TOPIC_SIZE * (long) numberOfTopics + dataSize;
}

@SuppressWarnings("unused")
public static long calculateStorageGasNeeded(
long numberOfBytes, long durationInSeconds, long byteHourCostInTinyBars, long gasPrice) {
long storageCostTinyBars = (durationInSeconds * byteHourCostInTinyBars) / 3600;
return Math.round((double) storageCostTinyBars / (double) gasPrice);
}

public static HederaFunctionality getFunctionType(MessageFrame frame) {
MessageFrame rootFrame = frame.getMessageFrameStack().getLast();
return rootFrame.getContextVariable("HederaFunctionality");
}

@SuppressWarnings("unused")
public static long logOperationGasCost(
final UsagePricesProvider usagePrices,
final HbarCentExchange exchange,
final MessageFrame frame,
final long storageDuration,
final long dataOffset,
final long dataLength,
final int numTopics) {
long gasPrice = frame.getGasPrice().toLong();
long timestamp = frame.getBlockValues().getTimestamp();
long logStorageTotalSize = GasCalculatorHederaUtil.calculateLogSize(numTopics, dataLength);
HederaFunctionality functionType = GasCalculatorHederaUtil.getFunctionType(frame);

return GasCalculatorHederaUtil.calculateStorageGasNeeded(
logStorageTotalSize,
storageDuration,
GasCalculatorHederaUtil.ramByteHoursTinyBarsGiven(usagePrices, exchange, timestamp, functionType),
gasPrice);
}
}
Loading