Skip to content

Commit

Permalink
fix(freezeV2): optimize stake2.0 code
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcmyf committed Nov 18, 2022
1 parent 48aa0ed commit d7a22e0
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("frozenBalance must be less than accountBalance");
}

// long maxFrozenNumber = dbManager.getDynamicPropertiesStore().getMaxFrozenNumber();
// if (accountCapsule.getFrozenCount() >= maxFrozenNumber) {
// throw new ContractValidateException("max frozen number is: " + maxFrozenNumber);
// }

long frozenDuration = freezeBalanceContract.getFrozenDuration();
long minFrozenTime = dynamicStore.getMinFrozenTime();
long maxFrozenTime = dynamicStore.getMaxFrozenTime();
Expand Down Expand Up @@ -224,8 +219,7 @@ public boolean validate() throws ContractValidateException {
//If the receiver is included in the contract, the receiver will receive the resource.
if (!ArrayUtils.isEmpty(receiverAddress) && dynamicStore.supportDR()) {
if (Arrays.equals(receiverAddress, ownerAddress)) {
throw new ContractValidateException(
"receiverAddress must not be the same as ownerAddress");
throw new ContractValidateException("receiverAddress must not be the same as ownerAddress");
}

if (!DecodeUtil.addressValid(receiverAddress)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
@Slf4j(topic = "actuator")
public class UnfreezeBalanceActuator extends AbstractActuator {

private static final String INVALID_RESOURCE_CODE =
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]";

public UnfreezeBalanceActuator() {
super(ContractType.UnfreezeBalanceContract, UnfreezeBalanceContract.class);
}
Expand Down Expand Up @@ -402,8 +405,7 @@ public boolean validate() throws ContractValidateException {
}
break;
default:
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
throw new ContractValidateException(INVALID_RESOURCE_CODE);
}

} else {
Expand Down Expand Up @@ -440,17 +442,15 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("It's not time to unfreeze(TronPower).");
}
} else {
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
throw new ContractValidateException(INVALID_RESOURCE_CODE);
}
break;
default:
if (dynamicStore.supportAllowNewResourceModel()) {
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy、TRON_POWER]");
} else {
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
throw new ContractValidateException(INVALID_RESOURCE_CODE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import static org.tron.core.actuator.ActuatorConstant.ACCOUNT_EXCEPTION_STR;
import static org.tron.core.config.Parameter.ChainConstant.FROZEN_PERIOD;
import static org.tron.core.config.Parameter.ChainConstant.TRX_PRECISION;
import static org.tron.protos.contract.Common.ResourceCode;
import static org.tron.protos.contract.Common.ResourceCode.BANDWIDTH;
import static org.tron.protos.contract.Common.ResourceCode.ENERGY;
import static org.tron.protos.contract.Common.ResourceCode.TRON_POWER;

import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
Expand All @@ -24,11 +28,12 @@
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.store.VotesStore;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Account.FreezeV2;
import org.tron.protos.Protocol.Account.UnFreezeV2;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.Transaction.Result.code;
import org.tron.protos.Protocol.Vote;
import org.tron.protos.contract.BalanceContract.UnfreezeBalanceV2Contract;
import org.tron.protos.contract.Common;

@Slf4j(topic = "actuator")
public class UnfreezeBalanceV2Actuator extends AbstractActuator {
Expand Down Expand Up @@ -73,7 +78,7 @@ public boolean execute(Object result) throws ContractExeException {
accountCapsule.initializeOldTronPower();
}

Common.ResourceCode freezeType = unfreezeBalanceV2Contract.getResource();
ResourceCode freezeType = unfreezeBalanceV2Contract.getResource();

this.updateAccountFrozenInfo(freezeType, accountCapsule, unfreezeBalance);

Expand Down Expand Up @@ -140,18 +145,18 @@ public boolean validate() throws ContractValidateException {
long now = dynamicStore.getLatestBlockHeaderTimestamp();
switch (unfreezeBalanceV2Contract.getResource()) {
case BANDWIDTH:
if (!this.checkExistFreezedBalance(accountCapsule, Common.ResourceCode.BANDWIDTH)) {
if (!checkExistFrozenBalance(accountCapsule, BANDWIDTH)) {
throw new ContractValidateException("no frozenBalance(BANDWIDTH)");
}
break;
case ENERGY:
if (!this.checkExistFreezedBalance(accountCapsule, Common.ResourceCode.ENERGY)) {
if (!checkExistFrozenBalance(accountCapsule, ENERGY)) {
throw new ContractValidateException("no frozenBalance(Energy)");
}
break;
case TRON_POWER:
if (dynamicStore.supportAllowNewResourceModel()) {
if (!this.checkExistFreezedBalance(accountCapsule, Common.ResourceCode.TRON_POWER)) {
if (!checkExistFrozenBalance(accountCapsule, TRON_POWER)) {
throw new ContractValidateException("no frozenBalance(TronPower)");
}
} else {
Expand Down Expand Up @@ -190,32 +195,24 @@ public long calcFee() {
return 0;
}

public boolean checkExistFreezedBalance(AccountCapsule accountCapsule, Common.ResourceCode freezeType) {
boolean checkOk = false;

long frozenAmount = 0;
List<Protocol.Account.FreezeV2> frozenV2List = accountCapsule.getFrozenV2List();
for (Protocol.Account.FreezeV2 frozenV2 : frozenV2List) {
if (frozenV2.getType().equals(freezeType)) {
frozenAmount = frozenV2.getAmount();
if (frozenAmount > 0) {
checkOk = true;
break;
}
public boolean checkExistFrozenBalance(AccountCapsule accountCapsule, ResourceCode freezeType) {
List<FreezeV2> frozenV2List = accountCapsule.getFrozenV2List();
for (FreezeV2 frozenV2 : frozenV2List) {
if (frozenV2.getType().equals(freezeType) && frozenV2.getAmount() > 0) {
return true;
}
}

return checkOk;
return false;
}

public boolean checkUnfreezeBalance(AccountCapsule accountCapsule,
final UnfreezeBalanceV2Contract unfreezeBalanceV2Contract,
Common.ResourceCode freezeType) {
ResourceCode freezeType) {
boolean checkOk = false;

long frozenAmount = 0L;
List<Protocol.Account.FreezeV2> freezeV2List = accountCapsule.getFrozenV2List();
for (Protocol.Account.FreezeV2 freezeV2 : freezeV2List) {
List<FreezeV2> freezeV2List = accountCapsule.getFrozenV2List();
for (FreezeV2 freezeV2 : freezeV2List) {
if (freezeV2.getType().equals(freezeType)) {
frozenAmount = freezeV2.getAmount();
break;
Expand All @@ -237,11 +234,11 @@ public long calcUnfreezeExpireTime(long now) {
return now + unfreezeDelayDays * FROZEN_PERIOD;
}

public void updateAccountFrozenInfo(Common.ResourceCode freezeType, AccountCapsule accountCapsule, long unfreezeBalance) {
List<Protocol.Account.FreezeV2> freezeV2List = accountCapsule.getFrozenV2List();
public void updateAccountFrozenInfo(ResourceCode freezeType, AccountCapsule accountCapsule, long unfreezeBalance) {
List<FreezeV2> freezeV2List = accountCapsule.getFrozenV2List();
for (int i = 0; i < freezeV2List.size(); i++) {
if (freezeV2List.get(i).getType().equals(freezeType)) {
Protocol.Account.FreezeV2 freezeV2 = Protocol.Account.FreezeV2.newBuilder()
FreezeV2 freezeV2 = FreezeV2.newBuilder()
.setAmount(freezeV2List.get(i).getAmount() - unfreezeBalance)
.setType(freezeV2List.get(i).getType())
.build();
Expand All @@ -254,12 +251,12 @@ public void updateAccountFrozenInfo(Common.ResourceCode freezeType, AccountCapsu
public long unfreezeExpire(AccountCapsule accountCapsule, long now) {
long unfreezeBalance = 0L;

List<Protocol.Account.UnFreezeV2> unFrozenV2List = Lists.newArrayList();
List<UnFreezeV2> unFrozenV2List = Lists.newArrayList();
unFrozenV2List.addAll(accountCapsule.getUnfrozenV2List());
Iterator<Protocol.Account.UnFreezeV2> iterator = unFrozenV2List.iterator();
Iterator<UnFreezeV2> iterator = unFrozenV2List.iterator();

while (iterator.hasNext()) {
Protocol.Account.UnFreezeV2 next = iterator.next();
UnFreezeV2 next = iterator.next();
if (next.getUnfreezeExpireTime() <= now) {
unfreezeBalance += next.getUnfreezeAmount();
iterator.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.tron.protos.Protocol.Vote;
import org.tron.protos.contract.AccountContract.AccountCreateContract;
import org.tron.protos.contract.AccountContract.AccountUpdateContract;
import org.tron.protos.contract.Common;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -453,7 +452,7 @@ public void addFrozenBalanceForEnergyV2(long balance) {
this.addFrozenBalanceForResource(ENERGY, balance);
}

private void addFrozenBalanceForResource(Common.ResourceCode type, long balance) {
private void addFrozenBalanceForResource(ResourceCode type, long balance) {
boolean doUpdate = false;
for (int i = 0; i < this.account.getFrozenV2List().size(); i++) {
if (this.account.getFrozenV2List().get(i).getType().equals(type)) {
Expand Down Expand Up @@ -564,9 +563,10 @@ public List<UnFreezeV2> getUnfrozenV2List() {
}

public void updateFrozenV2List(int index, FreezeV2 frozenV2) {
this.account = this.account.toBuilder()
.setFrozenV2(index, frozenV2)
.build();
if (Objects.isNull(frozenV2)) {
return;
}
this.account = this.account.toBuilder().setFrozenV2(index, frozenV2).build();
}

public void addFrozenV2List(FreezeV2 frozenV2) {
Expand Down Expand Up @@ -750,9 +750,8 @@ public boolean addAssetV2(byte[] key, long value) {
return true;
}

public boolean addAssetMapV2(Map<String, Long> assetMap) {
public void addAssetMapV2(Map<String, Long> assetMap) {
this.account = this.account.toBuilder().putAllAssetV2(assetMap).build();
return true;
}

public Long getAsset(DynamicPropertiesStore dynamicStore, String key) {
Expand Down Expand Up @@ -799,9 +798,8 @@ public Map<String, Long> getAssetV2MapForTest() {

/*************************** end asset ****************************************/

public boolean addAllLatestAssetOperationTimeV2(Map<String, Long> map) {
public void addAllLatestAssetOperationTimeV2(Map<String, Long> map) {
this.account = this.account.toBuilder().putAllLatestAssetOperationTimeV2(map).build();
return true;
}

public Map<String, Long> getLatestAssetOperationTimeMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ public long getFrozenBalanceForBandwidth() {
return this.delegatedResource.getFrozenBalanceForBandwidth();
}

public void setFrozenBalanceForBandwidth(long Bandwidth, long expireTime) {
public void setFrozenBalanceForBandwidth(long bandwidth, long expireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setFrozenBalanceForBandwidth(Bandwidth)
.setFrozenBalanceForBandwidth(bandwidth)
.setExpireTimeForBandwidth(expireTime)
.build();
}

public void addFrozenBalanceForBandwidth(long Bandwidth, long expireTime) {
public void addFrozenBalanceForBandwidth(long bandwidth, long expireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setFrozenBalanceForBandwidth(this.delegatedResource.getFrozenBalanceForBandwidth()
+ Bandwidth)
+ bandwidth)
.setExpireTimeForBandwidth(expireTime)
.build();
}
Expand All @@ -99,9 +99,9 @@ public long getExpireTimeForEnergy() {
return this.delegatedResource.getExpireTimeForEnergy();
}

public void setExpireTimeForBandwidth(long ExpireTime) {
public void setExpireTimeForBandwidth(long expireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setExpireTimeForBandwidth(ExpireTime)
.setExpireTimeForBandwidth(expireTime)
.build();
}

Expand All @@ -113,9 +113,9 @@ public long getExpireTimeForEnergy(DynamicPropertiesStore dynamicPropertiesStore
}
}

public void setExpireTimeForEnergy(long ExpireTime) {
public void setExpireTimeForEnergy(long expireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setExpireTimeForEnergy(ExpireTime)
.setExpireTimeForEnergy(expireTime)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public BandwidthProcessor(ChainBaseManager chainBaseManager) {
this.chainBaseManager = chainBaseManager;
}

@Override
public void updateUsage(AccountCapsule accountCapsule) {
long now = chainBaseManager.getHeadSlot();
updateUsage(accountCapsule, now);
Expand All @@ -61,8 +60,7 @@ private void updateUsage(AccountCapsule accountCapsule, long now) {
});
}
Map<String, Long> assetMapV2 = accountCapsule.getAssetMapV2();
Map<String, Long> map = new HashMap<>();
map.putAll(assetMapV2);
Map<String, Long> map = new HashMap<>(assetMapV2);
accountCapsule.getAllFreeAssetNetUsageV2().forEach((k, v) -> {
if (!map.containsKey(k)) {
map.put(k, 0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static long getHeadSlot(DynamicPropertiesStore dynamicPropertiesStore) {
/ BLOCK_PRODUCED_INTERVAL;
}

@Override
public void updateUsage(AccountCapsule accountCapsule) {
long now = getHeadSlot();
updateUsage(accountCapsule, now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public ResourceProcessor(DynamicPropertiesStore dynamicPropertiesStore,
AdaptiveResourceLimitConstants.PERIODS_MS / BLOCK_PRODUCED_INTERVAL;
}

abstract void updateUsage(AccountCapsule accountCapsule);

abstract void consume(TransactionCapsule trx, TransactionTrace trace)
throws ContractValidateException, AccountResourceInsufficientException, TooBigTransactionResultException;

Expand Down Expand Up @@ -63,12 +61,10 @@ protected long increase(long lastUsage, long usage, long lastTime, long now, lon
public long increase(AccountCapsule accountCapsule, ResourceCode resourceCode,
long lastUsage, long usage, long lastTime, long now) {
long oldWindowSize = accountCapsule.getWindowSize(resourceCode);
/* old logic */
long averageLastUsage = divideCeil(lastUsage * this.precision, oldWindowSize);
long averageUsage = divideCeil(usage * this.precision, this.windowSize);

if (lastTime != now) {
assert now > lastTime;
if (lastTime + oldWindowSize > now) {
long delta = now - lastTime;
double decay = (oldWindowSize - delta) / (double) oldWindowSize;
Expand All @@ -77,7 +73,7 @@ public long increase(AccountCapsule accountCapsule, ResourceCode resourceCode,
averageLastUsage = 0;
}
}
/* new logic */

long newUsage = getUsage(averageLastUsage, oldWindowSize) +
getUsage(averageUsage, this.windowSize);
if (dynamicPropertiesStore.supportUnfreezeDelay()) {
Expand Down Expand Up @@ -150,7 +146,6 @@ protected boolean consumeFeeForBandwidth(AccountCapsule accountCapsule, long fee
}
}


protected boolean consumeFeeForNewAccount(AccountCapsule accountCapsule, long fee) {
try {
long latestOperationTime = dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
Expand Down
12 changes: 5 additions & 7 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,9 @@ public Account getAccount(Account account) {
private void sortFrozenV2List(AccountCapsule accountCapsule) {
List<FreezeV2> oldFreezeV2List = accountCapsule.getFrozenV2List();
accountCapsule.clearFrozenV2();
ResourceCode[] codes = ResourceCode.values();
for (ResourceCode code : codes) {
for (ResourceCode code : ResourceCode.values()) {
if (ResourceCode.UNRECOGNIZED != code) {
accountCapsule.addFrozenV2List(FreezeV2.newBuilder()
.setType(code)
.setAmount(0)
.build());
accountCapsule.addFrozenV2List(FreezeV2.newBuilder().setType(code).setAmount(0).build());
}
}
List<FreezeV2> newFreezeV2List = accountCapsule.getFrozenV2List();
Expand All @@ -359,7 +355,9 @@ private void sortFrozenV2List(AccountCapsule accountCapsule) {
ResourceCode code = freezeV2.getType();
Optional<FreezeV2> optional = oldFreezeV2List
.stream().filter(o -> o.getType() == code).findFirst();
accountCapsule.updateFrozenV2List(i, optional.orElse(freezeV2));
if (optional.isPresent()) {
accountCapsule.updateFrozenV2List(i, optional.get());
}
}
}

Expand Down
Loading

0 comments on commit d7a22e0

Please sign in to comment.