Skip to content

Commit

Permalink
mdf DelegateResourceCapsule; mdf FreezeBalanceActuator
Browse files Browse the repository at this point in the history
  • Loading branch information
nanfengpo committed Oct 26, 2018
1 parent 1da2569 commit c4a2946
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 77 deletions.
118 changes: 63 additions & 55 deletions src/main/java/org/tron/core/actuator/FreezeBalanceActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.common.utils.StringUtil;
import org.tron.core.Wallet;
import org.tron.core.capsule.AccountCapsule;
Expand All @@ -14,8 +15,6 @@
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
import org.tron.protos.Contract.FreezeBalanceContract;
import org.tron.protos.Protocol.Account.AccountResource;
import org.tron.protos.Protocol.Account.Frozen;
import org.tron.protos.Protocol.Transaction.Result.code;

@Slf4j
Expand Down Expand Up @@ -44,69 +43,45 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException

long newBalance = accountCapsule.getBalance() - freezeBalanceContract.getFrozenBalance();

long frozenBalanceForEnergy = 0;
long frozenBalanceForBandwidth = 0;
long frozenBalance = freezeBalanceContract.getFrozenBalance();
long expireTime = now + duration;
byte[] ownerAddress = freezeBalanceContract.getOwnerAddress().toByteArray();
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();

switch (freezeBalanceContract.getResource()) {
case BANDWIDTH:
frozenBalanceForBandwidth = freezeBalanceContract.getFrozenBalance();
if (ArrayUtils.isEmpty(receiverAddress)) {
long newFrozenBalanceForBandwidth =
frozenBalance + accountCapsule.getFrozenBalance();
accountCapsule.setFrozenForBandwidth(newFrozenBalanceForBandwidth, expireTime);
} else {
delegateResource(ownerAddress, receiverAddress, true,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForBandwidth(frozenBalance);
}
dbManager.getDynamicPropertiesStore()
.addTotalNetWeight(frozenBalance / 1000_000L);
break;
case ENERGY:
frozenBalanceForEnergy = freezeBalanceContract.getFrozenBalance();
if (receiverAddress.length == 0) {
long newFrozenBalanceForEnergy =
frozenBalance + accountCapsule.getAccountResource()
.getFrozenBalanceForEnergy()
.getFrozenBalance();
accountCapsule.setFrozenForEnergy(newFrozenBalanceForEnergy, expireTime);
} else {
delegateResource(ownerAddress, receiverAddress, false,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForEnergy(frozenBalance);
}
dbManager.getDynamicPropertiesStore()
.addTotalEnergyWeight(frozenBalance / 1000_000L);
break;
}

byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();

if (receiverAddress.length == 0) {
//If the receiver is not included in the contract, the owner will receive the resource.
long newFrozenBalanceForEnergy =
frozenBalanceForEnergy + accountCapsule.getAccountResource()
.getFrozenBalanceForEnergy()
.getFrozenBalance();
long newFrozenBalanceForBandwidth =
frozenBalanceForBandwidth + accountCapsule.getFrozenBalance();

accountCapsule.setFrozenForEnergy(newFrozenBalanceForEnergy, expireTime);
accountCapsule.setFrozenForBandwidth(newFrozenBalanceForBandwidth, expireTime);
} else {
//If the receiver is included in the contract, the receiver will receive the resource.
byte[] key = DelegatedResourceCapsule
.createDbKey(freezeBalanceContract.getOwnerAddress().toByteArray(),
freezeBalanceContract.getReceiverAddress().toByteArray());
DelegatedResourceCapsule delegatedResourceCapsule = dbManager.getDelegatedResourceStore()
.get(key);
if (delegatedResourceCapsule != null) {
delegatedResourceCapsule
.addResource(frozenBalanceForBandwidth, frozenBalanceForEnergy, expireTime);
} else {
delegatedResourceCapsule = new DelegatedResourceCapsule(
freezeBalanceContract.getOwnerAddress(),
freezeBalanceContract.getReceiverAddress(),
frozenBalanceForEnergy,
frozenBalanceForBandwidth,
expireTime
);
}
dbManager.getDelegatedResourceStore().put(key, delegatedResourceCapsule);

AccountCapsule receiverCapsule = dbManager.getAccountStore().get(receiverAddress);
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(frozenBalanceForEnergy);
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(frozenBalanceForBandwidth);
dbManager.getAccountStore().put(receiverCapsule.createDbKey(), receiverCapsule);

accountCapsule.addDelegatedFrozenBalanceForEnergy(frozenBalanceForEnergy);
accountCapsule.addDelegatedFrozenBalanceForBandwidth(frozenBalanceForBandwidth);
}

accountCapsule.setBalance(newBalance);
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);

dbManager.getDynamicPropertiesStore()
.addTotalNetWeight(frozenBalanceForBandwidth / 1000_000L);
dbManager.getDynamicPropertiesStore()
.addTotalEnergyWeight(frozenBalanceForEnergy / 1000_000L);

ret.setStatus(fee, code.SUCESS);

return true;
Expand Down Expand Up @@ -190,7 +165,7 @@ public boolean validate() throws ContractValidateException {
//todo:need version control and config for delegating resource
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();
//If the receiver is included in the contract, the receiver will receive the resource.
if (receiverAddress.length != 0) {
if (!ArrayUtils.isEmpty(receiverAddress)) {
if (Arrays.equals(receiverAddress, ownerAddress)) {
throw new ContractValidateException(
"receiverAddress must not be the same as ownerAddress");
Expand Down Expand Up @@ -222,4 +197,37 @@ public long calcFee() {
return 0;
}

private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
long balance, long expireTime) {
byte[] key = DelegatedResourceCapsule.createDbKey(ownerAddress, receiverAddress);
DelegatedResourceCapsule delegatedResourceCapsule = dbManager.getDelegatedResourceStore()
.get(key);
if (delegatedResourceCapsule != null) {
if (isBandwidth) {
delegatedResourceCapsule.addFrozenBalanceForBandwidth(balance, expireTime);
} else {
delegatedResourceCapsule.addFrozenBalanceForEnergy(balance, expireTime);
}
} else {
delegatedResourceCapsule = new DelegatedResourceCapsule(
ByteString.copyFrom(ownerAddress),
ByteString.copyFrom(receiverAddress));
if (isBandwidth) {
delegatedResourceCapsule.setFrozenBalanceForBandwidth(balance, expireTime);
} else {
delegatedResourceCapsule.setFrozenBalanceForEnergy(balance, expireTime);
}
}
dbManager.getDelegatedResourceStore().put(key, delegatedResourceCapsule);

AccountCapsule receiverCapsule = dbManager.getAccountStore().get(receiverAddress);
if (isBandwidth) {
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(balance);
} else {
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(balance);
}

dbManager.getAccountStore().put(receiverCapsule.createDbKey(), receiverCapsule);
}

}
56 changes: 34 additions & 22 deletions src/main/java/org/tron/core/capsule/DelegatedResourceCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ public DelegatedResourceCapsule(final byte[] data) {
}
}

public DelegatedResourceCapsule(ByteString from, ByteString to, long energy, long bandwidth,
long expireTime) {
public DelegatedResourceCapsule(ByteString from, ByteString to) {
this.delegatedResource = DelegatedResource.newBuilder()
.setFrom(from)
.setTo(to)
.setFrozenBalanceForEnergy(energy)
.setFrozenBalanceForBandwidth(bandwidth)
.setExpireTime(expireTime)
.build();
}

Expand All @@ -46,41 +42,57 @@ public long getFrozenBalanceForEnergy() {
return this.delegatedResource.getFrozenBalanceForEnergy();
}

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

public void addFrozenBalanceForEnergy(long energy) {
public void addFrozenBalanceForEnergy(long energy, long expireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setFrozenBalanceForEnergy(this.delegatedResource.getFrozenBalanceForEnergy() + energy).build();
.setFrozenBalanceForEnergy(this.delegatedResource.getFrozenBalanceForEnergy() + energy)
.setExpireTimeForEnergy(expireTime)
.build();
}

public long getFrozenBalanceForBandwidth() {
return this.delegatedResource.getFrozenBalanceForBandwidth();
}

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

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

public void addResource(long Bandwidth, long energy, long ExpireTime) {
this.delegatedResource = this.delegatedResource.toBuilder()
.setFrozenBalanceForBandwidth(this.delegatedResource.getFrozenBalanceForBandwidth() + Bandwidth)
.setFrozenBalanceForEnergy(this.delegatedResource.getFrozenBalanceForEnergy() + energy)
.setExpireTime(ExpireTime).build();
public long getExpireTimeForBandwidth() {
return this.delegatedResource.getExpireTimeForBandwidth();
}

public long getExpireTimeForEnergy() {
return this.delegatedResource.getExpireTimeForBandwidth();
}

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

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

public byte[] createDbKey() {
Expand Down

0 comments on commit c4a2946

Please sign in to comment.