forked from flashbots/rbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.rs
581 lines (497 loc) · 21.6 KB
/
integration_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#[cfg(all(test, feature = "integration"))]
mod tests {
use crate::{
integration::{op_rbuilder::OpRbuilderConfig, op_reth::OpRethConfig, IntegrationFramework},
tester::{BlockGenerator, EngineApi},
tx_signer::Signer,
};
use alloy_consensus::{Transaction, TxEip1559};
use alloy_eips::{eip1559::MIN_PROTOCOL_BASE_FEE, eip2718::Encodable2718};
use alloy_primitives::hex;
use alloy_provider::{Identity, Provider, ProviderBuilder};
use alloy_rpc_types_eth::BlockTransactionsKind;
use futures_util::StreamExt;
use op_alloy_consensus::OpTypedTransaction;
use op_alloy_network::Optimism;
use std::{
cmp::max,
path::PathBuf,
sync::{Arc, Mutex},
time::Duration,
};
use tokio_tungstenite::connect_async;
use uuid::Uuid;
const BUILDER_PRIVATE_KEY: &str =
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
#[tokio::test]
#[cfg(not(feature = "flashblocks"))]
async fn integration_test_chain_produces_blocks() -> eyre::Result<()> {
// This is a simple test using the integration framework to test that the chain
// produces blocks.
let mut framework =
IntegrationFramework::new("integration_test_chain_produces_blocks").unwrap();
// we are going to use a genesis file pre-generated before the test
let mut genesis_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
genesis_path.push("../../genesis.json");
assert!(genesis_path.exists());
// create the builder
let builder_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let op_rbuilder_config = OpRbuilderConfig::new()
.chain_config_path(genesis_path.clone())
.data_dir(builder_data_dir)
.auth_rpc_port(1234)
.network_port(1235)
.http_port(1238)
.with_builder_private_key(BUILDER_PRIVATE_KEY);
// create the validation reth node
let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let reth = OpRethConfig::new()
.chain_config_path(genesis_path)
.data_dir(reth_data_dir)
.auth_rpc_port(1236)
.network_port(1237);
framework.start("op-reth", &reth).await.unwrap();
let op_rbuilder = framework
.start("op-rbuilder", &op_rbuilder_config)
.await
.unwrap();
let engine_api = EngineApi::new("http://localhost:1234").unwrap();
let validation_api = EngineApi::new("http://localhost:1236").unwrap();
let mut generator = BlockGenerator::new(&engine_api, Some(&validation_api), false, 1, None);
generator.init().await?;
let provider = ProviderBuilder::<Identity, Identity, Optimism>::default()
.on_http("http://localhost:1238".parse()?);
for _ in 0..10 {
let block_hash = generator.generate_block().await?;
// query the block and the transactions inside the block
let block = provider
.get_block_by_hash(block_hash)
.await?
.expect("block");
for hash in block.transactions.hashes() {
let _ = provider
.get_transaction_receipt(hash)
.await?
.expect("receipt");
}
}
// there must be a line logging the monitoring transaction
op_rbuilder
.find_log_line("Committed block built by builder")
.await?;
Ok(())
}
#[tokio::test]
#[cfg(not(feature = "flashblocks"))]
async fn integration_test_revert_protection() -> eyre::Result<()> {
// This is a simple test using the integration framework to test that the chain
// produces blocks.
let mut framework =
IntegrationFramework::new("integration_test_revert_protection").unwrap();
// we are going to use a genesis file pre-generated before the test
let mut genesis_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
genesis_path.push("../../genesis.json");
assert!(genesis_path.exists());
// create the builder
let builder_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let op_rbuilder_config = OpRbuilderConfig::new()
.chain_config_path(genesis_path.clone())
.data_dir(builder_data_dir)
.auth_rpc_port(1244)
.network_port(1245)
.http_port(1248)
.with_builder_private_key(BUILDER_PRIVATE_KEY);
// create the validation reth node
let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let reth = OpRethConfig::new()
.chain_config_path(genesis_path)
.data_dir(reth_data_dir)
.auth_rpc_port(1246)
.network_port(1247);
framework.start("op-reth", &reth).await.unwrap();
let _ = framework
.start("op-rbuilder", &op_rbuilder_config)
.await
.unwrap();
let engine_api = EngineApi::new("http://localhost:1244").unwrap();
let validation_api = EngineApi::new("http://localhost:1246").unwrap();
let mut generator = BlockGenerator::new(&engine_api, Some(&validation_api), false, 1, None);
let latest_block = generator.init().await?;
let provider = ProviderBuilder::<Identity, Identity, Optimism>::default()
.on_http("http://localhost:1248".parse()?);
let mut base_fee = max(
latest_block.header.base_fee_per_gas.unwrap(),
MIN_PROTOCOL_BASE_FEE,
);
for _ in 0..10 {
// Get builder's address
let known_wallet = Signer::try_from_secret(BUILDER_PRIVATE_KEY.parse()?)?;
let builder_address = known_wallet.address;
// Get current nonce from chain
let nonce = provider.get_transaction_count(builder_address).await?;
// Transaction from builder should succeed
let tx_request = OpTypedTransaction::Eip1559(TxEip1559 {
chain_id: 901,
nonce,
gas_limit: 210000,
max_fee_per_gas: base_fee.into(),
..Default::default()
});
let signed_tx = known_wallet.sign_tx(tx_request)?;
let known_tx = provider
.send_raw_transaction(signed_tx.encoded_2718().as_slice())
.await?;
// Create a reverting transaction
let tx_request = OpTypedTransaction::Eip1559(TxEip1559 {
chain_id: 901,
nonce: nonce + 1,
gas_limit: 300000,
max_fee_per_gas: base_fee.into(),
input: hex!("60006000fd").into(), // PUSH1 0x00 PUSH1 0x00 REVERT
..Default::default()
});
let signed_tx = known_wallet.sign_tx(tx_request)?;
let reverting_tx = provider
.send_raw_transaction(signed_tx.encoded_2718().as_slice())
.await?;
let block_hash = generator.generate_block().await?;
// query the block and the transactions inside the block
let block = provider
.get_block_by_hash(block_hash)
.await?
.expect("block");
// Verify known transaction is included
assert!(
block
.transactions
.hashes()
.any(|hash| hash == *known_tx.tx_hash()),
"successful transaction missing from block"
);
// Verify reverted transaction is NOT included
assert!(
!block
.transactions
.hashes()
.any(|hash| hash == *reverting_tx.tx_hash()),
"reverted transaction unexpectedly included in block"
);
for hash in block.transactions.hashes() {
let receipt = provider
.get_transaction_receipt(hash)
.await?
.expect("receipt");
let success = receipt.inner.inner.status();
assert!(success);
}
base_fee = max(
block.header.base_fee_per_gas.unwrap(),
MIN_PROTOCOL_BASE_FEE,
);
}
Ok(())
}
#[tokio::test]
#[cfg(not(feature = "flashblocks"))]
async fn integration_test_fee_priority_ordering() -> eyre::Result<()> {
// This test validates that transactions are ordered by fee priority in blocks
let mut framework =
IntegrationFramework::new("integration_test_fee_priority_ordering").unwrap();
// we are going to use a genesis file pre-generated before the test
let mut genesis_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
genesis_path.push("../../genesis.json");
assert!(genesis_path.exists());
// create the builder
let builder_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let op_rbuilder_config = OpRbuilderConfig::new()
.chain_config_path(genesis_path.clone())
.data_dir(builder_data_dir)
.auth_rpc_port(1264)
.network_port(1265)
.http_port(1268)
.with_builder_private_key(BUILDER_PRIVATE_KEY);
// create the validation reth node
let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let reth = OpRethConfig::new()
.chain_config_path(genesis_path)
.data_dir(reth_data_dir)
.auth_rpc_port(1266)
.network_port(1267);
framework.start("op-reth", &reth).await.unwrap();
let _ = framework
.start("op-rbuilder", &op_rbuilder_config)
.await
.unwrap();
let engine_api = EngineApi::new("http://localhost:1264").unwrap();
let validation_api = EngineApi::new("http://localhost:1266").unwrap();
let mut generator = BlockGenerator::new(&engine_api, Some(&validation_api), false, 1, None);
let latest_block = generator.init().await?;
let provider = ProviderBuilder::<Identity, Identity, Optimism>::default()
.on_http("http://localhost:1268".parse()?);
let base_fee = max(
latest_block.header.base_fee_per_gas.unwrap(),
MIN_PROTOCOL_BASE_FEE,
);
// Create transactions with increasing fee values
let priority_fees: [u128; 5] = [1, 3, 5, 2, 4]; // Deliberately not in order
let signers = vec![
Signer::random(),
Signer::random(),
Signer::random(),
Signer::random(),
Signer::random(),
];
let mut txs = Vec::new();
// Fund test accounts with deposits
for signer in &signers {
generator
.deposit(signer.address, 1000000000000000000)
.await?;
}
// Send transactions in non-optimal fee order
for (i, priority_fee) in priority_fees.iter().enumerate() {
let tx_request = OpTypedTransaction::Eip1559(TxEip1559 {
chain_id: 901,
nonce: 1,
gas_limit: 210000,
max_fee_per_gas: base_fee as u128 + *priority_fee,
max_priority_fee_per_gas: *priority_fee,
..Default::default()
});
let signed_tx = signers[i].sign_tx(tx_request)?;
let tx = provider
.send_raw_transaction(signed_tx.encoded_2718().as_slice())
.await?;
txs.push(tx);
}
// Generate a block that should include these transactions
let block_hash = generator.generate_block().await?;
// Query the block and check transaction ordering
let block = provider
.get_block_by_hash(block_hash)
.full()
.await?
.expect("block");
// Verify all transactions are included
for tx in &txs {
assert!(
block
.transactions
.hashes()
.any(|hash| hash == *tx.tx_hash()),
"transaction missing from block"
);
}
let tx_fees: Vec<_> = block
.transactions
.into_transactions()
.map(|tx| tx.effective_tip_per_gas(base_fee.into()))
.collect();
// Verify transactions are ordered by decreasing fee (highest fee first)
// Skip the first deposit transaction and last builder transaction
for i in 1..tx_fees.len() - 2 {
assert!(
tx_fees[i] >= tx_fees[i + 1],
"Transactions not ordered by decreasing fee: {:?}",
tx_fees
);
}
Ok(())
}
#[tokio::test]
#[cfg(feature = "flashblocks")]
async fn integration_test_chain_produces_blocks() -> eyre::Result<()> {
// This is a simple test using the integration framework to test that the chain
// produces blocks.
let mut framework =
IntegrationFramework::new("integration_test_chain_produces_blocks").unwrap();
// we are going to use a genesis file pre-generated before the test
let mut genesis_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
genesis_path.push("../../genesis.json");
assert!(genesis_path.exists());
// create the builder
let builder_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let op_rbuilder_config = OpRbuilderConfig::new()
.chain_config_path(genesis_path.clone())
.data_dir(builder_data_dir)
.auth_rpc_port(1234)
.network_port(1235)
.http_port(1238)
.with_builder_private_key(BUILDER_PRIVATE_KEY)
.with_flashblocks_ws_url("localhost:1239")
.with_chain_block_time(2000)
.with_flashbots_block_time(200);
// create the validation reth node
let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let reth = OpRethConfig::new()
.chain_config_path(genesis_path)
.data_dir(reth_data_dir)
.auth_rpc_port(1236)
.network_port(1237);
framework.start("op-reth", &reth).await.unwrap();
let op_rbuilder = framework
.start("op-rbuilder", &op_rbuilder_config)
.await
.unwrap();
// Create a struct to hold received messages
let received_messages = Arc::new(Mutex::new(Vec::new()));
let messages_clone = received_messages.clone();
// Spawn WebSocket listener task
let ws_handle = tokio::spawn(async move {
let (ws_stream, _) = connect_async("ws://localhost:1239").await?;
let (_, mut read) = ws_stream.split();
while let Some(Ok(msg)) = read.next().await {
if let Ok(text) = msg.into_text() {
messages_clone.lock().unwrap().push(text);
}
}
Ok::<_, eyre::Error>(())
});
let engine_api = EngineApi::new("http://localhost:1234").unwrap();
let validation_api = EngineApi::new("http://localhost:1236").unwrap();
let mut generator = BlockGenerator::new(&engine_api, Some(&validation_api), false, 2, None);
generator.init().await?;
let provider = ProviderBuilder::<Identity, Identity, Optimism>::default()
.on_http("http://localhost:1238".parse()?);
for _ in 0..10 {
let block_hash = generator.generate_block().await?;
// query the block and the transactions inside the block
let block = provider
.get_block_by_hash(block_hash)
.await?
.expect("block");
for hash in block.transactions.hashes() {
let _ = provider
.get_transaction_receipt(hash)
.await?
.expect("receipt");
}
}
// there must be a line logging the monitoring transaction
op_rbuilder
.find_log_line("Processing new chain commit") // no builder tx for flashblocks builder
.await?;
// check there's 10 flashblocks log lines (2000ms / 200ms)
op_rbuilder.find_log_line("Building flashblock 9").await?;
// Process websocket messages
let timeout_duration = Duration::from_secs(10);
tokio::time::timeout(timeout_duration, async {
let mut message_count = 0;
loop {
if message_count >= 10 {
break;
}
let messages = received_messages.lock().unwrap();
let messages_json: Vec<serde_json::Value> = messages
.iter()
.map(|msg| serde_json::from_str(msg).unwrap())
.collect();
for msg in messages_json.iter() {
let metadata = msg.get("metadata");
assert!(metadata.is_some(), "metadata field missing");
let metadata = metadata.unwrap();
assert!(
metadata.get("block_number").is_some(),
"block_number missing"
);
assert!(
metadata.get("new_account_balances").is_some(),
"new_account_balances missing"
);
assert!(metadata.get("receipts").is_some(), "receipts missing");
// also check if the length of the receipts is the same as the number of transactions
assert!(
metadata.get("receipts").unwrap().as_object().unwrap().len()
== msg
.get("diff")
.unwrap()
.get("transactions")
.unwrap()
.as_array()
.unwrap()
.len(),
"receipts length mismatch"
);
message_count += 1;
}
drop(messages);
tokio::time::sleep(Duration::from_millis(100)).await;
}
})
.await?;
ws_handle.abort();
Ok(())
}
#[tokio::test]
#[cfg(feature = "flashblocks")]
async fn integration_test_flashblocks_respects_gas_limit() -> eyre::Result<()> {
// This is a simple test using the integration framework to test that the chain
// produces blocks.
let mut framework =
IntegrationFramework::new("integration_test_flashblocks_respects_gas_limit").unwrap();
// we are going to use a genesis file pre-generated before the test
let mut genesis_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
genesis_path.push("../../genesis.json");
assert!(genesis_path.exists());
let block_time_ms = 1000;
let flashblock_time_ms = 100;
// create the builder
let builder_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let op_rbuilder_config = OpRbuilderConfig::new()
.chain_config_path(genesis_path.clone())
.data_dir(builder_data_dir)
.auth_rpc_port(1244)
.network_port(1245)
.http_port(1248)
.with_builder_private_key(BUILDER_PRIVATE_KEY)
.with_flashblocks_ws_url("localhost:1249")
.with_chain_block_time(block_time_ms)
.with_flashbots_block_time(flashblock_time_ms);
// create the validation reth node
let reth_data_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
let reth = OpRethConfig::new()
.chain_config_path(genesis_path)
.data_dir(reth_data_dir)
.auth_rpc_port(1246)
.network_port(1247);
framework.start("op-reth", &reth).await.unwrap();
let op_rbuilder = framework
.start("op-rbuilder", &op_rbuilder_config)
.await
.unwrap();
let engine_api = EngineApi::new("http://localhost:1244").unwrap();
let validation_api = EngineApi::new("http://localhost:1246").unwrap();
let mut generator = BlockGenerator::new(
&engine_api,
Some(&validation_api),
false,
block_time_ms / 1000,
None,
);
generator.init().await?;
let provider = ProviderBuilder::<Identity, Identity, Optimism>::default()
.on_http("http://localhost:1248".parse()?);
// Delay the payload building by 4s, ensure that the correct number of flashblocks are built
let block_hash = generator.generate_block_with_delay(4).await?;
// query the block and the transactions inside the block
let block = provider
.get_block_by_hash(block_hash)
.await?
.expect("block");
for hash in block.transactions.hashes() {
let _ = provider
.get_transaction_receipt(hash)
.await?
.expect("receipt");
}
op_rbuilder
.find_log_line("Processing new chain commit") // no builder tx for flashblocks builder
.await?;
// check there's no more than 10 flashblocks log lines (2000ms / 200ms)
op_rbuilder.find_log_line("Building flashblock 9").await?;
op_rbuilder
.find_log_line("Skipping flashblock reached target=10 idx=10")
.await?;
Ok(())
}
}