Repository navigation
Expand file tree
/
Copy pathBandwidthTest.java
More file actions
executable file
·211 lines (171 loc) · 7.54 KB
/
Copy pathBandwidthTest.java
File metadata and controls
executable file
·211 lines (171 loc) · 7.54 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
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.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.Manager;
import org.tron.protos.Contract;
import org.tron.protos.Contract.AssetIssueContract;
import org.tron.protos.Contract.TransferAssetContract;
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))
.build();
}
/**
* Last trading time exceeded 10s
*/
@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));
ownerCapsule.setLatestOperationTime(1526647820000L);
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
dbManager.consumeBandwidth(trx);
AccountCapsule ownerCapsuleNew = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
Assert.assertEquals(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
}
@Test
public void testConsumeIssue() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
TransferAssetContract contract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(contract);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.setLatestOperationTime(1526647830000L);
ownerCapsule.setLatestAssetOperationTimeMap(ASSET_NAME, 1526647820000L);
AccountCapsule assetCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(ASSET_ADDRESS));
assetCapsule.setBandwidth(200000L);
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(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(1526647838000L,
(long) ownerCapsuleNew.getLatestAssetOperationTimeMap().get(ASSET_NAME));
Assert.assertEquals(100000L, assetCapsuleNew.getBandwidth());
}
@Test
public void testConsumeOwner() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
TransferAssetContract contract = getTransferAssetContract();
TransactionCapsule trx = new TransactionCapsule(contract);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(OWNER_ADDRESS));
ownerCapsule.setLatestOperationTime(1526647830000L);
ownerCapsule.setLatestAssetOperationTimeMap(ASSET_NAME, 1526647838000L);
ownerCapsule.setBandwidth(200000L);
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(1526647838000L, ownerCapsuleNew.getLatestOperationTime());
Assert.assertEquals(1526647838000L,
(long) ownerCapsuleNew.getLatestAssetOperationTimeMap().get(ASSET_NAME));
Assert.assertEquals(100000L, ownerCapsuleNew.getBandwidth());
}
}