Skip to content

Commit cf8c5e5

Browse files
committed
review(vm): change some details
1 parent 68755ab commit cf8c5e5

8 files changed

Lines changed: 70 additions & 11 deletions

File tree

actuator/src/main/java/org/tron/core/actuator/VMActuator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class VMActuator implements Actuator2 {
8080

8181
@Getter
8282
@Setter
83-
private boolean isConstantCall;
83+
private boolean isConstantCall = false;
8484

8585
@Setter
8686
private boolean enableEventListener;

actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ public long getEnergyForData(byte[] data) {
11271127
}
11281128

11291129
@Override
1130-
public synchronized Pair<Boolean, byte[]> execute(byte[] data) {
1130+
public Pair<Boolean, byte[]> execute(byte[] data) {
11311131
if (data == null) {
11321132
return Pair.of(true, DataWord.ZERO().getData());
11331133
}
@@ -1574,7 +1574,9 @@ public long getEnergyForData(byte[] data) {
15741574

15751575
@Override
15761576
public Pair<Boolean, byte[]> execute(byte[] data) {
1577-
long rewardBalance = VoteRewardUtil.queryReward(getCallerAddress(), getDeposit());
1577+
1578+
long rewardBalance = VoteRewardUtil.queryReward(
1579+
TransactionTrace.convertToTronAddress(getCallerAddress()), getDeposit());
15781580
return Pair.of(true, longTo32Bytes(rewardBalance));
15791581
}
15801582
}
@@ -1620,7 +1622,7 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
16201622
AccountCapsule accountCapsule = this.getDeposit().getAccount(address);
16211623

16221624
long voteCount = 0;
1623-
if (accountCapsule != null) {
1625+
if (accountCapsule != null && !accountCapsule.getVotesList().isEmpty()) {
16241626
ByteString witness = ByteString.copyFrom(words[1].toTronAddress());
16251627
for (Protocol.Vote vote : accountCapsule.getVotesList()) {
16261628
if (witness.equals(vote.getVoteAddress())) {
@@ -1650,7 +1652,7 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
16501652
AccountCapsule accountCapsule = this.getDeposit().getAccount(address);
16511653

16521654
long usedVoteCount = 0;
1653-
if (accountCapsule != null) {
1655+
if (accountCapsule != null && !accountCapsule.getVotesList().isEmpty()) {
16541656
for (Protocol.Vote vote : accountCapsule.getVotesList()) {
16551657
usedVoteCount += vote.getVoteCount();
16561658
}

actuator/src/main/java/org/tron/core/vm/VM.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static void play(Program program) {
1818
if (VMConfig.vmTrace()) {
1919
program.saveOpTrace();
2020
}
21+
2122
try {
2223
Operation op = OperationRegistry.get(program.getCurrentOpIntValue());
2324
if (op == null) {
@@ -44,6 +45,7 @@ public static void play(Program program) {
4445
if (!(e instanceof TransferException)) {
4546
program.spendAllEnergy();
4647
}
48+
//program.resetFutureRefund();
4749
program.stop();
4850
throw e;
4951
} finally {

actuator/src/main/java/org/tron/core/vm/program/Program.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import org.tron.core.exception.ContractValidateException;
4141
import org.tron.core.exception.TronException;
4242
import org.tron.core.utils.TransactionUtil;
43-
import org.tron.core.vm.MessageCall;
4443
import org.tron.core.vm.EnergyCost;
44+
import org.tron.core.vm.MessageCall;
4545
import org.tron.core.vm.Op;
4646
import org.tron.core.vm.PrecompiledContracts;
4747
import org.tron.core.vm.VM;
@@ -753,6 +753,14 @@ public void callToAddress(MessageCall msg) {
753753
contextAddress = codeAddress;
754754
}
755755

756+
if (logger.isDebugEnabled()) {
757+
logger.debug(Op.getNameOf(msg.getOpCode())
758+
+ " for existing contract: address: [{}], outDataOffs: [{}], outDataSize: [{}] ",
759+
Hex.toHexString(contextAddress), msg.getOutDataOffs().longValue(),
760+
msg.getOutDataSize().longValue());
761+
}
762+
763+
756764
Repository deposit = getContractState().newRepositoryChild();
757765

758766
// 2.1 PERFORM THE VALUE (endowment) PART
@@ -849,9 +857,11 @@ public void callToAddress(MessageCall msg) {
849857
ProgramResult callResult = null;
850858
if (isNotEmpty(programCode)) {
851859
long vmStartInUs = System.nanoTime() / 1000;
852-
DataWord callValue = msg.getEndowment();
860+
DataWord callValue;
853861
if (msg.getOpCode() == Op.DELEGATECALL) {
854862
callValue = getCallValue();
863+
} else {
864+
callValue = msg.getEndowment();
855865
}
856866
ProgramInvoke programInvoke = ProgramInvokeFactory.createProgramInvoke(
857867
this, new DataWord(contextAddress),
@@ -984,6 +994,15 @@ public void refundEnergy(long energyValue, String cause) {
984994
getResult().refundEnergy(energyValue);
985995
}
986996

997+
// public void futureRefundEnergy(long energyValue) {
998+
// logger.debug("Future refund added: [{}]", energyValue);
999+
// getResult().addFutureRefund(energyValue);
1000+
// }
1001+
//
1002+
// public void resetFutureRefund() {
1003+
// getResult().resetFutureRefund();
1004+
// }
1005+
9871006
public void storageSave(DataWord word1, DataWord word2) {
9881007
DataWord keyWord = word1.clone();
9891008
DataWord valWord = word2.clone();
@@ -1303,12 +1322,11 @@ public ProgramTrace getTrace() {
13031322
}
13041323

13051324
public void createContract2(DataWord value, DataWord memStart, DataWord memSize, DataWord salt) {
1325+
byte[] senderAddress;
13061326
if (VMConfig.allowTvmCompatibleEvm() && getCallDeep() == MAX_DEPTH) {
13071327
stackPushZero();
13081328
return;
13091329
}
1310-
1311-
byte[] senderAddress;
13121330
if (VMConfig.allowTvmIstanbul()) {
13131331
senderAddress = getContextAddress();
13141332
} else {
@@ -1346,12 +1364,15 @@ public void callToPrecompiledAddress(MessageCall msg,
13461364
return;
13471365
}
13481366

1367+
13491368
Repository deposit = getContractState().newRepositoryChild();
13501369

13511370
byte[] senderAddress = getContextAddress();
1352-
byte[] contextAddress = msg.getCodeAddress().toTronAddress();
1371+
byte[] contextAddress;
13531372
if (msg.getOpCode() == Op.CALLCODE || msg.getOpCode() == Op.DELEGATECALL) {
13541373
contextAddress = senderAddress;
1374+
} else {
1375+
contextAddress = msg.getCodeAddress().toTronAddress();
13551376
}
13561377

13571378
long endowment = msg.getEndowment().value().longValueExact();

actuator/src/main/java/org/tron/core/vm/repository/RepositoryImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public byte[] getCode(byte[] address) {
473473

474474
@Override
475475
public void putStorageValue(byte[] address, DataWord key, DataWord value) {
476+
address = TransactionTrace.convertToTronAddress(address);
476477
if (getAccount(address) == null) {
477478
return;
478479
}
@@ -489,6 +490,7 @@ public void putStorageValue(byte[] address, DataWord key, DataWord value) {
489490

490491
@Override
491492
public DataWord getStorageValue(byte[] address, DataWord key) {
493+
address = TransactionTrace.convertToTronAddress(address);
492494
if (getAccount(address) == null) {
493495
return null;
494496
}

chainbase/src/main/java/org/tron/common/runtime/ProgramResult.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ public void addDeleteAccounts(Set<DataWord> accounts) {
134134
}
135135
}
136136

137+
// public void addTouchAccount(byte[] addr) {
138+
// touchedAccounts.add(addr);
139+
// }
140+
141+
// public Set<byte[]> getTouchedAccounts() {
142+
// return touchedAccounts;
143+
// }
144+
145+
// public void addTouchAccounts(Set<byte[]> accounts) {
146+
// if (!isEmpty(accounts)) {
147+
// getTouchedAccounts().addAll(accounts);
148+
// }
149+
// }
150+
137151
public List<LogInfo> getLogInfoList() {
138152
if (logInfoList == null) {
139153
logInfoList = new ArrayList<>();
@@ -193,16 +207,31 @@ public void rejectInternalTransactions() {
193207
}
194208
}
195209

210+
// public void addFutureRefund(long energyValue) {
211+
// futureRefund += energyValue;
212+
// }
213+
214+
// public long getFutureRefund() {
215+
// return futureRefund;
216+
// }
217+
218+
// public void resetFutureRefund() {
219+
// futureRefund = 0;
220+
// }
221+
196222
public void reset() {
197223
getDeleteAccounts().clear();
198224
getLogInfoList().clear();
225+
//resetFutureRefund();
199226
}
200227

201228
public void merge(ProgramResult another) {
202229
addInternalTransactions(another.getInternalTransactions());
203230
if (another.getException() == null && !another.isRevert()) {
204231
addDeleteAccounts(another.getDeleteAccounts());
205232
addLogInfos(another.getLogInfoList());
233+
//addFutureRefund(another.getFutureRefund());
234+
//addTouchAccounts(another.getTouchedAccounts());
206235
}
207236
}
208237

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public byte[] getLast20Bytes() {
186186
public byte[] toTronAddress() {
187187
byte[] ret = new byte[21];
188188
ret[0] = DecodeUtil.addressPreFixByte;
189-
System.arraycopy(this.data, 12, ret, 1, 20);
189+
System.arraycopy(data, 12, ret, 1, 20);
190190
return ret;
191191
}
192192

framework/src/main/java/org/tron/common/storage/DepositImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.tron.core.db.BlockStore;
2626
import org.tron.core.db.Manager;
2727
import org.tron.core.db.TransactionStore;
28+
import org.tron.core.db.TransactionTrace;
2829
import org.tron.core.exception.BadItemException;
2930
import org.tron.core.exception.ItemNotFoundException;
3031
import org.tron.core.store.AccountStore;
@@ -377,6 +378,7 @@ public synchronized AssetIssueCapsule getAssetIssue(byte[] tokenId) {
377378

378379
@Override
379380
public synchronized void putStorageValue(byte[] address, DataWord key, DataWord value) {
381+
address = TransactionTrace.convertToTronAddress(address);
380382
if (getAccount(address) == null) {
381383
return;
382384
}
@@ -393,6 +395,7 @@ public synchronized void putStorageValue(byte[] address, DataWord key, DataWord
393395

394396
@Override
395397
public synchronized DataWord getStorageValue(byte[] address, DataWord key) {
398+
address = TransactionTrace.convertToTronAddress(address);
396399
if (getAccount(address) == null) {
397400
return null;
398401
}

0 commit comments

Comments
 (0)