Repository navigation
Expand file tree
/
Copy pathEnergyProcessor.java
More file actions
168 lines (129 loc) · 5.69 KB
/
Copy pathEnergyProcessor.java
File metadata and controls
168 lines (129 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package org.tron.core.db;
import static java.lang.Long.max;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.exception.AccountResourceInsufficientException;
import org.tron.core.exception.ContractValidateException;
import org.tron.protos.Protocol.Account.AccountResource;
import org.tron.protos.Protocol.Transaction.Contract;
@Slf4j
public class EnergyProcessor extends ResourceProcessor {
public EnergyProcessor(Manager manager) {
super(manager);
}
@Override
public void updateUsage(AccountCapsule accountCapsule) {
long now = dbManager.getWitnessController().getHeadSlot();
updateUsage(accountCapsule, now);
}
private void updateUsage(AccountCapsule accountCapsule, long now) {
AccountResource accountResource = accountCapsule.getAccountResource();
long oldEnergyUsage = accountResource.getEnergyUsage();
long latestConsumeTime = accountResource.getLatestConsumeTimeForEnergy();
accountCapsule.setEnergyUsage(increase(oldEnergyUsage, 0, latestConsumeTime, now));
}
@Override
public void consume(TransactionCapsule trx,
TransactionTrace trace)
throws ContractValidateException, AccountResourceInsufficientException {
List<Contract> contracts =
trx.getInstance().getRawData().getContractList();
for (Contract contract : contracts) {
//todo
// if (contract.isPrecompiled()) {
// continue;
// }
//todo
// long energy = trx.getReceipt().getEnergy();
long energy = 100L;
logger.debug("trxId {},energy cost :{}", trx.getTransactionId(), energy);
byte[] address = TransactionCapsule.getOwner(contract);
AccountCapsule accountCapsule = dbManager.getAccountStore().get(address);
if (accountCapsule == null) {
throw new ContractValidateException("account not exists");
}
long now = dbManager.getWitnessController().getHeadSlot();
//todo
// int creatorRatio = contract.getUserEnergyConsumeRatio();
int creatorRatio = 50;
long creatorEnergy = energy * creatorRatio / 100;
AccountCapsule contractProvider = dbManager.getAccountStore()
.get(contract.getProvider().toByteArray());
if (!useEnergy(contractProvider, creatorEnergy, now)) {
throw new ContractValidateException(
"creator has not enough energy[" + creatorEnergy + "]");
}
long userEnergy = energy * (100 - creatorRatio) / 100;
//1.The creator and the use of this have sufficient resources
if (useEnergy(accountCapsule, userEnergy, now)) {
continue;
}
// todo long feeLimit = getUserFeeLimit();
long feeLimit = 1000000;//sun
long fee = calculateFee(userEnergy);
if (fee > feeLimit) {
throw new AccountResourceInsufficientException(
"Account has Insufficient Energy[" + userEnergy + "] and feeLimit[" + feeLimit
+ "] is not enough to trigger this contract");
}
//2.The creator of this have sufficient resources
if (useFee(accountCapsule, fee, trace)) {
continue;
}
throw new AccountResourceInsufficientException(
"Account has insufficient Energy[" + userEnergy + "] and balance[" + fee
+ "] to trigger this contract");
}
}
private long calculateFee(long userEnergy) {
return userEnergy * 30;// 30 drop / macroSecond, move to dynamicStore later
}
private boolean useFee(AccountCapsule accountCapsule, long fee,
TransactionTrace trace) {
if (consumeFee(accountCapsule, fee)) {
trace.setNetBill(0, fee);
return true;
} else {
return false;
}
}
public boolean useEnergy(AccountCapsule accountCapsule, long energy, long now) {
long energyUsage = accountCapsule.getEnergyUsage();
long latestConsumeTime = accountCapsule.getAccountResource().getLatestConsumeTimeForEnergy();
long energyLimit = calculateGlobalEnergyLimit(
accountCapsule.getAccountResource().getFrozenBalanceForEnergy().getFrozenBalance());
long newEnergyUsage = increase(energyUsage, 0, latestConsumeTime, now);
if (energy > (energyLimit - newEnergyUsage)) {
return false;
}
latestConsumeTime = now;
long latestOperationTime = dbManager.getHeadBlockTimeStamp();
newEnergyUsage = increase(newEnergyUsage, energy, latestConsumeTime, now);
accountCapsule.setEnergyUsage(newEnergyUsage);
accountCapsule.setLatestOperationTime(latestOperationTime);
accountCapsule.setLatestConsumeTimeForEnergy(latestConsumeTime);
dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule);
return true;
}
public long calculateGlobalEnergyLimit(long frozeBalance) {
if (frozeBalance < 1000_000L) {
return 0;
}
long energyWeight = frozeBalance / 1000_000L;
long totalEnergyLimit = dbManager.getDynamicPropertiesStore().getTotalEnergyLimit();
long totalEnergyWeight = dbManager.getDynamicPropertiesStore().getTotalEnergyWeight();
assert totalEnergyWeight > 0;
return (long) (energyWeight * ((double) totalEnergyLimit / totalEnergyWeight));
}
public long getAccountLeftEnergyFromFreeze(AccountCapsule accountCapsule) {
long now = dbManager.getWitnessController().getHeadSlot();
long energyUsage = accountCapsule.getEnergyUsage();
long latestConsumeTime = accountCapsule.getAccountResource().getLatestConsumeTimeForEnergy();
long energyLimit = calculateGlobalEnergyLimit(
accountCapsule.getAccountResource().getFrozenBalanceForEnergy().getFrozenBalance());
long newEnergyUsage = increase(energyUsage, 0, latestConsumeTime, now);
return max(energyLimit - newEnergyUsage, 0); // us
}
}