1
+ use crate :: config:: TX_BROADCAST_TIMEOUT_SECS ;
1
2
use crate :: logger:: { log_bytes, log_error, log_trace, Logger } ;
2
3
3
4
use lightning:: chain:: chaininterface:: BroadcasterInterface ;
@@ -12,6 +13,7 @@ use tokio::sync::mpsc;
12
13
use tokio:: sync:: Mutex ;
13
14
14
15
use std:: ops:: Deref ;
16
+ use std:: time:: Duration ;
15
17
16
18
const BCAST_PACKAGE_QUEUE_SIZE : usize = 50 ;
17
19
@@ -38,45 +40,68 @@ where
38
40
let mut receiver = self . queue_receiver . lock ( ) . await ;
39
41
while let Some ( next_package) = receiver. recv ( ) . await {
40
42
for tx in & next_package {
41
- match self . esplora_client . broadcast ( tx) . await {
42
- Ok ( ( ) ) => {
43
- log_trace ! ( self . logger, "Successfully broadcast transaction {}" , tx. txid( ) ) ;
44
- } ,
45
- Err ( e) => match e {
46
- esplora_client:: Error :: Reqwest ( err) => {
47
- if err. status ( ) == StatusCode :: from_u16 ( 400 ) . ok ( ) {
48
- // Ignore 400, as this just means bitcoind already knows the
49
- // transaction.
50
- // FIXME: We can further differentiate here based on the error
51
- // message which will be available with rust-esplora-client 0.7 and
52
- // later.
53
- } else {
43
+ let timeout_fut = tokio:: time:: timeout (
44
+ Duration :: from_secs ( TX_BROADCAST_TIMEOUT_SECS ) ,
45
+ self . esplora_client . broadcast ( tx) ,
46
+ ) ;
47
+ match timeout_fut. await {
48
+ Ok ( res) => match res {
49
+ Ok ( ( ) ) => {
50
+ log_trace ! (
51
+ self . logger,
52
+ "Successfully broadcast transaction {}" ,
53
+ tx. txid( )
54
+ ) ;
55
+ } ,
56
+ Err ( e) => match e {
57
+ esplora_client:: Error :: Reqwest ( err) => {
58
+ if err. status ( ) == StatusCode :: from_u16 ( 400 ) . ok ( ) {
59
+ // Ignore 400, as this just means bitcoind already knows the
60
+ // transaction.
61
+ // FIXME: We can further differentiate here based on the error
62
+ // message which will be available with rust-esplora-client 0.7 and
63
+ // later.
64
+ } else {
65
+ log_error ! (
66
+ self . logger,
67
+ "Failed to broadcast due to HTTP connection error: {}" ,
68
+ err
69
+ ) ;
70
+ }
71
+ log_trace ! (
72
+ self . logger,
73
+ "Failed broadcast transaction bytes: {}" ,
74
+ log_bytes!( tx. encode( ) )
75
+ ) ;
76
+ } ,
77
+ _ => {
54
78
log_error ! (
55
79
self . logger,
56
- "Failed to broadcast due to HTTP connection error: {}" ,
57
- err
80
+ "Failed to broadcast transaction {}: {}" ,
81
+ tx. txid( ) ,
82
+ e
58
83
) ;
59
84
log_trace ! (
60
85
self . logger,
61
86
"Failed broadcast transaction bytes: {}" ,
62
87
log_bytes!( tx. encode( ) )
63
88
) ;
64
- }
65
- } ,
66
- _ => {
67
- log_error ! (
68
- self . logger,
69
- "Failed to broadcast transaction {}: {}" ,
70
- tx. txid( ) ,
71
- e
72
- ) ;
73
- log_trace ! (
74
- self . logger,
75
- "Failed broadcast transaction bytes: {}" ,
76
- log_bytes!( tx. encode( ) )
77
- ) ;
89
+ } ,
78
90
} ,
79
91
} ,
92
+ Err ( e) => {
93
+ log_error ! (
94
+ self . logger,
95
+ "Failed to broadcast transaction due to timeout {}: {}" ,
96
+ tx. txid( ) ,
97
+ e
98
+ ) ;
99
+ log_trace ! (
100
+ self . logger,
101
+ "Failed broadcast transaction bytes: {}" ,
102
+ log_bytes!( tx. encode( ) )
103
+ ) ;
104
+ } ,
80
105
}
81
106
}
82
107
}
0 commit comments