Skip to content

Commit

Permalink
Merge pull request #1452 from tronprotocol/develop
Browse files Browse the repository at this point in the history
update master
  • Loading branch information
huzhenyuan authored Sep 5, 2018
2 parents 4234850 + da9a8ac commit 0a6b89b
Show file tree
Hide file tree
Showing 23 changed files with 209 additions and 186 deletions.
70 changes: 45 additions & 25 deletions src/main/java/org/tron/common/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.spongycastle.util.encoders.Hex;
import org.tron.common.runtime.config.SystemProperties;
import org.tron.common.runtime.config.VMConfig;
import org.tron.common.runtime.vm.DataWord;
import org.tron.common.runtime.vm.EnergyCost;
import org.tron.common.runtime.vm.PrecompiledContracts;
Expand All @@ -34,6 +34,7 @@
import org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType;
import org.tron.common.runtime.vm.program.Program;
import org.tron.common.runtime.vm.program.Program.JVMStackOverFlowException;
import org.tron.common.runtime.vm.program.Program.OutOfResourceException;
import org.tron.common.runtime.vm.program.ProgramPrecompile;
import org.tron.common.runtime.vm.program.ProgramResult;
import org.tron.common.runtime.vm.program.invoke.ProgramInvoke;
Expand Down Expand Up @@ -70,7 +71,7 @@
public class Runtime {


private SystemProperties config = SystemProperties.getInstance();
private VMConfig config = VMConfig.getInstance();

private Transaction trx;
private BlockCapsule blockCap = null;
Expand Down Expand Up @@ -184,18 +185,24 @@ public BigInteger getBlockCPULeftInUs() {
}

public void execute() throws ContractValidateException, ContractExeException {
switch (trxType) {
case TRX_PRECOMPILED_TYPE:
precompiled();
break;
case TRX_CONTRACT_CREATION_TYPE:
create();
break;
case TRX_CONTRACT_CALL_TYPE:
call();
break;
default:
throw new ContractValidateException("Unknown contract type");
try {
switch (trxType) {
case TRX_PRECOMPILED_TYPE:
precompiled();
break;
case TRX_CONTRACT_CREATION_TYPE:
create();
break;
case TRX_CONTRACT_CALL_TYPE:
call();
break;
default:
throw new ContractValidateException("Unknown contract type");
}
} catch (ContractExeException | ContractValidateException e) {
throw e;
} catch (Exception e) {
throw new ContractValidateException("Unknown contract error");
}
}

Expand Down Expand Up @@ -303,7 +310,10 @@ private void create()

CreateSmartContract contract = ContractCapsule.getSmartContractFromTransaction(trx);
SmartContract newSmartContract = contract.getNewContract();

if (!contract.getOwnerAddress().equals(newSmartContract.getOriginAddress())) {
logger.error("OwnerAddress not equals OriginAddress");
throw new ContractValidateException("OwnerAddress not equals OriginAddress");
}
byte[] code = newSmartContract.getBytecode().toByteArray();
byte[] contractAddress = Wallet.generateContractAddress(trx);
byte[] ownerAddress = contract.getOwnerAddress().toByteArray();
Expand Down Expand Up @@ -348,9 +358,10 @@ private void create()
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;

long feeLimit = trx.getRawData().getFeeLimit();
if (feeLimit < 0) {
logger.info("feeLimit < 0");
throw new ContractValidateException("feeLimit must be >= 0");
if (feeLimit < 0 || feeLimit > VMConfig.MAX_FEE_LIMIT) {
logger.warn("invalid feeLimit {}", feeLimit);
throw new ContractValidateException(
"feeLimit must be >= 0 and <= " + VMConfig.MAX_FEE_LIMIT);
}

long energyLimit = getEnergyLimit(creator, feeLimit, callValue);
Expand Down Expand Up @@ -425,9 +436,10 @@ private void call()
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;

long feeLimit = trx.getRawData().getFeeLimit();
if (feeLimit < 0) {
logger.info("feeLimit < 0");
throw new ContractValidateException("feeLimit must be >= 0");
if (feeLimit < 0 || feeLimit > VMConfig.MAX_FEE_LIMIT) {
logger.warn("invalid feeLimit {}", feeLimit);
throw new ContractValidateException(
"feeLimit must be >= 0 and <= " + VMConfig.MAX_FEE_LIMIT);
}
long energyLimit;
if (isCallConstant(contractAddress)) {
Expand Down Expand Up @@ -489,10 +501,12 @@ public void go() {
long saveCodeEnergy = getLength(code) * EnergyCost.getInstance().getCREATE_DATA();
long afterSpend = program.getEnergyLimitLeft().longValue() - saveCodeEnergy;
if (afterSpend < 0) {
result.setException(
Program.Exception
.notEnoughSpendEnergy("No energy to save just created contract code",
saveCodeEnergy, program.getEnergyLimitLeft().longValue()));
if (null == result.getException()) {
result.setException(
Program.Exception
.notEnoughSpendEnergy("save just created contract code",
saveCodeEnergy, program.getEnergyLimitLeft().longValue()));
}
} else {
result.spendEnergy(saveCodeEnergy);
// have saveCode in create()
Expand All @@ -518,6 +532,12 @@ public void go() {
deposit.commit();
}
} catch (JVMStackOverFlowException e) {
program.spendAllEnergy();
result.setException(e);
runtimeError = result.getException().getMessage();
logger.error("runtime error is :{}", result.getException().getMessage());
} catch (OutOfResourceException e) {
program.spendAllEnergy();
result.setException(e);
runtimeError = result.getException().getMessage();
logger.error("runtime error is :{}", result.getException().getMessage());
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/org/tron/common/runtime/config/DefaultConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,31 @@
*/
package org.tron.common.runtime.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* For developer only
* For developer only
*/
public class SystemProperties {
public class VMConfig {

public static final int MAX_CODE_LENGTH = 1024 * 1024;

private static Logger logger = LoggerFactory.getLogger("general");
public static final int MAX_FEE_LIMIT = 1_000_000_000; //1000 trx

private boolean vmTraceCompressed = false;
private boolean vmOn = true;
private boolean vmTrace = false;

private SystemProperties() {

private VMConfig() {
}

private static class SystemPropertiesInstance {

private static final SystemProperties INSTANCE = new SystemProperties();
private static final VMConfig INSTANCE = new VMConfig();
}

public static SystemProperties getInstance() {
public static VMConfig getInstance() {
return SystemPropertiesInstance.INSTANCE;
}

public boolean vmOn() {
return vmOn;
}

public boolean vmTrace() {
return vmTrace;
}
Expand Down
102 changes: 49 additions & 53 deletions src/main/java/org/tron/common/runtime/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ public class PrecompiledContracts {
private static final BN128Addition altBN128Add = new BN128Addition();
private static final BN128Multiplication altBN128Mul = new BN128Multiplication();
private static final BN128Pairing altBN128Pairing = new BN128Pairing();
private static final VoteWitnessNative voteContract = new VoteWitnessNative();
// private static final FreezeBalanceNative freezeBalance = new FreezeBalanceNative();
// private static final VoteWitnessNative voteContract = new VoteWitnessNative();
// private static final FreezeBalanceNative freezeBalance = new FreezeBalanceNative();
// private static final UnfreezeBalanceNative unFreezeBalance = new UnfreezeBalanceNative();
private static final WithdrawBalanceNative withdrawBalance = new WithdrawBalanceNative();
private static final ProposalApproveNative proposalApprove = new ProposalApproveNative();
private static final ProposalCreateNative proposalCreate = new ProposalCreateNative();
private static final ProposalDeleteNative proposalDelete = new ProposalDeleteNative();
private static final ConvertFromTronBytesAddressNative convertFromTronBytesAddress = new ConvertFromTronBytesAddressNative();
private static final ConvertFromTronBase58AddressNative convertFromTronBase58Address = new ConvertFromTronBase58AddressNative();
// private static final WithdrawBalanceNative withdrawBalance = new WithdrawBalanceNative();
// private static final ProposalApproveNative proposalApprove = new ProposalApproveNative();
// private static final ProposalCreateNative proposalCreate = new ProposalCreateNative();
// private static final ProposalDeleteNative proposalDelete = new ProposalDeleteNative();
// private static final ConvertFromTronBytesAddressNative convertFromTronBytesAddress = new ConvertFromTronBytesAddressNative();
// private static final ConvertFromTronBase58AddressNative convertFromTronBase58Address = new ConvertFromTronBase58AddressNative();
// private static final TransferAssetNative transferAsset = new TransferAssetNative();
private static final GetTransferAssetNative getTransferAssetAmount = new GetTransferAssetNative();
// private static final GetTransferAssetNative getTransferAssetAmount = new GetTransferAssetNative();

private static final ECKey addressCheckECKey = new ECKey();
private static final String addressCheckECKeyAddress = Wallet
Expand All @@ -121,28 +121,28 @@ public class PrecompiledContracts {
"0000000000000000000000000000000000000000000000000000000000000007");
private static final DataWord altBN128PairingAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000000008");
private static final DataWord voteContractAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010001");
// private static final DataWord voteContractAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010001");
// private static final DataWord freezeBalanceAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010002");
// private static final DataWord unFreezeBalanceAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010003");
private static final DataWord withdrawBalanceAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010004");
private static final DataWord proposalApproveAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010005");
private static final DataWord proposalCreateAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010006");
private static final DataWord proposalDeleteAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010007");
private static final DataWord convertFromTronBytesAddressAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010008");
private static final DataWord convertFromTronBase58AddressAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000010009");
// private static final DataWord withdrawBalanceAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010004");
// private static final DataWord proposalApproveAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010005");
// private static final DataWord proposalCreateAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010006");
// private static final DataWord proposalDeleteAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010007");
// private static final DataWord convertFromTronBytesAddressAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010008");
// private static final DataWord convertFromTronBase58AddressAddr = new DataWord(
// "0000000000000000000000000000000000000000000000000000000000010009");
// private static final DataWord transferAssetAddr = new DataWord(
// "000000000000000000000000000000000000000000000000000000000001000a");
private static final DataWord getTransferAssetAmountAddr = new DataWord(
"000000000000000000000000000000000000000000000000000000000001000b");
// private static final DataWord getTransferAssetAmountAddr = new DataWord(
// "000000000000000000000000000000000000000000000000000000000001000b");

public static PrecompiledContract getContractForAddress(DataWord address) {

Expand All @@ -161,39 +161,39 @@ public static PrecompiledContract getContractForAddress(DataWord address) {
if (address.equals(identityAddr)) {
return identity;
}
if (address.equals(voteContractAddr)) {
return voteContract;
}
// if (address.equals(voteContractAddr)) {
// return voteContract;
// }
// if (address.equals(freezeBalanceAddr)) {
// return freezeBalance;
// }
// if (address.equals(unFreezeBalanceAddr)) {
// return unFreezeBalance;
// }
if (address.equals(withdrawBalanceAddr)) {
return withdrawBalance;
}
if (address.equals(proposalApproveAddr)) {
return proposalApprove;
}
if (address.equals(proposalCreateAddr)) {
return proposalCreate;
}
if (address.equals(proposalDeleteAddr)) {
return proposalDelete;
}
if (address.equals(convertFromTronBytesAddressAddr)) {
return convertFromTronBytesAddress;
}
if (address.equals(convertFromTronBase58AddressAddr)) {
return convertFromTronBase58Address;
}
// if (address.equals(withdrawBalanceAddr)) {
// return withdrawBalance;
// }
// if (address.equals(proposalApproveAddr)) {
// return proposalApprove;
// }
// if (address.equals(proposalCreateAddr)) {
// return proposalCreate;
// }
// if (address.equals(proposalDeleteAddr)) {
// return proposalDelete;
// }
// if (address.equals(convertFromTronBytesAddressAddr)) {
// return convertFromTronBytesAddress;
// }
// if (address.equals(convertFromTronBase58AddressAddr)) {
// return convertFromTronBase58Address;
// }
// if (address.equals(transferAssetAddr)) {
// return transferAsset;
// }
if (address.equals(getTransferAssetAmountAddr)) {
return getTransferAssetAmount;
}
// if (address.equals(getTransferAssetAmountAddr)) {
// return getTransferAssetAmount;
// }

// Byzantium precompiles
if (address.equals(modExpAddr)) {
Expand Down Expand Up @@ -912,10 +912,6 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
return Pair.of(true, new DataWord(0).getData());
}

if (data == null) {
data = EMPTY_BYTE_ARRAY;
}

Contract.WithdrawBalanceContract.Builder builder = Contract.WithdrawBalanceContract
.newBuilder();
ByteString byteAddress = ByteString.copyFrom(getCallerAddress());
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/tron/common/runtime/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.tron.common.runtime.config.SystemProperties;
import org.tron.common.runtime.config.VMConfig;
import org.tron.common.runtime.vm.program.Program;
import org.tron.common.runtime.vm.program.Program.JVMStackOverFlowException;
import org.tron.common.runtime.vm.program.Program.OutOfEnergyException;
import org.tron.common.runtime.vm.program.Program.OutOfResourceException;
import org.tron.common.runtime.vm.program.Stack;

@Slf4j(topic = "VM")
Expand All @@ -41,14 +42,14 @@ public class VM {
private boolean vmTrace;
// private long dumpBlock;

private final SystemProperties config;
private final VMConfig config;

public VM() {
config = SystemProperties.getInstance();
config = VMConfig.getInstance();
}

@Autowired
public VM(SystemProperties config) {
public VM(VMConfig config) {
this.config = config;
// vmTrace = config.vmTrace();
// dumpBlock = config.dumpBlock();
Expand Down Expand Up @@ -1332,7 +1333,9 @@ public void play(Program program) {
}

} catch (JVMStackOverFlowException e) {
throw new JVMStackOverFlowException();
throw e;
} catch (OutOfResourceException e) {
throw e;
} catch (RuntimeException e) {
if (StringUtils.isEmpty(e.getMessage())) {
program.setRuntimeFailure(new RuntimeException("Unknown Exception"));
Expand Down
Loading

0 comments on commit 0a6b89b

Please sign in to comment.