2
2
#![ allow( unused_imports) ]
3
3
//! Multi-module tests that use database fixtures
4
4
5
+ use std:: cell:: { RefCell , RefMut } ;
5
6
use std:: sync:: Arc ;
6
7
use std:: time:: { SystemTime , UNIX_EPOCH } ;
7
8
use bitcoin:: { BlockHash , Network } ;
8
9
use bitcoin:: secp256k1:: ecdsa:: Signature ;
9
10
use bitcoin:: secp256k1:: { Secp256k1 , SecretKey } ;
10
11
use bitcoin:: hashes:: Hash ;
12
+ use bitcoin:: hashes:: hex:: ToHex ;
11
13
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
12
14
use lightning:: ln:: features:: ChannelFeatures ;
13
15
use lightning:: ln:: msgs:: { ChannelAnnouncement , ChannelUpdate , UnsignedChannelAnnouncement , UnsignedChannelUpdate } ;
@@ -21,6 +23,10 @@ use crate::types::{GossipMessage, tests::TestLogger};
21
23
22
24
const CLIENT_BACKDATE_INTERVAL : u32 = 3600 * 24 * 7 ; // client backdates RGS by a week
23
25
26
+ thread_local ! {
27
+ static DB_TEST_SCHEMA : RefCell <Option <String >> = RefCell :: new( None ) ;
28
+ }
29
+
24
30
fn blank_signature ( ) -> Signature {
25
31
Signature :: from_compact ( & [ 0u8 ; 64 ] ) . unwrap ( )
26
32
}
@@ -33,6 +39,29 @@ fn current_time() -> u32 {
33
39
SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( "Time must be > 1970" ) . as_secs ( ) as u32
34
40
}
35
41
42
+ pub ( crate ) fn db_test_schema ( ) -> String {
43
+ DB_TEST_SCHEMA . with ( |suffix_reference| {
44
+ let mut suffix_option = suffix_reference. borrow_mut ( ) ;
45
+ match suffix_option. as_ref ( ) {
46
+ None => {
47
+ let current_time = SystemTime :: now ( ) ;
48
+ let unix_time = current_time. duration_since ( UNIX_EPOCH ) . expect ( "Time went backwards" ) ;
49
+ let timestamp_seconds = unix_time. as_secs ( ) ;
50
+ let timestamp_nanos = unix_time. as_nanos ( ) ;
51
+ let preimage = format ! ( "{}" , timestamp_nanos) ;
52
+ let suffix = Sha256dHash :: hash ( preimage. as_bytes ( ) ) . into_inner ( ) . to_hex ( ) ;
53
+ // the schema must start with a letter
54
+ let schema = format ! ( "test_{}_{}" , timestamp_seconds, suffix) ;
55
+ suffix_option. replace ( schema. clone ( ) ) ;
56
+ schema
57
+ }
58
+ Some ( suffix) => {
59
+ suffix. clone ( )
60
+ }
61
+ }
62
+ } )
63
+ }
64
+
36
65
fn generate_announcement ( short_channel_id : u64 ) -> ChannelAnnouncement {
37
66
let secp_context = Secp256k1 :: new ( ) ;
38
67
@@ -88,25 +117,13 @@ fn generate_update(scid: u64, direction: bool, timestamp: u32, expiry_delta: u16
88
117
}
89
118
90
119
async fn clean_test_db ( ) {
91
- let connection_config = config:: db_connection_config ( ) ;
92
- let ( client, connection) = connection_config. connect ( NoTls ) . await . unwrap ( ) ;
93
-
94
- tokio:: spawn ( async move {
95
- if let Err ( e) = connection. await {
96
- panic ! ( "connection error: {}" , e) ;
97
- }
98
- } ) ;
99
-
100
- client. query ( "TRUNCATE TABLE channel_announcements RESTART IDENTITY CASCADE" , & [ ] ) . await . unwrap ( ) ;
101
- client. query ( "TRUNCATE TABLE channel_updates RESTART IDENTITY CASCADE" , & [ ] ) . await . unwrap ( ) ;
102
- client. query ( "TRUNCATE TABLE config RESTART IDENTITY CASCADE" , & [ ] ) . await . unwrap ( ) ;
120
+ let client = crate :: connect_to_db ( ) . await ;
121
+ let schema = db_test_schema ( ) ;
122
+ client. execute ( & format ! ( "DROP SCHEMA IF EXISTS {} CASCADE" , schema) , & [ ] ) . await . unwrap ( ) ;
103
123
}
104
124
105
125
#[ tokio:: test]
106
126
async fn test_trivial_setup ( ) {
107
- // start off with a clean slate
108
- clean_test_db ( ) . await ;
109
-
110
127
let logger = Arc :: new ( TestLogger :: new ( ) ) ;
111
128
let network_graph = NetworkGraph :: new ( Network :: Bitcoin , logger. clone ( ) ) ;
112
129
let network_graph_arc = Arc :: new ( network_graph) ;
@@ -149,12 +166,12 @@ async fn test_trivial_setup() {
149
166
let update_result = rgs. update_network_graph ( & serialization. data ) . unwrap ( ) ;
150
167
println ! ( "update result: {}" , update_result) ;
151
168
// the update result must be a multiple of our snapshot granularity
152
- assert_eq ! ( update_result % config:: SNAPSHOT_CALCULATION_INTERVAL , 0 ) ;
169
+ assert_eq ! ( update_result % config:: snapshot_generation_interval ( ) , 0 ) ;
153
170
assert ! ( update_result < timestamp) ;
154
171
155
172
let timestamp_delta = timestamp - update_result;
156
173
println ! ( "timestamp delta: {}" , timestamp_delta) ;
157
- assert ! ( timestamp_delta < config:: SNAPSHOT_CALCULATION_INTERVAL ) ;
174
+ assert ! ( timestamp_delta < config:: snapshot_generation_interval ( ) ) ;
158
175
159
176
let readonly_graph = client_graph_arc. read_only ( ) ;
160
177
let channels = readonly_graph. channels ( ) ;
@@ -200,7 +217,7 @@ async fn test_unidirectional_intermediate_update_consideration() {
200
217
network_graph_arc. update_channel_unsigned ( & update_3. contents ) . unwrap ( ) ;
201
218
network_graph_arc. update_channel_unsigned ( & update_4. contents ) . unwrap ( ) ;
202
219
203
- receiver. send ( GossipMessage :: ChannelAnnouncement ( announcement) ) . await . unwrap ( ) ;
220
+ receiver. send ( GossipMessage :: ChannelAnnouncement ( announcement, Some ( current_timestamp ) ) ) . await . unwrap ( ) ;
204
221
receiver. send ( GossipMessage :: ChannelUpdate ( update_1) ) . await . unwrap ( ) ;
205
222
receiver. send ( GossipMessage :: ChannelUpdate ( update_2) ) . await . unwrap ( ) ;
206
223
receiver. send ( GossipMessage :: ChannelUpdate ( update_3) ) . await . unwrap ( ) ;
@@ -247,7 +264,7 @@ async fn test_unidirectional_intermediate_update_consideration() {
247
264
println ! ( "last sync timestamp: {}" , last_sync_timestamp) ;
248
265
println ! ( "next timestamp: {}" , next_timestamp) ;
249
266
// the update result must be a multiple of our snapshot granularity
250
- assert_eq ! ( next_timestamp % config:: SNAPSHOT_CALCULATION_INTERVAL , 0 ) ;
267
+ assert_eq ! ( next_timestamp % config:: snapshot_generation_interval ( ) , 0 ) ;
251
268
252
269
let readonly_graph = client_graph_arc. read_only ( ) ;
253
270
let channels = readonly_graph. channels ( ) ;
0 commit comments