@@ -44,11 +44,12 @@ const MAX_RECEIPT_VALUE: u128 = GRT_BASE / 1_000;
4444const WAIT_TIME_BATCHES : u64 = 40 ;
4545
4646// Bellow constant is to define the number of receipts
47- const NUM_RECEIPTS : u32 = 200 ;
47+ const NUM_RECEIPTS : u32 = 70 ;
4848
4949// Send receipts in batches with a delay in between
5050// to ensure some receipts get outside the timestamp buffer
51- const BATCHES : u32 = 4 ;
51+ const BATCHES : u32 = 2 ;
52+ const MAX_TRIGGERS : usize = 100 ;
5253
5354#[ tokio:: main]
5455async fn main ( ) -> Result < ( ) > {
@@ -82,9 +83,6 @@ async fn tap_rav_test() -> Result<()> {
8283 . send ( )
8384 . await ?;
8485
85- // Default to a fallback allocation ID
86- let mut allocation_id = Address :: from_str ( "0x0000000000000000000000000000000000000000" ) ?;
87-
8886 if !response. status ( ) . is_success ( ) {
8987 return Err ( anyhow:: anyhow!(
9088 "Network subgraph request failed with status: {}" ,
@@ -96,9 +94,6 @@ async fn tap_rav_test() -> Result<()> {
9694 let response_text = response. text ( ) . await ?;
9795 println ! ( "Network subgraph response: {}" , response_text) ;
9896
99- // Extract allocation_id, if not found return an error
100- // this should not happen as we run the fund escrow script
101- // before(in theory tho)
10297 let json_value = serde_json:: from_str :: < serde_json:: Value > ( & response_text) ?;
10398 let allocation_id = json_value
10499 . get ( "data" )
@@ -115,52 +110,44 @@ async fn tap_rav_test() -> Result<()> {
115110 let metrics_checker =
116111 MetricsChecker :: new ( http_client. clone ( ) , TAP_AGENT_METRICS_URL . to_string ( ) ) ;
117112
113+ // First check initial metrics
118114 let initial_metrics = metrics_checker. get_current_metrics ( ) . await ?;
119- // Extract the initial metrics we care about
120- // in this case the number of created/requested ravs
121- // and the amount of unaggregated fees
122- // so later at the end of the test we compare them
123- // to see if we triggered any RAV generation
124115 let initial_ravs_created =
125116 initial_metrics. ravs_created_by_allocation ( & allocation_id. to_string ( ) ) ;
126117 let initial_unaggregated =
127118 initial_metrics. unaggregated_fees_by_allocation ( & allocation_id. to_string ( ) ) ;
128119
129120 println ! (
130- "\n === Sending {} receipts to trigger RAV generation ===" ,
131- NUM_RECEIPTS
132- ) ;
133- println ! (
134- "Each receipt value: {} GRT" ,
135- MAX_RECEIPT_VALUE as f64 / GRT_BASE as f64
136- ) ;
137- println ! (
138- "Total value to be sent: {} GRT" ,
139- ( MAX_RECEIPT_VALUE as f64 * NUM_RECEIPTS as f64 ) / GRT_BASE as f64
121+ "\n === Initial metrics: RAVs created: {}, Unaggregated fees: {} ===" ,
122+ initial_ravs_created, initial_unaggregated
140123 ) ;
141124
142- let receipts_per_batch = NUM_RECEIPTS / BATCHES ;
125+ // Calculate required receipts to trigger RAV
126+ // With MAX_RECEIPT_VALUE = GRT_BASE / 1_000 (0.001 GRT)
127+ // And trigger_value = 0.002 GRT
128+ // We need at least 3 receipts to trigger a RAV (0.003 GRT > 0.002 GRT)
129+ const RECEIPTS_NEEDED : u32 = 3 ;
130+
131+ println ! ( "\n === STAGE 1: Sending large receipt batches with small pauses ===" ) ;
132+
133+ // Send multiple receipts in two batches with a gap between them
143134 let mut total_successful = 0 ;
144135
145- for batch in 0 ..BATCHES {
136+ for batch in 0 ..2 {
146137 println ! (
147- "Sending batch {} of {} ( {} receipts per batch) " ,
138+ "Sending batch {} of 2 with {} receipts each... " ,
148139 batch + 1 ,
149- BATCHES ,
150- receipts_per_batch
140+ RECEIPTS_NEEDED
151141 ) ;
152142
153- for i in 0 ..receipts_per_batch {
154- let receipt_index = batch * receipts_per_batch + i;
155- println ! ( "Sending receipt {} of {}" , receipt_index + 1 , NUM_RECEIPTS ) ;
156-
157- // Create TAP receipt
143+ for i in 0 ..RECEIPTS_NEEDED {
158144 let receipt = create_tap_receipt (
159145 MAX_RECEIPT_VALUE ,
160146 & allocation_id,
161147 TAP_ESCROW_CONTRACT ,
162148 & wallet,
163149 ) ?;
150+
164151 let receipt_json = serde_json:: to_string ( & receipt) . unwrap ( ) ;
165152
166153 let response = http_client
@@ -175,81 +162,112 @@ async fn tap_rav_test() -> Result<()> {
175162 . send ( )
176163 . await ?;
177164
178- let status = response. status ( ) ;
179- if status. is_success ( ) {
165+ if response. status ( ) . is_success ( ) {
180166 total_successful += 1 ;
181- println ! ( "Receipt {} sent successfully" , receipt_index + 1 ) ;
167+ println ! ( "Receipt {} of batch {} sent successfully" , i + 1 , batch + 1 ) ;
182168 } else {
183- println ! ( "Failed to send receipt {}: {}" , receipt_index + 1 , status) ;
184- let response_text = response. text ( ) . await ?;
185- println ! ( "Response: {}" , response_text) ;
169+ println ! ( "Failed to send receipt: {}" , response. status( ) ) ;
186170 }
187171
188- // Small delay between queries to avoid flooding
172+ // Small pause between receipts within batch
189173 tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
190174 }
191175
192- // After each batch, wait longer than the timestamp buffer
193- // (typically 30 seconds) to ensure receipts are outside buffer
194- if batch < BATCHES - 1 {
195- println ! (
196- "\n Batch {} complete. Waiting to exceed timestamp buffer..." ,
197- batch + 1
198- ) ;
199- tokio:: time:: sleep ( Duration :: from_secs ( WAIT_TIME_BATCHES * 2 ) ) . await ;
176+ // Check metrics after batch
177+ let batch_metrics = metrics_checker. get_current_metrics ( ) . await ?;
178+ println ! (
179+ "After batch {}: RAVs created: {}, Unaggregated fees: {}" ,
180+ batch + 1 ,
181+ batch_metrics. ravs_created_by_allocation( & allocation_id. to_string( ) ) ,
182+ batch_metrics. unaggregated_fees_by_allocation( & allocation_id. to_string( ) )
183+ ) ;
184+
185+ // Wait between batches - long enough for first batch to exit buffer
186+ if batch < 1 {
187+ println ! ( "Waiting for buffer period + 5s..." ) ;
188+ tokio:: time:: sleep ( Duration :: from_secs ( WAIT_TIME_BATCHES + 5 ) ) . await ;
200189 }
201190 }
202191
203- println ! ( "\n === Summary ===" ) ;
204- println ! (
205- "Total receipts sent successfully: {}/{}" ,
206- total_successful, NUM_RECEIPTS
207- ) ;
208- println ! (
209- "Total value sent: {} GRT" ,
210- ( MAX_RECEIPT_VALUE as f64 * total_successful as f64 ) / GRT_BASE as f64
211- ) ;
212-
213- // Give the system enough time to process the receipts
214- // ensuring the aged beyong timestamp buffer
215- tokio:: time:: sleep ( Duration :: from_secs ( WAIT_TIME_BATCHES * 4 ) ) . await ;
216-
217- // Check for RAV generation
218- println ! ( "\n === Checking for RAV generation ===" ) ;
219-
220- // Get final metrics
221- println ! ( "Getting final metrics snapshot..." ) ;
222- let final_metrics = metrics_checker. get_current_metrics ( ) . await ?;
192+ println ! ( "\n === STAGE 2: Sending continuous trigger receipts ===" ) ;
193+
194+ // Now send a series of regular receipts with short intervals until RAV is detected
195+ for i in 0 ..MAX_TRIGGERS {
196+ println ! ( "Sending trigger receipt {}/{}..." , i + 1 , MAX_TRIGGERS ) ;
197+
198+ let trigger_receipt = create_tap_receipt (
199+ MAX_RECEIPT_VALUE ,
200+ & allocation_id,
201+ TAP_ESCROW_CONTRACT ,
202+ & wallet,
203+ ) ?;
204+
205+ let receipt_json = serde_json:: to_string ( & trigger_receipt) . unwrap ( ) ;
206+
207+ let response = http_client
208+ . post ( format ! ( "{}/api/subgraphs/id/{}" , GATEWAY_URL , SUBGRAPH_ID ) )
209+ . header ( "Content-Type" , "application/json" )
210+ . header ( "Authorization" , format ! ( "Bearer {}" , GATEWAY_API_KEY ) )
211+ . header ( "Tap-Receipt" , receipt_json)
212+ . json ( & json ! ( {
213+ "query" : "{ _meta { block { number } } }"
214+ } ) )
215+ . timeout ( Duration :: from_secs ( 10 ) )
216+ . send ( )
217+ . await ?;
218+
219+ if response. status ( ) . is_success ( ) {
220+ total_successful += 1 ;
221+ println ! ( "Trigger receipt {} sent successfully" , i + 1 ) ;
222+ } else {
223+ return Err ( anyhow:: anyhow!(
224+ "Failed to send trigger receipt: {}" ,
225+ response. status( )
226+ ) ) ;
227+ }
223228
224- // Extract the final metrics
225- let final_ravs_created = final_metrics. ravs_created_by_allocation ( & allocation_id. to_string ( ) ) ;
226- let final_unaggregated =
227- final_metrics. unaggregated_fees_by_allocation ( & allocation_id. to_string ( ) ) ;
229+ // Check after each trigger
230+ tokio:: time:: sleep ( Duration :: from_secs ( 5 ) ) . await ;
228231
229- println ! (
230- "Final state: RAVs created: {}, Unaggregated fees: {}" ,
231- final_ravs_created, final_unaggregated
232- ) ;
232+ let current_metrics = metrics_checker. get_current_metrics ( ) . await ?;
233+ let current_ravs_created =
234+ current_metrics. ravs_created_by_allocation ( & allocation_id. to_string ( ) ) ;
235+ let current_unaggregated =
236+ current_metrics. unaggregated_fees_by_allocation ( & allocation_id. to_string ( ) ) ;
233237
234- // Check for success criteria
235- if final_ravs_created > initial_ravs_created {
236238 println ! (
237- "✅ TEST PASSED: RAVs created increased from {} to {}!" ,
238- initial_ravs_created, final_ravs_created
239+ "After trigger {}: RAVs created: {}, Unaggregated fees: {}" ,
240+ i + 1 ,
241+ current_ravs_created,
242+ current_unaggregated
239243 ) ;
240- return Ok ( ( ) ) ;
241- }
242244
243- if final_unaggregated < initial_unaggregated * 0.9 {
244- println ! (
245- "✅ TEST PASSED: Unaggregated fees decreased significantly from {} to {}!" ,
246- initial_unaggregated, final_unaggregated
247- ) ;
248- return Ok ( ( ) ) ;
245+ // If we've succeeded, exit early
246+ if current_ravs_created > initial_ravs_created {
247+ println ! (
248+ "✅ TEST PASSED: RAVs created increased from {} to {}!" ,
249+ initial_ravs_created, current_ravs_created
250+ ) ;
251+ return Ok ( ( ) ) ;
252+ }
253+
254+ if current_unaggregated < initial_unaggregated * 0.9 {
255+ println ! (
256+ "✅ TEST PASSED: Unaggregated fees decreased significantly from {} to {}!" ,
257+ initial_unaggregated, current_unaggregated
258+ ) ;
259+ return Ok ( ( ) ) ;
260+ }
249261 }
250262
263+ println ! ( "\n === Summary ===" ) ;
264+ println ! ( "Total receipts sent successfully: {}" , total_successful) ;
265+ println ! (
266+ "Total value sent: {} GRT" ,
267+ ( MAX_RECEIPT_VALUE as f64 * total_successful as f64 ) / GRT_BASE as f64
268+ ) ;
269+
251270 // If we got here, test failed
252271 println ! ( "❌ TEST FAILED: No RAV generation detected" ) ;
253-
254272 Err ( anyhow:: anyhow!( "Failed to detect RAV generation" ) )
255273}
0 commit comments