@ -61,21 +61,25 @@ static void peer_offer_channel(struct lightningd *ld,
struct funding_channel * fc ,
struct funding_channel * fc ,
const struct wireaddr * addr ,
const struct wireaddr * addr ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
const u8 * gfeatures , const u8 * lfeatures ,
const u8 * gfeatures , const u8 * lfeatures ,
int peer_fd , int gossip_fd ) ;
int peer_fd , int gossip_fd ) ;
static bool peer_start_channeld ( struct peer * peer ,
static bool peer_start_channeld ( struct peer * peer ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
const u8 * funding_signed ,
const u8 * funding_signed ,
bool reconnected ) ;
bool reconnected ) ;
static void peer_start_closingd ( struct peer * peer ,
static void peer_start_closingd ( struct peer * peer ,
struct crypto_state * cs ,
struct crypto_state * cs ,
u64 gossip_index ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
bool reconnected ) ;
bool reconnected ) ;
static void peer_accept_channel ( struct lightningd * ld ,
static void peer_accept_channel ( struct lightningd * ld ,
const struct pubkey * peer_id ,
const struct pubkey * peer_id ,
const struct wireaddr * addr ,
const struct wireaddr * addr ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
const u8 * gfeatures , const u8 * lfeatures ,
const u8 * gfeatures , const u8 * lfeatures ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
const u8 * open_msg ) ;
const u8 * open_msg ) ;
@ -548,9 +552,10 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
u8 * error ;
u8 * error ;
struct peer * peer ;
struct peer * peer ;
struct wireaddr addr ;
struct wireaddr addr ;
u64 gossip_index ;
if ( ! fromwire_gossip_peer_connected ( msg , msg , NULL ,
if ( ! fromwire_gossip_peer_connected ( msg , msg , NULL ,
& id , & addr , & cs ,
& id , & addr , & cs , & gossip_index ,
& gfeatures , & lfeatures ) )
& gfeatures , & lfeatures ) )
fatal ( " Gossip gave bad GOSSIP_PEER_CONNECTED message %s " ,
fatal ( " Gossip gave bad GOSSIP_PEER_CONNECTED message %s " ,
tal_hex ( msg , msg ) ) ;
tal_hex ( msg , msg ) ) ;
@ -620,7 +625,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
peer_set_owner ( peer , NULL ) ;
peer_set_owner ( peer , NULL ) ;
peer - > addr = addr ;
peer - > addr = addr ;
peer_start_channeld ( peer , & cs , peer_fd , gossip_fd , NULL ,
peer_start_channeld ( peer , & cs , gossip_index ,
peer_fd , gossip_fd , NULL ,
true ) ;
true ) ;
return ;
return ;
@ -631,7 +637,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
peer_set_owner ( peer , NULL ) ;
peer_set_owner ( peer , NULL ) ;
peer - > addr = addr ;
peer - > addr = addr ;
peer_start_closingd ( peer , & cs , peer_fd , gossip_fd ,
peer_start_closingd ( peer , & cs , gossip_index ,
peer_fd , gossip_fd ,
true ) ;
true ) ;
return ;
return ;
}
}
@ -640,7 +647,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
return_to_gossipd :
return_to_gossipd :
/* Otherwise, we hand back to gossipd, to continue. */
/* Otherwise, we hand back to gossipd, to continue. */
msg = towire_gossipctl_hand_back_peer ( msg , & id , & cs , NULL ) ;
msg = towire_gossipctl_hand_back_peer ( msg , & id , & cs , gossip_index , NULL ) ;
subd_send_msg ( ld - > gossip , take ( msg ) ) ;
subd_send_msg ( ld - > gossip , take ( msg ) ) ;
subd_send_fd ( ld - > gossip , peer_fd ) ;
subd_send_fd ( ld - > gossip , peer_fd ) ;
subd_send_fd ( ld - > gossip , gossip_fd ) ;
subd_send_fd ( ld - > gossip , gossip_fd ) ;
@ -652,7 +659,8 @@ return_to_gossipd:
send_error :
send_error :
/* Hand back to gossipd, with an error packet. */
/* Hand back to gossipd, with an error packet. */
connect_failed ( ld , & id , sanitize_error ( msg , error , NULL ) ) ;
connect_failed ( ld , & id , sanitize_error ( msg , error , NULL ) ) ;
msg = towire_gossipctl_hand_back_peer ( msg , & id , & cs , error ) ;
msg = towire_gossipctl_hand_back_peer ( msg , & id , & cs , gossip_index ,
error ) ;
subd_send_msg ( ld - > gossip , take ( msg ) ) ;
subd_send_msg ( ld - > gossip , take ( msg ) ) ;
subd_send_fd ( ld - > gossip , peer_fd ) ;
subd_send_fd ( ld - > gossip , peer_fd ) ;
subd_send_fd ( ld - > gossip , gossip_fd ) ;
subd_send_fd ( ld - > gossip , gossip_fd ) ;
@ -662,6 +670,7 @@ void peer_sent_nongossip(struct lightningd *ld,
const struct pubkey * id ,
const struct pubkey * id ,
const struct wireaddr * addr ,
const struct wireaddr * addr ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
const u8 * gfeatures ,
const u8 * gfeatures ,
const u8 * lfeatures ,
const u8 * lfeatures ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
@ -689,7 +698,8 @@ void peer_sent_nongossip(struct lightningd *ld,
/* Open request? */
/* Open request? */
if ( fromwire_peektype ( in_msg ) = = WIRE_OPEN_CHANNEL ) {
if ( fromwire_peektype ( in_msg ) = = WIRE_OPEN_CHANNEL ) {
peer_accept_channel ( ld , id , addr , cs , gfeatures , lfeatures ,
peer_accept_channel ( ld , id , addr , cs , gossip_index ,
gfeatures , lfeatures ,
peer_fd , gossip_fd , in_msg ) ;
peer_fd , gossip_fd , in_msg ) ;
return ;
return ;
}
}
@ -702,7 +712,7 @@ void peer_sent_nongossip(struct lightningd *ld,
send_error :
send_error :
/* Hand back to gossipd, with an error packet. */
/* Hand back to gossipd, with an error packet. */
connect_failed ( ld , id , sanitize_error ( error , error , NULL ) ) ;
connect_failed ( ld , id , sanitize_error ( error , error , NULL ) ) ;
msg = towire_gossipctl_hand_back_peer ( ld , id , cs , error ) ;
msg = towire_gossipctl_hand_back_peer ( ld , id , cs , gossip_index , error ) ;
subd_send_msg ( ld - > gossip , take ( msg ) ) ;
subd_send_msg ( ld - > gossip , take ( msg ) ) ;
subd_send_fd ( ld - > gossip , peer_fd ) ;
subd_send_fd ( ld - > gossip , peer_fd ) ;
subd_send_fd ( ld - > gossip , gossip_fd ) ;
subd_send_fd ( ld - > gossip , gossip_fd ) ;
@ -1528,7 +1538,8 @@ static enum watch_result funding_lockin_cb(struct peer *peer,
static void opening_got_hsm_funding_sig ( struct funding_channel * fc ,
static void opening_got_hsm_funding_sig ( struct funding_channel * fc ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
const u8 * resp ,
const u8 * resp ,
const struct crypto_state * cs )
const struct crypto_state * cs ,
u64 gossip_index )
{
{
secp256k1_ecdsa_signature * sigs ;
secp256k1_ecdsa_signature * sigs ;
struct bitcoin_tx * tx = fc - > funding_tx ;
struct bitcoin_tx * tx = fc - > funding_tx ;
@ -1580,7 +1591,8 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc,
fc - > peer - > opening_cmd = NULL ;
fc - > peer - > opening_cmd = NULL ;
/* Start normal channel daemon. */
/* Start normal channel daemon. */
peer_start_channeld ( fc - > peer , cs , peer_fd , gossip_fd , NULL , false ) ;
peer_start_channeld ( fc - > peer , cs , gossip_index ,
peer_fd , gossip_fd , NULL , false ) ;
peer_set_condition ( fc - > peer , OPENINGD , CHANNELD_AWAITING_LOCKIN ) ;
peer_set_condition ( fc - > peer , OPENINGD , CHANNELD_AWAITING_LOCKIN ) ;
wallet_confirm_utxos ( fc - > peer - > ld - > wallet , fc - > utxomap ) ;
wallet_confirm_utxos ( fc - > peer - > ld - > wallet , fc - > utxomap ) ;
@ -1829,7 +1841,10 @@ static void peer_received_closing_signature(struct peer *peer, const u8 *msg)
static void peer_closing_complete ( struct peer * peer , const u8 * msg )
static void peer_closing_complete ( struct peer * peer , const u8 * msg )
{
{
if ( ! fromwire_closing_complete ( msg , NULL ) ) {
/* FIXME: We should save this, to return to gossipd */
u64 gossip_index ;
if ( ! fromwire_closing_complete ( msg , NULL , & gossip_index ) ) {
peer_internal_error ( peer , " Bad closing_complete %s " ,
peer_internal_error ( peer , " Bad closing_complete %s " ,
tal_hex ( peer , msg ) ) ;
tal_hex ( peer , msg ) ) ;
return ;
return ;
@ -1867,6 +1882,7 @@ static unsigned closing_msg(struct subd *sd, const u8 *msg, const int *fds)
static void peer_start_closingd ( struct peer * peer ,
static void peer_start_closingd ( struct peer * peer ,
struct crypto_state * cs ,
struct crypto_state * cs ,
u64 gossip_index ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
bool reconnected )
bool reconnected )
{
{
@ -1942,6 +1958,7 @@ static void peer_start_closingd(struct peer *peer,
*/
*/
initmsg = towire_closing_init ( tmpctx ,
initmsg = towire_closing_init ( tmpctx ,
cs ,
cs ,
gossip_index ,
peer - > seed ,
peer - > seed ,
peer - > funding_txid ,
peer - > funding_txid ,
peer - > funding_outnum ,
peer - > funding_outnum ,
@ -1970,18 +1987,19 @@ static void peer_start_closingd_after_shutdown(struct peer *peer, const u8 *msg,
const int * fds )
const int * fds )
{
{
struct crypto_state cs ;
struct crypto_state cs ;
u64 gossip_index ;
/* We expect 2 fds. */
/* We expect 2 fds. */
assert ( tal_count ( fds ) = = 2 ) ;
assert ( tal_count ( fds ) = = 2 ) ;
if ( ! fromwire_channel_shutdown_complete ( msg , NULL , & cs ) ) {
if ( ! fromwire_channel_shutdown_complete ( msg , NULL , & cs , & gossip_index ) ) {
peer_internal_error ( peer , " bad shutdown_complete: %s " ,
peer_internal_error ( peer , " bad shutdown_complete: %s " ,
tal_hex ( peer , msg ) ) ;
tal_hex ( peer , msg ) ) ;
return ;
return ;
}
}
/* This sets peer->owner, closes down channeld. */
/* This sets peer->owner, closes down channeld. */
peer_start_closingd ( peer , & cs , fds [ 0 ] , fds [ 1 ] , false ) ;
peer_start_closingd ( peer , & cs , gossip_index , fds [ 0 ] , fds [ 1 ] , false ) ;
peer_set_condition ( peer , CHANNELD_SHUTTING_DOWN , CLOSINGD_SIGEXCHANGE ) ;
peer_set_condition ( peer , CHANNELD_SHUTTING_DOWN , CLOSINGD_SIGEXCHANGE ) ;
}
}
@ -2045,6 +2063,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
static bool peer_start_channeld ( struct peer * peer ,
static bool peer_start_channeld ( struct peer * peer ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
const u8 * funding_signed ,
const u8 * funding_signed ,
bool reconnected )
bool reconnected )
@ -2135,7 +2154,7 @@ static bool peer_start_channeld(struct peer *peer,
get_feerate ( peer - > ld - > topology , FEERATE_NORMAL ) ,
get_feerate ( peer - > ld - > topology , FEERATE_NORMAL ) ,
get_feerate ( peer - > ld - > topology , FEERATE_IMMEDIATE ) * 5 ,
get_feerate ( peer - > ld - > topology , FEERATE_IMMEDIATE ) * 5 ,
peer - > last_sig ,
peer - > last_sig ,
cs ,
cs , gossip_index ,
& peer - > channel_info - > remote_fundingkey ,
& peer - > channel_info - > remote_fundingkey ,
& peer - > channel_info - > theirbase . revocation ,
& peer - > channel_info - > theirbase . revocation ,
& peer - > channel_info - > theirbase . payment ,
& peer - > channel_info - > theirbase . payment ,
@ -2196,6 +2215,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp,
struct crypto_state cs ;
struct crypto_state cs ;
secp256k1_ecdsa_signature remote_commit_sig ;
secp256k1_ecdsa_signature remote_commit_sig ;
struct bitcoin_tx * remote_commit ;
struct bitcoin_tx * remote_commit ;
u64 gossip_index ;
assert ( tal_count ( fds ) = = 2 ) ;
assert ( tal_count ( fds ) = = 2 ) ;
@ -2212,6 +2232,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp,
remote_commit ,
remote_commit ,
& remote_commit_sig ,
& remote_commit_sig ,
& cs ,
& cs ,
& gossip_index ,
& channel_info - > theirbase . revocation ,
& channel_info - > theirbase . revocation ,
& channel_info - > theirbase . payment ,
& channel_info - > theirbase . payment ,
& channel_info - > theirbase . htlc ,
& channel_info - > theirbase . htlc ,
@ -2301,7 +2322,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp,
fatal ( " Could not write to HSM: %s " , strerror ( errno ) ) ;
fatal ( " Could not write to HSM: %s " , strerror ( errno ) ) ;
msg = hsm_sync_read ( fc , fc - > peer - > ld ) ;
msg = hsm_sync_read ( fc , fc - > peer - > ld ) ;
opening_got_hsm_funding_sig ( fc , fds [ 0 ] , fds [ 1 ] , msg , & cs ) ;
opening_got_hsm_funding_sig ( fc , fds [ 0 ] , fds [ 1 ] , msg , & cs , gossip_index ) ;
}
}
static void opening_fundee_finished ( struct subd * opening ,
static void opening_fundee_finished ( struct subd * opening ,
@ -2312,6 +2333,7 @@ static void opening_fundee_finished(struct subd *opening,
u8 * funding_signed ;
u8 * funding_signed ;
struct channel_info * channel_info ;
struct channel_info * channel_info ;
struct crypto_state cs ;
struct crypto_state cs ;
u64 gossip_index ;
secp256k1_ecdsa_signature remote_commit_sig ;
secp256k1_ecdsa_signature remote_commit_sig ;
struct bitcoin_tx * remote_commit ;
struct bitcoin_tx * remote_commit ;
@ -2331,6 +2353,7 @@ static void opening_fundee_finished(struct subd *opening,
remote_commit ,
remote_commit ,
& remote_commit_sig ,
& remote_commit_sig ,
& cs ,
& cs ,
& gossip_index ,
& channel_info - > theirbase . revocation ,
& channel_info - > theirbase . revocation ,
& channel_info - > theirbase . payment ,
& channel_info - > theirbase . payment ,
& channel_info - > theirbase . htlc ,
& channel_info - > theirbase . htlc ,
@ -2376,7 +2399,8 @@ static void opening_fundee_finished(struct subd *opening,
peer_set_owner ( peer , NULL ) ;
peer_set_owner ( peer , NULL ) ;
/* On to normal operation! */
/* On to normal operation! */
peer_start_channeld ( peer , & cs , fds [ 0 ] , fds [ 1 ] , funding_signed , false ) ;
peer_start_channeld ( peer , & cs , gossip_index ,
fds [ 0 ] , fds [ 1 ] , funding_signed , false ) ;
peer_set_condition ( peer , OPENINGD , CHANNELD_AWAITING_LOCKIN ) ;
peer_set_condition ( peer , OPENINGD , CHANNELD_AWAITING_LOCKIN ) ;
}
}
@ -2386,6 +2410,7 @@ static unsigned int opening_negotiation_failed(struct subd *openingd,
const int * fds )
const int * fds )
{
{
struct crypto_state cs ;
struct crypto_state cs ;
u64 gossip_index ;
struct peer * peer = openingd - > peer ;
struct peer * peer = openingd - > peer ;
u8 * err ;
u8 * err ;
const char * why ;
const char * why ;
@ -2394,14 +2419,16 @@ static unsigned int opening_negotiation_failed(struct subd *openingd,
if ( tal_count ( fds ) = = 0 )
if ( tal_count ( fds ) = = 0 )
return 2 ;
return 2 ;
if ( ! fromwire_opening_negotiation_failed ( msg , msg , NULL , & cs , & err ) ) {
if ( ! fromwire_opening_negotiation_failed ( msg , msg , NULL ,
& cs , & gossip_index , & err ) ) {
peer_internal_error ( peer ,
peer_internal_error ( peer ,
" bad OPENING_NEGOTIATION_FAILED %s " ,
" bad OPENING_NEGOTIATION_FAILED %s " ,
tal_hex ( msg , msg ) ) ;
tal_hex ( msg , msg ) ) ;
return 0 ;
return 0 ;
}
}
msg = towire_gossipctl_hand_back_peer ( msg , & peer - > id , & cs , NULL ) ;
msg = towire_gossipctl_hand_back_peer ( msg , & peer - > id , & cs , gossip_index ,
NULL ) ;
subd_send_msg ( openingd - > ld - > gossip , take ( msg ) ) ;
subd_send_msg ( openingd - > ld - > gossip , take ( msg ) ) ;
subd_send_fd ( openingd - > ld - > gossip , fds [ 0 ] ) ;
subd_send_fd ( openingd - > ld - > gossip , fds [ 0 ] ) ;
subd_send_fd ( openingd - > ld - > gossip , fds [ 1 ] ) ;
subd_send_fd ( openingd - > ld - > gossip , fds [ 1 ] ) ;
@ -2419,6 +2446,7 @@ static void peer_accept_channel(struct lightningd *ld,
const struct pubkey * peer_id ,
const struct pubkey * peer_id ,
const struct wireaddr * addr ,
const struct wireaddr * addr ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
const u8 * gfeatures , const u8 * lfeatures ,
const u8 * gfeatures , const u8 * lfeatures ,
int peer_fd , int gossip_fd ,
int peer_fd , int gossip_fd ,
const u8 * open_msg )
const u8 * open_msg )
@ -2470,7 +2498,7 @@ static void peer_accept_channel(struct lightningd *ld,
& peer - > our_config ,
& peer - > our_config ,
max_to_self_delay ,
max_to_self_delay ,
min_effective_htlc_capacity_msat ,
min_effective_htlc_capacity_msat ,
cs , peer - > seed ) ;
cs , gossip_index , peer - > seed ) ;
subd_send_msg ( peer - > owner , take ( msg ) ) ;
subd_send_msg ( peer - > owner , take ( msg ) ) ;
@ -2493,6 +2521,7 @@ static void peer_offer_channel(struct lightningd *ld,
struct funding_channel * fc ,
struct funding_channel * fc ,
const struct wireaddr * addr ,
const struct wireaddr * addr ,
const struct crypto_state * cs ,
const struct crypto_state * cs ,
u64 gossip_index ,
const u8 * gfeatures , const u8 * lfeatures ,
const u8 * gfeatures , const u8 * lfeatures ,
int peer_fd , int gossip_fd )
int peer_fd , int gossip_fd )
{
{
@ -2550,7 +2579,7 @@ static void peer_offer_channel(struct lightningd *ld,
& fc - > peer - > our_config ,
& fc - > peer - > our_config ,
max_to_self_delay ,
max_to_self_delay ,
min_effective_htlc_capacity_msat ,
min_effective_htlc_capacity_msat ,
cs , fc - > peer - > seed ) ;
cs , gossip_index , fc - > peer - > seed ) ;
subd_send_msg ( fc - > peer - > owner , take ( msg ) ) ;
subd_send_msg ( fc - > peer - > owner , take ( msg ) ) ;
utxos = from_utxoptr_arr ( fc , fc - > utxomap ) ;
utxos = from_utxoptr_arr ( fc , fc - > utxomap ) ;
@ -2579,6 +2608,7 @@ static void gossip_peer_released(struct subd *gossip,
{
{
struct lightningd * ld = gossip - > ld ;
struct lightningd * ld = gossip - > ld ;
struct crypto_state cs ;
struct crypto_state cs ;
u64 gossip_index ;
u8 * gfeatures , * lfeatures ;
u8 * gfeatures , * lfeatures ;
struct wireaddr addr ;
struct wireaddr addr ;
@ -2586,6 +2616,7 @@ static void gossip_peer_released(struct subd *gossip,
fc - > peer = peer_by_id ( ld , & fc - > peerid ) ;
fc - > peer = peer_by_id ( ld , & fc - > peerid ) ;
if ( ! fromwire_gossipctl_release_peer_reply ( fc , resp , NULL , & addr , & cs ,
if ( ! fromwire_gossipctl_release_peer_reply ( fc , resp , NULL , & addr , & cs ,
& gossip_index ,
& gfeatures , & lfeatures ) ) {
& gfeatures , & lfeatures ) ) {
if ( ! fromwire_gossipctl_release_peer_replyfail ( resp , NULL ) ) {
if ( ! fromwire_gossipctl_release_peer_replyfail ( resp , NULL ) ) {
fatal ( " Gossip daemon gave invalid reply %s " ,
fatal ( " Gossip daemon gave invalid reply %s " ,
@ -2611,7 +2642,8 @@ static void gossip_peer_released(struct subd *gossip,
}
}
/* OK, offer peer a channel. */
/* OK, offer peer a channel. */
peer_offer_channel ( ld , fc , & addr , & cs , gfeatures , lfeatures ,
peer_offer_channel ( ld , fc , & addr , & cs , gossip_index ,
gfeatures , lfeatures ,
fds [ 0 ] , fds [ 1 ] ) ;
fds [ 0 ] , fds [ 1 ] ) ;
}
}