Skip to content

Commit

Permalink
opt(vm): optimize contract (#6101)
Browse files Browse the repository at this point in the history
* opt(vm): get optimized contract when calling is constant
  • Loading branch information
yanghang8612 authored Nov 28, 2024
1 parent e82620f commit eafada1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/OperationActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,9 @@ public static void exeCall(Program program, DataWord adjustedCallEnergy,
PrecompiledContracts.PrecompiledContract contract =
PrecompiledContracts.getContractForAddress(codeAddress);
if (contract != null) {
if (program.isConstantCall()) {
contract = PrecompiledContracts.getOptimizedContractForConstant(contract);
}
program.callToPrecompiledAddress(msg, contract);
} else {
program.callToAddress(msg);
Expand Down
10 changes: 10 additions & 0 deletions actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;

import com.google.protobuf.ByteString;

import java.lang.reflect.Constructor;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
Expand Down Expand Up @@ -194,6 +196,14 @@ public class PrecompiledContracts {
private static final DataWord blake2FAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000020009");

public static PrecompiledContract getOptimizedContractForConstant(PrecompiledContract contract) {
try {
Constructor<?> constructor = contract.getClass().getDeclaredConstructor();
return (PrecompiledContracts.PrecompiledContract) constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static PrecompiledContract getContractForAddress(DataWord address) {

Expand Down
76 changes: 76 additions & 0 deletions framework/src/test/java/org/tron/core/WalletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.tron.api.GrpcAPI.ProposalList;
import org.tron.common.BaseTest;
import org.tron.common.crypto.ECKey;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Utils;
import org.tron.core.actuator.DelegateResourceActuator;
Expand All @@ -72,6 +73,8 @@
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.utils.ProposalUtil.ProposalType;
import org.tron.core.utils.TransactionUtil;
import org.tron.core.vm.config.ConfigLoader;
import org.tron.core.vm.config.VMConfig;
import org.tron.core.vm.program.Program;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Block;
Expand Down Expand Up @@ -1043,6 +1046,79 @@ public void testGetAssetIssueByName() {
chainBaseManager.getDynamicPropertiesStore().saveAllowSameTokenName(0);
}

@Test
@SneakyThrows
public void testTriggerConstant() {
boolean preDebug = CommonParameter.getInstance().debug;
CommonParameter.getInstance().debug = true;
ConfigLoader.disable = true;
VMConfig.initAllowTvmTransferTrc10(1);
VMConfig.initAllowTvmConstantinople(1);
VMConfig.initAllowTvmShangHai(1);

String contractAddress =
Wallet.getAddressPreFixString() + "1A622D84ed49f01045f5f1a5AfcEb9c57e9cC3cc";

AccountCapsule accountCap = new AccountCapsule(
ByteString.copyFrom(ByteArray.fromHexString(contractAddress)),
Protocol.AccountType.Normal);
dbManager.getAccountStore().put(accountCap.createDbKey(), accountCap);

SmartContractOuterClass.SmartContract smartContract =
SmartContractOuterClass.SmartContract.newBuilder().build();
ContractCapsule contractCap = new ContractCapsule(smartContract);
dbManager.getContractStore().put(ByteArray.fromHexString(contractAddress), contractCap);

String codeString = "608060405234801561000f575f80fd5b50d3801561001b575f80fd5b50d280156100"
+ "27575f80fd5b506004361061004c575f3560e01c80638da5cb5b14610050578063f8a8fd6d1461006e57"
+ "5b5f80fd5b61005861008c565b6040516100659190610269565b60405180910390f35b6100766100af565b"
+ "6040516100839190610269565b60405180910390f35b5f8054906101000a900473ffffffffffffffffffff"
+ "ffffffffffffffffffff1681565b5f60017fbe0166938e2ea2f3f3e0746fdaf46e25c4d8de37ce56d70400"
+ "cf284c80d47bbe601b7f10afab946e2be82aa3e4280cf24e2cab294911c3beb06ca9dd7ead06081265d07f"
+ "1e1855bcdc3ed57c6f3c3874cde035782427d1236e2d819bd16c75676ecc003a6040515f81526020016040"
+ "52604051610133949392919061038f565b6020604051602081039080840390855afa158015610153573d5f"
+ "803e3d5ffd5b505050602060405103515f806101000a81548173ffffffffffffffffffffffffffffffffff"
+ "ffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550734c95a52686a9b3"
+ "ff9cf787b94b8549a988334c5773ffffffffffffffffffffffffffffffffffffffff165f8054906101000a"
+ "900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffff"
+ "ffff1614610205575f80fd5b5f8054906101000a900473ffffffffffffffffffffffffffffffffffffffff"
+ "16905090565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6102538261"
+ "022a565b9050919050565b61026381610249565b82525050565b5f60208201905061027c5f83018461025a"
+ "565b92915050565b5f819050919050565b5f819050919050565b5f815f1b9050919050565b5f6102b96102"
+ "b46102af84610282565b610294565b61028b565b9050919050565b6102c98161029f565b82525050565b5f"
+ "819050919050565b5f60ff82169050919050565b5f819050919050565b5f6103076103026102fd846102cf"
+ "565b6102e4565b6102d8565b9050919050565b610317816102ed565b82525050565b5f819050919050565b"
+ "5f61034061033b6103368461031d565b610294565b61028b565b9050919050565b61035081610326565b82"
+ "525050565b5f819050919050565b5f61037961037461036f84610356565b610294565b61028b565b905091"
+ "9050565b6103898161035f565b82525050565b5f6080820190506103a25f8301876102c0565b6103af6020"
+ "83018661030e565b6103bc6040830185610347565b6103c96060830184610380565b9594505050505056fe"
+ "a26474726f6e58221220e967690f9c06386434cbe4d8dd6dce394130f190d17621cbd4ae4cabdef4ad7964"
+ "736f6c63430008140033";
CodeCapsule codeCap = new CodeCapsule(ByteArray.fromHexString(codeString));
dbManager.getCodeStore().put(ByteArray.fromHexString(contractAddress), codeCap);

SmartContractOuterClass.TriggerSmartContract contract =
SmartContractOuterClass.TriggerSmartContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(contractAddress)))
.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(contractAddress)))
.setData(ByteString.copyFrom(ByteArray.fromHexString("f8a8fd6d")))
.build();
TransactionCapsule trxCap = wallet.createTransactionCapsule(contract,
ContractType.TriggerSmartContract);

GrpcAPI.TransactionExtention.Builder trxExtBuilder = GrpcAPI.TransactionExtention.newBuilder();
GrpcAPI.Return.Builder retBuilder = GrpcAPI.Return.newBuilder();

Transaction tx = wallet.triggerConstantContract(contract, trxCap, trxExtBuilder, retBuilder);
Assert.assertEquals(Transaction.Result.code.SUCESS, tx.getRet(0).getRet());

VMConfig.initAllowTvmTransferTrc10(0);
VMConfig.initAllowTvmConstantinople(0);
VMConfig.initAllowTvmShangHai(0);
ConfigLoader.disable = false;
CommonParameter.getInstance().debug = preDebug;
}

@Test
@SneakyThrows
public void testEstimateEnergy() {
Expand Down

0 comments on commit eafada1

Please sign in to comment.