Skip to content

Commit 183c5b6

Browse files
committed
Merge branch 'Odyssey_v3.2' into add_energy_limit
2 parents 89633de + 09c3a09 commit 183c5b6

28 files changed

Lines changed: 1460 additions & 199 deletions

‎src/main/java/org/tron/common/application/TronApplicationContext.java‎

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,45 @@
22

33
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
44
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.tron.common.overlay.discover.DiscoverServer;
6+
import org.tron.common.overlay.discover.node.NodeManager;
7+
import org.tron.common.overlay.server.ChannelManager;
58
import org.tron.core.db.Manager;
69

710
public class TronApplicationContext extends AnnotationConfigApplicationContext {
811

9-
public TronApplicationContext() {
10-
}
12+
public TronApplicationContext() {
13+
}
1114

12-
public TronApplicationContext(DefaultListableBeanFactory beanFactory) {
13-
super(beanFactory);
14-
}
15+
public TronApplicationContext(DefaultListableBeanFactory beanFactory) {
16+
super(beanFactory);
17+
}
1518

16-
public TronApplicationContext(Class<?>... annotatedClasses) {
17-
super(annotatedClasses);
18-
}
19+
public TronApplicationContext(Class<?>... annotatedClasses) {
20+
super(annotatedClasses);
21+
}
1922

20-
public TronApplicationContext(String... basePackages) {
21-
super(basePackages);
22-
}
23+
public TronApplicationContext(String... basePackages) {
24+
super(basePackages);
25+
}
2326

24-
@Override
25-
public void destroy() {
27+
@Override
28+
public void destroy() {
2629

27-
Manager dbManager = getBean(Manager.class);
28-
dbManager.stopRepushThread();
30+
Application appT = ApplicationFactory.create(this);
31+
appT.shutdownServices();
32+
appT.shutdown();
2933

30-
super.destroy();
31-
}
34+
DiscoverServer discoverServer = getBean(DiscoverServer.class);
35+
discoverServer.close();
36+
ChannelManager channelManager = getBean(ChannelManager.class);
37+
channelManager.close();
38+
NodeManager nodeManager = getBean(NodeManager.class);
39+
nodeManager.close();
40+
41+
Manager dbManager = getBean(Manager.class);
42+
dbManager.stopRepushThread();
43+
44+
super.destroy();
45+
}
3246
}

‎src/main/java/org/tron/common/runtime/RuntimeImpl.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@ private long getEnergyLimit(AccountCapsule account, long feeLimit, long callValu
220220
.floorDiv(max(account.getBalance() - callValue, 0), sunPerEnergy);
221221

222222
long energyFromFeeLimit;
223-
long totalBalanceForEnergyFreeze = account.getAccountResource().getFrozenBalanceForEnergy()
224-
.getFrozenBalance();
223+
long totalBalanceForEnergyFreeze = account.getAllFrozenBalanceForEnergy();
225224
if (0 == totalBalanceForEnergyFreeze) {
226225
energyFromFeeLimit =
227226
feeLimit / sunPerEnergy;
228227
} else {
229228
long totalEnergyFromFreeze = energyProcessor
230-
.calculateGlobalEnergyLimit(totalBalanceForEnergyFreeze);
229+
.calculateGlobalEnergyLimit(account);
231230
long leftBalanceForEnergyFreeze = getEnergyFee(totalBalanceForEnergyFreeze,
232231
leftEnergyFromFreeze,
233232
totalEnergyFromFreeze);

‎src/main/java/org/tron/common/storage/DepositImpl.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.tron.core.db.BlockStore;
2424
import org.tron.core.db.CodeStore;
2525
import org.tron.core.db.ContractStore;
26+
import org.tron.core.db.DelegatedResourceStore;
2627
import org.tron.core.db.DynamicPropertiesStore;
2728
import org.tron.core.db.Manager;
2829
import org.tron.core.db.ProposalStore;
@@ -114,6 +115,11 @@ private StorageRowStore getStorageRowStore() {
114115
return dbManager.getStorageRowStore();
115116
}
116117

118+
private DelegatedResourceStore getDelegatedResourceStore() {
119+
return dbManager.getDelegatedResourceStore();
120+
}
121+
122+
117123
private AssetIssueStore getAssetIssueStore() {
118124
return dbManager.getAssetIssueStore();
119125
}

‎src/main/java/org/tron/core/Wallet.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.tron.api.GrpcAPI.Address;
4747
import org.tron.api.GrpcAPI.AssetIssueList;
4848
import org.tron.api.GrpcAPI.BlockList;
49+
import org.tron.api.GrpcAPI.DelegatedResourceList;
4950
import org.tron.api.GrpcAPI.ExchangeList;
5051
import org.tron.api.GrpcAPI.Node;
5152
import org.tron.api.GrpcAPI.NodeList;
@@ -75,6 +76,7 @@
7576
import org.tron.core.capsule.AssetIssueCapsule;
7677
import org.tron.core.capsule.BlockCapsule;
7778
import org.tron.core.capsule.ContractCapsule;
79+
import org.tron.core.capsule.DelegatedResourceCapsule;
7880
import org.tron.core.capsule.ExchangeCapsule;
7981
import org.tron.core.capsule.ProposalCapsule;
8082
import org.tron.core.capsule.TransactionCapsule;
@@ -111,6 +113,7 @@
111113
import org.tron.protos.Protocol;
112114
import org.tron.protos.Protocol.Account;
113115
import org.tron.protos.Protocol.Block;
116+
import org.tron.protos.Protocol.DelegatedResourceAccountIndex;
114117
import org.tron.protos.Protocol.Exchange;
115118
import org.tron.protos.Protocol.Proposal;
116119
import org.tron.protos.Protocol.SmartContract;
@@ -523,6 +526,23 @@ public ProposalList getProposalList() {
523526
return builder.build();
524527
}
525528

529+
public DelegatedResourceList getDelegatedResource(ByteString fromAddress, ByteString toAddress) {
530+
DelegatedResourceList.Builder builder = DelegatedResourceList.newBuilder();
531+
byte[] dbKey = DelegatedResourceCapsule
532+
.createDbKey(fromAddress.toByteArray(), toAddress.toByteArray());
533+
DelegatedResourceCapsule delegatedResourceCapsule = dbManager.getDelegatedResourceStore()
534+
.get(dbKey);
535+
if (delegatedResourceCapsule != null) {
536+
builder.addDelegatedResource(delegatedResourceCapsule.getInstance());
537+
}
538+
return builder.build();
539+
}
540+
541+
public DelegatedResourceAccountIndex getDelegatedResourceAccountIndex(ByteString address) {
542+
return dbManager.getDelegatedResourceAccountIndexStore()
543+
.get(address.toByteArray()).getInstance();
544+
}
545+
526546
public ExchangeList getExchangeList() {
527547
ExchangeList.Builder builder = ExchangeList.newBuilder();
528548
List<ExchangeCapsule> exchangeCapsuleList = dbManager.getExchangeStore().getAllExchanges();
@@ -605,7 +625,8 @@ public AccountNetMessage getAccountNet(ByteString accountAddress) {
605625
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
606626
processor.updateUsage(accountCapsule);
607627

608-
long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
628+
long netLimit = processor
629+
.calculateGlobalNetLimit(accountCapsule);
609630
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
610631
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
611632
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
@@ -643,12 +664,13 @@ public AccountResourceMessage getAccountResource(ByteString accountAddress) {
643664
EnergyProcessor energyProcessor = new EnergyProcessor(dbManager);
644665
energyProcessor.updateUsage(accountCapsule);
645666

646-
long netLimit = processor.calculateGlobalNetLimit(accountCapsule.getFrozenBalance());
667+
long netLimit = processor
668+
.calculateGlobalNetLimit(accountCapsule);
647669
long freeNetLimit = dbManager.getDynamicPropertiesStore().getFreeNetLimit();
648670
long totalNetLimit = dbManager.getDynamicPropertiesStore().getTotalNetLimit();
649671
long totalNetWeight = dbManager.getDynamicPropertiesStore().getTotalNetWeight();
650672
long energyLimit = energyProcessor
651-
.calculateGlobalEnergyLimit(accountCapsule.getEnergyFrozenBalance());
673+
.calculateGlobalEnergyLimit(accountCapsule);
652674
long totalEnergyLimit = dbManager.getDynamicPropertiesStore().getTotalEnergyLimit();
653675
long totalEnergyWeight = dbManager.getDynamicPropertiesStore().getTotalEnergyWeight();
654676

‎src/main/java/org/tron/core/actuator/FreezeBalanceActuator.java‎

Lines changed: 128 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import com.google.protobuf.Any;
44
import com.google.protobuf.ByteString;
55
import com.google.protobuf.InvalidProtocolBufferException;
6+
import java.util.Arrays;
7+
import java.util.List;
68
import lombok.extern.slf4j.Slf4j;
9+
import org.apache.commons.lang3.ArrayUtils;
710
import org.tron.common.utils.StringUtil;
811
import org.tron.core.Wallet;
912
import org.tron.core.capsule.AccountCapsule;
13+
import org.tron.core.capsule.DelegatedResourceAccountIndexCapsule;
14+
import org.tron.core.capsule.DelegatedResourceCapsule;
1015
import org.tron.core.capsule.TransactionResultCapsule;
1116
import org.tron.core.db.Manager;
1217
import org.tron.core.exception.ContractExeException;
1318
import org.tron.core.exception.ContractValidateException;
1419
import org.tron.protos.Contract.FreezeBalanceContract;
15-
import org.tron.protos.Protocol.Account.AccountResource;
16-
import org.tron.protos.Protocol.Account.Frozen;
1720
import org.tron.protos.Protocol.Transaction.Result.code;
1821

1922
@Slf4j
@@ -42,59 +45,43 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
4245

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

48+
long frozenBalance = freezeBalanceContract.getFrozenBalance();
49+
long expireTime = now + duration;
50+
byte[] ownerAddress = freezeBalanceContract.getOwnerAddress().toByteArray();
51+
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();
52+
4553
switch (freezeBalanceContract.getResource()) {
4654
case BANDWIDTH:
47-
long currentFrozenBalance = accountCapsule.getFrozenBalance();
48-
long newFrozenBalance = freezeBalanceContract.getFrozenBalance() + currentFrozenBalance;
49-
50-
Frozen newFrozen = Frozen.newBuilder()
51-
.setFrozenBalance(newFrozenBalance)
52-
.setExpireTime(now + duration)
53-
.build();
54-
55-
long frozenCount = accountCapsule.getFrozenCount();
56-
if (frozenCount == 0) {
57-
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
58-
.addFrozen(newFrozen)
59-
.setBalance(newBalance)
60-
.build());
55+
if (ArrayUtils.isEmpty(receiverAddress)) {
56+
long newFrozenBalanceForBandwidth =
57+
frozenBalance + accountCapsule.getFrozenBalance();
58+
accountCapsule.setFrozenForBandwidth(newFrozenBalanceForBandwidth, expireTime);
6159
} else {
62-
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
63-
.setFrozen(0, newFrozen)
64-
.setBalance(newBalance)
65-
.build()
66-
);
60+
delegateResource(ownerAddress, receiverAddress, true,
61+
frozenBalance, expireTime);
62+
accountCapsule.addDelegatedFrozenBalanceForBandwidth(frozenBalance);
6763
}
6864
dbManager.getDynamicPropertiesStore()
69-
.addTotalNetWeight(freezeBalanceContract.getFrozenBalance() / 1000_000L);
65+
.addTotalNetWeight(frozenBalance / 1000_000L);
7066
break;
7167
case ENERGY:
72-
long currentFrozenBalanceForEnergy = accountCapsule.getAccountResource()
73-
.getFrozenBalanceForEnergy()
74-
.getFrozenBalance();
75-
long newFrozenBalanceForEnergy =
76-
freezeBalanceContract.getFrozenBalance() + currentFrozenBalanceForEnergy;
77-
78-
Frozen newFrozenForEnergy = Frozen.newBuilder()
79-
.setFrozenBalance(newFrozenBalanceForEnergy)
80-
.setExpireTime(now + duration)
81-
.build();
82-
83-
AccountResource newAccountResource = accountCapsule.getAccountResource().toBuilder()
84-
.setFrozenBalanceForEnergy(newFrozenForEnergy).build();
85-
86-
accountCapsule.setInstance(accountCapsule.getInstance().toBuilder()
87-
.setAccountResource(newAccountResource)
88-
.setBalance(newBalance)
89-
.build());
68+
if (ArrayUtils.isEmpty(receiverAddress)) {
69+
long newFrozenBalanceForEnergy =
70+
frozenBalance + accountCapsule.getAccountResource()
71+
.getFrozenBalanceForEnergy()
72+
.getFrozenBalance();
73+
accountCapsule.setFrozenForEnergy(newFrozenBalanceForEnergy, expireTime);
74+
} else {
75+
delegateResource(ownerAddress, receiverAddress, false,
76+
frozenBalance, expireTime);
77+
accountCapsule.addDelegatedFrozenBalanceForEnergy(frozenBalance);
78+
}
9079
dbManager.getDynamicPropertiesStore()
91-
.addTotalEnergyWeight(freezeBalanceContract.getFrozenBalance() / 1000_000L);
92-
break;
93-
default:
94-
//this should never happen
80+
.addTotalEnergyWeight(frozenBalance / 1000_000L);
9581
break;
9682
}
9783

84+
accountCapsule.setBalance(newBalance);
9885
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);
9986

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

167+
//todo:need version control and config for delegating resource
168+
byte[] receiverAddress = freezeBalanceContract.getReceiverAddress().toByteArray();
169+
//If the receiver is included in the contract, the receiver will receive the resource.
170+
if (!ArrayUtils.isEmpty(receiverAddress)) {
171+
if (!dbManager.getDynamicPropertiesStore().supportDR()) {
172+
throw new ContractValidateException("Delegating resource is NOT ALLOWED,"
173+
+ " need to be opened by the committee");
174+
}
175+
176+
if (Arrays.equals(receiverAddress, ownerAddress)) {
177+
throw new ContractValidateException(
178+
"receiverAddress must not be the same as ownerAddress");
179+
}
180+
181+
if (!Wallet.addressValid(receiverAddress)) {
182+
throw new ContractValidateException("Invalid receiverAddress");
183+
}
184+
185+
AccountCapsule receiverCapsule = dbManager.getAccountStore().get(receiverAddress);
186+
if (receiverCapsule == null) {
187+
String readableOwnerAddress = StringUtil.createReadableString(receiverAddress);
188+
throw new ContractValidateException(
189+
"Account[" + readableOwnerAddress + "] not exists");
190+
}
191+
192+
}
193+
180194
return true;
181195
}
182196

@@ -190,4 +204,74 @@ public long calcFee() {
190204
return 0;
191205
}
192206

207+
private void delegateResource(byte[] ownerAddress, byte[] receiverAddress, boolean isBandwidth,
208+
long balance, long expireTime) {
209+
byte[] key = DelegatedResourceCapsule.createDbKey(ownerAddress, receiverAddress);
210+
//modify DelegatedResourceStore
211+
DelegatedResourceCapsule delegatedResourceCapsule = dbManager.getDelegatedResourceStore()
212+
.get(key);
213+
if (delegatedResourceCapsule != null) {
214+
if (isBandwidth) {
215+
delegatedResourceCapsule.addFrozenBalanceForBandwidth(balance, expireTime);
216+
} else {
217+
delegatedResourceCapsule.addFrozenBalanceForEnergy(balance, expireTime);
218+
}
219+
} else {
220+
delegatedResourceCapsule = new DelegatedResourceCapsule(
221+
ByteString.copyFrom(ownerAddress),
222+
ByteString.copyFrom(receiverAddress));
223+
if (isBandwidth) {
224+
delegatedResourceCapsule.setFrozenBalanceForBandwidth(balance, expireTime);
225+
} else {
226+
delegatedResourceCapsule.setFrozenBalanceForEnergy(balance, expireTime);
227+
}
228+
229+
}
230+
dbManager.getDelegatedResourceStore().put(key, delegatedResourceCapsule);
231+
232+
//modify DelegatedResourceAccountIndexStore
233+
{
234+
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = dbManager
235+
.getDelegatedResourceAccountIndexStore()
236+
.get(ownerAddress);
237+
if (delegatedResourceAccountIndexCapsule == null) {
238+
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
239+
ByteString.copyFrom(ownerAddress));
240+
}
241+
List<ByteString> toAccountsList = delegatedResourceAccountIndexCapsule.getToAccountsList();
242+
if (!toAccountsList.contains(ByteString.copyFrom(receiverAddress))) {
243+
delegatedResourceAccountIndexCapsule.addToAccount(ByteString.copyFrom(receiverAddress));
244+
}
245+
dbManager.getDelegatedResourceAccountIndexStore()
246+
.put(ownerAddress, delegatedResourceAccountIndexCapsule);
247+
}
248+
249+
{
250+
DelegatedResourceAccountIndexCapsule delegatedResourceAccountIndexCapsule = dbManager
251+
.getDelegatedResourceAccountIndexStore()
252+
.get(receiverAddress);
253+
if (delegatedResourceAccountIndexCapsule == null) {
254+
delegatedResourceAccountIndexCapsule = new DelegatedResourceAccountIndexCapsule(
255+
ByteString.copyFrom(receiverAddress));
256+
}
257+
List<ByteString> fromAccountsList = delegatedResourceAccountIndexCapsule
258+
.getFromAccountsList();
259+
if (!fromAccountsList.contains(ByteString.copyFrom(ownerAddress))) {
260+
delegatedResourceAccountIndexCapsule.addFromAccount(ByteString.copyFrom(ownerAddress));
261+
}
262+
dbManager.getDelegatedResourceAccountIndexStore()
263+
.put(receiverAddress, delegatedResourceAccountIndexCapsule);
264+
}
265+
266+
//modify AccountStore
267+
AccountCapsule receiverCapsule = dbManager.getAccountStore().get(receiverAddress);
268+
if (isBandwidth) {
269+
receiverCapsule.addAcquiredDelegatedFrozenBalanceForBandwidth(balance);
270+
} else {
271+
receiverCapsule.addAcquiredDelegatedFrozenBalanceForEnergy(balance);
272+
}
273+
274+
dbManager.getAccountStore().put(receiverCapsule.createDbKey(), receiverCapsule);
275+
}
276+
193277
}

0 commit comments

Comments
 (0)