Repository navigation
Expand file tree
/
Copy pathEnergyProcessorTest.java
More file actions
executable file
·182 lines (152 loc) · 6.77 KB
/
Copy pathEnergyProcessorTest.java
File metadata and controls
executable file
·182 lines (152 loc) · 6.77 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.tron.core;
import com.google.protobuf.ByteString;
import java.io.File;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tron.common.application.TronApplicationContext;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.Parameter.AdaptiveResourceLimitConstants;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.config.args.Args;
import org.tron.core.db.EnergyProcessor;
import org.tron.core.db.Manager;
import org.tron.protos.Contract;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Protocol.AccountType;
@Slf4j
public class EnergyProcessorTest {
private static Manager dbManager;
private static final String dbPath = "EnergyProcessorTest";
private static TronApplicationContext context;
private static final String ASSET_NAME;
private static final String CONTRACT_PROVIDER_ADDRESS;
private static final String USER_ADDRESS;
static {
Args.setParam(new String[]{"--output-directory", dbPath}, Constant.TEST_CONF);
context = new TronApplicationContext(DefaultConfig.class);
ASSET_NAME = "test_token";
CONTRACT_PROVIDER_ADDRESS =
Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
USER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
}
/**
* Init data.
*/
@BeforeClass
public static void init() {
dbManager = context.getBean(Manager.class);
}
/**
* Release resources.
*/
@AfterClass
public static void destroy() {
Args.clearParam();
context.destroy();
if (FileUtil.deleteDir(new File(dbPath))) {
logger.info("Release resources successful.");
} else {
logger.info("Release resources failure.");
}
}
/**
* create temp Capsule test need.
*/
@Before
public void createCapsule() {
AccountCapsule contractProvierCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("owner"),
ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_PROVIDER_ADDRESS)),
AccountType.Normal,
0L);
contractProvierCapsule.addAsset(ASSET_NAME.getBytes(), 100L);
AccountCapsule userCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("asset"),
ByteString.copyFrom(ByteArray.fromHexString(USER_ADDRESS)),
AccountType.AssetIssue,
dbManager.getDynamicPropertiesStore().getAssetIssueFee());
dbManager.getAccountStore().reset();
dbManager.getAccountStore()
.put(contractProvierCapsule.getAddress().toByteArray(), contractProvierCapsule);
dbManager.getAccountStore().put(userCapsule.getAddress().toByteArray(), userCapsule);
}
//todo ,replaced by smartContract later
private AssetIssueContract getAssetIssueContract() {
return Contract.AssetIssueContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(USER_ADDRESS)))
.setName(ByteString.copyFromUtf8(ASSET_NAME))
.setFreeAssetNetLimit(1000L)
.setPublicFreeAssetNetLimit(1000L)
.build();
}
@Test
public void testUseContractCreatorEnergy() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
dbManager.getDynamicPropertiesStore().saveTotalEnergyWeight(10_000_000L);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(CONTRACT_PROVIDER_ADDRESS));
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
EnergyProcessor processor = new EnergyProcessor(dbManager);
long energy = 10000;
long now = 1526647838000L;
boolean result = processor.useEnergy(ownerCapsule, energy, now);
Assert.assertEquals(false, result);
ownerCapsule.setFrozenForEnergy(10_000_000L, 0L);
result = processor.useEnergy(ownerCapsule, energy, now);
Assert.assertEquals(true, result);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(CONTRACT_PROVIDER_ADDRESS));
Assert.assertEquals(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(1526647838000L,
ownerCapsuleNew.getAccountResource().getLatestConsumeTimeForEnergy());
Assert.assertEquals(10000L, ownerCapsuleNew.getAccountResource().getEnergyUsage());
}
@Test
public void updateAdaptiveTotalEnergyLimit() {
EnergyProcessor processor = new EnergyProcessor(dbManager);
// open
dbManager.getDynamicPropertiesStore().saveAllowAdaptiveEnergy(1);
// Test resource usage auto reply
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
long now = dbManager.getWitnessController().getHeadSlot();
dbManager.getDynamicPropertiesStore().saveTotalEnergyAverageTime(now);
dbManager.getDynamicPropertiesStore().saveTotalEnergyAverageUsage(4000L);
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(
1526647838000L + AdaptiveResourceLimitConstants.PERIODS_MS / 2);
processor.updateTotalEnergyAverageUsage();
Assert.assertEquals(2000L,
dbManager.getDynamicPropertiesStore().getTotalEnergyAverageUsage());
// test saveTotalEnergyLimit
long ratio = ChainConstant.WINDOW_SIZE_MS / AdaptiveResourceLimitConstants.PERIODS_MS;
dbManager.getDynamicPropertiesStore().saveTotalEnergyLimit(10000L * ratio);
Assert.assertEquals(1000L,
dbManager.getDynamicPropertiesStore().getTotalEnergyTargetLimit());
//Test exceeds resource limit
dbManager.getDynamicPropertiesStore().saveTotalEnergyCurrentLimit(10000L * ratio);
dbManager.getDynamicPropertiesStore().saveTotalEnergyAverageUsage(3000L);
processor.updateAdaptiveTotalEnergyLimit();
Assert.assertEquals(10000L * ratio,
dbManager.getDynamicPropertiesStore().getTotalEnergyCurrentLimit());
//Test exceeds resource limit 2
dbManager.getDynamicPropertiesStore().saveTotalEnergyCurrentLimit(20000L * ratio);
dbManager.getDynamicPropertiesStore().saveTotalEnergyAverageUsage(3000L);
processor.updateAdaptiveTotalEnergyLimit();
Assert.assertEquals(20000L * ratio * 99 / 100L,
dbManager.getDynamicPropertiesStore().getTotalEnergyCurrentLimit());
//Test less than resource limit
dbManager.getDynamicPropertiesStore().saveTotalEnergyCurrentLimit(20000L * ratio);
dbManager.getDynamicPropertiesStore().saveTotalEnergyAverageUsage(500L);
processor.updateAdaptiveTotalEnergyLimit();
Assert.assertEquals(20000L * ratio * 1000 / 999L,
dbManager.getDynamicPropertiesStore().getTotalEnergyCurrentLimit());
}
}