1
1
#[ cfg( test) ]
2
2
mod tests {
3
3
use alloy:: {
4
- consensus:: { SignableTransaction , TxEip1559 , TxEnvelope } ,
5
- node_bindings:: Anvil ,
6
- primitives:: { Address , TxKind , U256 } ,
7
- providers:: ProviderBuilder ,
8
- signers:: { SignerSync , local:: PrivateKeySigner } ,
4
+ node_bindings:: Anvil , primitives:: U256 , providers:: ProviderBuilder ,
5
+ signers:: local:: PrivateKeySigner ,
9
6
} ;
10
7
use builder:: {
11
- config:: BuilderConfig ,
12
- tasks:: {
13
- block:: { BlockBuilder , PECORINO_CHAIN_ID } ,
14
- oauth:: Authenticator ,
15
- } ,
8
+ tasks:: block:: { BlockBuilder , PECORINO_CHAIN_ID } ,
9
+ test_utils:: { new_signed_tx, setup_logging, setup_test_config} ,
16
10
} ;
17
- use eyre :: Result ;
11
+
18
12
use signet_sim:: { SimCache , SimItem } ;
19
- use std:: str:: FromStr ;
20
- use tracing_subscriber:: { EnvFilter , Layer , layer:: SubscriberExt , util:: SubscriberInitExt } ;
13
+ use signet_types:: SlotCalculator ;
14
+ use std:: {
15
+ sync:: Arc ,
16
+ time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ,
17
+ } ;
18
+ use tokio:: { sync:: mpsc:: unbounded_channel, time:: timeout} ;
21
19
22
- #[ tokio:: test( flavor = "multi_thread" , worker_threads = 1 ) ]
23
- async fn test_spawn ( ) {
24
- let filter = EnvFilter :: from_default_env ( ) ;
25
- let fmt = tracing_subscriber:: fmt:: layer ( ) . with_filter ( filter) ;
26
- let registry = tracing_subscriber:: registry ( ) . with ( fmt) ;
27
- registry. try_init ( ) . unwrap ( ) ;
20
+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
21
+ async fn test_handle_build ( ) {
22
+ setup_logging ( ) ;
28
23
29
24
// Make a test config
30
- let config = setup_test_config ( ) ;
25
+ let config = setup_test_config ( ) . unwrap ( ) ;
31
26
let constants = config. load_pecorino_constants ( ) ;
32
27
33
- // Create an authenticator for bundle integration testing
34
- let authenticator = Authenticator :: new ( & config) ;
35
-
36
28
// Create an anvil instance for testing
37
29
let anvil_instance = Anvil :: new ( ) . chain_id ( PECORINO_CHAIN_ID ) . spawn ( ) ;
38
30
@@ -44,8 +36,14 @@ mod tests {
44
36
// Create a rollup provider
45
37
let ru_provider = ProviderBuilder :: new ( ) . on_http ( anvil_instance. endpoint_url ( ) ) ;
46
38
47
- // Create a block builder
48
- let block_builder = BlockBuilder :: new ( & config, authenticator, ru_provider. clone ( ) ) ;
39
+ // Create a block builder with a slot calculator for testing
40
+ let now = SystemTime :: now ( )
41
+ . duration_since ( UNIX_EPOCH )
42
+ . expect ( "Clock may have gone backwards" )
43
+ . as_secs ( ) ;
44
+ dbg ! ( now) ;
45
+ let slot_calculator = SlotCalculator :: new ( now, 0 , 12 ) ;
46
+ let block_builder = BlockBuilder :: new ( & config, ru_provider. clone ( ) , slot_calculator) ;
49
47
50
48
// Setup a sim cache
51
49
let sim_items = SimCache :: new ( ) ;
@@ -57,64 +55,68 @@ mod tests {
57
55
let tx_2 = new_signed_tx ( & test_key_1, 0 , U256 :: from ( 2_f64 ) , 10_000 ) . unwrap ( ) ;
58
56
sim_items. add_item ( SimItem :: Tx ( tx_2) ) ;
59
57
58
+ let finish_by = Instant :: now ( ) + Duration :: from_secs ( 2 ) ;
59
+
60
60
// Spawn the block builder task
61
- let got = block_builder. handle_build ( constants, ru_provider , sim_items ) . await ;
61
+ let got = block_builder. handle_build ( constants, sim_items , finish_by ) . await ;
62
62
63
63
// Assert on the built block
64
64
assert ! ( got. is_ok( ) ) ;
65
65
assert ! ( got. unwrap( ) . tx_count( ) == 2 ) ;
66
66
}
67
67
68
- fn setup_test_config ( ) -> BuilderConfig {
69
- let config = BuilderConfig {
70
- host_chain_id : 1 ,
71
- ru_chain_id : PECORINO_CHAIN_ID ,
72
- host_rpc_url : "https://host-rpc.example.com" . into ( ) ,
73
- ru_rpc_url : "https://rpc.pecorino.signet.sh" . into ( ) ,
74
- zenith_address : Address :: default ( ) ,
75
- quincey_url : "http://localhost:8080" . into ( ) ,
76
- builder_port : 8080 ,
77
- sequencer_key : None ,
78
- builder_key : "0000000000000000000000000000000000000000000000000000000000000000" . into ( ) ,
79
- block_confirmation_buffer : 1 ,
80
- chain_offset : 0 ,
81
- target_slot_time : 1 ,
82
- builder_rewards_address : Address :: default ( ) ,
83
- rollup_block_gas_limit : 100_000_000 ,
84
- tx_pool_url : "http://localhost:9000/" . into ( ) ,
85
- tx_pool_cache_duration : 5 ,
86
- oauth_client_id : "some_client_id" . into ( ) ,
87
- oauth_client_secret : "some_client_secret" . into ( ) ,
88
- oauth_authenticate_url : "http://localhost:9000" . into ( ) ,
89
- oauth_token_url : "http://localhost:9000" . into ( ) ,
90
- tx_broadcast_urls : vec ! [ "http://localhost:9000" . into( ) ] ,
91
- oauth_token_refresh_interval : 300 , // 5 minutes
92
- builder_helper_address : Address :: default ( ) ,
93
- concurrency_limit : 1000 ,
94
- } ;
95
- config
96
- }
68
+ /// Tests the full block builder loop
69
+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
70
+ async fn test_spawn ( ) {
71
+ setup_logging ( ) ;
72
+
73
+ // Make a test config
74
+ let config = setup_test_config ( ) . unwrap ( ) ;
75
+ let constants = config. load_pecorino_constants ( ) ;
76
+
77
+ // Create an anvil instance for testing
78
+ let anvil_instance = Anvil :: new ( ) . chain_id ( PECORINO_CHAIN_ID ) . spawn ( ) ;
79
+
80
+ // Create a wallet
81
+ let keys = anvil_instance. keys ( ) ;
82
+ let test_key_0 = PrivateKeySigner :: from_signing_key ( keys[ 0 ] . clone ( ) . into ( ) ) ;
83
+ let test_key_1 = PrivateKeySigner :: from_signing_key ( keys[ 1 ] . clone ( ) . into ( ) ) ;
84
+
85
+ // Plumb inputs for the test setup
86
+ let ( tx_sender, tx_receiver) = unbounded_channel ( ) ;
87
+ let ( _, bundle_receiver) = unbounded_channel ( ) ;
88
+ let ( block_sender, mut block_receiver) = unbounded_channel ( ) ;
89
+
90
+ // Create a rollup provider
91
+ let ru_provider = ProviderBuilder :: new ( ) . on_http ( anvil_instance. endpoint_url ( ) ) ;
92
+
93
+ // Create a builder with a test slot calculator
94
+ let slot_calculator = SlotCalculator :: new (
95
+ config. start_timestamp ,
96
+ config. chain_offset ,
97
+ config. target_slot_time ,
98
+ ) ;
99
+ let sim_cache = SimCache :: new ( ) ;
100
+ let builder = Arc :: new ( BlockBuilder :: new ( & config, ru_provider. clone ( ) , slot_calculator) ) ;
101
+
102
+ // Create a sim cache and start filling it with items
103
+ let _ =
104
+ builder. clone ( ) . spawn_cache_handler ( tx_receiver, bundle_receiver, sim_cache. clone ( ) ) ;
105
+
106
+ // Finally, Kick off the block builder task.
107
+ let _ = builder. clone ( ) . spawn_builder_task ( constants, sim_cache. clone ( ) , block_sender) ;
108
+
109
+ let tx_1 = new_signed_tx ( & test_key_0, 0 , U256 :: from ( 1_f64 ) , 11_000 ) . unwrap ( ) ;
110
+ let tx_2 = new_signed_tx ( & test_key_1, 0 , U256 :: from ( 2_f64 ) , 10_000 ) . unwrap ( ) ;
111
+ tx_sender. send ( tx_1) . unwrap ( ) ;
112
+ tx_sender. send ( tx_2) . unwrap ( ) ;
97
113
98
- // Returns a new signed test transaction with default values
99
- fn new_signed_tx (
100
- wallet : & PrivateKeySigner ,
101
- nonce : u64 ,
102
- value : U256 ,
103
- mpfpg : u128 ,
104
- ) -> Result < TxEnvelope > {
105
- let tx = TxEip1559 {
106
- chain_id : PECORINO_CHAIN_ID ,
107
- nonce,
108
- max_fee_per_gas : 50_000 ,
109
- max_priority_fee_per_gas : mpfpg,
110
- to : TxKind :: Call (
111
- Address :: from_str ( "0x0000000000000000000000000000000000000000" ) . unwrap ( ) ,
112
- ) ,
113
- value,
114
- gas_limit : 50_000 ,
115
- ..Default :: default ( )
116
- } ;
117
- let signature = wallet. sign_hash_sync ( & tx. signature_hash ( ) ) ?;
118
- Ok ( TxEnvelope :: Eip1559 ( tx. into_signed ( signature) ) )
114
+ // Wait for a block with timeout
115
+ let result = timeout ( Duration :: from_secs ( 5 ) , block_receiver. recv ( ) ) . await ;
116
+ assert ! ( result. is_ok( ) , "Did not receive block within 5 seconds" ) ;
117
+ let block = result. unwrap ( ) ;
118
+ dbg ! ( & block) ;
119
+ assert ! ( block. is_some( ) , "Block channel closed without receiving a block" ) ;
120
+ assert ! ( block. unwrap( ) . tx_count( ) == 2 ) ; // TODO: Why is this failing? I'm seeing EVM errors but haven't tracked them down yet.
119
121
}
120
122
}
0 commit comments