11package com .unicity .sdk .e2e ;
22
33import com .unicity .sdk .api .AggregatorClient ;
4+ import com .unicity .sdk .api .Authenticator ;
5+ import com .unicity .sdk .api .RequestId ;
6+ import com .unicity .sdk .api .SubmitCommitmentStatus ;
7+ import com .unicity .sdk .shared .hash .DataHasher ;
8+ import com .unicity .sdk .shared .hash .HashAlgorithm ;
9+ import com .unicity .sdk .shared .signing .SigningService ;
410import org .junit .jupiter .api .Test ;
511import org .junit .jupiter .api .condition .EnabledIfEnvironmentVariable ;
612
13+ import java .security .SecureRandom ;
14+ import java .util .concurrent .ExecutorService ;
15+ import java .util .function .Function ;
16+
717import static org .junit .jupiter .api .Assertions .*;
818
919/**
@@ -24,4 +34,106 @@ void testVerifyBlockHeight() throws Exception {
2434 assertNotNull (blockHeight );
2535 assertTrue (blockHeight > 0 );
2636 }
27- }
37+
38+ @ Test
39+ void testCommitmentPerformance () throws Exception {
40+ String aggregatorUrl = System .getenv ("AGGREGATOR_URL" );
41+ assertNotNull (aggregatorUrl , "AGGREGATOR_URL environment variable must be set" );
42+
43+ AggregatorClient aggregatorClient = new AggregatorClient (aggregatorUrl );
44+
45+ long startTime = System .currentTimeMillis ();
46+ var sr = new SecureRandom ();
47+ byte [] randomSecret = new byte [32 ];
48+ sr .nextBytes (randomSecret );
49+ byte [] stateBytes = new byte [32 ];
50+ sr .nextBytes (stateBytes );
51+ var stateHash = DataHasher .digest (HashAlgorithm .SHA256 , stateBytes );
52+ var txDataHash = DataHasher .digest (HashAlgorithm .SHA256 , "test commitment performance" .getBytes ());
53+ var res = SigningService .createFromSecret (randomSecret , null ).thenCompose (
54+ ss -> {
55+ var req = RequestId .createFromImprint (ss .getPublicKey (), stateHash .getImprint ());
56+ var auth = Authenticator .create (ss , txDataHash , stateHash );
57+ return req .thenCombine (auth , (requestId , authenticator ) ->
58+ aggregatorClient .submitTransaction (
59+ requestId ,
60+ txDataHash ,
61+ authenticator
62+ )).thenCompose (Function .identity ());
63+ }
64+ ).get ();
65+ if (res .getStatus () != SubmitCommitmentStatus .SUCCESS ) {
66+ System .err .println ("Commitment submission failed with status: " + res .getStatus ());
67+ }
68+ long endTime = System .currentTimeMillis ();
69+
70+ long duration = endTime - startTime ;
71+ System .out .println ("Commitment submission took: " + duration + " ms" );
72+
73+ assertTrue (duration < 5000 , "Commitment submission should take less than 5 seconds" );
74+ }
75+
76+ @ Test
77+ void testCommitmentPerformanceMultiThreaded () throws Exception {
78+ int threadCount = 100 ; // configure as needed
79+ int commitmentsPerThread = 10 ; // configure as needed
80+
81+ String aggregatorUrl = System .getenv ("AGGREGATOR_URL" );
82+ assertNotNull (aggregatorUrl , "AGGREGATOR_URL environment variable must be set" );
83+
84+ AggregatorClient aggregatorClient = new AggregatorClient (aggregatorUrl );
85+ ExecutorService executor = java .util .concurrent .Executors .newFixedThreadPool (threadCount );
86+ java .util .concurrent .CountDownLatch latch = new java .util .concurrent .CountDownLatch (threadCount * commitmentsPerThread );
87+
88+ long startTime = System .currentTimeMillis ();
89+ java .util .List <java .util .concurrent .Future <Boolean >> results = new java .util .ArrayList <>();
90+
91+ for (int t = 0 ; t < threadCount ; t ++) {
92+ for (int c = 0 ; c < commitmentsPerThread ; c ++) {
93+ results .add (executor .submit (() -> {
94+ try {
95+ var sr = new SecureRandom ();
96+ byte [] randomSecret = new byte [32 ];
97+ sr .nextBytes (randomSecret );
98+ byte [] stateBytes = new byte [32 ];
99+ sr .nextBytes (stateBytes );
100+ byte [] txData = new byte [32 ];
101+ sr .nextBytes (txData );
102+ var stateHash = DataHasher .digest (HashAlgorithm .SHA256 , stateBytes );
103+ var txDataHash = DataHasher .digest (HashAlgorithm .SHA256 , txData );
104+ var res = SigningService .createFromSecret (randomSecret , null ).thenCompose (
105+ ss -> {
106+ var req = RequestId .createFromImprint (ss .getPublicKey (), stateHash .getImprint ());
107+ var auth = Authenticator .create (ss , txDataHash , stateHash );
108+ return req .thenCombine (auth , (requestId , authenticator ) ->
109+ aggregatorClient .submitTransaction (
110+ requestId ,
111+ txDataHash ,
112+ authenticator
113+ )).thenCompose (Function .identity ());
114+ }
115+ ).get ();
116+ return res .getStatus () == SubmitCommitmentStatus .SUCCESS ;
117+ } finally {
118+ latch .countDown ();
119+ }
120+ }));
121+ }
122+ }
123+
124+ latch .await ();
125+ long endTime = System .currentTimeMillis ();
126+ executor .shutdown ();
127+
128+ long duration = endTime - startTime ;
129+ long successCount = results .stream ().filter (f -> {
130+ try { return f .get (); } catch (Exception e ) { return false ; }
131+ }).count ();
132+
133+ System .out .println ("Total commitments: " + (threadCount * commitmentsPerThread ));
134+ System .out .println ("Successful: " + successCount );
135+ System .out .println ("Commitment submission took: " + duration + " ms" );
136+
137+ assertEquals (threadCount * commitmentsPerThread , successCount , "All commitments should succeed" );
138+ }
139+ }
0 commit comments