@@ -1361,6 +1361,41 @@ impl ChannelManager {
1361
1361
pub fn test_restore_channel_monitor ( & self ) {
1362
1362
unimplemented ! ( ) ;
1363
1363
}
1364
+
1365
+ fn internal_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < msgs:: AcceptChannel , HandleError > {
1366
+ if msg. chain_hash != self . genesis_hash {
1367
+ return Err ( HandleError { err : "Unknown genesis block hash" , action : Some ( msgs:: ErrorAction :: IgnoreError ) } ) ;
1368
+ }
1369
+ let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1370
+ if channel_state. by_id . contains_key ( & msg. temporary_channel_id ) {
1371
+ return Err ( HandleError { err : "temporary_channel_id collision!" , action : Some ( msgs:: ErrorAction :: SendErrorMessage { msg : msgs:: ErrorMessage { channel_id : msg. temporary_channel_id , data : String :: new ( ) } } ) } ) ;
1372
+ }
1373
+
1374
+ let chan_keys = if cfg ! ( feature = "fuzztarget" ) {
1375
+ ChannelKeys {
1376
+ funding_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ) . unwrap ( ) ,
1377
+ revocation_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 ] ) . unwrap ( ) ,
1378
+ payment_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , 0 ] ) . unwrap ( ) ,
1379
+ delayed_payment_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , 0 ] ) . unwrap ( ) ,
1380
+ htlc_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , 0 ] ) . unwrap ( ) ,
1381
+ channel_close_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , 0 ] ) . unwrap ( ) ,
1382
+ channel_monitor_claim_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 0 ] ) . unwrap ( ) ,
1383
+ commitment_seed : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
1384
+ }
1385
+ } else {
1386
+ let mut key_seed = [ 0u8 ; 32 ] ;
1387
+ rng:: fill_bytes ( & mut key_seed) ;
1388
+ match ChannelKeys :: new_from_seed ( & key_seed) {
1389
+ Ok ( key) => key,
1390
+ Err ( _) => panic ! ( "RNG is busted!" )
1391
+ }
1392
+ } ;
1393
+
1394
+ let channel = Channel :: new_from_req ( & * self . fee_estimator , chan_keys, their_node_id. clone ( ) , msg, 0 , false , self . announce_channels_publicly , Arc :: clone ( & self . logger ) ) ?;
1395
+ let accept_msg = channel. get_accept_channel ( ) ?;
1396
+ channel_state. by_id . insert ( channel. channel_id ( ) , channel) ;
1397
+ Ok ( accept_msg)
1398
+ }
1364
1399
}
1365
1400
1366
1401
impl events:: EventsProvider for ChannelManager {
@@ -1487,41 +1522,42 @@ impl ChainListener for ChannelManager {
1487
1522
}
1488
1523
}
1489
1524
1490
- impl ChannelMessageHandler for ChannelManager {
1491
- //TODO: Handle errors and close channel (or so)
1492
- fn handle_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < msgs:: AcceptChannel , HandleError > {
1493
- if msg. chain_hash != self . genesis_hash {
1494
- return Err ( HandleError { err : "Unknown genesis block hash" , action : None } ) ;
1495
- }
1496
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1497
- if channel_state. by_id . contains_key ( & msg. temporary_channel_id ) {
1498
- return Err ( HandleError { err : "temporary_channel_id collision!" , action : None } ) ;
1525
+ macro_rules! handle_error {
1526
+ ( $self: ident, $internal: expr, $their_node_id: expr) => {
1527
+ match $internal {
1528
+ Ok ( msg) => Ok ( msg) ,
1529
+ Err ( e) => {
1530
+ match e. action {
1531
+ Some ( msgs:: ErrorAction :: DisconnectPeer { msg: error_msg } ) => {
1532
+ let error_msg = error_msg. unwrap( ) ;
1533
+ $self. close_channel( & error_msg. channel_id) ;
1534
+ let mut pending_events = $self. pending_events. lock( ) . unwrap( ) ;
1535
+ pending_events. push( events:: Event :: HandleError {
1536
+ node_id: $their_node_id,
1537
+ action: Some ( msgs:: ErrorAction :: DisconnectPeer { msg: Some ( error_msg) } ) ,
1538
+ } ) ;
1539
+ return Err ( false ) ;
1540
+ } ,
1541
+ Some ( msgs:: ErrorAction :: IgnoreError ) => {
1542
+ return Err ( false ) ;
1543
+ } ,
1544
+ Some ( msgs:: ErrorAction :: SendErrorMessage { msg: error_msg } ) => {
1545
+ $self. close_channel( & error_msg. channel_id) ;
1546
+ return Err ( false ) ;
1547
+ } ,
1548
+ None => {
1549
+ return Err ( false ) ;
1550
+ } ,
1551
+ }
1552
+ } ,
1499
1553
}
1554
+ }
1555
+ }
1500
1556
1501
- let chan_keys = if cfg ! ( feature = "fuzztarget" ) {
1502
- ChannelKeys {
1503
- funding_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ] ) . unwrap ( ) ,
1504
- revocation_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 ] ) . unwrap ( ) ,
1505
- payment_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 3 , 0 ] ) . unwrap ( ) ,
1506
- delayed_payment_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , 0 ] ) . unwrap ( ) ,
1507
- htlc_base_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , 0 ] ) . unwrap ( ) ,
1508
- channel_close_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , 0 ] ) . unwrap ( ) ,
1509
- channel_monitor_claim_key : SecretKey :: from_slice ( & self . secp_ctx , & [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 0 ] ) . unwrap ( ) ,
1510
- commitment_seed : [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
1511
- }
1512
- } else {
1513
- let mut key_seed = [ 0u8 ; 32 ] ;
1514
- rng:: fill_bytes ( & mut key_seed) ;
1515
- match ChannelKeys :: new_from_seed ( & key_seed) {
1516
- Ok ( key) => key,
1517
- Err ( _) => panic ! ( "RNG is busted!" )
1518
- }
1519
- } ;
1520
-
1521
- let channel = Channel :: new_from_req ( & * self . fee_estimator , chan_keys, their_node_id. clone ( ) , msg, 0 , false , self . announce_channels_publicly , Arc :: clone ( & self . logger ) ) ?;
1522
- let accept_msg = channel. get_accept_channel ( ) ?;
1523
- channel_state. by_id . insert ( channel. channel_id ( ) , channel) ;
1524
- Ok ( accept_msg)
1557
+ impl ChannelMessageHandler for ChannelManager {
1558
+ //TODO: Handle errors and close channel (or so)
1559
+ fn handle_open_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: OpenChannel ) -> Result < msgs:: AcceptChannel , bool > {
1560
+ handle_error ! ( self , self . internal_open_channel( their_node_id, msg) , * their_node_id)
1525
1561
}
1526
1562
1527
1563
fn handle_accept_channel ( & self , their_node_id : & PublicKey , msg : & msgs:: AcceptChannel ) -> Result < ( ) , HandleError > {
0 commit comments