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

Feature/new resource model #3726

Merged
merged 29 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
32d7d67
add protocol
sean-liu55 Mar 31, 2021
7ab03de
add proposal for new resource model
sean-liu55 Mar 31, 2021
063297c
handle VOTE_POWER in FreezeBalanceActuator.java
sean-liu55 Mar 31, 2021
d9e868e
handle VOTE_POWER in UnFreezeBalanceActuator.java
sean-liu55 Mar 31, 2021
2d5acdc
handle VOTE_POWER in VoteWitnessActuator.java 、StakeProcessor.java an…
sean-liu55 Mar 31, 2021
d724d20
Initialize/Invalidate OldVotePower
sean-liu55 Mar 31, 2021
ed075fd
change parameter name
sean-liu55 Mar 31, 2021
83a2f34
format code
sean-liu55 Mar 31, 2021
c647304
rm useless code
sean-liu55 Mar 31, 2021
656b776
rm useless code
sean-liu55 Mar 31, 2021
35fef4d
Merge branch 'release_v4.2' into feature/new_resource_model
sean-liu55 Apr 1, 2021
82df563
change clearing vote logic in UnfreezeBalanceActuator
sean-liu55 Apr 2, 2021
c24f86d
change parameter name
sean-liu55 Apr 2, 2021
1c4bd0e
vote_power to tron_power
sean-liu55 Apr 2, 2021
acb78ce
add test for new resource model
sean-liu55 Apr 2, 2021
8f7b3b9
optimize code
sean-liu55 Apr 2, 2021
13a7dfc
typo
Apr 2, 2021
7a58030
add test
sean-liu55 Apr 5, 2021
ee6c267
Merge remote-tracking branch 'origin/feature/new_resource_model' into…
sean-liu55 Apr 5, 2021
aa9c3f5
typo
Apr 6, 2021
fbbf092
fix test conflict
sean-liu55 Apr 6, 2021
070e67a
Merge remote-tracking branch 'origin/feature/new_resource_model' into…
sean-liu55 Apr 6, 2021
0334e99
fix typo
sean-liu55 Apr 6, 2021
4282f9c
typo
Apr 6, 2021
efb33f0
add method to query tron power
sean-liu55 Apr 6, 2021
9a0d85f
Merge remote-tracking branch 'origin/feature/new_resource_model' into…
sean-liu55 Apr 6, 2021
bc01753
add tronPowerUsed into AccountResourceMessage
sean-liu55 Apr 6, 2021
81d93ca
add tronPowerUsed into AccountResourceMessage
sean-liu55 Apr 6, 2021
e60420b
change method name
sean-liu55 Apr 6, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public boolean execute(Object result) throws ContractExeException {
AccountCapsule accountCapsule = accountStore
.get(freezeBalanceContract.getOwnerAddress().toByteArray());

if (dynamicStore.supportAllowNewResourceModel()
&& accountCapsule.oldTronPowerIsNotInitialized()) {
accountCapsule.initializeOldTronPower();
}

long now = dynamicStore.getLatestBlockHeaderTimestamp();
long duration = freezeBalanceContract.getFrozenDuration() * FROZEN_PERIOD;

Expand Down Expand Up @@ -98,6 +103,14 @@ public boolean execute(Object result) throws ContractExeException {
dynamicStore
.addTotalEnergyWeight(frozenBalance / TRX_PRECISION);
break;
case TRON_POWER:
long newFrozenBalanceForTronPower =
frozenBalance + accountCapsule.getTronPowerFrozenBalance();
accountCapsule.setFrozenForTronPower(newFrozenBalanceForTronPower, expireTime);

dynamicStore
.addTotalTronPowerWeight(frozenBalance / TRX_PRECISION);
break;
default:
logger.debug("Resource Code Error.");
}
Expand Down Expand Up @@ -182,12 +195,28 @@ public boolean validate() throws ContractValidateException {

switch (freezeBalanceContract.getResource()) {
case BANDWIDTH:
break;
case ENERGY:
break;
case TRON_POWER:
if (dynamicStore.supportAllowNewResourceModel()) {
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();
if (!ArrayUtils.isEmpty(receiverAddress)) {
throw new ContractValidateException(
"TRON_POWER is not allowed to delegate to other accounts.");
}
} else {
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
}
break;
default:
throw new ContractValidateException(
"ResourceCode error,valid ResourceCode[BANDWIDTH、ENERGY]");
if (dynamicStore.supportAllowNewResourceModel()) {
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY、TRON_POWER]");
} else {
throw new ContractValidateException(
"ResourceCode error, valid ResourceCode[BANDWIDTH、ENERGY]");
}
}

//todo:need version control and config for delegating resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public boolean execute(Object result) throws ContractExeException {

long unfreezeBalance = 0L;

if (dynamicStore.supportAllowNewResourceModel()
&& accountCapsule.oldTronPowerIsNotInitialized()) {
accountCapsule.initializeOldTronPower();
}

byte[] receiverAddress = unfreezeBalanceContract.getReceiverAddress().toByteArray();
//If the receiver is not included in the contract, unfreeze frozen balance for this account.
//otherwise,unfreeze delegated frozen balance provided this account.
Expand Down Expand Up @@ -195,7 +200,12 @@ public boolean execute(Object result) throws ContractExeException {
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
.setBalance(oldBalance + unfreezeBalance)
.setAccountResource(newAccountResource).build());

break;
case TRON_POWER:
unfreezeBalance = accountCapsule.getTronPowerFrozenBalance();
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
.setBalance(oldBalance + unfreezeBalance)
.clearTronPower().build());
break;
default:
//this should never happen
Expand All @@ -213,24 +223,47 @@ public boolean execute(Object result) throws ContractExeException {
dynamicStore
.addTotalEnergyWeight(-unfreezeBalance / TRX_PRECISION);
break;
case TRON_POWER:
dynamicStore
.addTotalTronPowerWeight(-unfreezeBalance / TRX_PRECISION);
break;
default:
//this should never happen
break;
}

VotesCapsule votesCapsule;
if (!votesStore.has(ownerAddress)) {
votesCapsule = new VotesCapsule(unfreezeBalanceContract.getOwnerAddress(),
accountCapsule.getVotesList());
} else {
votesCapsule = votesStore.get(ownerAddress);
boolean needToClearVote = true;
if (dynamicStore.supportAllowNewResourceModel()
&& accountCapsule.oldTronPowerIsInvalid()) {
switch (unfreezeBalanceContract.getResource()) {
case BANDWIDTH:
case ENERGY:
needToClearVote = false;
break;
default:
break;
}
}
accountCapsule.clearVotes();
votesCapsule.clearNewVotes();

accountStore.put(ownerAddress, accountCapsule);
if (needToClearVote) {
VotesCapsule votesCapsule;
if (!votesStore.has(ownerAddress)) {
votesCapsule = new VotesCapsule(unfreezeBalanceContract.getOwnerAddress(),
accountCapsule.getVotesList());
} else {
votesCapsule = votesStore.get(ownerAddress);
}
accountCapsule.clearVotes();
votesCapsule.clearNewVotes();
votesStore.put(ownerAddress, votesCapsule);
}

votesStore.put(ownerAddress, votesCapsule);
if (dynamicStore.supportAllowNewResourceModel()
&& !accountCapsule.oldTronPowerIsInvalid()) {
accountCapsule.invalidateOldTronPower();
}

accountStore.put(ownerAddress, accountCapsule);

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

break;
case TRON_POWER:
if (dynamicStore.supportAllowNewResourceModel()) {
Frozen frozenBalanceForTronPower = accountCapsule.getInstance().getTronPower();
if (frozenBalanceForTronPower.getFrozenBalance() <= 0) {
throw new ContractValidateException("no frozenBalance(TronPower)");
}
if (frozenBalanceForTronPower.getExpireTime() > now) {
throw new ContractValidateException("It's not time to unfreeze(TronPower).");
}
} else {
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
}
break;
default:
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
if (dynamicStore.supportAllowNewResourceModel()) {
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy、TRON_POWER]");
} else {
throw new ContractValidateException(
"ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.tron.core.exception.ContractValidateException;
import org.tron.core.service.MortgageService;
import org.tron.core.store.AccountStore;
import org.tron.core.store.DynamicPropertiesStore;
import org.tron.core.store.VotesStore;
import org.tron.core.store.WitnessStore;
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
Expand Down Expand Up @@ -125,7 +126,13 @@ public boolean validate() throws ContractValidateException {
ACCOUNT_EXCEPTION_STR + readableOwnerAddress + NOT_EXIST_STR);
}

long tronPower = accountCapsule.getTronPower();
long tronPower;
DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
if (dynamicStore.supportAllowNewResourceModel()) {
tronPower = accountCapsule.getAllTronPower();
} else {
tronPower = accountCapsule.getTronPower();
}

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

AccountCapsule accountCapsule = accountStore.get(ownerAddress);

DynamicPropertiesStore dynamicStore = chainBaseManager.getDynamicPropertiesStore();
if (dynamicStore.supportAllowNewResourceModel()
&& accountCapsule.oldTronPowerIsNotInitialized()) {
accountCapsule.initializeOldTronPower();
}

if (!votesStore.has(ownerAddress)) {
votesCapsule = new VotesCapsule(voteContract.getOwnerAddress(),
accountCapsule.getVotesList());
Expand Down
17 changes: 16 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 @@ -444,6 +444,20 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
}
break;
}
case ALLOW_NEW_RESOURCE_MODEL: {
if (!forkController.pass(ForkBlockVersionEnum.VERSION_4_2)) {
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_NEW_RESOURCE_MODEL]");
}
if (value != 1 && value != 0) {
throw new ContractValidateException(
"This value[ALLOW_NEW_RESOURCE_MODEL] is only allowed to be 1 or 0");
}
break;
}



default:
break;
}
Expand Down Expand Up @@ -497,7 +511,8 @@ public enum ProposalType { // current value, value range
MAX_FEE_LIMIT(47), // [0, 10_000_000_000]
ALLOW_TRANSACTION_FEE_POOL(48), // 0, 1
ALLOW_BLACKHOLE_OPTIMIZATION(49),// 0,1
ALLOW_RECEIPTS_MERKLE_ROOT(50);// 0,1
ALLOW_RECEIPTS_MERKLE_ROOT(50),// 0,1
ALLOW_NEW_RESOURCE_MODEL(51);// 0,1

private long code;

Expand Down
64 changes: 64 additions & 0 deletions chainbase/src/main/java/org/tron/core/capsule/AccountCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ public List<Vote> getVotesList() {
}
}

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

//tp:Tron_Power
public long getTronPower() {
long tp = 0;
Expand All @@ -469,6 +477,16 @@ public long getTronPower() {
return tp;
}

public long getAllTronPower() {
if (account.getOldTronPower() == -1) {
return getTronPowerFrozenBalance();
} else if (account.getOldTronPower() == 0) {
return getTronPower() + getTronPowerFrozenBalance();
} else {
return account.getOldTronPower() + getTronPowerFrozenBalance();
}
}

/**
* asset balance enough
*/
Expand Down Expand Up @@ -836,6 +854,52 @@ public long getEnergyFrozenBalance() {
return this.account.getAccountResource().getFrozenBalanceForEnergy().getFrozenBalance();
}

public boolean oldTronPowerIsNotInitialized() {
return this.account.getOldTronPower() == 0;
}

public boolean oldTronPowerIsInvalid() {
return this.account.getOldTronPower() == -1;
}

public void initializeOldTronPower() {
long value = getTronPower();
if (value == 0) {
value = -1;
}
setInstance(getInstance().toBuilder()
.setOldTronPower(value)
.build());
}

public void invalidateOldTronPower() {
setInstance(getInstance().toBuilder()
.setOldTronPower(-1)
.build());
}


public void setOldTronPower(long value) {
setInstance(getInstance().toBuilder()
.setOldTronPower(value)
.build());
}

public void setFrozenForTronPower(long frozenBalance, long expireTime) {
Frozen newFrozen = Frozen.newBuilder()
.setFrozenBalance(frozenBalance)
.setExpireTime(expireTime)
.build();

setInstance(getInstance().toBuilder()
.setTronPower(newFrozen)
.build());
}

public long getTronPowerFrozenBalance() {
return this.account.getTronPower().getFrozenBalance();
}

public long getEnergyUsage() {
return this.account.getAccountResource().getEnergyUsage();
}
Expand Down
Loading