Skip to content
Open
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
18 changes: 17 additions & 1 deletion actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,21 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_TVM_VOTE_POWER_OPTIMIZATION: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_8_3)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_TVM_VOTE_POWER_OPTIMIZATION]");
}
if (dynamicPropertiesStore.getAllowTvmVotePowerOptimization() == 1) {
throw new ContractValidateException(
"[ALLOW_TVM_VOTE_POWER_OPTIMIZATION] has been valid, no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_TVM_VOTE_POWER_OPTIMIZATION] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -1106,7 +1121,8 @@ public enum ProposalType { // current value, value range
ALLOW_OPTIMIZE_TVM_STORAGE(99), // 0, 1
ALLOW_STRICT_ECDSA_VALIDATION(100), // 0, 1
CLOSE_EXCHANGE(101), // 0, 1, 2
ALLOW_OPTIMIZE_TVM_STORAGE_KEY(102); // 0, 1
ALLOW_OPTIMIZE_TVM_STORAGE_KEY(102), // 0, 1
ALLOW_TVM_VOTE_POWER_OPTIMIZATION(103); // 0, 1
private long code;

ProposalType(long code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static void load(StoreFactory storeFactory, boolean isolate) {
snapshot.allowHardenResourceCalculation = ds.getAllowHardenResourceCalculation() == 1;
snapshot.allowStrictEcdsaValidation = ds.getAllowStrictEcdsaValidation() == 1;
snapshot.allowOptimizeTvmStorageKey = ds.allowOptimizeTvmStorageKey();
snapshot.allowTvmVotePowerOptimization = ds.getAllowTvmVotePowerOptimization() == 1;
if (isolate) {
VMConfig.setLocalSnapshot(snapshot);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.tron.core.capsule.DelegatedResourceCapsule;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.vm.config.VMConfig;
import org.tron.core.vm.nativecontract.param.FreezeBalanceParam;
import org.tron.core.vm.repository.Repository;
import org.tron.protos.Protocol;
Expand Down Expand Up @@ -80,6 +81,11 @@ public void execute(FreezeBalanceParam param, Repository repo) {
byte[] receiverAddress = param.getReceiverAddress();
long frozenBalance = param.getFrozenBalance();
AccountCapsule accountCapsule = repo.getAccount(ownerAddress);
if (VMConfig.allowTvmVotePowerOptimization()
&& dynamicStore.supportAllowNewResourceModel()
&& accountCapsule.oldTronPowerIsNotInitialized()) {
accountCapsule.initializeOldTronPower();
}
// acquire or delegate resource
if (param.isDelegating()) { // delegate resource
switch (param.getResourceType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ public long execute(UnfreezeBalanceParam param, Repository repo) {
byte[] ownerAddress = param.getOwnerAddress();
byte[] receiverAddress = param.getReceiverAddress();

boolean optimizeVotePower = VMConfig.allowTvmVotePowerOptimization()
&& repo.getDynamicPropertiesStore().supportAllowNewResourceModel();
if (optimizeVotePower) {
VoteRewardUtil.withdrawReward(ownerAddress, repo);
}
AccountCapsule accountCapsule = repo.getAccount(ownerAddress);
if (optimizeVotePower && accountCapsule.oldTronPowerIsNotInitialized()) {
accountCapsule.initializeOldTronPower();
}
long oldBalance = accountCapsule.getBalance();
long unfreezeBalance = 0L;

Expand Down Expand Up @@ -202,6 +210,22 @@ public long execute(UnfreezeBalanceParam param, Repository repo) {

repo.updateAccount(accountCapsule.createDbKey(), accountCapsule);

if (optimizeVotePower && repo.getDynamicPropertiesStore().supportUnfreezeDelay()) {
if (!accountCapsule.oldTronPowerIsInvalid()) {
VotesCapsule votesCapsule = repo.getVotes(ownerAddress);
if (votesCapsule == null) {
votesCapsule = new VotesCapsule(ByteString.copyFrom(ownerAddress),
accountCapsule.getVotesList());
}
votesCapsule.clearNewVotes();
accountCapsule.clearVotes();
accountCapsule.invalidateOldTronPower();
repo.updateVotes(ownerAddress, votesCapsule);
repo.updateAccount(ownerAddress, accountCapsule);
}
return unfreezeBalance;
}

if (VMConfig.allowTvmVote() && !accountCapsule.getVotesList().isEmpty()) {
long usedTronPower = 0;
for (Protocol.Vote vote : accountCapsule.getVotesList()) {
Expand All @@ -223,6 +247,11 @@ public long execute(UnfreezeBalanceParam param, Repository repo) {
}
}

if (optimizeVotePower && !accountCapsule.oldTronPowerIsInvalid()) {
accountCapsule.invalidateOldTronPower();
repo.updateAccount(ownerAddress, accountCapsule);
}

return unfreezeBalance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.tron.core.capsule.VotesCapsule;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.vm.config.VMConfig;
import org.tron.core.vm.nativecontract.param.VoteWitnessParam;
import org.tron.core.vm.repository.Repository;
import org.tron.core.vm.utils.VoteRewardUtil;
Expand All @@ -41,6 +42,11 @@ public void execute(VoteWitnessParam param, Repository repo) throws ContractExeE
VoteRewardUtil.withdrawReward(ownerAddress, repo);

AccountCapsule accountCapsule = repo.getAccount(ownerAddress);
if (VMConfig.allowTvmVotePowerOptimization()
&& repo.getDynamicPropertiesStore().supportAllowNewResourceModel()
&& accountCapsule.oldTronPowerIsNotInitialized()) {
accountCapsule.initializeOldTronPower();
}

VotesCapsule votesCapsule = repo.getVotes(ownerAddress);
if (votesCapsule == null) {
Expand Down
24 changes: 23 additions & 1 deletion actuator/src/main/java/org/tron/core/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ private void transferDelegatedResourceToInheritor(byte[] ownerAddr, byte[] inher
ownerCapsule.getAccountResource().getFrozenBalanceForEnergy().getFrozenBalance();
repo.addTotalEnergyWeight(-frozenBalanceForEnergyOfOwner / TRX_PRECISION);

if (optimizeVotePowerState(repo)
&& (frozenBalanceForBandwidthOfOwner > 0 || frozenBalanceForEnergyOfOwner > 0)) {
ownerCapsule.invalidateOldTronPower();
repo.updateAccount(ownerAddr, ownerCapsule);
}

// transfer all kinds of frozen balance to BlackHole
repo.addBalance(inheritorAddr, frozenBalanceForBandwidthOfOwner + frozenBalanceForEnergyOfOwner);

Expand All @@ -631,6 +637,15 @@ private long transferFrozenV2BalanceToInheritor(byte[] ownerAddr, byte[] inherit
AccountCapsule inheritorCapsule = repo.getAccount(inheritorAddr);
long now = repo.getHeadSlot();

if (optimizeVotePowerState(repo)
&& ownerCapsule.getFrozenV2List().stream().anyMatch(frozen -> frozen.getAmount() > 0)) {
if (inheritorCapsule.oldTronPowerIsNotInitialized()) {
inheritorCapsule.initializeOldTronPower();
}
// Invalidate before returning, including accounts queued for deletion at finalization.
ownerCapsule.invalidateOldTronPower();
}

// transfer frozen resource
ownerCapsule.getFrozenV2List().stream()
.filter(freezeV2 -> freezeV2.getAmount() > 0)
Expand Down Expand Up @@ -703,6 +718,11 @@ private void clearOwnerFreezeV2(AccountCapsule ownerCapsule) {
ownerCapsule.clearUnfrozenV2();
}

private boolean optimizeVotePowerState(Repository repo) {
return VMConfig.allowTvmVotePowerOptimization()
&& repo.getDynamicPropertiesStore().supportAllowNewResourceModel();
}

private void withdrawRewardAndCancelVote(byte[] owner, Repository repo) {
VoteRewardUtil.withdrawReward(owner, repo);

Expand All @@ -716,7 +736,9 @@ private void withdrawRewardAndCancelVote(byte[] owner, Repository repo) {
votesCapsule.clearNewVotes();
}
ownerCapsule.clearVotes();
ownerCapsule.setOldTronPower(0);
if (!optimizeVotePowerState(repo)) {
ownerCapsule.setOldTronPower(0);
}
repo.updateVotes(owner, votesCapsule);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
private static final byte[] ALLOW_OPTIMIZE_TVM_STORAGE_KEY =
"ALLOW_OPTIMIZE_TVM_STORAGE_KEY".getBytes();

private static final byte[] ALLOW_TVM_VOTE_POWER_OPTIMIZATION =
"ALLOW_TVM_VOTE_POWER_OPTIMIZATION".getBytes();

@Autowired
private DynamicPropertiesStore(@Value("properties") String dbName) {
super(dbName);
Expand Down Expand Up @@ -3149,6 +3152,21 @@ public boolean allowOptimizeTvmStorageKey() {
return getAllowOptimizeTvmStorageKey() == 1L;
}

public long getAllowTvmVotePowerOptimization() {
return Optional.ofNullable(getUnchecked(ALLOW_TVM_VOTE_POWER_OPTIMIZATION))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElse(0L);
}

public void saveAllowTvmVotePowerOptimization(long value) {
this.put(ALLOW_TVM_VOTE_POWER_OPTIMIZATION, new BytesCapsule(ByteArray.fromLong(value)));
}

public boolean allowTvmVotePowerOptimization() {
return getAllowTvmVotePowerOptimization() == 1L;
}

public boolean allowTvmStorageOptimization() {
return allowOptimizeTvmStorage() || allowOptimizeTvmStorageKey();
}
Expand Down
9 changes: 9 additions & 0 deletions common/src/main/java/org/tron/core/vm/config/VMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static class Snapshot {
public boolean allowHardenResourceCalculation;
public boolean allowStrictEcdsaValidation;
public boolean allowOptimizeTvmStorageKey;
public boolean allowTvmVotePowerOptimization;
}

// HEAD / block-processing config, written by the consensus path; read by everyone with no
Expand Down Expand Up @@ -204,6 +205,10 @@ public static void initAllowOptimizeTvmStorageKey(long allow) {
globalSnapshot.allowOptimizeTvmStorageKey = allow == 1;
}

public static void initAllowTvmVotePowerOptimization(long allow) {
globalSnapshot.allowTvmVotePowerOptimization = allow == 1;
}

public static void initAllowStrictEcdsaValidation(long allow) {
globalSnapshot.allowStrictEcdsaValidation = allow == 1;
}
Expand Down Expand Up @@ -320,6 +325,10 @@ public static boolean allowOptimizeTvmStorageKey() {
return current().allowOptimizeTvmStorageKey;
}

public static boolean allowTvmVotePowerOptimization() {
return current().allowTvmVotePowerOptimization;
}

public static boolean allowStrictEcdsaValidation() {
return current().allowStrictEcdsaValidation;
}
Expand Down
5 changes: 5 additions & 0 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,11 @@ public Protocol.ChainParameters getChainParameters() {
.setValue(dbManager.getDynamicPropertiesStore().getAllowOptimizeTvmStorageKey())
.build());

builder.addChainParameter(Protocol.ChainParameters.ChainParameter.newBuilder()
.setKey("getAllowTvmVotePowerOptimization")
.setValue(dbManager.getDynamicPropertiesStore().getAllowTvmVotePowerOptimization())
.build());

return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ public static boolean process(Manager manager, ProposalCapsule proposalCapsule)
.saveAllowOptimizeTvmStorageKey(entry.getValue());
break;
}
case ALLOW_TVM_VOTE_POWER_OPTIMIZATION: {
manager.getDynamicPropertiesStore()
.saveAllowTvmVotePowerOptimization(entry.getValue());
break;
}
default:
find = false;
break;
Expand Down
Loading
Loading