forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBandwidthTest.java
More file actions
executable file
·331 lines (259 loc) · 13.1 KB
/
BandwidthTest.java
File metadata and controls
executable file
·331 lines (259 loc) · 13.1 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package org.tron.core;
import com.google.protobuf.Any;
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.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.AssetIssueCapsule;
import org.tron.core.capsule.TransactionCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.config.args.Args;
import org.tron.core.db.BandwidthProcessor;
import org.tron.core.db.Manager;
import org.tron.protos.Contract;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Contract.TransferAssetContract;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.AccountType;
@Slf4j
public class BandwidthTest {
private static Manager dbManager;
private static final String dbPath = "bandwidth_test";
private static AnnotationConfigApplicationContext context;
private static final String ASSET_NAME;
private static final String OWNER_ADDRESS;
private static final String ASSET_ADDRESS;
private static final String TO_ADDRESS;
static {
Args.setParam(new String[]{"--output-directory", dbPath}, Constant.TEST_CONF);
context = new AnnotationConfigApplicationContext(DefaultConfig.class);
ASSET_NAME = "test_token";
OWNER_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc";
TO_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc";
ASSET_ADDRESS = Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a3456";
}
/**
* Init data.
*/
@BeforeClass
public static void init() {
dbManager = context.getBean(Manager.class);
}
/**
* Release resources.
*/
@AfterClass
public static void destroy() {
Args.clearParam();
if (FileUtil.deleteDir(new File(dbPath))) {
logger.info("Release resources successful.");
} else {
logger.info("Release resources failure.");
}
context.destroy();
}
/**
* create temp Capsule test need.
*/
@Before
public void createCapsule() {
AccountCapsule ownerCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("owner"),
ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)),
AccountType.Normal,
0L);
ownerCapsule.addAsset(ASSET_NAME, 100L);
AccountCapsule toAccountCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("toAccount"),
ByteString.copyFrom(ByteArray.fromHexString(TO_ADDRESS)),
AccountType.Normal,
0L);
AccountCapsule assetCapsule =
new AccountCapsule(
ByteString.copyFromUtf8("asset"),
ByteString.copyFrom(ByteArray.fromHexString(ASSET_ADDRESS)),
AccountType.AssetIssue,
ChainConstant.ASSET_ISSUE_FEE);
dbManager.getAccountStore().reset();
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.getAccountStore().put(toAccountCapsule.getAddress().toByteArray(), toAccountCapsule);
dbManager.getAccountStore().put(assetCapsule.getAddress().toByteArray(), assetCapsule);
dbManager
.getAssetIssueStore()
.put(
ByteArray.fromString(ASSET_NAME),
new AssetIssueCapsule(getAssetIssueContract()));
}
private TransferAssetContract getTransferAssetContract() {
return Contract.TransferAssetContract.newBuilder()
.setAssetName(ByteString.copyFrom(ByteArray.fromString(ASSET_NAME)))
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(TO_ADDRESS)))
.setAmount(100L)
.build();
}
private AssetIssueContract getAssetIssueContract() {
return Contract.AssetIssueContract.newBuilder()
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(ASSET_ADDRESS)))
.setName(ByteString.copyFromUtf8(ASSET_NAME))
.setFreeAssetNetLimit(1000L)
.setPublicFreeAssetNetLimit(1000L)
.build();
}
@Test
public void testCreateNewAccount() throws Exception {
BandwidthProcessor processor = new BandwidthProcessor(dbManager);
TransferAssetContract transferAssetContract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(transferAssetContract);
String NOT_EXISTS_ADDRESS =
Wallet.getAddressPreFixString() + "008794500882809695a8a687866e76d4271a1abc";
transferAssetContract = transferAssetContract.toBuilder()
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(NOT_EXISTS_ADDRESS))).build();
org.tron.protos.Protocol.Transaction.Contract contract = org.tron.protos.Protocol.Transaction.Contract
.newBuilder()
.setType(Protocol.Transaction.Contract.ContractType.TransferAssetContract).setParameter(
Any.pack(transferAssetContract)).build();
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
dbManager.getDynamicPropertiesStore()
.saveTotalNetWeight(10_000_000L);//only owner has frozen balance
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.setFrozen(10_000_000L, 0L);
Assert.assertEquals(true, processor.contractCreateNewAccount(contract));
long bytes = trx.getSerializedSize();
processor.consumeBandwidthForCreateNewAccount(ownerCapsule, bytes, 1526647838000L);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Assert.assertEquals(122L, ownerCapsuleNew.getNetUsage());
}
@Test
public void testFree() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
TransferAssetContract contract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(contract);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.consumeBandwidth(trx);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Assert.assertEquals(122L, ownerCapsuleNew.getFreeNetUsage());
Assert.assertEquals(508882612L, ownerCapsuleNew.getLatestConsumeFreeTime());//slot
Assert.assertEquals(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(122L, dbManager.getDynamicPropertiesStore().getPublicNetUsage());
Assert.assertEquals(508882612L, dbManager.getDynamicPropertiesStore().getPublicNetTime());
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526691038000L); // + 12h
dbManager.consumeBandwidth(trx);
ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Assert.assertEquals(61L + 122L, ownerCapsuleNew.getFreeNetUsage());
Assert.assertEquals(508897012L,
ownerCapsuleNew.getLatestConsumeFreeTime()); // 508882612L + 28800L/2
Assert.assertEquals(1526691038000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(61L + 122L, dbManager.getDynamicPropertiesStore().getPublicNetUsage());
Assert.assertEquals(508897012L, dbManager.getDynamicPropertiesStore().getPublicNetTime());
}
@Test
public void testConsumeAssetAccount() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
dbManager.getDynamicPropertiesStore()
.saveTotalNetWeight(10_000_000L);//only assetAccount has frozen balance
TransferAssetContract contract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(contract);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule assetCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(ASSET_ADDRESS));
assetCapsule.setFrozen(10_000_000L, 0L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.getAccountStore().put(assetCapsule.getAddress().toByteArray(), assetCapsule);
dbManager.consumeBandwidth(trx);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule assetCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(ASSET_ADDRESS));
Assert.assertEquals(122L, assetCapsuleNew.getNetUsage());
Assert.assertEquals(508882612L, assetCapsuleNew.getLatestConsumeTime());
Assert.assertEquals(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(508882612L, ownerCapsuleNew.getLatestAssetOperationTime(ASSET_NAME));
Assert.assertEquals(122L, ownerCapsuleNew.getFreeAssetNetUsage(ASSET_NAME));
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526691038000L); // + 12h
dbManager.consumeBandwidth(trx);
ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
assetCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(ASSET_ADDRESS));
Assert.assertEquals(61L + 122L, assetCapsuleNew.getNetUsage());
Assert.assertEquals(508897012L, assetCapsuleNew.getLatestConsumeTime());
Assert.assertEquals(1526691038000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(508897012L, ownerCapsuleNew.getLatestAssetOperationTime(ASSET_NAME));
Assert.assertEquals(61L + 122L, ownerCapsuleNew.getFreeAssetNetUsage(ASSET_NAME));
}
@Test
public void testConsumeOwner() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
dbManager.getDynamicPropertiesStore()
.saveTotalNetWeight(10_000_000L);//only owner has frozen balance
TransferAssetContract contract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(contract);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.setFrozen(10_000_000L, 0L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.consumeBandwidth(trx);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
AccountCapsule assetCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(ASSET_ADDRESS));
Assert.assertEquals(122L, ownerCapsuleNew.getNetUsage());
Assert.assertEquals(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(508882612L, ownerCapsuleNew.getLatestConsumeTime());
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526691038000L); // + 12h
dbManager.consumeBandwidth(trx);
ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Assert.assertEquals(61L + 122L, ownerCapsuleNew.getNetUsage());
Assert.assertEquals(1526691038000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(508897012L, ownerCapsuleNew.getLatestConsumeTime());
}
@Test
public void testUsingFee() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
dbManager.getDynamicPropertiesStore().saveFreeNetLimit(0L);
TransferAssetContract contract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(contract);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.setBalance(10_000_000L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.consumeBandwidth(trx);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
long transactionFee = 122L * dbManager.getDynamicPropertiesStore().getTransactionFee();
Assert.assertEquals(transactionFee,
dbManager.getDynamicPropertiesStore().getTotalTransactionCost());
Assert.assertEquals(
10_000_000L - transactionFee,
ownerCapsuleNew.getBalance());
dbManager.getAccountStore().delete(ByteArray.fromHexString(TO_ADDRESS));
dbManager.consumeBandwidth(trx);
long createAccountFee = dbManager.getDynamicPropertiesStore().getCreateAccountFee();
ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Assert.assertEquals(dbManager.getDynamicPropertiesStore().getCreateAccountFee(),
dbManager.getDynamicPropertiesStore().getTotalCreateAccountCost());
Assert.assertEquals(
10_000_000L - transactionFee - createAccountFee, ownerCapsuleNew.getBalance());
}
}