Skip to content

Commit

Permalink
fix(freezeV2): sort frozenV2 list for getaccount interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcmyf committed Nov 16, 2022
1 parent e91d78e commit 84849b4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 67 deletions.
69 changes: 27 additions & 42 deletions chainbase/src/main/java/org/tron/core/capsule/AccountCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package org.tron.core.capsule;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -333,24 +332,24 @@ public long getLatestOperationTime() {
return this.account.getLatestOprationTime();
}

public void setLatestOperationTime(long latest_time) {
this.account = this.account.toBuilder().setLatestOprationTime(latest_time).build();
public void setLatestOperationTime(long latestTime) {
this.account = this.account.toBuilder().setLatestOprationTime(latestTime).build();
}

public long getLatestConsumeTime() {
return this.account.getLatestConsumeTime();
}

public void setLatestConsumeTime(long latest_time) {
this.account = this.account.toBuilder().setLatestConsumeTime(latest_time).build();
public void setLatestConsumeTime(long latestTime) {
this.account = this.account.toBuilder().setLatestConsumeTime(latestTime).build();
}

public long getLatestConsumeFreeTime() {
return this.account.getLatestConsumeFreeTime();
}

public void setLatestConsumeFreeTime(long latest_time) {
this.account = this.account.toBuilder().setLatestConsumeFreeTime(latest_time).build();
public void setLatestConsumeFreeTime(long latestTime) {
this.account = this.account.toBuilder().setLatestConsumeFreeTime(latestTime).build();
}

public void addDelegatedFrozenBalanceForBandwidth(long balance) {
Expand All @@ -363,8 +362,7 @@ public long getAcquiredDelegatedFrozenBalanceForBandwidth() {
}

public void addFrozenBalanceForBandwidthV2(long balance) {
Common.ResourceCode type = Common.ResourceCode.BANDWIDTH;
this.addFrozenBalanceForResource(type, balance);
this.addFrozenBalanceForResource(BANDWIDTH, balance);
}

public void setAcquiredDelegatedFrozenBalanceForBandwidth(long balance) {
Expand Down Expand Up @@ -452,16 +450,15 @@ public void addDelegatedFrozenBalanceForEnergy(long balance) {
}

public void addFrozenBalanceForEnergyV2(long balance) {
Common.ResourceCode type = Common.ResourceCode.ENERGY;
this.addFrozenBalanceForResource(type, balance);
this.addFrozenBalanceForResource(ENERGY, balance);
}

private void addFrozenBalanceForResource(Common.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)) {
long newAmount = this.account.getFrozenV2(i).getAmount() + balance;
Account.FreezeV2 freezeV2 = Account.FreezeV2.newBuilder()
FreezeV2 freezeV2 = FreezeV2.newBuilder()
.setType(type)
.setAmount(newAmount)
.build();
Expand All @@ -472,7 +469,7 @@ private void addFrozenBalanceForResource(Common.ResourceCode type, long balance)
}

if (!doUpdate) {
Account.FreezeV2 freezeV2 = Account.FreezeV2.newBuilder()
FreezeV2 freezeV2 = FreezeV2.newBuilder()
.setType(type)
.setAmount(balance)
.build();
Expand Down Expand Up @@ -520,19 +517,14 @@ public void clearVotes() {
* get votes.
*/
public List<Vote> getVotesList() {
if (this.account.getVotesList() != null) {
return this.account.getVotesList();
} else {
return Lists.newArrayList();
}
return this.account.getVotesList();
}

public long getTronPowerUsage() {
if (this.account.getVotesList() != null) {
return this.account.getVotesList().stream().mapToLong(Vote::getVoteCount).sum();
} else {
if (getVotesList().isEmpty()) {
return 0L;
}
return this.account.getVotesList().stream().mapToLong(Vote::getVoteCount).sum();
}

//tp:Tron_Power
Expand Down Expand Up @@ -563,47 +555,42 @@ public long getAllTronPower() {



public List<Account.FreezeV2> getFrozenV2List() {
public List<FreezeV2> getFrozenV2List() {
return account.getFrozenV2List();
}

public List<Account.UnFreezeV2> getUnfrozenV2List() {
public List<UnFreezeV2> getUnfrozenV2List() {
return account.getUnfrozenV2List();
}

public void updateFrozenV2List(int i, Account.FreezeV2 frozenV2) {
public void updateFrozenV2List(int index, FreezeV2 frozenV2) {
this.account = this.account.toBuilder()
.setFrozenV2(i, frozenV2)
.setFrozenV2(index, frozenV2)
.build();
}

public void addFrozenV2List(Account.FreezeV2 frozenV2) {
public void addFrozenV2List(FreezeV2 frozenV2) {
this.account = this.account.toBuilder().addFrozenV2(frozenV2).build();
}

public void addUnfrozenV2List(Common.ResourceCode type, long unfreezeAmount, long expireTime) {

Account.UnFreezeV2 unFreezeV2 = Account.UnFreezeV2.newBuilder()
public void addUnfrozenV2List(ResourceCode type, long unfreezeAmount, long expireTime) {
UnFreezeV2 unFreezeV2 = UnFreezeV2.newBuilder()
.setType(type)
.setUnfreezeAmount(unfreezeAmount)
.setUnfreezeExpireTime(expireTime)
.build();
this.account = this.account.toBuilder()
.addUnfrozenV2(unFreezeV2)
.build();
this.account = this.account.toBuilder().addUnfrozenV2(unFreezeV2).build();
}


public int getUnfreezingV2Count(long now) {
int count = 0;

List<Account.UnFreezeV2> unFreezeV2List = account.getUnfrozenV2List();
for (Account.UnFreezeV2 item : unFreezeV2List) {
List<UnFreezeV2> unFreezeV2List = account.getUnfrozenV2List();
for (UnFreezeV2 item : unFreezeV2List) {
if (item.getUnfreezeExpireTime() > now) {
count++;
}
}

return count;
}

Expand Down Expand Up @@ -1063,8 +1050,7 @@ public void setFrozenForTronPower(long frozenBalance, long expireTime) {
}

public void addFrozenForTronPowerV2(long balance) {
Common.ResourceCode type = Common.ResourceCode.TRON_POWER;
this.addFrozenBalanceForResource(type, balance);
this.addFrozenBalanceForResource(TRON_POWER, balance);
}

public long getTronPowerFrozenBalance() {
Expand Down Expand Up @@ -1096,10 +1082,10 @@ public long getLatestConsumeTimeForEnergy() {
return this.account.getAccountResource().getLatestConsumeTimeForEnergy();
}

public void setLatestConsumeTimeForEnergy(long latest_time) {
public void setLatestConsumeTimeForEnergy(long latestTime) {
this.account = this.account.toBuilder()
.setAccountResource(
this.account.getAccountResource().toBuilder().setLatestConsumeTimeForEnergy(latest_time)
this.account.getAccountResource().toBuilder().setLatestConsumeTimeForEnergy(latestTime)
.build()).build();
}

Expand All @@ -1108,8 +1094,7 @@ public long getFreeNetUsage() {
}

public void setFreeNetUsage(long freeNetUsage) {
this.account = this.account.toBuilder()
.setFreeNetUsage(freeNetUsage).build();
this.account = this.account.toBuilder().setFreeNetUsage(freeNetUsage).build();
}

public boolean addAllFreeAssetNetUsageV2(Map<String, Long> map) {
Expand Down
57 changes: 32 additions & 25 deletions framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.tron.core.config.Parameter.DatabaseConstants.PROPOSAL_COUNT_LIMIT_MAX;
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.parseEnergyFee;
import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR;
import static org.tron.protos.contract.Common.ResourceCode;

import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
Expand Down Expand Up @@ -204,6 +205,8 @@
import org.tron.core.zen.note.OutgoingPlaintext;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Account.FreezeV2;
import org.tron.protos.Protocol.Account.UnFreezeV2;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.DelegatedResourceAccountIndex;
import org.tron.protos.Protocol.Exchange;
Expand Down Expand Up @@ -271,8 +274,8 @@ public class Wallet {

@Autowired
private NodeManager nodeManager;
private int minEffectiveConnection = Args.getInstance().getMinEffectiveConnection();
private boolean trxCacheEnable = Args.getInstance().isTrxCacheEnable();
private int minEffectiveConnection = CommonParameter.getInstance().getMinEffectiveConnection();
private boolean trxCacheEnable = CommonParameter.getInstance().isTrxCacheEnable();
public static final String CONTRACT_VALIDATE_EXCEPTION = "ContractValidateException: {}";
public static final String CONTRACT_VALIDATE_ERROR = "Contract validate error : ";

Expand All @@ -281,7 +284,7 @@ public class Wallet {
*/
public Wallet() {
this.cryptoEngine = SignUtils.getGeneratedRandomSign(Utils.getRandom(),
Args.getInstance().isECKeyCryptoEngine());
CommonParameter.getInstance().isECKeyCryptoEngine());
}

/**
Expand All @@ -308,24 +311,6 @@ public static void setAddressPreFixByte(byte addressPreFixByte) {
DecodeUtil.addressPreFixByte = addressPreFixByte;
}

// public ShieldAddress generateShieldAddress() {
// ShieldAddress.Builder builder = ShieldAddress.newBuilder();
// ShieldAddressGenerator shieldAddressGenerator = new ShieldAddressGenerator();
//
// byte[] privateKey = shieldAddressGenerator.generatePrivateKey();
// byte[] publicKey = shieldAddressGenerator.generatePublicKey(privateKey);
//
// byte[] privateKeyEnc = shieldAddressGenerator.generatePrivateKeyEnc(privateKey);
// byte[] publicKeyEnc = shieldAddressGenerator.generatePublicKeyEnc(privateKeyEnc);
//
// byte[] addPrivate = ByteUtil.merge(privateKey, privateKeyEnc);
// byte[] addPublic = ByteUtil.merge(publicKey, publicKeyEnc);
//
// builder.setPrivateAddress(ByteString.copyFrom(addPrivate));
// builder.setPublicAddress(ByteString.copyFrom(addPublic));
// return builder.build();
// }

public byte[] getAddress() {
return cryptoEngine.getAddress();
}
Expand All @@ -352,10 +337,32 @@ public Account getAccount(Account account) {
+ BLOCK_PRODUCED_INTERVAL * accountCapsule.getLatestConsumeFreeTime());
accountCapsule.setLatestConsumeTimeForEnergy(genesisTimeStamp
+ BLOCK_PRODUCED_INTERVAL * accountCapsule.getLatestConsumeTimeForEnergy());

sortFrozenV2List(accountCapsule);
return accountCapsule.getInstance();
}

private void sortFrozenV2List(AccountCapsule accountCapsule) {
List<FreezeV2> oldFreezeV2List = accountCapsule.getFrozenV2List();
accountCapsule.clearFrozenV2();
ResourceCode[] codes = ResourceCode.values();
for (ResourceCode code : codes) {
if (ResourceCode.UNRECOGNIZED != code) {
accountCapsule.addFrozenV2List(FreezeV2.newBuilder()
.setType(code)
.setAmount(0)
.build());
}
}
List<FreezeV2> newFreezeV2List = accountCapsule.getFrozenV2List();
for (int i = 0; i < newFreezeV2List.size(); i++) {
FreezeV2 freezeV2 = newFreezeV2List.get(i);
ResourceCode code = freezeV2.getType();
Optional<FreezeV2> optional = oldFreezeV2List
.stream().filter(o -> o.getType() == code).findFirst();
accountCapsule.updateFrozenV2List(i, optional.orElse(freezeV2));
}
}

public Account getAccountById(Account account) {
AccountStore accountStore = chainBaseManager.getAccountStore();
AccountIdIndexStore accountIdIndexStore = chainBaseManager.getAccountIdIndexStore();
Expand Down Expand Up @@ -784,15 +791,15 @@ public GrpcAPI.CanWithdrawUnfreezeAmountResponseMessage getCanWithdrawUnfreezeAm
timestamp = dynamicStore.getLatestBlockHeaderTimestamp();
}

List<Account.UnFreezeV2> unfrozenV2List = accountCapsule.getInstance().getUnfrozenV2List();
List<UnFreezeV2> unfrozenV2List = accountCapsule.getInstance().getUnfrozenV2List();
long finalTimestamp = timestamp;

canWithdrawUnfreezeAmount = unfrozenV2List
.stream()
.filter(unfrozenV2 ->
(unfrozenV2.getUnfreezeAmount() > 0
&& unfrozenV2.getUnfreezeExpireTime() <= finalTimestamp))
.mapToLong(Account.UnFreezeV2::getUnfreezeAmount)
.mapToLong(UnFreezeV2::getUnfreezeAmount)
.sum();


Expand Down Expand Up @@ -830,7 +837,7 @@ public GrpcAPI.GetAvailableUnfreezeCountResponseMessage getAvailableUnfreezeCoun
}
long now = dynamicStore.getLatestBlockHeaderTimestamp();

List<Account.UnFreezeV2> unfrozenV2List = accountCapsule.getInstance().getUnfrozenV2List();
List<UnFreezeV2> unfrozenV2List = accountCapsule.getInstance().getUnfrozenV2List();
long getUsedUnfreezeCount = unfrozenV2List
.stream()
.filter(unfrozenV2 ->
Expand Down

0 comments on commit 84849b4

Please sign in to comment.