1
+ use std:: net:: SocketAddr ;
1
2
use std:: path:: PathBuf ;
3
+ use std:: str:: FromStr ;
2
4
use std:: sync:: Arc ;
3
5
use std:: time:: { Duration , Instant } ;
4
6
5
7
use anyhow:: { bail, Context } ;
8
+ use async_trait:: async_trait;
6
9
use futures:: TryStreamExt ;
7
10
use tokio:: process:: Command ;
11
+ use tokio:: time:: sleep;
8
12
9
13
use super :: config:: BridgeBackendConfig ;
10
14
use super :: docker:: DockerEnv ;
11
15
use super :: framework:: TestContext ;
12
- use super :: node:: { LogProvider , Node , Restart , SpawnOutput } ;
13
16
use super :: Result ;
17
+ use crate :: bridge_backend_client:: BridgeBackendClient ;
14
18
use crate :: node:: NodeKind ;
15
- use crate :: test_client :: TestClient ;
19
+ use crate :: traits :: { ContainerSpawnOutput , LogProvider , Node , Restart , SpawnOutput } ;
16
20
17
21
pub struct BridgeBackendNode {
18
22
spawn_output : SpawnOutput ,
19
23
pub config : BridgeBackendConfig ,
20
24
docker_env : Arc < Option < DockerEnv > > ,
25
+ client : BridgeBackendClient ,
21
26
}
22
27
23
28
impl BridgeBackendNode {
24
29
pub async fn new ( config : & BridgeBackendConfig , docker : Arc < Option < DockerEnv > > ) -> Result < Self > {
25
30
let spawn_output = Self :: spawn ( config, & docker) . await ?;
31
+ let rpc_url = SocketAddr :: from_str ( & ( config. host . clone ( ) + & config. port . to_string ( ) ) ) ?;
26
32
27
33
Ok ( Self {
28
34
spawn_output,
29
35
config : config. clone ( ) ,
30
36
docker_env : docker,
37
+ client : BridgeBackendClient :: new ( rpc_url) . await ?,
31
38
} )
32
39
}
33
40
@@ -41,11 +48,38 @@ impl BridgeBackendNode {
41
48
None => <Self as Node >:: spawn ( config) ,
42
49
}
43
50
}
51
+
52
+ async fn wait_for_shutdown ( & self ) -> Result < ( ) > {
53
+ let timeout_duration = Duration :: from_secs ( 30 ) ;
54
+ let start = std:: time:: Instant :: now ( ) ;
55
+
56
+ while start. elapsed ( ) < timeout_duration {
57
+ if !self . is_process_running ( ) . await ? {
58
+ println ! ( "Bridge backend has stopped successfully" ) ;
59
+ return Ok ( ( ) ) ;
60
+ }
61
+ sleep ( Duration :: from_millis ( 200 ) ) . await ;
62
+ }
63
+
64
+ bail ! ( "Timeout waiting for bridge backend to stop" )
65
+ }
66
+
67
+ async fn is_process_running ( & self ) -> Result < bool > {
68
+ // let data_dir = &self.config.data_dir;
69
+ // let output = Command::new("pgrep")
70
+ // .args(["-f", &format!("bitcoind.*{}", data_dir.display())])
71
+ // .output()
72
+ // .await?;
73
+
74
+ // Ok(output.status.success())
75
+ todo ! ( )
76
+ }
44
77
}
45
78
79
+ #[ async_trait]
46
80
impl Node for BridgeBackendNode {
47
81
type Config = BridgeBackendConfig ;
48
- type Client = TestClient ;
82
+ type Client = BridgeBackendClient ;
49
83
50
84
fn spawn ( config : & Self :: Config ) -> Result < SpawnOutput > {
51
85
let env = config. get_env ( ) ;
@@ -87,10 +121,6 @@ impl Node for BridgeBackendNode {
87
121
anyhow:: bail!( "Node failed to become ready within the specified timeout" )
88
122
}
89
123
90
- fn client ( & self ) -> & Self :: Client {
91
- & self . client ( )
92
- }
93
-
94
124
fn config_mut ( & mut self ) -> & mut Self :: Config {
95
125
& mut self . config
96
126
}
@@ -104,7 +134,7 @@ impl Node for BridgeBackendNode {
104
134
. context ( "Failed to kill child process" ) ?;
105
135
Ok ( ( ) )
106
136
}
107
- SpawnOutput :: Container ( crate :: node :: ContainerSpawnOutput { id, .. } ) => {
137
+ SpawnOutput :: Container ( ContainerSpawnOutput { id, .. } ) => {
108
138
std:: println!( "Stopping container {id}" ) ;
109
139
let docker = bollard:: Docker :: connect_with_local_defaults ( )
110
140
. context ( "Failed to connect to Docker" ) ?;
@@ -116,11 +146,21 @@ impl Node for BridgeBackendNode {
116
146
}
117
147
}
118
148
}
149
+
150
+ fn client ( & self ) -> & Self :: Client {
151
+ & self . client
152
+ }
153
+
154
+ fn env ( & self ) -> Vec < ( & ' static str , & ' static str ) > {
155
+ // self.config.get_env()
156
+ todo ! ( )
157
+ }
119
158
}
120
159
160
+ #[ async_trait]
121
161
impl Restart for BridgeBackendNode {
122
162
async fn wait_until_stopped ( & mut self ) -> Result < ( ) > {
123
- self . client . stop ( ) . await ?;
163
+ // self.client.stop().await?;
124
164
self . stop ( ) . await ?;
125
165
126
166
match & self . spawn_output {
@@ -150,9 +190,6 @@ impl Restart for BridgeBackendNode {
150
190
151
191
self . wait_for_ready ( None ) . await ?;
152
192
153
- // Reload wallets after restart
154
- self . load_wallets ( ) . await ;
155
-
156
193
Ok ( ( ) )
157
194
}
158
195
}
@@ -163,21 +200,21 @@ impl LogProvider for BridgeBackendNode {
163
200
}
164
201
165
202
fn log_path ( & self ) -> PathBuf {
166
- self . config . data_dir . join ( "regtest" ) . join ( "debug.log" )
203
+ todo ! ( )
167
204
}
168
205
}
169
206
170
- pub struct BitcoinNodeCluster {
207
+ pub struct BridgeBackendNodeCluster {
171
208
inner : Vec < BridgeBackendNode > ,
172
209
}
173
210
174
- impl BitcoinNodeCluster {
211
+ impl BridgeBackendNodeCluster {
175
212
pub async fn new ( ctx : & TestContext ) -> Result < Self > {
176
213
let n_nodes = ctx. config . test_case . n_nodes ;
177
214
let mut cluster = Self {
178
215
inner : Vec :: with_capacity ( n_nodes) ,
179
216
} ;
180
- for config in ctx. config . bitcoin . iter ( ) {
217
+ for config in ctx. config . bridge_backend . iter ( ) {
181
218
let node = BridgeBackendNode :: new ( config, Arc :: clone ( & ctx. docker ) ) . await ?;
182
219
cluster. inner . push ( node)
183
220
}
@@ -193,42 +230,6 @@ impl BitcoinNodeCluster {
193
230
Ok ( ( ) )
194
231
}
195
232
196
- pub async fn wait_for_sync ( & self , timeout : Duration ) -> Result < ( ) > {
197
- let start = Instant :: now ( ) ;
198
- while start. elapsed ( ) < timeout {
199
- // let mut heights = HashSet::new();
200
- // for node in &self.inner {
201
- // let height = node.get_block_count().await?;
202
- // heights.insert(height);
203
- // }
204
-
205
- // if heights.len() == 1 {
206
- return Ok ( ( ) ) ;
207
- // }
208
-
209
- // sleep(Duration::from_secs(1)).await;
210
- }
211
- bail ! ( "Nodes failed to sync within the specified timeout" )
212
- }
213
-
214
- // Connect all bitcoin nodes between them
215
- pub async fn connect_nodes ( & self ) -> Result < ( ) > {
216
- for ( i, from_node) in self . inner . iter ( ) . enumerate ( ) {
217
- for ( j, to_node) in self . inner . iter ( ) . enumerate ( ) {
218
- if i != j {
219
- let ip = match & to_node. spawn_output {
220
- SpawnOutput :: Container ( container) => container. ip . clone ( ) ,
221
- _ => "127.0.0.1" . to_string ( ) ,
222
- } ;
223
-
224
- let add_node_arg = format ! ( "{}:{}" , ip, to_node. config. p2p_port) ;
225
- from_node. add_node ( & add_node_arg) . await ?;
226
- }
227
- }
228
- }
229
- Ok ( ( ) )
230
- }
231
-
232
233
pub fn get ( & self , index : usize ) -> Option < & BridgeBackendNode > {
233
234
self . inner . get ( index)
234
235
}
0 commit comments