Skip to content

Commit a6bf35c

Browse files
author
Liulei
committed
feat(test): refactor codes
1 parent b168067 commit a6bf35c

File tree

5 files changed

+8440
-23
lines changed

5 files changed

+8440
-23
lines changed

framework/src/main/java/org/tron/core/zen/KZGPointEvaluationInitService.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,42 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.InputStream;
6+
import java.util.concurrent.atomic.AtomicBoolean;
67
import javax.annotation.PostConstruct;
7-
8-
import org.tron.common.crypto.ckzg4844.CKZG4844JNI;
98
import lombok.extern.slf4j.Slf4j;
109
import org.apache.commons.io.FileUtils;
1110
import org.springframework.stereotype.Component;
11+
import org.tron.common.crypto.ckzg4844.CKZG4844JNI;
1212

1313
@Slf4j
1414
@Component
1515
public class KZGPointEvaluationInitService {
1616

17+
private static final AtomicBoolean loaded = new AtomicBoolean(false);
18+
1719
@PostConstruct
1820
private void init() {
1921
initCKZG4844();
2022
}
2123

2224
public static void freeSetup() {
23-
CKZG4844JNI.freeTrustedSetup();
25+
if (loaded.compareAndSet(true, false)) {
26+
CKZG4844JNI.freeTrustedSetup();
27+
}
2428
}
2529

2630
public static void initCKZG4844() {
27-
logger.info("init ckzg 4844 begin");
31+
if (loaded.compareAndSet(false, true)) {
32+
logger.info("init ckzg 4844 begin");
2833

29-
CKZG4844JNI.loadNativeLibrary();
34+
CKZG4844JNI.loadNativeLibrary();
3035

31-
String setupFile = getSetupFile("trusted_setup.txt");
36+
String setupFile = getSetupFile("trusted_setup.txt");
3237

33-
CKZG4844JNI.loadTrustedSetup(setupFile, 0);
38+
CKZG4844JNI.loadTrustedSetup(setupFile, 0);
3439

35-
logger.info("init ckzg 4844 done");
40+
logger.info("init ckzg 4844 done");
41+
}
3642
}
3743

3844
private static String getSetupFile(String fileName) {

framework/src/test/java/org/tron/common/runtime/vm/PrecompiledContractsTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import org.tron.core.vm.config.VMConfig;
4141
import org.tron.core.vm.repository.Repository;
4242
import org.tron.core.vm.repository.RepositoryImpl;
43-
import org.tron.core.zen.KZGPointEvaluationInitService;
44-
import org.tron.core.zen.ZksnarkInitService;
4543
import org.tron.protos.Protocol;
4644
import org.tron.protos.Protocol.AccountType;
4745
import org.tron.protos.Protocol.Proposal.State;
@@ -1179,13 +1177,14 @@ public void kzgPointEvaluationTest() {
11791177
Repository tempRepository = RepositoryImpl.createRoot(StoreFactory.getInstance());
11801178
contract.setRepository(tempRepository);
11811179

1182-
byte[] versionedHash = Hex.decode("015a4cab4911426699ed34483de6640cf55a568afc5c5edffdcbd8bcd4452f68");
1180+
byte[] versionedHash =
1181+
Hex.decode("015a4cab4911426699ed34483de6640cf55a568afc5c5edffdcbd8bcd4452f68");
11831182
byte[] z = Hex.decode("0000000000000000000000000000000000000000000000000000000000000065");
11841183
byte[] y = Hex.decode("60f557194475973322b33dc989896381844508234bfa6fbeefe5fa165ae15a0a");
1185-
byte[] commitment = Hex.decode("a70477b56251e8770969c83eaed665d3ab99b96b72270a4" +
1186-
"1009f2752b5c06a06bd089ad48952c12b1dbf83dccd9d373f");
1187-
byte[] proof = Hex.decode("879f9a41956deae578bc65e7133f164394b8677bc2e7b1356be61" +
1188-
"d47720ed2a3326bfddebc67cd37ee9e7537d7814afe");
1184+
byte[] commitment = Hex.decode("a70477b56251e8770969c83eaed665d3ab99b96b72270a4"
1185+
+ "1009f2752b5c06a06bd089ad48952c12b1dbf83dccd9d373f");
1186+
byte[] proof = Hex.decode("879f9a41956deae578bc65e7133f164394b8677bc2e7b1356be61"
1187+
+ "d47720ed2a3326bfddebc67cd37ee9e7537d7814afe");
11891188

11901189
byte[] data = new byte[192];
11911190
System.arraycopy(versionedHash, 0, data, 0, 32);
@@ -1213,8 +1212,8 @@ public void kzgPointEvaluationTest() {
12131212

12141213
res = contract.execute(data);
12151214
byte[] expected = Hex.decode(
1216-
"0000000000000000000000000000000000000000000000000000000000001000" +
1217-
"73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
1215+
"0000000000000000000000000000000000000000000000000000000000001000"
1216+
+ "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
12181217
Assert.assertTrue(res.getLeft());
12191218
Assert.assertArrayEquals(expected, res.getRight());
12201219
VMConfig.initAllowTvmBlob(0);

framework/src/test/java/org/tron/core/actuator/utils/ProposalUtilTest.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ public void validateCheck() {
435435

436436
testConsensusLogicOptimizationProposal();
437437

438+
testAllowTvmCancunProposal();
439+
440+
testAllowTvmBlobProposal();
441+
438442
forkUtils.getManager().getDynamicPropertiesStore()
439443
.statsByVersion(ForkBlockVersionEnum.ENERGY_LIMIT.getValue(), stats);
440444
forkUtils.reset();
@@ -551,6 +555,110 @@ private void testConsensusLogicOptimizationProposal() {
551555

552556
}
553557

558+
private void testAllowTvmCancunProposal() {
559+
byte[] stats = new byte[27];
560+
forkUtils.getManager().getDynamicPropertiesStore()
561+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0.getValue(), stats);
562+
try {
563+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
564+
ProposalType.ALLOW_TVM_CANCUN.getCode(), 1);
565+
Assert.fail();
566+
} catch (ContractValidateException e) {
567+
Assert.assertEquals(
568+
"Bad chain parameter id [ALLOW_TVM_CANCUN]",
569+
e.getMessage());
570+
}
571+
572+
long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore()
573+
.getMaintenanceTimeInterval();
574+
575+
long hardForkTime =
576+
((ForkBlockVersionEnum.VERSION_4_8_0.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
577+
* maintenanceTimeInterval;
578+
forkUtils.getManager().getDynamicPropertiesStore()
579+
.saveLatestBlockHeaderTimestamp(hardForkTime + 1);
580+
581+
stats = new byte[27];
582+
Arrays.fill(stats, (byte) 1);
583+
forkUtils.getManager().getDynamicPropertiesStore()
584+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0.getValue(), stats);
585+
586+
// Should fail because the proposal value is invalid
587+
try {
588+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
589+
ProposalType.ALLOW_TVM_CANCUN.getCode(), 2);
590+
Assert.fail();
591+
} catch (ContractValidateException e) {
592+
Assert.assertEquals(
593+
"This value[ALLOW_TVM_CANCUN] is only allowed to be 1",
594+
e.getMessage());
595+
}
596+
597+
dynamicPropertiesStore.saveAllowTvmCancun(1);
598+
try {
599+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
600+
ProposalType.ALLOW_TVM_CANCUN.getCode(), 1);
601+
Assert.fail();
602+
} catch (ContractValidateException e) {
603+
Assert.assertEquals(
604+
"[ALLOW_TVM_CANCUN] has been valid, no need to propose again",
605+
e.getMessage());
606+
}
607+
608+
}
609+
610+
private void testAllowTvmBlobProposal() {
611+
byte[] stats = new byte[27];
612+
forkUtils.getManager().getDynamicPropertiesStore()
613+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0.getValue(), stats);
614+
try {
615+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
616+
ProposalType.ALLOW_TVM_BLOB.getCode(), 1);
617+
Assert.fail();
618+
} catch (ContractValidateException e) {
619+
Assert.assertEquals(
620+
"Bad chain parameter id [ALLOW_TVM_BLOB]",
621+
e.getMessage());
622+
}
623+
624+
long maintenanceTimeInterval = forkUtils.getManager().getDynamicPropertiesStore()
625+
.getMaintenanceTimeInterval();
626+
627+
long hardForkTime =
628+
((ForkBlockVersionEnum.VERSION_4_8_0.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
629+
* maintenanceTimeInterval;
630+
forkUtils.getManager().getDynamicPropertiesStore()
631+
.saveLatestBlockHeaderTimestamp(hardForkTime + 1);
632+
633+
stats = new byte[27];
634+
Arrays.fill(stats, (byte) 1);
635+
forkUtils.getManager().getDynamicPropertiesStore()
636+
.statsByVersion(ForkBlockVersionEnum.VERSION_4_8_0.getValue(), stats);
637+
638+
// Should fail because the proposal value is invalid
639+
try {
640+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
641+
ProposalType.ALLOW_TVM_BLOB.getCode(), 2);
642+
Assert.fail();
643+
} catch (ContractValidateException e) {
644+
Assert.assertEquals(
645+
"This value[ALLOW_TVM_BLOB] is only allowed to be 1",
646+
e.getMessage());
647+
}
648+
649+
dynamicPropertiesStore.saveAllowTvmBlob(1);
650+
try {
651+
ProposalUtil.validator(dynamicPropertiesStore, forkUtils,
652+
ProposalType.ALLOW_TVM_BLOB.getCode(), 1);
653+
Assert.fail();
654+
} catch (ContractValidateException e) {
655+
Assert.assertEquals(
656+
"[ALLOW_TVM_BLOB] has been valid, no need to propose again",
657+
e.getMessage());
658+
}
659+
660+
}
661+
554662
@Test
555663
public void blockVersionCheck() {
556664
for (ForkBlockVersionEnum forkVersion : ForkBlockVersionEnum.values()) {

framework/src/test/java/org/tron/core/zksnark/KZGPointEvaluationTest.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package org.tron.core.zksnark;
22

3+
import java.util.Arrays;
34
import lombok.extern.slf4j.Slf4j;
45
import org.bouncycastle.util.encoders.Hex;
6+
import org.junit.Assert;
57
import org.junit.Test;
68
import org.tron.common.crypto.ckzg4844.CKZG4844JNI;
9+
import org.tron.common.crypto.ckzg4844.CKZGException;
10+
import org.tron.common.crypto.ckzg4844.CellsAndProofs;
11+
import org.tron.common.crypto.ckzg4844.ProofAndY;
712
import org.tron.core.zen.KZGPointEvaluationInitService;
813

9-
import static org.junit.Assert.assertArrayEquals;
10-
import static org.junit.Assert.assertTrue;
11-
1214
@Slf4j
1315
public class KZGPointEvaluationTest {
1416

@@ -22,16 +24,59 @@ public void testVerifyBlobKzgProof() {
2224
byte[] expectedCommitment =
2325
Hex.decode("a70477b56251e8770969c83eaed665d3ab99b96b72270a41009f27"
2426
+ "52b5c06a06bd089ad48952c12b1dbf83dccd9d373f");
25-
assertArrayEquals(expectedCommitment, commitment);
27+
Assert.assertArrayEquals(expectedCommitment, commitment);
2628

2729
byte[] proof = CKZG4844JNI.computeBlobKzgProof(blob, commitment);
2830
byte[] expectedProof =
2931
Hex.decode("b7f576f2442febaa035d3c6f34bbdad6acebcaec70236ff40b3373b"
3032
+ "d2ca00547d3ca7bb1d0ed3e728ca9dab610a4cfa4");
31-
assertArrayEquals(expectedProof, proof);
33+
Assert.assertArrayEquals(expectedProof, proof);
3234

3335
boolean isValidProof = CKZG4844JNI.verifyBlobKzgProof(blob, commitment, proof);
34-
assertTrue(isValidProof);
36+
Assert.assertTrue(isValidProof);
37+
38+
byte[] z =
39+
Hex.decode("0000000000000000000000000000000000000000000000000000000000000065");
40+
byte[] y =
41+
Hex.decode("60f557194475973322b33dc989896381844508234bfa6fbeefe5fa165ae15a0a");
42+
ProofAndY proofAndY = CKZG4844JNI.computeKzgProof(blob, z);
43+
byte[] expectedZProof =
44+
Hex.decode("879f9a41956deae578bc65e7133f164394b8677bc2e7b1356be61"
45+
+ "d47720ed2a3326bfddebc67cd37ee9e7537d7814afe");
46+
Assert.assertArrayEquals(y, proofAndY.getY());
47+
Assert.assertArrayEquals(expectedZProof, proofAndY.getProof());
48+
49+
CellsAndProofs cellsAndProofs = CKZG4844JNI.computeCellsAndKzgProofs(blob);
50+
byte[] blockCells = Arrays.copyOfRange(cellsAndProofs.getCells(), 0, 2048);
51+
byte[] blockProof = Arrays.copyOfRange(cellsAndProofs.getProofs(), 0, 48);
52+
long[] cellIndices = new long[1];
53+
boolean isValid =
54+
CKZG4844JNI.verifyCellKzgProofBatch(commitment, cellIndices, blockCells, blockProof);
55+
Assert.assertTrue(isValid);
56+
57+
long[] errorCellIndices = new long[2];
58+
errorCellIndices[1] = 1;
59+
CKZGException expectedE = new CKZGException(1,
60+
"Invalid cellIndices size. Expected 1 bytes but got 2.");
61+
try {
62+
CKZG4844JNI.verifyCellKzgProofBatch(commitment, errorCellIndices, blockCells, blockProof);
63+
} catch (Exception e) {
64+
Assert.assertEquals(CKZGException.class, e.getClass());
65+
CKZGException exception = (CKZGException) e;
66+
Assert.assertEquals(expectedE.getError(), exception.getError());
67+
Assert.assertEquals(expectedE.getErrorMessage(), exception.getErrorMessage());
68+
}
69+
70+
try {
71+
String testSetupResource = "/kzg-trusted-setups/test.txt";
72+
CKZG4844JNI.loadTrustedSetupFromResource(
73+
testSetupResource, KZGPointEvaluationTest.class, 0);
74+
} catch (Exception e) {
75+
Assert.assertEquals(RuntimeException.class, e.getClass());
76+
Assert.assertEquals(
77+
"Trusted Setup is already loaded. Free it before loading a new one.",
78+
e.getMessage());
79+
}
3580

3681
KZGPointEvaluationInitService.freeSetup();
3782
}

0 commit comments

Comments
 (0)