Repository navigation
Expand file tree
/
Copy pathCpuProcessorTest.java
More file actions
executable file
·135 lines (113 loc) · 4.57 KB
/
Copy pathCpuProcessorTest.java
File metadata and controls
executable file
·135 lines (113 loc) · 4.57 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
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.TransactionCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.DefaultConfig;
import org.tron.core.config.args.Args;
import org.tron.core.db.CpuProcessor;
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 CpuProcessorTest {
private static Manager dbManager;
private static final String dbPath = "CpuProcessorTest";
private static AnnotationConfigApplicationContext 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 AnnotationConfigApplicationContext(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();
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 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 testUseContractCreatorCpu() throws Exception {
dbManager.getDynamicPropertiesStore().saveLatestBlockHeaderTimestamp(1526647838000L);
dbManager.getDynamicPropertiesStore().saveTotalCpuWeight(10_000_000L);
AccountCapsule ownerCapsule = dbManager.getAccountStore()
.get(ByteArray.fromHexString(CONTRACT_PROVIDER_ADDRESS));
dbManager.getAccountStore().put(ownerCapsule.getAddress().toByteArray(), ownerCapsule);
CpuProcessor processor = new CpuProcessor(dbManager);
long cpuTime = 10000;
long now = 1526647838000L;
boolean result = processor.useCpu(ownerCapsule, cpuTime, now);
Assert.assertEquals(false, result);
ownerCapsule.setFrozenForCpu(10_000_000L, 0L);
result = processor.useCpu(ownerCapsule, cpuTime, 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().getLatestConsumeTimeForCpu());
Assert.assertEquals(10000L, ownerCapsuleNew.getAccountResource().getCpuUsage());
}
}