-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HIP-584: Fix too low estimate values for auto account creation and co…
…ntract create (#6633) Currently gas estimate for hollow account creation and contract creates is too low. This PR adds some missing logic and aligns gas calculator behaviour with services codebase. Signed-off-by: IvanKavaldzhiev <ivankavaldzhiev@gmail.com>
- Loading branch information
1 parent
56c5e32
commit 52a3fd7
Showing
13 changed files
with
549 additions
and
161 deletions.
There are no files selected for viewing
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
33 changes: 0 additions & 33 deletions
33
...-web3/src/main/java/com/hedera/mirror/web3/evm/contracts/execution/PricesAndFeesImpl.java
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
...a-mirror-web3/src/main/java/com/hedera/services/contracts/execution/LivePricesSource.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,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; | ||
} 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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...b3/src/main/java/com/hedera/services/contracts/gascalculator/GasCalculatorHederaUtil.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,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"); | ||
} | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.