Skip to content

Commit b4dea0d

Browse files
committed
Merge branch 'develop' of github.com:tronprotocol/java-tron into consensus_logic_opt
2 parents 46826a7 + 92ae447 commit b4dea0d

File tree

12 files changed

+2542
-12
lines changed

12 files changed

+2542
-12
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ subprojects {
1111
targetCompatibility = JavaVersion.VERSION_1_8
1212

1313
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
14+
jacoco {
15+
toolVersion = "0.8.12" // see https://www.jacoco.org/jacoco/trunk/doc/changes.html
16+
}
1417

1518
buildscript {
1619
repositories {
@@ -52,7 +55,8 @@ subprojects {
5255
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
5356

5457
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
55-
testImplementation "org.mockito:mockito-core:2.13.0"
58+
testImplementation "org.mockito:mockito-core:4.11.0"
59+
testImplementation "org.mockito:mockito-inline:4.11.0"
5660
}
5761

5862
task sourcesJar(type: Jar, dependsOn: classes) {

chainbase/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ description = "chainbase – a decentralized database for blockchain."
22

33
// Dependency versions
44
// ---------------------------------------
5-
def jacocoVersion = "0.8.0"
65
def jansiVersion = "1.16"
76
// --------------------------------------
87

@@ -41,10 +40,6 @@ test {
4140
}
4241
}
4342

44-
jacoco {
45-
toolVersion = jacocoVersion // See http://www.eclemma.org/jacoco/.
46-
}
47-
4843
jacocoTestReport {
4944
reports {
5045
xml.enabled = true

framework/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ def versions = [
1515
checkstyle: '8.7',
1616
]
1717

18-
jacoco {
19-
toolVersion = "0.8.1"
20-
}
2118

2219

2320
configurations {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package org.tron.common.logsfilter;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.spy;
5+
import static org.mockito.Mockito.when;
6+
7+
import com.google.protobuf.ByteString;
8+
9+
import java.lang.reflect.Method;
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
14+
import org.junit.After;
15+
import org.junit.Assert;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.tron.common.logsfilter.capsule.TransactionLogTriggerCapsule;
19+
import org.tron.common.logsfilter.trigger.InternalTransactionPojo;
20+
import org.tron.common.runtime.InternalTransaction;
21+
import org.tron.common.runtime.ProgramResult;
22+
import org.tron.common.runtime.RuntimeImpl;
23+
import org.tron.common.utils.Sha256Hash;
24+
import org.tron.core.capsule.BlockCapsule;
25+
import org.tron.core.capsule.ReceiptCapsule;
26+
import org.tron.core.capsule.TransactionCapsule;
27+
import org.tron.core.db.TransactionTrace;
28+
import org.tron.p2p.utils.ByteArray;
29+
import org.tron.protos.Protocol;
30+
import org.tron.protos.contract.BalanceContract;
31+
32+
33+
34+
35+
public class TransactionLogTriggerCapsuleMockTest {
36+
37+
private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
38+
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";
39+
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";
40+
41+
private TransactionCapsule transactionCapsule;
42+
private BlockCapsule blockCapsule;
43+
44+
@Before
45+
public void setup() {
46+
blockCapsule = new BlockCapsule(1,
47+
Sha256Hash.ZERO_HASH,
48+
System.currentTimeMillis(),
49+
Sha256Hash.ZERO_HASH.getByteString()
50+
);
51+
}
52+
53+
@After
54+
public void clearMocks() {
55+
56+
}
57+
58+
59+
@Test
60+
public void testConstructorWithTransactionTrace() {
61+
BalanceContract.TransferContract.Builder builder2 =
62+
BalanceContract.TransferContract.newBuilder()
63+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
64+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
65+
transactionCapsule = spy(new TransactionCapsule(builder2.build(),
66+
Protocol.Transaction.Contract.ContractType.TransferContract));
67+
68+
TransactionTrace trace = mock(TransactionTrace.class);
69+
ReceiptCapsule receiptCapsule = new ReceiptCapsule(Sha256Hash.ZERO_HASH);
70+
RuntimeImpl runtime = mock(RuntimeImpl.class);
71+
List<Protocol.TransactionInfo.Log> logs = new ArrayList<>();
72+
logs.add(Protocol.TransactionInfo.Log.newBuilder()
73+
.setAddress(ByteString.copyFrom("address".getBytes()))
74+
.setData(ByteString.copyFrom("data".getBytes()))
75+
.addTopics(ByteString.copyFrom("topic".getBytes()))
76+
.build());
77+
78+
Protocol.TransactionInfo.Builder builder = Protocol.TransactionInfo.newBuilder()
79+
.addAllLog(logs);
80+
81+
ProgramResult programResult = ProgramResult.createEmpty();
82+
programResult.setHReturn("hreturn".getBytes());
83+
programResult.setContractAddress(CONTRACT_ADDRESS.getBytes());
84+
85+
when(transactionCapsule.getTrxTrace()).thenReturn(trace);
86+
when(trace.getReceipt()).thenReturn(receiptCapsule);
87+
when(trace.getRuntime()).thenReturn(runtime);
88+
when(runtime.getResult()).thenReturn(programResult);
89+
90+
transactionCapsule.setTrxTrace(trace);
91+
92+
TransactionLogTriggerCapsule triggerCapsule = new TransactionLogTriggerCapsule(
93+
transactionCapsule, blockCapsule,0,0,0,
94+
builder.build(),0);
95+
96+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger());
97+
}
98+
99+
@Test
100+
public void testGetInternalTransactionList() throws Exception {
101+
BalanceContract.TransferContract.Builder builder2 =
102+
BalanceContract.TransferContract.newBuilder()
103+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
104+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
105+
transactionCapsule = new TransactionCapsule(builder2.build(),
106+
Protocol.Transaction.Contract.ContractType.TransferContract);
107+
InternalTransaction internalTransaction = new InternalTransaction(
108+
"parentHash".getBytes(), 10, 0,
109+
"sendAddress".getBytes(),
110+
"transferToAddress".getBytes(),
111+
100L, "data".getBytes(), "note",
112+
0L, new HashMap<>()
113+
);
114+
List<InternalTransaction> internalTransactionList = new ArrayList<>();
115+
internalTransactionList.add(internalTransaction);
116+
TransactionLogTriggerCapsule triggerCapsule =
117+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
118+
119+
Method privateMethod = TransactionLogTriggerCapsule.class.getDeclaredMethod(
120+
"getInternalTransactionList", List.class);
121+
privateMethod.setAccessible(true);
122+
List<InternalTransactionPojo> pojoList = (List<InternalTransactionPojo>)
123+
privateMethod.invoke(triggerCapsule, internalTransactionList);
124+
125+
Assert.assertNotNull(pojoList);
126+
}
127+
128+
}

framework/src/test/java/org/tron/common/logsfilter/TransactionLogTriggerCapsuleTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import org.tron.core.capsule.TransactionCapsule;
1414
import org.tron.p2p.utils.ByteArray;
1515
import org.tron.protos.Protocol;
16+
import org.tron.protos.contract.AssetIssueContractOuterClass;
1617
import org.tron.protos.contract.BalanceContract;
1718
import org.tron.protos.contract.Common;
19+
import org.tron.protos.contract.SmartContractOuterClass;
1820

1921
public class TransactionLogTriggerCapsuleTest {
2022

2123
private static final String OWNER_ADDRESS = "41548794500882809695a8a687866e76d4271a1abc";
2224
private static final String RECEIVER_ADDRESS = "41abd4b9367799eaa3197fecb144eb71de1e049150";
25+
private static final String CONTRACT_ADDRESS = "A0B4750E2CD76E19DCA331BF5D089B71C3C2798548";
2326

2427
public TransactionCapsule transactionCapsule;
2528
public BlockCapsule blockCapsule;
@@ -175,4 +178,70 @@ public void testConstructorWithCancelAllUnfreezeTrxCapsule() {
175178
triggerCapsule.getTransactionLogTrigger().getExtMap().get(BANDWIDTH.name()).longValue());
176179
}
177180

181+
182+
@Test
183+
public void testConstructorWithTransferCapsule() {
184+
BalanceContract.TransferContract.Builder builder2 =
185+
BalanceContract.TransferContract.newBuilder()
186+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
187+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
188+
transactionCapsule = new TransactionCapsule(builder2.build(),
189+
Protocol.Transaction.Contract.ContractType.TransferContract);
190+
191+
TransactionLogTriggerCapsule triggerCapsule =
192+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
193+
194+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
195+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
196+
}
197+
198+
@Test
199+
public void testConstructorWithTransferAssetCapsule() {
200+
AssetIssueContractOuterClass.TransferAssetContract.Builder builder2 =
201+
AssetIssueContractOuterClass.TransferAssetContract.newBuilder()
202+
.setAssetName(ByteString.copyFrom("AssetName".getBytes()))
203+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
204+
.setToAddress(ByteString.copyFrom(ByteArray.fromHexString(RECEIVER_ADDRESS)));
205+
transactionCapsule = new TransactionCapsule(builder2.build(),
206+
Protocol.Transaction.Contract.ContractType.TransferAssetContract);
207+
208+
TransactionLogTriggerCapsule triggerCapsule =
209+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
210+
211+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
212+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
213+
}
214+
215+
@Test
216+
public void testConstructorWithTriggerSmartContract() {
217+
SmartContractOuterClass.TriggerSmartContract.Builder builder2 =
218+
SmartContractOuterClass.TriggerSmartContract.newBuilder()
219+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)))
220+
.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS)));
221+
transactionCapsule = new TransactionCapsule(builder2.build(),
222+
Protocol.Transaction.Contract.ContractType.TriggerSmartContract);
223+
224+
TransactionLogTriggerCapsule triggerCapsule =
225+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
226+
227+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
228+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getToAddress());
229+
}
230+
231+
@Test
232+
public void testConstructorWithCreateSmartContract() {
233+
SmartContractOuterClass.CreateSmartContract.Builder builder2 =
234+
SmartContractOuterClass.CreateSmartContract.newBuilder()
235+
.setOwnerAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)));
236+
transactionCapsule = new TransactionCapsule(builder2.build(),
237+
Protocol.Transaction.Contract.ContractType.CreateSmartContract);
238+
239+
TransactionLogTriggerCapsule triggerCapsule =
240+
new TransactionLogTriggerCapsule(transactionCapsule, blockCapsule);
241+
242+
Assert.assertNotNull(triggerCapsule.getTransactionLogTrigger().getFromAddress());
243+
}
244+
245+
246+
178247
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.tron.common.runtime;
2+
3+
import java.lang.reflect.Method;
4+
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.After;
7+
import org.junit.Test;
8+
import org.tron.core.vm.program.Program;
9+
10+
11+
12+
@Slf4j
13+
public class RuntimeImplMockTest {
14+
@After
15+
public void clearMocks() {
16+
17+
}
18+
19+
@Test
20+
public void testSetResultCode1() throws Exception {
21+
RuntimeImpl runtime = new RuntimeImpl();
22+
ProgramResult programResult = new ProgramResult();
23+
Method privateMethod = RuntimeImpl.class.getDeclaredMethod(
24+
"setResultCode", ProgramResult.class);
25+
privateMethod.setAccessible(true);
26+
27+
Program.BadJumpDestinationException badJumpDestinationException
28+
= new Program.BadJumpDestinationException("Operation with pc isn't 'JUMPDEST': PC[%d];", 0);
29+
programResult.setException(badJumpDestinationException);
30+
privateMethod.invoke(runtime, programResult);
31+
32+
Program.OutOfTimeException outOfTimeException
33+
= new Program.OutOfTimeException("CPU timeout for 0x0a executing");
34+
programResult.setException(outOfTimeException);
35+
privateMethod.invoke(runtime, programResult);
36+
37+
Program.PrecompiledContractException precompiledContractException
38+
= new Program.PrecompiledContractException("precompiled contract exception");
39+
programResult.setException(precompiledContractException);
40+
privateMethod.invoke(runtime, programResult);
41+
42+
Program.StackTooSmallException stackTooSmallException
43+
= new Program.StackTooSmallException("Expected stack size %d but actual %d;", 100, 10);
44+
programResult.setException(stackTooSmallException);
45+
privateMethod.invoke(runtime, programResult);
46+
47+
Program.JVMStackOverFlowException jvmStackOverFlowException
48+
= new Program.JVMStackOverFlowException();
49+
programResult.setException(jvmStackOverFlowException);
50+
privateMethod.invoke(runtime, programResult);
51+
}
52+
53+
}

0 commit comments

Comments
 (0)