Skip to content

Commit 7f87363

Browse files
committed
chore(testing): Some enhacements and wait for the system to process receipts
1 parent 72682f1 commit 7f87363

File tree

1 file changed

+71
-77
lines changed

1 file changed

+71
-77
lines changed

rav_e2e/src/main.rs

Lines changed: 71 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,28 @@ mod receipt;
1818
use metrics::MetricsChecker;
1919
use receipt::create_tap_receipt;
2020

21-
// Constants taken from local-network/.env
22-
// it could be possible to read that file
23-
// along with the local-network/contracts.json
24-
// and regex over to get bellow values
21+
// TODO: Would be nice to read this values from:
22+
// contrib/tap-agent/config.toml
23+
// and contrib/local-network/.env
2524
const GATEWAY_URL: &str = "http://localhost:7700";
2625
const SUBGRAPH_ID: &str = "BFr2mx7FgkJ36Y6pE5BiXs1KmNUmVDCnL82KUSdcLW1g";
2726
const TAP_ESCROW_CONTRACT: &str = "0x0355B7B8cb128fA5692729Ab3AAa199C1753f726";
2827
const GATEWAY_API_KEY: &str = "deadbeefdeadbeefdeadbeefdeadbeef";
2928
const RECEIVER_ADDRESS: &str = "0xf4EF6650E48d099a4972ea5B414daB86e1998Bd3";
3029
const TAP_AGENT_METRICS_URL: &str = "http://localhost:7300/metrics";
3130

31+
const MNEMONIC: &str = "test test test test test test test test test test test junk";
32+
const GRAPH_URL: &str = "http://localhost:8000/subgraphs/name/graph-network";
33+
34+
const GRT_DECIMALS: u8 = 18;
35+
const GRT_BASE: u128 = 10u128.pow(GRT_DECIMALS as u32);
36+
37+
const MAX_AMOUNT_WILLING_TO_LOSE_GRT: u128 = 1_000 * GRT_BASE;
38+
const MAX_RECEIPT_VALUE: u128 = GRT_BASE / 1_000;
39+
// This value should match the timestamp_buffer_secs
40+
// in the tap-agent setting + 10 seconds
41+
const WAIT_TIME_BATCHES: u64 = 40;
42+
3243
#[tokio::main]
3344
async fn main() -> Result<()> {
3445
// Run the TAP receipt test
@@ -39,7 +50,7 @@ async fn tap_rav_test() -> Result<()> {
3950
// Setup wallet using your MnemonicBuilder
4051
let index: u32 = 0;
4152
let wallet: PrivateKeySigner = MnemonicBuilder::<English>::default()
42-
.phrase("test test test test test test test test test test test junk")
53+
.phrase(MNEMONIC)
4354
.index(index)
4455
.unwrap()
4556
.build()
@@ -53,45 +64,42 @@ async fn tap_rav_test() -> Result<()> {
5364

5465
// Query the network subgraph to find active allocations
5566
println!("Querying for active allocations...");
56-
let allocations_query = http_client
57-
.post("http://localhost:8000/subgraphs/name/graph-network")
67+
let response = http_client
68+
.post(GRAPH_URL)
5869
.json(&json!({
5970
"query": "{ allocations(where: { status: Active }) { id indexer { id } subgraphDeployment { id } } }"
6071
}))
6172
.send()
62-
.await;
73+
.await?;
6374

6475
// Default to a fallback allocation ID
6576
let mut allocation_id = Address::from_str("0x0000000000000000000000000000000000000000")?;
6677

67-
// Try to find a valid allocation
68-
if let Ok(response) = allocations_query {
69-
if response.status().is_success() {
70-
let response_text = response.text().await.unwrap();
71-
println!("Network subgraph response: {}", response_text);
72-
73-
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&response_text) {
74-
if let Some(allocations) = json_value
75-
.get("data")
76-
.and_then(|d| d.get("allocations"))
77-
.and_then(|a| a.as_array())
78-
{
79-
if !allocations.is_empty() {
80-
if let Some(id_str) = allocations[0].get("id").and_then(|id| id.as_str()) {
81-
println!("Found allocation ID: {}", id_str);
82-
allocation_id = Address::from_str(id_str)?;
83-
}
84-
}
85-
}
86-
}
87-
}
78+
if !response.status().is_success() {
79+
return Err(anyhow::anyhow!(
80+
"Network subgraph request failed with status: {}",
81+
response.status()
82+
));
8883
}
8984

90-
// If we still don't have an allocation ID, create a mock one based on the receiver address
91-
if allocation_id == Address::from_str("0x0000000000000000000000000000000000000000")? {
92-
println!("No allocation found, using a mock allocation based on receiver address");
93-
allocation_id = Address::from_str(RECEIVER_ADDRESS)?;
94-
}
85+
// Try to find a valid allocation
86+
let response_text = response.text().await?;
87+
println!("Network subgraph response: {}", response_text);
88+
89+
// Extract allocation_id, if not found return an error
90+
// this should not happen as we run the fund escrow script
91+
// before(in theory tho)
92+
let json_value = serde_json::from_str::<serde_json::Value>(&response_text)?;
93+
let allocation_id = json_value
94+
.get("data")
95+
.and_then(|d| d.get("allocations"))
96+
.and_then(|a| a.as_array())
97+
.filter(|arr| !arr.is_empty())
98+
.and_then(|arr| arr[0].get("id"))
99+
.and_then(|id| id.as_str())
100+
.ok_or_else(|| anyhow::anyhow!("No valid allocation ID found"))?;
101+
102+
let allocation_id = Address::from_str(allocation_id)?;
95103

96104
// Create a metrics checker
97105
let metrics_checker =
@@ -108,40 +116,25 @@ async fn tap_rav_test() -> Result<()> {
108116
let initial_unaggregated =
109117
initial_metrics.unaggregated_fees_by_allocation(&allocation_id.to_string());
110118

111-
// The value for each receipt
112-
let value = 100_000_000_000_000u128; // 0.0001 GRT
113-
114119
// With trigger_value_divisor = 10,000 and max_amount_willing_to_lose_grt = 20
115120
// trigger_value = 20 / 10,000 = 0.002 GRT
116121
// We need to send at least 20 receipts to reach the trigger threshold
117122
// Sending slightly more than required to ensure triggering
118-
let num_receipts = 60;
123+
let num_receipts = 90;
119124

120125
println!(
121126
"\n=== Sending {} receipts to trigger RAV generation ===",
122127
num_receipts
123128
);
124129
println!(
125130
"Each receipt value: {} GRT",
126-
value as f64 / 1_000_000_000_000_000f64
131+
MAX_RECEIPT_VALUE as f64 / GRT_BASE as f64
127132
);
128133
println!(
129134
"Total value to be sent: {} GRT",
130-
(value as f64 * num_receipts as f64) / 1_000_000_000_000_000f64
135+
(MAX_RECEIPT_VALUE as f64 * num_receipts as f64) / GRT_BASE as f64
131136
);
132137

133-
// let mut trigger_value = 0.0;
134-
135-
let trigger_value = initial_metrics.trigger_value_by_sender(&sender_address.to_string());
136-
137-
if trigger_value > 0.0 {
138-
println!(
139-
"With trigger value of {} GRT, we need to send at least {} receipts",
140-
trigger_value,
141-
(trigger_value * 1_000_000_000_000_000f64 / value as f64).ceil()
142-
);
143-
}
144-
145138
// Send receipts in batches with a delay in between
146139
// to ensure some receipts get outside the timestamp buffer
147140
let batches = 3;
@@ -161,10 +154,15 @@ async fn tap_rav_test() -> Result<()> {
161154
println!("Sending receipt {} of {}", receipt_index + 1, num_receipts);
162155

163156
// Create TAP receipt
164-
let receipt = create_tap_receipt(value, &allocation_id, TAP_ESCROW_CONTRACT, &wallet)?;
157+
let receipt = create_tap_receipt(
158+
MAX_RECEIPT_VALUE,
159+
&allocation_id,
160+
TAP_ESCROW_CONTRACT,
161+
&wallet,
162+
)?;
165163
let receipt_json = serde_json::to_string(&receipt).unwrap();
166164

167-
let query_response = http_client
165+
let response = http_client
168166
.post(format!("{}/api/subgraphs/id/{}", GATEWAY_URL, SUBGRAPH_ID))
169167
.header("Content-Type", "application/json")
170168
.header("Authorization", format!("Bearer {}", GATEWAY_API_KEY))
@@ -174,38 +172,30 @@ async fn tap_rav_test() -> Result<()> {
174172
}))
175173
.timeout(Duration::from_secs(10))
176174
.send()
177-
.await;
178-
179-
match query_response {
180-
Ok(response) => {
181-
let status = response.status();
182-
if status.is_success() {
183-
total_successful += 1;
184-
println!("Receipt {} sent successfully", receipt_index + 1);
185-
} else {
186-
println!("Failed to send receipt {}: {}", receipt_index + 1, status);
187-
let response_text = response.text().await?;
188-
println!("Response: {}", response_text);
189-
}
190-
}
191-
Err(e) => {
192-
println!("Error sending receipt {}: {}", receipt_index + 1, e);
193-
return Err(e.into());
194-
}
175+
.await?;
176+
177+
let status = response.status();
178+
if status.is_success() {
179+
total_successful += 1;
180+
println!("Receipt {} sent successfully", receipt_index + 1);
181+
} else {
182+
println!("Failed to send receipt {}: {}", receipt_index + 1, status);
183+
let response_text = response.text().await?;
184+
println!("Response: {}", response_text);
195185
}
196186

197187
// Small delay between queries to avoid flooding
198-
tokio::time::sleep(Duration::from_millis(200)).await;
188+
tokio::time::sleep(Duration::from_millis(100)).await;
199189
}
200190

201191
// After each batch, wait longer than the timestamp buffer
202-
// (typically 60 seconds) to ensure receipts are outside buffer
192+
// (typically 30 seconds) to ensure receipts are outside buffer
203193
if batch < batches - 1 {
204194
println!(
205-
"\nBatch {} complete. Waiting 65 seconds to exceed timestamp buffer...",
195+
"\nBatch {} complete. Waiting to exceed timestamp buffer...",
206196
batch + 1
207197
);
208-
tokio::time::sleep(Duration::from_secs(65)).await;
198+
tokio::time::sleep(Duration::from_secs(WAIT_TIME_BATCHES)).await;
209199
}
210200
}
211201

@@ -216,9 +206,13 @@ async fn tap_rav_test() -> Result<()> {
216206
);
217207
println!(
218208
"Total value sent: {} GRT",
219-
(value as f64 * total_successful as f64) / 1_000_000_000_000_000f64
209+
(MAX_RECEIPT_VALUE as f64 * total_successful as f64) / GRT_BASE as f64
220210
);
221211

212+
// Give the system enough time to process the receipts
213+
// ensuring the aged beyong timestamp buffer
214+
tokio::time::sleep(Duration::from_secs(WAIT_TIME_BATCHES * 2)).await;
215+
222216
// Check for RAV generation
223217
println!("\n=== Checking for RAV generation ===");
224218

0 commit comments

Comments
 (0)