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

test: Contract Potential Inaccurate fee estimation #16761 #17147

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (C) 2024 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.bdd.suites.fees;

import static com.hedera.services.bdd.junit.TestTags.SMART_CONTRACT;
import static com.hedera.services.bdd.spec.HapiSpec.hapiTest;
import static com.hedera.services.bdd.spec.queries.QueryVerbs.contractCallLocal;
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.contractCreate;
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.cryptoTransfer;
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.ethereumCryptoTransfer;
import static com.hedera.services.bdd.spec.transactions.TxnVerbs.uploadInitCode;
import static com.hedera.services.bdd.spec.transactions.crypto.HapiCryptoTransfer.tinyBarsFromAccountToAlias;
import static com.hedera.services.bdd.spec.utilops.CustomSpecAssert.allRunFor;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.newKeyNamed;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.validateChargedUsdWithin;
import static com.hedera.services.bdd.spec.utilops.UtilVerbs.withOpContext;
import static com.hedera.services.bdd.suites.HapiSuite.FIVE_HBARS;
import static com.hedera.services.bdd.suites.HapiSuite.GENESIS;
import static com.hedera.services.bdd.suites.HapiSuite.ONE_HUNDRED_HBARS;
import static com.hedera.services.bdd.suites.HapiSuite.SECP_256K1_SHAPE;
import static com.hedera.services.bdd.suites.HapiSuite.SECP_256K1_SOURCE_KEY;

import com.hedera.node.app.hapi.utils.ethereum.EthTxData;
import com.hedera.services.bdd.junit.HapiTest;
import com.hedera.services.bdd.junit.HapiTestLifecycle;
import com.hedera.services.bdd.junit.OrderedInIsolation;
import com.hedera.services.bdd.junit.support.TestLifecycle;
import com.hedera.services.bdd.spec.dsl.annotations.Account;
import com.hedera.services.bdd.spec.dsl.annotations.Contract;
import com.hedera.services.bdd.spec.dsl.entities.SpecAccount;
import com.hedera.services.bdd.spec.dsl.entities.SpecContract;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;

@HapiTestLifecycle
@OrderedInIsolation
@Tag(SMART_CONTRACT)
public class SmartContractServiceFeesTest {

@Contract(contract = "SmartContractsFees")
static SpecContract contract;

@Account(tinybarBalance = ONE_HUNDRED_HBARS)
static SpecAccount civilian;

@BeforeAll
public static void setup(final TestLifecycle lifecycle) {
lifecycle.doAdhoc(contract.getInfo(), civilian.getInfo());
}

@HapiTest
@DisplayName("Create a smart contract and assure proper fee charged")
@Order(0)
final Stream<DynamicTest> contractCreateBaseUSDFee() {
final var creation = "creation";

return hapiTest(
uploadInitCode("EmptyOne"),
contractCreate("EmptyOne")
.gas(500_000L)
.payingWith(civilian.name())
.via(creation),
validateChargedUsdWithin(creation, 1, 50));
}

@HapiTest
@DisplayName("Call a smart contract and assure proper fee charged")
@Order(1)
final Stream<DynamicTest> contractCallBaseUSDFee() {
final var creation = "creation";

return hapiTest(
contract.call("contractCall1Byte", new byte[] {0}).gas(500_000L).via(creation),
validateChargedUsdWithin(creation, 0.05, 50));
}

@HapiTest
@DisplayName("Do an ethereum transaction and assure proper fee charged")
@Order(2)
final Stream<DynamicTest> ethereumTransactionBaseUSDFee(
@Account(tinybarBalance = ONE_HUNDRED_HBARS) final SpecAccount receiver) {
return hapiTest(
receiver.getInfo(),
newKeyNamed(SECP_256K1_SOURCE_KEY).shape(SECP_256K1_SHAPE),
cryptoTransfer(tinyBarsFromAccountToAlias(GENESIS, SECP_256K1_SOURCE_KEY, ONE_HUNDRED_HBARS))
.via("autoAccount"),
ethereumCryptoTransfer(receiver.name(), FIVE_HBARS)
.type(EthTxData.EthTransactionType.EIP1559)
.payingWith(civilian.name())
.via("creation"),
validateChargedUsdWithin("creation", 0.006, 50));
}

@HapiTest
@DisplayName("Call a local smart contract local and assure proper fee charged")
@Order(3)
final Stream<DynamicTest> contractLocalCallBaseUSDFee() {

return hapiTest(withOpContext((spec, opLog) -> allRunFor(
spec,
contractCallLocal(contract.name(), "contractLocalCallGet1Byte")
.gas(500_000L)
.payingWith(civilian.name())
.via("creation"),
validateChargedUsdWithin("creation", 0.0001, 50))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6080604052348015600e575f5ffd5b506101618061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610034575f3560e01c806332abfd7214610038578063dcf4dd6c14610056575b5f5ffd5b610040610072565b60405161004d91906100b9565b60405180910390f35b610070600480360381019061006b9190610100565b61007c565b005b5f5f60f81b905090565b50565b5f7fff0000000000000000000000000000000000000000000000000000000000000082169050919050565b6100b38161007f565b82525050565b5f6020820190506100cc5f8301846100aa565b92915050565b5f5ffd5b6100df8161007f565b81146100e9575f5ffd5b50565b5f813590506100fa816100d6565b92915050565b5f60208284031215610115576101146100d2565b5b5f610122848285016100ec565b9150509291505056fea264697066735822122085034f5503d2b3aa8470034ccb0ceee6a46a200fc7076dafbbd387219f64059964736f6c634300081c0033
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"inputs": [
{
"internalType": "bytes1",
"name": "value",
"type": "bytes1"
}
],
"name": "contractCall1Byte",
"outputs": [],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [],
"name": "contractLocalCallGet1Byte",
"outputs": [
{
"internalType": "bytes1",
"name": "",
"type": "bytes1"
}
],
"stateMutability": "pure",
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract SmartContractsFees {
function contractCall1Byte(bytes1 value) public pure {
}

function contractLocalCallGet1Byte() public pure returns (bytes1) {
return 0x00;
}
}
Loading