Skip to content
Merged
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
20 changes: 12 additions & 8 deletions src/main/java/org/tron/common/runtime/RuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class RuntimeImpl implements Runtime {

@Getter
@Setter
private boolean isStaticCall = false;
private boolean isConstantCall = false;

@Setter
private boolean enableEventLinstener;
Expand Down Expand Up @@ -136,9 +136,9 @@ public RuntimeImpl(TransactionTrace trace, BlockCapsule block, Deposit deposit,
* For constant trx with latest blockCap.
*/
public RuntimeImpl(Transaction tx, BlockCapsule block, DepositImpl deposit,
ProgramInvokeFactory programInvokeFactory, boolean isStaticCall) {
ProgramInvokeFactory programInvokeFactory, boolean isConstantCall) {
this(tx, block, deposit, programInvokeFactory);
this.isStaticCall = isStaticCall;
this.isConstantCall = isConstantCall;
}

private RuntimeImpl(Transaction tx, BlockCapsule block, DepositImpl deposit,
Expand Down Expand Up @@ -546,7 +546,7 @@ private void call()
}
AccountCapsule caller = this.deposit.getAccount(callerAddress);
long energyLimit;
if (isStaticCall) {
if (isConstantCall) {
energyLimit = Constant.ENERGY_LIMIT_IN_CONSTANT_TX;
} else {
AccountCapsule creator = this.deposit
Expand All @@ -564,8 +564,8 @@ private void call()
.createProgramInvoke(TrxType.TRX_CONTRACT_CALL_TYPE, executorType, trx,
tokenValue, tokenId, blockCap.getInstance(), deposit, vmStartInUs,
vmShouldEndInUs, energyLimit);
if (isStaticCall) {
programInvoke.setStaticCall();
if (isConstantCall) {
programInvoke.setConstantCall();
}
this.vm = new VM(config);
rootInternalTransaction = new InternalTransaction(trx, trxType);
Expand Down Expand Up @@ -616,14 +616,18 @@ public void go() {
vm.play(program);
result = program.getResult();

if (isStaticCall) {
if (isConstantCall) {
long callValue = TransactionCapsule.getCallValue(trx.getRawData().getContract(0));
long callTokenValue = TransactionCapsule
.getCallTokenValue(trx.getRawData().getContract(0));
if (callValue > 0 || callTokenValue > 0) {
runtimeError = "constant cannot set call value or call token value.";
result.rejectInternalTransactions();
}
if (result.getException() != null) {
runtimeError = result.getException().getMessage();
result.rejectInternalTransactions();
}
return;
}

Expand Down Expand Up @@ -702,7 +706,7 @@ public void go() {
}
logger.info("runtime result is :{}", result.getException().getMessage());
}
if (!isStaticCall) {
if (!isConstantCall) {
trace.setBill(result.getEnergyUsed());
}
}
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/org/tron/common/runtime/utils/MUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,4 @@ public static byte[] convertToTronAddress(byte[] address) {
}
return address;
}

public static byte[] allZero32TronAddress() {
byte[] newAddress = new byte[32];
byte[] temp = new byte[]{Wallet.getAddressPreFixByte()};
System.arraycopy(temp, 0, newAddress, 11, temp.length);

return newAddress;
}

}
15 changes: 15 additions & 0 deletions src/main/java/org/tron/common/runtime/vm/DataWord.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,19 @@ public static DataWord[] parseArray(byte[] data) {
}
return words;
}

public static boolean equalAddressByteArray(byte[] arr1, byte[] arr2){
if (arr1 == arr2)
return true;
if (arr1==null || arr2==null || arr1.length < 20 || arr2.length < 20)
return false;

int i = arr1.length - 20;
int j = arr2.length - 20;

for (; i < arr1.length && j < arr2.length; i++, j++)
if (arr1[i] != arr2[j])
return false;
return true;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/tron/common/runtime/vm/EnergyCost.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ public int getEXT_CODE_HASH() {
return EXT_CODE_HASH;
}


private static EnergyCost instance = null;

public static EnergyCost getInstance() {
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/org/tron/common/runtime/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.spongycastle.util.encoders.Hex;
import org.tron.common.crypto.ECKey;
Expand All @@ -60,7 +61,6 @@
import org.tron.common.crypto.zksnark.Fp;
import org.tron.common.crypto.zksnark.PairingCheck;
import org.tron.common.runtime.config.VMConfig;
import org.tron.common.runtime.utils.MUtil;
import org.tron.common.runtime.vm.program.Program;
import org.tron.common.runtime.vm.program.ProgramResult;
import org.tron.common.storage.Deposit;
Expand Down Expand Up @@ -227,7 +227,7 @@ public ProgramResult getResult() {

@Setter
@Getter
private boolean isStaticCall;
private boolean isConstantCall;

@Getter
@Setter
Expand Down Expand Up @@ -683,7 +683,7 @@ public long getEnergyForData(byte[] data) {
@Override
public Pair<Boolean, byte[]> execute(byte[] data) {

if (isStaticCall()) {
if (isConstantCall()) {
return Pair.of(true, new DataWord(0).getData());
}
if (data == null || data.length != 2 * DataWord.WORD_SIZE) {
Expand Down Expand Up @@ -880,7 +880,7 @@ public long getEnergyForData(byte[] data) {
@Override
public Pair<Boolean, byte[]> execute(byte[] data) {

if (isStaticCall()) {
if (isConstantCall()) {
return Pair.of(true, new DataWord(0).getData());
}

Expand Down Expand Up @@ -946,7 +946,7 @@ public long getEnergyForData(byte[] data) {
@Override
public Pair<Boolean, byte[]> execute(byte[] data) {

if (isStaticCall()) {
if (isConstantCall()) {
return Pair.of(true, new DataWord(0).getData());
}

Expand Down Expand Up @@ -1022,7 +1022,7 @@ public long getEnergyForData(byte[] data) {
@Override
public Pair<Boolean, byte[]> execute(byte[] data) {

if (isStaticCall()) {
if (isConstantCall()) {
return Pair.of(true, new DataWord(0).getData());
}

Expand Down Expand Up @@ -1107,7 +1107,7 @@ public long getEnergyForData(byte[] data) {
@Override
public Pair<Boolean, byte[]> execute(byte[] data) {

if (isStaticCall()) {
if (isConstantCall()) {
return Pair.of(true, new DataWord(0).getData());
}

Expand Down Expand Up @@ -1331,7 +1331,6 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
public static class MultiValidateSign extends PrecompiledContract {
private static final ExecutorService workers;
private static final int ENGERYPERSIGN = 1500;
private static final byte[] ZEROADDR = MUtil.allZero32TronAddress();
private static final byte[] EMPTYADDR = new byte[DataWord.WORD_SIZE];

static {
Expand Down Expand Up @@ -1361,6 +1360,7 @@ public ValidateSignResult call() {
@Data
@AllArgsConstructor
private static class ValidateSignResult {

private Boolean res;
private int nonce;
}
Expand Down Expand Up @@ -1395,7 +1395,7 @@ private Pair<Boolean, byte[]> doExecute(byte[] data)
}
int min = Math.min(cnt, DataWord.WORD_SIZE);
byte[] res = new byte[DataWord.WORD_SIZE];
if (isStaticCall()) {
if (isConstantCall()) {
//for static call not use thread pool to avoid potential effect
for (int i = 0; i < min; i++) {
if (validSign(signatures[i], hash, addresses[i])) {
Expand All @@ -1412,7 +1412,8 @@ private Pair<Boolean, byte[]> doExecute(byte[] data)
.submit(new ValidateSignTask(countDownLatch, hash, signatures[i], addresses[i], i));
futures.add(future);
}
boolean withNoTimeout = countDownLatch.await(getCPUTimeLeftInUs() * 1000, TimeUnit.NANOSECONDS);
boolean withNoTimeout = countDownLatch
.await(getCPUTimeLeftInUs() * 1000, TimeUnit.NANOSECONDS);

if (!withNoTimeout) {
logger.info("MultiValidateSign timeout");
Expand All @@ -1433,9 +1434,9 @@ private static boolean validSign(byte[] sign, byte[] hash, byte[] address) {
byte v;
byte[] r;
byte[] s;
DataWord out = null;
if (sign.length < 65 || Arrays.equals(ZEROADDR, address)
|| Arrays.equals(EMPTYADDR, address)) {
byte[] out = null;
if (ArrayUtils.isEmpty(sign) || sign.length < 65
|| DataWord.equalAddressByteArray(EMPTYADDR, address)) {
return false;
}
try {
Expand All @@ -1447,13 +1448,12 @@ private static boolean validSign(byte[] sign, byte[] hash, byte[] address) {
}
ECKey.ECDSASignature signature = ECKey.ECDSASignature.fromComponents(r, s, v);
if (signature.validateComponents()) {
out = new DataWord(ECKey.signatureToAddress(hash, signature));
out = ECKey.signatureToAddress(hash, signature);
}
} catch (Throwable any) {
logger.info("ECRecover error", any.getMessage());
}
return out != null && Arrays.equals(new DataWord(address).getLast20Bytes(),
out.getLast20Bytes());
return DataWord.equalAddressByteArray(address, out);
}

private static byte[][] extractBytes32Array(DataWord[] words, int offset) {
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/org/tron/common/runtime/vm/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ public void step(Program program) {
case LOG3:
case LOG4: {

if (program.isStaticCall()) {
if (program.isConstantCall()) {
throw new Program.StaticCallModificationException();
}
DataWord address = program.getContractAddress();
Expand Down Expand Up @@ -1152,7 +1152,7 @@ public void step(Program program) {
}
break;
case SSTORE: {
if (program.isStaticCall()) {
if (program.isConstantCall()) {
throw new Program.StaticCallModificationException();
}

Expand Down Expand Up @@ -1283,7 +1283,7 @@ public void step(Program program) {
}
break;
case CREATE: {
if (program.isStaticCall()) {
if (program.isConstantCall()) {
throw new Program.StaticCallModificationException();
}
DataWord value = program.stackPop();
Expand All @@ -1295,7 +1295,7 @@ public void step(Program program) {
}
break;
case CREATE2: {
if (program.isStaticCall()) {
if (program.isConstantCall()) {
throw new Program.StaticCallModificationException();
}
DataWord value = program.stackPop();
Expand Down Expand Up @@ -1330,7 +1330,7 @@ public void step(Program program) {
value = DataWord.ZERO;
}

if (program.isStaticCall() && (op == CALL || op == CALLTOKEN) && !value.isZero()) {
if (program.isConstantCall() && (op == CALL || op == CALLTOKEN) && !value.isZero()) {
throw new Program.StaticCallModificationException();
}

Expand Down Expand Up @@ -1409,7 +1409,7 @@ public void step(Program program) {
break;
}
case SUICIDE: {
if (program.isStaticCall()) {
if (program.isConstantCall()) {
throw new Program.StaticCallModificationException();
}

Expand All @@ -1427,7 +1427,6 @@ public void step(Program program) {
default:
break;
}

program.setPreviouslyExecutedOp(op.val());
} catch (RuntimeException e) {
logger.info("VM halted: [{}]", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public Manager getDbManager() {
return deposit.getDbManager();
}

@Override
public AccountCapsule createNormalAccount(byte[] address) {
return deposit.createNormalAccount(address);
}

@Override
public void setProgramListener(ProgramListener listener) {
this.programListener = listener;
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/org/tron/common/runtime/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ public void callToAddress(MessageCall msg) {
msg.getEndowment().getNoLeadZeroesData());
} else if (!ArrayUtils.isEmpty(senderAddress) && !ArrayUtils.isEmpty(contextAddress)
&& senderAddress != contextAddress && endowment > 0) {
createAccountIfNotExist(deposit, contextAddress);
if (!isTokenTransfer) {
try {
TransferActuator
Expand Down Expand Up @@ -749,7 +750,7 @@ this, new DataWord(contextAddress),
!isTokenTransfer ? callValue : new DataWord(0),
!isTokenTransfer ? new DataWord(0) : callValue,
!isTokenTransfer ? new DataWord(0) : msg.getTokenId(),
contextBalance, data, deposit, msg.getType().callIsStatic() || isStaticCall(),
contextBalance, data, deposit, msg.getType().callIsStatic() || isConstantCall(),
byTestingSuite(), vmStartInUs, getVmShouldEndInUs(), msg.getEnergy().longValueSafe());
VM vm = new VM(config);
Program program = new Program(programCode, programInvoke, internalTx, config,
Expand Down Expand Up @@ -1044,8 +1045,8 @@ public DataWord getDifficulty() {
return invoke.getDifficulty().clone();
}

public boolean isStaticCall() {
return invoke.isStaticCall();
public boolean isConstantCall() {
return invoke.isConstantCall();
}

public ProgramResult getResult() {
Expand Down Expand Up @@ -1450,7 +1451,7 @@ public void callToPrecompiledAddress(MessageCall msg,
// this is the depositImpl, not contractState as above
contract.setDeposit(deposit);
contract.setResult(this.result);
contract.setStaticCall(isStaticCall());
contract.setConstantCall(isConstantCall());
contract.setVmShouldEndInUs(getVmShouldEndInUs());
Pair<Boolean, byte[]> out = contract.execute(data);

Expand Down Expand Up @@ -1779,4 +1780,14 @@ public long getVmStartInUs() {
private boolean isContractExist(AccountCapsule existingAddr, Deposit deposit) {
return deposit.getContract(existingAddr.getAddress().toByteArray()) != null;
}

private void createAccountIfNotExist(Deposit deposit, byte[] contextAddress) {
if (VMConfig.allowTvmSolidity059()) {
//after solidity059 proposal , allow contract transfer trc10 or trx to non-exist address(would create one)
AccountCapsule sender = deposit.getAccount(contextAddress);
if (sender == null) {
deposit.createNormalAccount(contextAddress);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public interface ProgramInvoke {

Deposit getDeposit();

boolean isStaticCall();
boolean isConstantCall();

long getVmShouldEndInUs();

long getVmStartInUs();

long getEnergyLimit();

void setStaticCall();
void setConstantCall();

BlockCapsule getBlockByNum(int index);
}
Loading