Skip to content

Commit

Permalink
Merge branch 'Odyssey_v3.2' of https://github.com/tronprotocol/java-tron
Browse files Browse the repository at this point in the history
 into Odyssey_v3.2
  • Loading branch information
Yrp committed Oct 30, 2018
2 parents 42e33b5 + 451101b commit f2f89a2
Show file tree
Hide file tree
Showing 23 changed files with 1,356 additions and 138 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/tron/common/runtime/RuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ private long getEnergyLimit(AccountCapsule account, long feeLimit, long callValu
.floorDiv(max(account.getBalance() - callValue, 0), sunPerEnergy);

long energyFromFeeLimit;
long totalBalanceForEnergyFreeze = account.getAccountResource().getFrozenBalanceForEnergy()
.getFrozenBalance();
long totalBalanceForEnergyFreeze = account.getAllFrozenBalanceForEnergy();
if (0 == totalBalanceForEnergyFreeze) {
energyFromFeeLimit =
feeLimit / sunPerEnergy;
} else {
long totalEnergyFromFreeze = energyProcessor
.calculateGlobalEnergyLimit(totalBalanceForEnergyFreeze);
.calculateGlobalEnergyLimit(account);
long leftBalanceForEnergyFreeze = getEnergyFee(totalBalanceForEnergyFreeze,
leftEnergyFromFreeze,
totalEnergyFromFreeze);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/tron/common/storage/DepositImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.tron.core.db.BlockStore;
import org.tron.core.db.CodeStore;
import org.tron.core.db.ContractStore;
import org.tron.core.db.DelegatedResourceStore;
import org.tron.core.db.DynamicPropertiesStore;
import org.tron.core.db.Manager;
import org.tron.core.db.ProposalStore;
Expand Down Expand Up @@ -114,6 +115,11 @@ private StorageRowStore getStorageRowStore() {
return dbManager.getStorageRowStore();
}

private DelegatedResourceStore getDelegatedResourceStore() {
return dbManager.getDelegatedResourceStore();
}


private AssetIssueStore getAssetIssueStore() {
return dbManager.getAssetIssueStore();
}
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.tron.api.GrpcAPI.Address;
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.api.GrpcAPI.BlockList;
import org.tron.api.GrpcAPI.DelegatedResourceList;
import org.tron.api.GrpcAPI.ExchangeList;
import org.tron.api.GrpcAPI.Node;
import org.tron.api.GrpcAPI.NodeList;
Expand Down Expand Up @@ -75,6 +76,7 @@
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.capsule.DelegatedResourceCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.capsule.ProposalCapsule;
import org.tron.core.capsule.TransactionCapsule;
Expand Down Expand Up @@ -111,6 +113,7 @@
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.DelegatedResourceAccountIndex;
import org.tron.protos.Protocol.Exchange;
import org.tron.protos.Protocol.Proposal;
import org.tron.protos.Protocol.SmartContract;
Expand Down Expand Up @@ -523,6 +526,23 @@ public ProposalList getProposalList() {
return builder.build();
}

public DelegatedResourceList getDelegatedResource(ByteString fromAddress, ByteString toAddress) {
DelegatedResourceList.Builder builder = DelegatedResourceList.newBuilder();
byte[] dbKey = DelegatedResourceCapsule
.createDbKey(fromAddress.toByteArray(), toAddress.toByteArray());
DelegatedResourceCapsule delegatedResourceCapsule = dbManager.getDelegatedResourceStore()
.get(dbKey);
if (delegatedResourceCapsule != null) {
builder.addDelegatedResource(delegatedResourceCapsule.getInstance());
}
return builder.build();
}

public DelegatedResourceAccountIndex getDelegatedResourceAccountIndex(ByteString address) {
return dbManager.getDelegatedResourceAccountIndexStore()
.get(address.toByteArray()).getInstance();
}

public ExchangeList getExchangeList() {
ExchangeList.Builder builder = ExchangeList.newBuilder();
List<ExchangeCapsule> exchangeCapsuleList = dbManager.getExchangeStore().getAllExchanges();
Expand Down Expand Up @@ -605,7 +625,8 @@ public AccountNetMessage getAccountNet(ByteString accountAddress) {
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
processor.updateUsage(accountCapsule);

long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
long netLimit = processor
.calculateGlobalNetLimit(accountCapsule);
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
Expand Down Expand Up @@ -643,12 +664,13 @@ public AccountResourceMessage getAccountResource(ByteString accountAddress) {
EnergyProcessor energyProcessor = new EnergyProcessor(dbManager);
energyProcessor.updateUsage(accountCapsule);

long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
long netLimit = processor
.calculateGlobalNetLimit(accountCapsule);
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
long energyLimit = energyProcessor
.calculateGlobalEnergyLimit(accountCapsule.getEnergyFrozenBalance());
.calculateGlobalEnergyLimit(accountCapsule);
long totalEnergyLimit = dbManager.getDynamicPropertiesStore().getTotalEnergyLimit();
long totalEnergyWeight = dbManager.getDynamicPropertiesStore().getTotalEnergyWeight();

Expand Down
172 changes: 128 additions & 44 deletions src/main/java/org/tron/core/actuator/FreezeBalanceActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
import java.util.List;
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;
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
import org.tron.core.capsule.DelegatedResourceCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.db.Manager;
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 @@ -42,59 +45,43 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException

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

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

switch (freezeBalanceContract.getResource()) {
case BANDWIDTH:
long currentFrozenBalance = accountCapsule.getFrozenBalance();
long newFrozenBalance = freezeBalanceContract.getFrozenBalance() + currentFrozenBalance;

Frozen newFrozen = Frozen.newBuilder()
.setFrozenBalance(newFrozenBalance)
.setExpireTime(now + duration)
.build();

long frozenCount = accountCapsule.getFrozenCount();
if (frozenCount == 0) {
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
.addFrozen(newFrozen)
.setBalance(newBalance)
.build());
if (ArrayUtils.isEmpty(receiverAddress)) {
long newFrozenBalanceForBandwidth =
frozenBalance + accountCapsule.getFrozenBalance();
accountCapsule.setFrozenForBandwidth(newFrozenBalanceForBandwidth, expireTime);
} else {
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
.setFrozen(0, newFrozen)
.setBalance(newBalance)
.build()
);
delegateResource(ownerAddress, receiverAddress, true,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForBandwidth(frozenBalance);
}
dbManager.getDynamicPropertiesStore()
.addTotalNetWeight(freezeBalanceContract.getFrozenBalance() / 1000_000L);
.addTotalNetWeight(frozenBalance / 1000_000L);
break;
case ENERGY:
long currentFrozenBalanceForEnergy = accountCapsule.getAccountResource()
.getFrozenBalanceForEnergy()
.getFrozenBalance();
long newFrozenBalanceForEnergy =
freezeBalanceContract.getFrozenBalance() + currentFrozenBalanceForEnergy;

Frozen newFrozenForEnergy = Frozen.newBuilder()
.setFrozenBalance(newFrozenBalanceForEnergy)
.setExpireTime(now + duration)
.build();

AccountResource newAccountResource = accountCapsule.getAccountResource().toBuilder()
.setFrozenBalanceForEnergy(newFrozenForEnergy).build();

accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
.setAccountResource(newAccountResource)
.setBalance(newBalance)
.build());
if (ArrayUtils.isEmpty(receiverAddress)) {
long newFrozenBalanceForEnergy =
frozenBalance + accountCapsule.getAccountResource()
.getFrozenBalanceForEnergy()
.getFrozenBalance();
accountCapsule.setFrozenForEnergy(newFrozenBalanceForEnergy, expireTime);
} else {
delegateResource(ownerAddress, receiverAddress, false,
frozenBalance, expireTime);
accountCapsule.addDelegatedFrozenBalanceForEnergy(frozenBalance);
}
dbManager.getDynamicPropertiesStore()
.addTotalEnergyWeight(freezeBalanceContract.getFrozenBalance() / 1000_000L);
break;
default:
//this should never happen
.addTotalEnergyWeight(frozenBalance / 1000_000L);
break;
}

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

ret.setStatus(fee, code.SUCESS);
Expand Down Expand Up @@ -177,6 +164,33 @@ public boolean validate() throws ContractValidateException {
"ResourceCode error,valid ResourceCode[BANDWIDTH、ENERGY]");
}

//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 (!ArrayUtils.isEmpty(receiverAddress)) {
if (!dbManager.getDynamicPropertiesStore().supportDR()) {
throw new ContractValidateException("Delegating resource is NOT ALLOWED,"
+ " need to be opened by the committee");
}

if (Arrays.equals(receiverAddress, ownerAddress)) {
throw new ContractValidateException(
"receiverAddress must not be the same as ownerAddress");
}

if (!Wallet.addressValid(receiverAddress)) {
throw new ContractValidateException("Invalid receiverAddress");
}

AccountCapsule receiverCapsule = dbManager.getAccountStore().get(receiverAddress);
if (receiverCapsule == null) {
String readableOwnerAddress = StringUtil.createReadableString(receiverAddress);
throw new ContractValidateException(
"Account[" + readableOwnerAddress + "] not exists");
}

}

return true;
}

Expand All @@ -190,4 +204,74 @@ public long calcFee() {
return 0;
}

private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
long balance, long expireTime) {
byte[] key = DelegatedResourceCapsule.createDbKey(ownerAddress, receiverAddress);
//modify DelegatedResourceStore
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);

//modify DelegatedResourceAccountIndexStore
{
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = dbManager
.getDelegatedResourceAccountIndexStore()
.get(ownerAddress);
if (delegatedResourceAccountIndexCapsule == null) {
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
ByteString.copyFrom(ownerAddress));
}
List<ByteString> toAccountsList = delegatedResourceAccountIndexCapsule.getToAccountsList();
if (!toAccountsList.contains(ByteString.copyFrom(receiverAddress))) {
delegatedResourceAccountIndexCapsule.addToAccount(ByteString.copyFrom(receiverAddress));
}
dbManager.getDelegatedResourceAccountIndexStore()
.put(ownerAddress, delegatedResourceAccountIndexCapsule);
}

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ private void validateValue(Map.Entry<Long, Long> entry) throws ContractValidateE
}
break;
}
case (16): {
if (entry.getValue() != 1) {
throw new ContractValidateException(
"This value[ALLOW_DELEGATE_RESOURCE] is only allowed to be 1");
}
break;
}
default:
break;
}
Expand Down
Loading

0 comments on commit f2f89a2

Please sign in to comment.