Skip to content

Commit c433410

Browse files
authored
Merge pull request #3753 from tronprotocol/release_v4.2
Release v4.2
2 parents af0c5b0 + daefe87 commit c433410

File tree

148 files changed

+7374
-8606
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+7374
-8606
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public boolean execute(Object result) throws ContractExeException {
5757
AccountCapsule accountCapsule = accountStore
5858
.get(freezeBalanceContract.getOwnerAddress().toByteArray());
5959

60+
if (dynamicStore.supportAllowNewResourceModel()
61+
&& accountCapsule.oldTronPowerIsNotInitialized()) {
62+
accountCapsule.initializeOldTronPower();
63+
}
64+
6065
long now = dynamicStore.getLatestBlockHeaderTimestamp();
6166
long duration = freezeBalanceContract.getFrozenDuration() * FROZEN_PERIOD;
6267

@@ -98,6 +103,14 @@ public boolean execute(Object result) throws ContractExeException {
98103
dynamicStore
99104
.addTotalEnergyWeight(frozenBalance / TRX_PRECISION);
100105
break;
106+
case TRON_POWER:
107+
long newFrozenBalanceForTronPower =
108+
frozenBalance + accountCapsule.getTronPowerFrozenBalance();
109+
accountCapsule.setFrozenForTronPower(newFrozenBalanceForTronPower, expireTime);
110+
111+
dynamicStore
112+
.addTotalTronPowerWeight(frozenBalance / TRX_PRECISION);
113+
break;
101114
default:
102115
logger.debug("Resource Code Error.");
103116
}
@@ -182,12 +195,28 @@ public boolean validate() throws ContractValidateException {
182195

183196
switch (freezeBalanceContract.getResource()) {
184197
case BANDWIDTH:
185-
break;
186198
case ENERGY:
187199
break;
200+
case TRON_POWER:
201+
if (dynamicStore.supportAllowNewResourceModel()) {
202+
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();
203+
if (!ArrayUtils.isEmpty(receiverAddress)) {
204+
throw new ContractValidateException(
205+
"TRON_POWER is not allowed to delegate to other accounts.");
206+
}
207+
} else {
208+
throw new ContractValidateException(
209+
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
210+
}
211+
break;
188212
default:
189-
throw new ContractValidateException(
190-
"ResourceCode error,valid ResourceCode[BANDWIDTH、ENERGY]");
213+
if (dynamicStore.supportAllowNewResourceModel()) {
214+
throw new ContractValidateException(
215+
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY、TRON_POWER]");
216+
} else {
217+
throw new ContractValidateException(
218+
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
219+
}
191220
}
192221

193222
//todo:need version control and config for delegating resource

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

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public boolean execute(Object result) throws ContractExeException {
7575

7676
long unfreezeBalance = 0L;
7777

78+
if (dynamicStore.supportAllowNewResourceModel()
79+
&& accountCapsule.oldTronPowerIsNotInitialized()) {
80+
accountCapsule.initializeOldTronPower();
81+
}
82+
7883
byte[] receiverAddress = unfreezeBalanceContract.getReceiverAddress().toByteArray();
7984
//If the receiver is not included in the contract, unfreeze frozen balance for this account.
8085
//otherwise,unfreeze delegated frozen balance provided this account.
@@ -195,7 +200,12 @@ public boolean execute(Object result) throws ContractExeException {
195200
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
196201
.setBalance(oldBalance + unfreezeBalance)
197202
.setAccountResource(newAccountResource).build());
198-
203+
break;
204+
case TRON_POWER:
205+
unfreezeBalance = accountCapsule.getTronPowerFrozenBalance();
206+
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
207+
.setBalance(oldBalance + unfreezeBalance)
208+
.clearTronPower().build());
199209
break;
200210
default:
201211
//this should never happen
@@ -213,24 +223,47 @@ public boolean execute(Object result) throws ContractExeException {
213223
dynamicStore
214224
.addTotalEnergyWeight(-unfreezeBalance / TRX_PRECISION);
215225
break;
226+
case TRON_POWER:
227+
dynamicStore
228+
.addTotalTronPowerWeight(-unfreezeBalance / TRX_PRECISION);
229+
break;
216230
default:
217231
//this should never happen
218232
break;
219233
}
220234

221-
VotesCapsule votesCapsule;
222-
if (!votesStore.has(ownerAddress)) {
223-
votesCapsule = new VotesCapsule(unfreezeBalanceContract.getOwnerAddress(),
224-
accountCapsule.getVotesList());
225-
} else {
226-
votesCapsule = votesStore.get(ownerAddress);
235+
boolean needToClearVote = true;
236+
if (dynamicStore.supportAllowNewResourceModel()
237+
&& accountCapsule.oldTronPowerIsInvalid()) {
238+
switch (unfreezeBalanceContract.getResource()) {
239+
case BANDWIDTH:
240+
case ENERGY:
241+
needToClearVote = false;
242+
break;
243+
default:
244+
break;
245+
}
227246
}
228-
accountCapsule.clearVotes();
229-
votesCapsule.clearNewVotes();
230247

231-
accountStore.put(ownerAddress, accountCapsule);
248+
if (needToClearVote) {
249+
VotesCapsule votesCapsule;
250+
if (!votesStore.has(ownerAddress)) {
251+
votesCapsule = new VotesCapsule(unfreezeBalanceContract.getOwnerAddress(),
252+
accountCapsule.getVotesList());
253+
} else {
254+
votesCapsule = votesStore.get(ownerAddress);
255+
}
256+
accountCapsule.clearVotes();
257+
votesCapsule.clearNewVotes();
258+
votesStore.put(ownerAddress, votesCapsule);
259+
}
232260

233-
votesStore.put(ownerAddress, votesCapsule);
261+
if (dynamicStore.supportAllowNewResourceModel()
262+
&& !accountCapsule.oldTronPowerIsInvalid()) {
263+
accountCapsule.invalidateOldTronPower();
264+
}
265+
266+
accountStore.put(ownerAddress, accountCapsule);
234267

235268
ret.setUnfreezeAmount(unfreezeBalance);
236269
ret.setStatus(fee, code.SUCESS);
@@ -396,10 +429,29 @@ public boolean validate() throws ContractValidateException {
396429
throw new ContractValidateException("It's not time to unfreeze(Energy).");
397430
}
398431

432+
break;
433+
case TRON_POWER:
434+
if (dynamicStore.supportAllowNewResourceModel()) {
435+
Frozen frozenBalanceForTronPower = accountCapsule.getInstance().getTronPower();
436+
if (frozenBalanceForTronPower.getFrozenBalance() <= 0) {
437+
throw new ContractValidateException("no frozenBalance(TronPower)");
438+
}
439+
if (frozenBalanceForTronPower.getExpireTime() > now) {
440+
throw new ContractValidateException("It's not time to unfreeze(TronPower).");
441+
}
442+
} else {
443+
throw new ContractValidateException(
444+
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
445+
}
399446
break;
400447
default:
401-
throw new ContractValidateException(
402-
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
448+
if (dynamicStore.supportAllowNewResourceModel()) {
449+
throw new ContractValidateException(
450+
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy、TRON_POWER]");
451+
} else {
452+
throw new ContractValidateException(
453+
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
454+
}
403455
}
404456

405457
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
import org.tron.common.utils.StorageUtils;
2727
import org.tron.common.utils.StringUtil;
2828
import org.tron.common.utils.WalletUtil;
29-
import org.tron.core.capsule.AccountCapsule;
30-
import org.tron.core.capsule.BlockCapsule;
31-
import org.tron.core.capsule.ContractCapsule;
32-
import org.tron.core.capsule.TransactionCapsule;
29+
import org.tron.core.capsule.*;
3330
import org.tron.core.db.TransactionContext;
3431
import org.tron.core.exception.ContractExeException;
3532
import org.tron.core.exception.ContractValidateException;
@@ -68,6 +65,7 @@ public class VMActuator implements Actuator2 {
6865
private Repository repository;
6966
private InternalTransaction rootInternalTransaction;
7067
private ProgramInvokeFactory programInvokeFactory;
68+
private ReceiptCapsule receipt;
7169

7270

7371
private VM vm;
@@ -115,6 +113,9 @@ public void validate(Object object) throws ContractValidateException {
115113
ConfigLoader.load(context.getStoreFactory());
116114
trx = context.getTrxCap().getInstance();
117115
blockCap = context.getBlockCap();
116+
if (VMConfig.allowTvmFreeze() && context.getTrxCap().getTrxTrace() != null) {
117+
receipt = context.getTrxCap().getTrxTrace().getReceipt();
118+
}
118119
//Route Type
119120
ContractType contractType = this.trx.getRawData().getContract(0).getType();
120121
//Prepare Repository
@@ -211,8 +212,6 @@ public void execute(Object object) throws ContractExeException {
211212
result.getLogInfoList().clear();
212213
result.resetFutureRefund();
213214
result.rejectInternalTransactions();
214-
result.getDeleteVotes().clear();
215-
result.getDeleteDelegation().clear();
216215

217216
if (result.getException() != null) {
218217
if (!(result.getException() instanceof TransferException)) {
@@ -521,6 +520,9 @@ public long getAccountEnergyLimitWithFixRatio(AccountCapsule account, long feeLi
521520
}
522521

523522
long leftFrozenEnergy = repository.getAccountLeftEnergyFromFreeze(account);
523+
if (VMConfig.allowTvmFreeze()) {
524+
receipt.setCallerEnergyLeft(leftFrozenEnergy);
525+
}
524526

525527
long energyFromBalance = max(account.getBalance() - callValue, 0) / sunPerEnergy;
526528
long availableEnergy = Math.addExact(leftFrozenEnergy, energyFromBalance);
@@ -648,9 +650,15 @@ public long getTotalEnergyLimitWithFixRatio(AccountCapsule creator, AccountCapsu
648650
throw new ContractValidateException("originEnergyLimit can't be < 0");
649651
}
650652

653+
long originEnergyLeft = 0;
654+
if (consumeUserResourcePercent < VMConstant.ONE_HUNDRED) {
655+
originEnergyLeft = repository.getAccountLeftEnergyFromFreeze(creator);
656+
if (VMConfig.allowTvmFreeze()) {
657+
receipt.setOriginEnergyLeft(originEnergyLeft);
658+
}
659+
}
651660
if (consumeUserResourcePercent <= 0) {
652-
creatorEnergyLimit = min(repository.getAccountLeftEnergyFromFreeze(creator),
653-
originEnergyLimit);
661+
creatorEnergyLimit = min(originEnergyLeft, originEnergyLimit);
654662
} else {
655663
if (consumeUserResourcePercent < VMConstant.ONE_HUNDRED) {
656664
// creatorEnergyLimit =
@@ -660,7 +668,7 @@ public long getTotalEnergyLimitWithFixRatio(AccountCapsule creator, AccountCapsu
660668
BigInteger.valueOf(callerEnergyLimit)
661669
.multiply(BigInteger.valueOf(VMConstant.ONE_HUNDRED - consumeUserResourcePercent))
662670
.divide(BigInteger.valueOf(consumeUserResourcePercent)).longValueExact(),
663-
min(repository.getAccountLeftEnergyFromFreeze(creator), originEnergyLimit)
671+
min(originEnergyLeft, originEnergyLimit)
664672
);
665673
}
666674
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.tron.core.exception.ContractValidateException;
2323
import org.tron.core.service.MortgageService;
2424
import org.tron.core.store.AccountStore;
25+
import org.tron.core.store.DynamicPropertiesStore;
2526
import org.tron.core.store.VotesStore;
2627
import org.tron.core.store.WitnessStore;
2728
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
@@ -125,7 +126,13 @@ public boolean validate() throws ContractValidateException {
125126
ACCOUNT_EXCEPTION_STR + readableOwnerAddress + NOT_EXIST_STR);
126127
}
127128

128-
long tronPower = accountCapsule.getTronPower();
129+
long tronPower;
130+
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
131+
if (dynamicStore.supportAllowNewResourceModel()) {
132+
tronPower = accountCapsule.getAllTronPower();
133+
} else {
134+
tronPower = accountCapsule.getTronPower();
135+
}
129136

130137
sum = LongMath
131138
.checkedMultiply(sum, TRX_PRECISION); //trx -> drop. The vote count is based on TRX
@@ -155,6 +162,12 @@ private void countVoteAccount(VoteWitnessContract voteContract) {
155162

156163
AccountCapsule accountCapsule = accountStore.get(ownerAddress);
157164

165+
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
166+
if (dynamicStore.supportAllowNewResourceModel()
167+
&& accountCapsule.oldTronPowerIsNotInitialized()) {
168+
accountCapsule.initializeOldTronPower();
169+
}
170+
158171
if (!votesStore.has(ownerAddress)) {
159172
votesCapsule = new VotesCapsule(voteContract.getOwnerAddress(),
160173
accountCapsule.getVotesList());

actuator/src/main/java/org/tron/core/utils/ProposalUtil.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
340340
}
341341
break;
342342
}
343-
// case ALLOW_TVM_STAKE: {
343+
// case ALLOW_TVM_STAKE: {
344344
// if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_1)) {
345345
// throw new ContractValidateException(
346346
// "Bad chain parameter id [ALLOW_TVM_STAKE]");
@@ -433,6 +433,48 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
433433
}
434434
break;
435435
}
436+
case ALLOW_NEW_RESOURCE_MODEL: {
437+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_2)) {
438+
throw new ContractValidateException(
439+
"Bad chain parameter id [ALLOW_NEW_RESOURCE_MODEL]");
440+
}
441+
if (value != 1) {
442+
throw new ContractValidateException(
443+
"This value[ALLOW_NEW_RESOURCE_MODEL] is only allowed to be 1");
444+
}
445+
break;
446+
}
447+
case ALLOW_TVM_FREEZE: {
448+
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_2)) {
449+
throw new ContractValidateException(
450+
"Bad chain parameter id [ALLOW_TVM_FREEZE]");
451+
}
452+
if (value != 1) {
453+
throw new ContractValidateException(
454+
PRE_VALUE_NOT_ONE_ERROR + "ALLOW_TVM_FREEZE" + VALUE_NOT_ONE_ERROR);
455+
}
456+
if (dynamicPropertiesStore.getAllowDelegateResource() == 0) {
457+
throw new ContractValidateException(
458+
"[ALLOW_DELEGATE_RESOURCE] proposal must be approved "
459+
+ "before [ALLOW_TVM_FREEZE] can be proposed");
460+
}
461+
if (dynamicPropertiesStore.getAllowMultiSign() == 0) {
462+
throw new ContractValidateException(
463+
"[ALLOW_MULTI_SIGN] proposal must be approved "
464+
+ "before [ALLOW_TVM_FREEZE] can be proposed");
465+
}
466+
if (dynamicPropertiesStore.getAllowTvmConstantinople() == 0) {
467+
throw new ContractValidateException(
468+
"[ALLOW_TVM_CONSTANTINOPLE] proposal must be approved "
469+
+ "before [ALLOW_TVM_FREEZE] can be proposed");
470+
}
471+
if (dynamicPropertiesStore.getAllowTvmSolidity059() == 0) {
472+
throw new ContractValidateException(
473+
"[ALLOW_TVM_SOLIDITY_059] proposal must be approved "
474+
+ "before [ALLOW_TVM_FREEZE] can be proposed");
475+
}
476+
break;
477+
}
436478

437479

438480
default:
@@ -487,7 +529,9 @@ public enum ProposalType { // current value, value range
487529
MARKET_CANCEL_FEE(46), // 0 [0,10_000_000_000]
488530
MAX_FEE_LIMIT(47), // [0, 10_000_000_000]
489531
ALLOW_TRANSACTION_FEE_POOL(48), // 0, 1
490-
ALLOW_BLACKHOLE_OPTIMIZATION(49);// 0,1
532+
ALLOW_BLACKHOLE_OPTIMIZATION(49),// 0,1
533+
ALLOW_NEW_RESOURCE_MODEL(51),// 0,1
534+
ALLOW_TVM_FREEZE(52); // 0, 1
491535

492536
private long code;
493537

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public class EnergyCost {
5959
private final int EXT_CODE_COPY = 20;
6060
private final int EXT_CODE_HASH = 400;
6161
private final int NEW_ACCT_SUICIDE = 0;
62+
private final int FREEZE = 20000;
63+
private final int UNFREEZE = 20000;
64+
private final int FREEZE_EXPIRE_TIME = 50;
6265
private final int STAKE_UNSTAKE = 35000;
6366
private final int WITHDRAW_REWARD = 25000;
6467
private final int TOKEN_ISSUE = 25000;
@@ -284,6 +287,18 @@ public int getEXT_CODE_HASH() {
284287
return EXT_CODE_HASH;
285288
}
286289

290+
public int getFREEZE() {
291+
return FREEZE;
292+
}
293+
294+
public int getUNFREEZE() {
295+
return UNFREEZE;
296+
}
297+
298+
public int getFREEZE_EXPIRE_TIME() {
299+
return FREEZE_EXPIRE_TIME;
300+
}
301+
287302
public int getStakeAndUnstake() {
288303
return STAKE_UNSTAKE;
289304
}

0 commit comments

Comments
 (0)