Skip to content

Commit db38c88

Browse files
committed
don't allow constant call touch db, when constant call is from root
1 parent 62ecd42 commit db38c88

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎src/main/java/org/tron/common/runtime/Runtime.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ private void create()
408408
this.program = new Program(ops, programInvoke, internalTransaction, config);
409409
Program.setRootTransactionId(new TransactionCapsule(trx).getTransactionId().getBytes());
410410
Program.resetNonce();
411+
Program.setRootCallConstant(isCallConstant());
411412
} catch (Exception e) {
412413
logger.error(e.getMessage());
413414
throw new ContractExeException(e.getMessage());
@@ -489,6 +490,7 @@ private void call()
489490
this.program = new Program(null, code, programInvoke, internalTransaction, config);
490491
Program.setRootTransactionId(new TransactionCapsule(trx).getTransactionId().getBytes());
491492
Program.resetNonce();
493+
Program.setRootCallConstant(isCallConstant());
492494
}
493495

494496
program.getResult().setContractAddress(contractAddress);
@@ -525,7 +527,9 @@ public void go() throws OutOfSlotTimeException {
525527
runtimeError = "REVERT opcode executed";
526528
}
527529
} else {
528-
deposit.commit();
530+
if (isCallConstant()){
531+
deposit.commit();
532+
}
529533
}
530534
} else {
531535
deposit.commit();

‎src/main/java/org/tron/common/runtime/vm/PrecompiledContracts.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ public Deposit getDeposit() {
249249
public ProgramResult getResult() {
250250
return result;
251251
}
252+
253+
public boolean isRootCallConstant() {
254+
return isRootCallConstant;
255+
}
256+
257+
public void setRootCallConstant(boolean rootCallConstant) {
258+
isRootCallConstant = rootCallConstant;
259+
}
260+
261+
private boolean isRootCallConstant;
262+
263+
252264
}
253265

254266
public static class Identity extends PrecompiledContract {
@@ -689,6 +701,9 @@ public long getEnergyForData(byte[] data) {
689701
@Override
690702
public Pair<Boolean, byte[]> execute(byte[] data) {
691703

704+
if (isRootCallConstant()){
705+
return Pair.of(true, new DataWord(0).getData());
706+
}
692707
if (data == null || data.length != 2 * DataWord.DATAWORD_UNIT_SIZE) {
693708
return Pair.of(false, new DataWord(0).getData());
694709
}
@@ -872,6 +887,10 @@ public long getEnergyForData(byte[] data) {
872887
@Override
873888
public Pair<Boolean, byte[]> execute(byte[] data) {
874889

890+
if (isRootCallConstant()){
891+
return Pair.of(true, new DataWord(0).getData());
892+
}
893+
875894
if (data == null) {
876895
data = EMPTY_BYTE_ARRAY;
877896
}
@@ -925,6 +944,10 @@ public long getEnergyForData(byte[] data) {
925944
@Override
926945
public Pair<Boolean, byte[]> execute(byte[] data) {
927946

947+
if (isRootCallConstant()){
948+
return Pair.of(true, new DataWord(0).getData());
949+
}
950+
928951
if (data == null || data.length != 2 * DataWord.DATAWORD_UNIT_SIZE) {
929952
return Pair.of(false, new DataWord(0).getData());
930953
}
@@ -985,6 +1008,10 @@ public long getEnergyForData(byte[] data) {
9851008
@Override
9861009
public Pair<Boolean, byte[]> execute(byte[] data) {
9871010

1011+
if (isRootCallConstant()){
1012+
return Pair.of(true, new DataWord(0).getData());
1013+
}
1014+
9881015
if (data == null || data.length == 0 || (data.length % (2 * DataWord.DATAWORD_UNIT_SIZE) != 0 )) {
9891016
return Pair.of(false, new DataWord(0).getData());
9901017
}
@@ -1051,6 +1078,10 @@ public long getEnergyForData(byte[] data) {
10511078
@Override
10521079
public Pair<Boolean, byte[]> execute(byte[] data) {
10531080

1081+
if (isRootCallConstant()){
1082+
return Pair.of(true, new DataWord(0).getData());
1083+
}
1084+
10541085
if (data == null || data.length != DataWord.DATAWORD_UNIT_SIZE) {
10551086
return Pair.of(false, new DataWord(0).getData());
10561087
}
@@ -1161,6 +1192,10 @@ public long getEnergyForData(byte[] data) {
11611192
@Override
11621193
public Pair<Boolean, byte[]> execute(byte[] data) {
11631194

1195+
if (isRootCallConstant()){
1196+
return Pair.of(true, new DataWord(0).getData());
1197+
}
1198+
11641199
if (data == null || (data.length <= DataWord.DATAWORD_UNIT_SIZE * 2 || data.length > DataWord.DATAWORD_UNIT_SIZE * 3)) {
11651200
return Pair.of(false, new DataWord(0).getData());
11661201
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,17 @@ public static void setNonce(long nonceValue) {
105105
nonce = nonceValue;
106106
}
107107

108+
public static Boolean getRootCallConstant() {
109+
return isRootCallConstant;
110+
}
111+
112+
public static void setRootCallConstant(Boolean rootCallConstant) {
113+
isRootCallConstant = rootCallConstant;
114+
}
115+
108116
private static long nonce = 0;
109117
private static byte[] rootTransactionId = null;
118+
private static Boolean isRootCallConstant = null;
110119

111120
private InternalTransaction transaction;
112121

@@ -1295,6 +1304,7 @@ public void callToPrecompiledAddress(MessageCall msg,
12951304
// this is the depositImpl, not contractState as above
12961305
contract.setDeposit(this.invoke.getDeposit());
12971306
contract.setResult(this.result);
1307+
contract.setRootCallConstant(Program.getRootCallConstant().booleanValue());
12981308
Pair<Boolean, byte[]> out = contract.execute(data);
12991309

13001310
if (out.getLeft()) { // success

0 commit comments

Comments
 (0)