Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(freezeV2): optimize stake2.0 code #4798

Merged
merged 26 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
23e27d9
feat(memo-fee): add a proposal for memo fee
zhang0125 Nov 1, 2022
8a3877b
feat(memo-fee): add a interface of memo fee
zhang0125 Nov 2, 2022
1e952f3
Merge pull request #4758 from tronprotocol/feature/memo_fee
zhang0125 Nov 3, 2022
5711a01
func(proposal): optimize proposal check
yanghang8612 Nov 3, 2022
b9b39b7
fix(test): some related unit tests
yanghang8612 Nov 3, 2022
e77b3d5
Merge pull request #4768 from tronprotocol/feature/optimize_proposal_…
CodeNinjaEvan Nov 4, 2022
344d149
fix(mechanism): fix deflation or expansion problems
lxcmyf Nov 3, 2022
005709e
Merge pull request #4769 from lxcmyf/release_4.6.0
zhang0125 Nov 4, 2022
aa08143
fix(mechanism): optimize deflation problems
lxcmyf Nov 7, 2022
87032e2
Merge pull request #4772 from lxcmyf/release_4.6.0
zhang0125 Nov 8, 2022
3033ed3
fix(mechanism): optimize freezing precision query
lxcmyf Nov 8, 2022
3fcf8b0
Merge pull request #4776 from lxcmyf/release_4.6.0
zhang0125 Nov 9, 2022
d63d9e1
fix(mechanism): optimize total weight when unfreeze owner
lxcmyf Nov 10, 2022
ad1c591
Merge pull request #4780 from lxcmyf/release_4.6.0
zhang0125 Nov 10, 2022
afee145
fix(mechanism): optimize name of proposal
lxcmyf Nov 10, 2022
aeb108e
Merge pull request #4784 from lxcmyf/release_4.6.0
halibobo1205 Nov 11, 2022
342d242
feat(delegate): optimize delegate index storage
zhang0125 Nov 12, 2022
5ac7f0c
feat(delegate): add test for delegate optimization
zhang0125 Nov 14, 2022
2c2c137
Merge pull request #4788 from tronprotocol/feature/optimize_delegates
zhang0125 Nov 14, 2022
da34af1
feat(CONTRIBUTING.md): update CONTRIBUTING.md
forfreeday Nov 14, 2022
34ab92b
Merge pull request #4789 from forfreeday/feature/update_contributing-md
forfreeday Nov 14, 2022
5ebad2a
feat(version): update Version
forfreeday Nov 18, 2022
f724e05
Merge pull request #4797 from forfreeday/feature/update_version
forfreeday Nov 18, 2022
429f433
fix(freezeV2): optimize stake2.0 code
lxcmyf Nov 18, 2022
9b20432
Merge remote-tracking branch 'origin/release_4.6.0' into feature/stak…
lxcmyf Nov 22, 2022
3dd7415
Merge remote-tracking branch 'origin/release_4.6.0' into feature/stak…
lxcmyf Nov 22, 2022
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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ If you want to contribute codes to java-tron, please follow the following steps:

Submit a pull request (PR) from your repository to `tronprotocol/java-tron`.
Please be sure to click on the link in the red box shown below. Select the base branch for tronprotocol and the compare branch for your personal fork repository.
![](https://codimd.s3.shivering-isles.com/demo/uploads/e24435ab42e4287d9369a2136.png)
![image](https://raw.githubusercontent.com/tronprotocol/documentation-en/master/images/javatron_pr.png)



## Code Review Guidelines
The only way to get code into java-tron is to send a pull request. Those pull requests need to be reviewed by someone. there a guide that explains our expectations around PRs for both authors and reviewers.
The only way to get code into java-tron is to send a pull request. Those pull requests need to be reviewed by someone. The following guide explains our expectations around PRs for both authors and reviewers.

### Terminology
- The author of a pull request is the entity who wrote the diff and submitted it to GitHub.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tron.core.actuator;

import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
Expand All @@ -16,13 +18,10 @@
import org.tron.core.store.AbiStore;
import org.tron.core.store.AccountStore;
import org.tron.core.store.ContractStore;
import org.tron.core.vm.config.VMConfig;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
import org.tron.protos.Protocol.Transaction.Result.code;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI;
import org.tron.protos.contract.SmartContractOuterClass.ClearABIContract;

import static org.tron.core.actuator.ActuatorConstant.NOT_EXIST_STR;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract.ABI;

@Slf4j(topic = "actuator")
public class ClearABIContractActuator extends AbstractActuator {
Expand Down Expand Up @@ -57,17 +56,16 @@ public boolean execute(Object result) throws ContractExeException {

@Override
public boolean validate() throws ContractValidateException {
if (!VMConfig.allowTvmConstantinople()) {
throw new ContractValidateException(
"contract type error,unexpected type [ClearABIContract]");
}

if (this.any == null) {
throw new ContractValidateException(ActuatorConstant.CONTRACT_NOT_EXIST);
}
if (chainBaseManager == null) {
throw new ContractValidateException("No account store or contract store!");
}
if (chainBaseManager.getDynamicPropertiesStore().getAllowTvmConstantinople() == 0) {
throw new ContractValidateException(
"contract type error,unexpected type [ClearABIContract]");
}
AccountStore accountStore = chainBaseManager.getAccountStore();
ContractStore contractStore = chainBaseManager.getContractStore();
if (!this.any.is(ClearABIContract.class)) {
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.NOT_EXIST_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.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
Expand Down Expand Up @@ -72,44 +76,48 @@ public boolean execute(Object result) throws ContractExeException {
byte[] ownerAddress = freezeBalanceContract.getOwnerAddress().toByteArray();
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();

long increment;
switch (freezeBalanceContract.getResource()) {
case BANDWIDTH:
if (!ArrayUtils.isEmpty(receiverAddress)
&& dynamicStore.supportDR()) {
delegateResource(ownerAddress, receiverAddress, true,
frozenBalance, expireTime);
increment = delegateResource(ownerAddress, receiverAddress, true,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForBandwidth(frozenBalance);
} else {
long oldNetWeight = accountCapsule.getFrozenBalance() / TRX_PRECISION;
long newFrozenBalanceForBandwidth =
frozenBalance + accountCapsule.getFrozenBalance();
accountCapsule.setFrozenForBandwidth(newFrozenBalanceForBandwidth, expireTime);
long newNetWeight = accountCapsule.getFrozenBalance() / TRX_PRECISION;
increment = newNetWeight - oldNetWeight;
}
dynamicStore
.addTotalNetWeight(frozenBalance / TRX_PRECISION);
addTotalWeight(BANDWIDTH, dynamicStore, frozenBalance, increment);
break;
case ENERGY:
if (!ArrayUtils.isEmpty(receiverAddress)
&& dynamicStore.supportDR()) {
delegateResource(ownerAddress, receiverAddress, false,
frozenBalance, expireTime);
increment = delegateResource(ownerAddress, receiverAddress, false,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForEnergy(frozenBalance);
} else {
long oldEnergyWeight = accountCapsule.getEnergyFrozenBalance() / TRX_PRECISION;
long newFrozenBalanceForEnergy =
frozenBalance + accountCapsule.getAccountResource()
.getFrozenBalanceForEnergy()
.getFrozenBalance();
frozenBalance + accountCapsule.getEnergyFrozenBalance();
accountCapsule.setFrozenForEnergy(newFrozenBalanceForEnergy, expireTime);
long newEnergyWeight = accountCapsule.getEnergyFrozenBalance() / TRX_PRECISION;
increment = newEnergyWeight - oldEnergyWeight;
}
dynamicStore
.addTotalEnergyWeight(frozenBalance / TRX_PRECISION);
addTotalWeight(ENERGY, dynamicStore, frozenBalance, increment);
break;
case TRON_POWER:
long oldTPWeight = accountCapsule.getTronPowerFrozenBalance() / TRX_PRECISION;
long newFrozenBalanceForTronPower =
frozenBalance + accountCapsule.getTronPowerFrozenBalance();
accountCapsule.setFrozenForTronPower(newFrozenBalanceForTronPower, expireTime);

dynamicStore
.addTotalTronPowerWeight(frozenBalance / TRX_PRECISION);
long newTPWeight = accountCapsule.getTronPowerFrozenBalance() / TRX_PRECISION;
increment = newTPWeight - oldTPWeight;
addTotalWeight(TRON_POWER, dynamicStore, frozenBalance, increment);
break;
default:
logger.debug("Resource Code Error.");
Expand All @@ -123,6 +131,23 @@ public boolean execute(Object result) throws ContractExeException {
return true;
}

private void addTotalWeight(ResourceCode resourceCode, DynamicPropertiesStore dynamicStore,
long frozenBalance, long increment) {
long weight = dynamicStore.allowNewReward() ? increment : frozenBalance / TRX_PRECISION;
switch (resourceCode) {
case BANDWIDTH:
dynamicStore.addTotalNetWeight(weight);
break;
case ENERGY:
dynamicStore.addTotalEnergyWeight(weight);
break;
case TRON_POWER:
dynamicStore.addTotalTronPowerWeight(weight);
break;
default:
logger.debug("Resource Code Error.");
}
}

@Override
public boolean validate() throws ContractValidateException {
Expand Down Expand Up @@ -175,11 +200,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 +244,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 Expand Up @@ -267,9 +286,10 @@ public long calcFee() {
return 0;
}

private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
private long delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
long balance, long expireTime) {
AccountStore accountStore = chainBaseManager.getAccountStore();
DynamicPropertiesStore dynamicPropertiesStore = chainBaseManager.getDynamicPropertiesStore();
DelegatedResourceStore delegatedResourceStore = chainBaseManager.getDelegatedResourceStore();
DelegatedResourceAccountIndexStore delegatedResourceAccountIndexStore = chainBaseManager
.getDelegatedResourceAccountIndexStore();
Expand Down Expand Up @@ -297,46 +317,56 @@ private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boole
delegatedResourceStore.put(key, delegatedResourceCapsule);

//modify DelegatedResourceAccountIndexStore
{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
.get(ownerAddress);
if (delegatedResourceAccountIndexCapsule == null) {
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
if (!dynamicPropertiesStore.supportAllowDelegateOptimization()) {

DelegatedResourceAccountIndexCapsule ownerIndexCapsule =
delegatedResourceAccountIndexStore.get(ownerAddress);
if (ownerIndexCapsule == null) {
ownerIndexCapsule = new DelegatedResourceAccountIndexCapsule(
ByteString.copyFrom(ownerAddress));
}
List<ByteString> toAccountsList = delegatedResourceAccountIndexCapsule.getToAccountsList();
List<ByteString> toAccountsList = ownerIndexCapsule.getToAccountsList();
if (!toAccountsList.contains(ByteString.copyFrom(receiverAddress))) {
delegatedResourceAccountIndexCapsule.addToAccount(ByteString.copyFrom(receiverAddress));
ownerIndexCapsule.addToAccount(ByteString.copyFrom(receiverAddress));
}
delegatedResourceAccountIndexStore
.put(ownerAddress, delegatedResourceAccountIndexCapsule);
}
delegatedResourceAccountIndexStore.put(ownerAddress, ownerIndexCapsule);

{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = delegatedResourceAccountIndexStore
.get(receiverAddress);
if (delegatedResourceAccountIndexCapsule == null) {
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
DelegatedResourceAccountIndexCapsule receiverIndexCapsule
= delegatedResourceAccountIndexStore.get(receiverAddress);
if (receiverIndexCapsule == null) {
receiverIndexCapsule = new DelegatedResourceAccountIndexCapsule(
ByteString.copyFrom(receiverAddress));
}
List<ByteString> fromAccountsList = delegatedResourceAccountIndexCapsule
List<ByteString> fromAccountsList = receiverIndexCapsule
.getFromAccountsList();
if (!fromAccountsList.contains(ByteString.copyFrom(ownerAddress))) {
delegatedResourceAccountIndexCapsule.addFromAccount(ByteString.copyFrom(ownerAddress));
receiverIndexCapsule.addFromAccount(ByteString.copyFrom(ownerAddress));
}
delegatedResourceAccountIndexStore
.put(receiverAddress, delegatedResourceAccountIndexCapsule);
delegatedResourceAccountIndexStore.put(receiverAddress, receiverIndexCapsule);

} else {
// modify DelegatedResourceAccountIndexStore new
delegatedResourceAccountIndexStore.convert(ownerAddress);
delegatedResourceAccountIndexStore.convert(receiverAddress);
delegatedResourceAccountIndexStore.delegate(ownerAddress, receiverAddress,
dynamicPropertiesStore.getLatestBlockHeaderTimestamp());
}

//modify AccountStore
AccountCapsule receiverCapsule = accountStore.get(receiverAddress);
long oldWeight;
long newWeight;
if (isBandwidth) {
oldWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() / TRX_PRECISION;
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(balance);
newWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForBandwidth() / TRX_PRECISION;
} else {
oldWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() / TRX_PRECISION;
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(balance);
newWeight = receiverCapsule.getAcquiredDelegatedFrozenBalanceForEnergy() / TRX_PRECISION;
}

accountStore.put(receiverCapsule.createDbKey(), receiverCapsule);
return newWeight - oldWeight;
}

}
Loading