Skip to content

Commit d47b56a

Browse files
committed
change isStaticCall to isConstantCall in Runtime
add isRootConstantCall in Program
1 parent 3373eaa commit d47b56a

File tree

7 files changed

+63
-43
lines changed

7 files changed

+63
-43
lines changed

src/main/java/org/tron/common/runtime/RuntimeImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ private void call()
565565
tokenValue, tokenId, blockCap.getInstance(), deposit, vmStartInUs,
566566
vmShouldEndInUs, energyLimit);
567567
if (isConstantCall) {
568-
programInvoke.setConstantCall();
568+
programInvoke.setRootConstantCall();
569569
}
570570
this.vm = new VM(config);
571571
rootInternalTransaction = new InternalTransaction(trx, trxType);

src/main/java/org/tron/common/runtime/vm/VM.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ public void step(Program program) {
10741074
case LOG3:
10751075
case LOG4: {
10761076

1077-
if (program.isConstantCall()) {
1077+
if (program.isStaticCall()) {
10781078
throw new Program.StaticCallModificationException();
10791079
}
10801080
DataWord address = program.getContractAddress();
@@ -1152,7 +1152,7 @@ public void step(Program program) {
11521152
}
11531153
break;
11541154
case SSTORE: {
1155-
if (program.isConstantCall()) {
1155+
if (program.isStaticCall()) {
11561156
throw new Program.StaticCallModificationException();
11571157
}
11581158

@@ -1283,7 +1283,7 @@ public void step(Program program) {
12831283
}
12841284
break;
12851285
case CREATE: {
1286-
if (program.isConstantCall()) {
1286+
if (program.isStaticCall()) {
12871287
throw new Program.StaticCallModificationException();
12881288
}
12891289
DataWord value = program.stackPop();
@@ -1295,7 +1295,7 @@ public void step(Program program) {
12951295
}
12961296
break;
12971297
case CREATE2: {
1298-
if (program.isConstantCall()) {
1298+
if (program.isStaticCall()) {
12991299
throw new Program.StaticCallModificationException();
13001300
}
13011301
DataWord value = program.stackPop();
@@ -1330,7 +1330,7 @@ public void step(Program program) {
13301330
value = DataWord.ZERO;
13311331
}
13321332

1333-
if (program.isConstantCall() && (op == CALL || op == CALLTOKEN) && !value.isZero()) {
1333+
if (program.isStaticCall() && (op == CALL || op == CALLTOKEN) && !value.isZero()) {
13341334
throw new Program.StaticCallModificationException();
13351335
}
13361336

@@ -1409,7 +1409,7 @@ public void step(Program program) {
14091409
break;
14101410
}
14111411
case SUICIDE: {
1412-
if (program.isConstantCall()) {
1412+
if (program.isStaticCall()) {
14131413
throw new Program.StaticCallModificationException();
14141414
}
14151415

@@ -1478,7 +1478,7 @@ private boolean isDeadAccount(Program program, DataWord address) {
14781478
* + size, unless size is 0, in which case the result is also 0.
14791479
*
14801480
* @param offset starting position of the memory
1481-
* @param size number of bytes needed
1481+
* @param size number of bytes needed
14821482
* @return offset + size, unless size is 0. In that case memNeeded is also 0.
14831483
*/
14841484
private static BigInteger memNeeded(DataWord offset, DataWord size) {

src/main/java/org/tron/common/runtime/vm/program/Program.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public int getCallDeep() {
182182

183183
/**
184184
* @param transferAddress the address send trx to.
185-
* @param value the trx value transferred in the internaltransaction
185+
* @param value the trx value transferred in the internaltransaction
186186
*/
187187
private InternalTransaction addInternalTx(DataWord energyLimit, byte[] senderAddress,
188188
byte[] transferAddress,
@@ -349,9 +349,9 @@ public void memorySave(int addr, byte[] value) {
349349
/**
350350
* . Allocates a piece of memory and stores value at given offset address
351351
*
352-
* @param addr is the offset address
352+
* @param addr is the offset address
353353
* @param allocSize size of memory needed to write
354-
* @param value the data to write to memory
354+
* @param value the data to write to memory
355355
*/
356356
public void memorySave(int addr, int allocSize, byte[] value) {
357357
memory.extendAndWrite(addr, allocSize, value);
@@ -383,7 +383,7 @@ public byte[] memoryChunk(int offset, int size) {
383383
* . Allocates extra memory in the program for a specified size, calculated from a given offset
384384
*
385385
* @param offset the memory address offset
386-
* @param size the number of bytes to allocate
386+
* @param size the number of bytes to allocate
387387
*/
388388
public void allocateMemory(int offset, int size) {
389389
memory.extend(offset, size);
@@ -537,7 +537,9 @@ this, new DataWord(newAddress), getContractAddress(), value, new DataWord(0),
537537
new DataWord(0),
538538
newBalance, null, deposit, false, byTestingSuite(), vmStartInUs,
539539
getVmShouldEndInUs(), energyLimit.longValueSafe());
540-
540+
if (isRootConstantCall()) {
541+
programInvoke.setRootConstantCall();
542+
}
541543
ProgramResult createResult = ProgramResult.createEmpty();
542544

543545
if (contractAlreadyExists) {
@@ -751,8 +753,11 @@ this, new DataWord(contextAddress),
751753
!isTokenTransfer ? callValue : new DataWord(0),
752754
!isTokenTransfer ? new DataWord(0) : callValue,
753755
!isTokenTransfer ? new DataWord(0) : msg.getTokenId(),
754-
contextBalance, data, deposit, msg.getType().callIsStatic() || isConstantCall(),
756+
contextBalance, data, deposit, msg.getType().callIsStatic() || isStaticCall(),
755757
byTestingSuite(), vmStartInUs, getVmShouldEndInUs(), msg.getEnergy().longValueSafe());
758+
if (isRootConstantCall()) {
759+
programInvoke.setRootConstantCall();
760+
}
756761
VM vm = new VM(config);
757762
Program program = new Program(programCode, programInvoke, internalTx, config,
758763
this.blockCap);
@@ -1046,8 +1051,12 @@ public DataWord getDifficulty() {
10461051
return invoke.getDifficulty().clone();
10471052
}
10481053

1049-
public boolean isConstantCall() {
1050-
return invoke.isConstantCall();
1054+
public boolean isStaticCall() {
1055+
return invoke.isStaticCall();
1056+
}
1057+
1058+
public boolean isRootConstantCall() {
1059+
return invoke.isRootConstantCall();
10511060
}
10521061

10531062
public ProgramResult getResult() {
@@ -1452,7 +1461,7 @@ public void callToPrecompiledAddress(MessageCall msg,
14521461
// this is the depositImpl, not contractState as above
14531462
contract.setDeposit(deposit);
14541463
contract.setResult(this.result);
1455-
contract.setConstantCall(isConstantCall());
1464+
contract.setConstantCall(isRootConstantCall());
14561465
contract.setVmShouldEndInUs(getVmShouldEndInUs());
14571466
Pair<Boolean, byte[]> out = contract.execute(data);
14581467

src/main/java/org/tron/common/runtime/vm/program/invoke/ProgramInvoke.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,17 @@ public interface ProgramInvoke {
6363

6464
Deposit getDeposit();
6565

66-
boolean isConstantCall();
66+
boolean isStaticCall();
6767

6868
long getVmShouldEndInUs();
6969

7070
long getVmStartInUs();
7171

7272
long getEnergyLimit();
7373

74-
void setConstantCall();
75-
7674
BlockCapsule getBlockByNum(int index);
75+
76+
void setRootConstantCall();
77+
78+
boolean isRootConstantCall();
7779
}

src/main/java/org/tron/common/runtime/vm/program/invoke/ProgramInvokeImpl.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ public class ProgramInvokeImpl implements ProgramInvoke {
5555
private boolean byTransaction = true;
5656
private boolean byTestingSuite = false;
5757
private int callDeep = 0;
58-
private boolean isConstantCall = false;
58+
private boolean isStaticCall = false;
59+
private boolean isRootConstantCall = false;
5960

6061
public ProgramInvokeImpl(DataWord address, DataWord origin, DataWord caller, DataWord balance,
6162
DataWord callValue, DataWord tokenValue, DataWord tokenId, byte[] msgData,
6263
DataWord lastHash, DataWord coinbase, DataWord timestamp, DataWord number,
6364
DataWord difficulty,
64-
Deposit deposit, int callDeep, boolean isConstantCall, boolean byTestingSuite,
65+
Deposit deposit, int callDeep, boolean isStaticCall, boolean byTestingSuite,
6566
long vmStartInUs, long vmShouldEndInUs, long energyLimit) {
6667
this.address = address;
6768
this.origin = origin;
@@ -83,7 +84,7 @@ public ProgramInvokeImpl(DataWord address, DataWord origin, DataWord caller, Dat
8384

8485
this.deposit = deposit;
8586
this.byTransaction = false;
86-
this.isConstantCall = isConstantCall;
87+
this.isStaticCall = isStaticCall;
8788
this.byTestingSuite = byTestingSuite;
8889
this.vmStartInUs = vmStartInUs;
8990
this.vmShouldEndInUs = vmShouldEndInUs;
@@ -260,8 +261,8 @@ public Deposit getDeposit() {
260261
}
261262

262263
@Override
263-
public boolean isConstantCall() {
264-
return isConstantCall;
264+
public boolean isStaticCall() {
265+
return isStaticCall;
265266
}
266267

267268
@Override
@@ -371,11 +372,6 @@ public long getEnergyLimit() {
371372
return energyLimit;
372373
}
373374

374-
@Override
375-
public void setConstantCall() {
376-
isConstantCall = true;
377-
}
378-
379375
@Override
380376
public BlockCapsule getBlockByNum(int index) {
381377
try {
@@ -385,4 +381,14 @@ public BlockCapsule getBlockByNum(int index) {
385381
}
386382
}
387383

384+
@Override
385+
public void setRootConstantCall() {
386+
isRootConstantCall = true;
387+
}
388+
389+
@Override
390+
public boolean isRootConstantCall() {
391+
return isRootConstantCall;
392+
}
393+
388394
}

src/main/java/org/tron/common/runtime/vm/program/invoke/ProgramInvokeMockImpl.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void setOwnerAddress(byte[] ownerAddress) {
224224
}
225225

226226
@Override
227-
public boolean isConstantCall() {
227+
public boolean isStaticCall() {
228228
return isConstantCall;
229229
}
230230

@@ -242,11 +242,6 @@ public void setEnergyLimit(long customizedEnergyLimit) {
242242
energyLimit = customizedEnergyLimit;
243243
}
244244

245-
@Override
246-
public void setConstantCall() {
247-
isConstantCall = true;
248-
}
249-
250245
@Override
251246
public BlockCapsule getBlockByNum(int index) {
252247
try {
@@ -256,6 +251,15 @@ public BlockCapsule getBlockByNum(int index) {
256251
}
257252
}
258253

254+
@Override
255+
public void setRootConstantCall() {
256+
}
257+
258+
@Override
259+
public boolean isRootConstantCall() {
260+
return false;
261+
}
262+
259263
@Override
260264
public boolean byTestingSuite() {
261265
return true;

src/test/java/org/tron/common/runtime/vm/TransferToAccountTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ private long createAsset(String tokenName) {
112112

113113
/**
114114
* pragma solidity ^0.5.4;
115-
*
115+
* <p>
116116
* contract TestTransferTo { constructor() public payable{}
117-
*
117+
* <p>
118118
* function depositIn() public payable{}
119-
*
119+
* <p>
120120
* function transferTokenTo(address payable toAddress, trcToken id,uint256 amount) public payable
121121
* { toAddress.transferToken(amount,id); }
122-
*
122+
* <p>
123123
* function transferTo(address payable toAddress ,uint256 amount) public payable {
124124
* toAddress.transfer(amount); }
125-
*
125+
* <p>
126126
* }
127127
*/
128128
@Test
@@ -264,8 +264,7 @@ public void TransferTokenTest()
264264
runtimeImpl.execute();
265265
runtimeImpl.go();
266266

267-
Assert.assertEquals("Attempt to call a state modifying opcode inside STATICCALL",
268-
runtimeImpl.getRuntimeError());
267+
Assert.assertNull(runtimeImpl.getRuntimeError());
269268

270269

271270
}

0 commit comments

Comments
 (0)