@ -105,6 +105,8 @@ struct tracked_output {
/* If it is resolved. */
/* If it is resolved. */
struct resolution * resolved ;
struct resolution * resolved ;
const struct chainparams * chainparams ;
} ;
} ;
/* We vary feerate until signature they offered matches. */
/* We vary feerate until signature they offered matches. */
@ -305,7 +307,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
u8 * msg ;
u8 * msg ;
u8 * * witness ;
u8 * * witness ;
tx = bitcoin_tx ( ctx , 1 , 1 ) ;
tx = bitcoin_tx ( ctx , out - > chainparams , 1 , 1 ) ;
tx - > wtx - > locktime = locktime ;
tx - > wtx - > locktime = locktime ;
bitcoin_tx_add_input ( tx , & out - > txid , out - > outnum , to_self_delay ,
bitcoin_tx_add_input ( tx , & out - > txid , out - > outnum , to_self_delay ,
& out - > sat , NULL ) ;
& out - > sat , NULL ) ;
@ -399,16 +401,17 @@ static void hsm_get_per_commitment_point(struct pubkey *per_commitment_point)
}
}
static struct tracked_output *
static struct tracked_output *
new_tracked_output ( struct tracked_output * * * outs ,
new_tracked_output ( const struct chainparams * chainparams ,
const struct bitcoin_txid * txid ,
struct tracked_output * * * outs ,
u32 tx_blockheight ,
const struct bitcoin_txid * txid ,
enum tx_type tx_type ,
u32 tx_blockheight ,
u32 outnum ,
enum tx_type tx_type ,
struct amount_sat sat ,
u32 outnum ,
enum output_type output_type ,
struct amount_sat sat ,
const struct htlc_stub * htlc ,
enum output_type output_type ,
const u8 * wscript ,
const struct htlc_stub * htlc ,
const secp256k1_ecdsa_signature * remote_htlc_sig )
const u8 * wscript ,
const secp256k1_ecdsa_signature * remote_htlc_sig )
{
{
struct tracked_output * out = tal ( * outs , struct tracked_output ) ;
struct tracked_output * out = tal ( * outs , struct tracked_output ) ;
@ -427,6 +430,7 @@ static struct tracked_output *
out - > output_type = output_type ;
out - > output_type = output_type ;
out - > proposal = NULL ;
out - > proposal = NULL ;
out - > resolved = NULL ;
out - > resolved = NULL ;
out - > chainparams = chainparams ;
if ( htlc )
if ( htlc )
out - > htlc = * htlc ;
out - > htlc = * htlc ;
out - > wscript = tal_steal ( out , wscript ) ;
out - > wscript = tal_steal ( out , wscript ) ;
@ -900,7 +904,8 @@ static void handle_htlc_onchain_fulfill(struct tracked_output *out,
& preimage ) ) ) ;
& preimage ) ) ) ;
}
}
static void resolve_htlc_tx ( struct tracked_output * * * outs ,
static void resolve_htlc_tx ( const struct chainparams * chainparams ,
struct tracked_output * * * outs ,
size_t out_index ,
size_t out_index ,
const struct bitcoin_tx * htlc_tx ,
const struct bitcoin_tx * htlc_tx ,
const struct bitcoin_txid * htlc_txid ,
const struct bitcoin_txid * htlc_txid ,
@ -925,7 +930,7 @@ static void resolve_htlc_tx(struct tracked_output ***outs,
* output .
* output .
*/
*/
amt = bitcoin_tx_output_get_amount ( htlc_tx , 0 ) ;
amt = bitcoin_tx_output_get_amount ( htlc_tx , 0 ) ;
out = new_tracked_output ( outs , htlc_txid , tx_blockheight ,
out = new_tracked_output ( chainparams , outs , htlc_txid , tx_blockheight ,
( * outs ) [ out_index ] - > resolved - > tx_type ,
( * outs ) [ out_index ] - > resolved - > tx_type ,
0 , amt ,
0 , amt ,
DELAYED_OUTPUT_TO_US ,
DELAYED_OUTPUT_TO_US ,
@ -981,7 +986,8 @@ static void onchain_transaction_annotate(const struct bitcoin_txid *txid,
wire_sync_write ( REQ_FD , take ( msg ) ) ;
wire_sync_write ( REQ_FD , take ( msg ) ) ;
}
}
/* An output has been spent: see if it resolves something we care about. */
/* An output has been spent: see if it resolves something we care about. */
static void output_spent ( struct tracked_output * * * outs ,
static void output_spent ( const struct chainparams * chainparams ,
struct tracked_output * * * outs ,
const struct bitcoin_tx * tx ,
const struct bitcoin_tx * tx ,
u32 input_num ,
u32 input_num ,
u32 tx_blockheight )
u32 tx_blockheight )
@ -1008,7 +1014,7 @@ static void output_spent(struct tracked_output ***outs,
/* If it's our htlc tx, we need to resolve that, too. */
/* If it's our htlc tx, we need to resolve that, too. */
if ( out - > resolved - > tx_type = = OUR_HTLC_SUCCESS_TX
if ( out - > resolved - > tx_type = = OUR_HTLC_SUCCESS_TX
| | out - > resolved - > tx_type = = OUR_HTLC_TIMEOUT_TX )
| | out - > resolved - > tx_type = = OUR_HTLC_TIMEOUT_TX )
resolve_htlc_tx ( outs , i , tx , & txid ,
resolve_htlc_tx ( chainparams , outs , i , tx , & txid ,
tx_blockheight ) ;
tx_blockheight ) ;
return ;
return ;
}
}
@ -1194,7 +1200,8 @@ static void tx_new_depth(struct tracked_output **outs,
* - MUST NOT * resolve * the output by spending it .
* - MUST NOT * resolve * the output by spending it .
*/
*/
/* Master makes sure we only get told preimages once other node is committed. */
/* Master makes sure we only get told preimages once other node is committed. */
static void handle_preimage ( struct tracked_output * * outs ,
static void handle_preimage ( const struct chainparams * chainparams ,
struct tracked_output * * outs ,
const struct preimage * preimage )
const struct preimage * preimage )
{
{
size_t i ;
size_t i ;
@ -1249,7 +1256,9 @@ static void handle_preimage(struct tracked_output **outs,
type_to_string ( tmpctx ,
type_to_string ( tmpctx ,
struct amount_sat ,
struct amount_sat ,
& outs [ i ] - > sat ) ) ;
& outs [ i ] - > sat ) ) ;
tx = htlc_success_tx ( outs [ i ] , & outs [ i ] - > txid ,
tx = htlc_success_tx ( outs [ i ] ,
chainparams ,
& outs [ i ] - > txid ,
outs [ i ] - > outnum ,
outs [ i ] - > outnum ,
htlc_amount ,
htlc_amount ,
to_self_delay [ LOCAL ] ,
to_self_delay [ LOCAL ] ,
@ -1347,7 +1356,8 @@ static bool handle_dev_memleak(struct tracked_output **outs, const u8 *msg)
* - MUST monitor the blockchain for transactions that spend any output that
* - MUST monitor the blockchain for transactions that spend any output that
* is NOT * irrevocably resolved * .
* is NOT * irrevocably resolved * .
*/
*/
static void wait_for_resolved ( struct tracked_output * * outs )
static void wait_for_resolved ( const struct chainparams * chainparams ,
struct tracked_output * * outs )
{
{
billboard_update ( outs ) ;
billboard_update ( outs ) ;
@ -1365,9 +1375,9 @@ static void wait_for_resolved(struct tracked_output **outs)
tx_new_depth ( outs , & txid , depth ) ;
tx_new_depth ( outs , & txid , depth ) ;
else if ( fromwire_onchain_spent ( msg , msg , & tx , & input_num ,
else if ( fromwire_onchain_spent ( msg , msg , & tx , & input_num ,
& tx_blockheight ) )
& tx_blockheight ) )
output_spent ( & outs , tx , input_num , tx_blockheight ) ;
output_spent ( chainparams , & outs , tx , input_num , tx_blockheight ) ;
else if ( fromwire_onchain_known_preimage ( msg , & preimage ) )
else if ( fromwire_onchain_known_preimage ( msg , & preimage ) )
handle_preimage ( outs , & preimage ) ;
handle_preimage ( chainparams , outs , & preimage ) ;
else if ( ! handle_dev_memleak ( outs , msg ) )
else if ( ! handle_dev_memleak ( outs , msg ) )
master_badmsg ( - 1 , msg ) ;
master_badmsg ( - 1 , msg ) ;
@ -1387,7 +1397,8 @@ static void init_reply(const char *what)
peer_billboard ( true , what ) ;
peer_billboard ( true , what ) ;
}
}
static void handle_mutual_close ( const struct bitcoin_txid * txid ,
static void handle_mutual_close ( const struct chainparams * chainparams ,
const struct bitcoin_txid * txid ,
struct tracked_output * * outs )
struct tracked_output * * outs )
{
{
init_reply ( " Tracking mutual close transaction " ) ;
init_reply ( " Tracking mutual close transaction " ) ;
@ -1402,7 +1413,7 @@ static void handle_mutual_close(const struct bitcoin_txid *txid,
*/
*/
resolved_by_other ( outs [ 0 ] , txid , MUTUAL_CLOSE ) ;
resolved_by_other ( outs [ 0 ] , txid , MUTUAL_CLOSE ) ;
wait_for_resolved ( outs ) ;
wait_for_resolved ( chainparams , outs ) ;
}
}
static u8 * * derive_htlc_scripts ( const struct htlc_stub * htlcs , enum side side )
static u8 * * derive_htlc_scripts ( const struct htlc_stub * htlcs , enum side side )
@ -1432,7 +1443,8 @@ static u8 **derive_htlc_scripts(const struct htlc_stub *htlcs, enum side side)
return htlc_scripts ;
return htlc_scripts ;
}
}
static size_t resolve_our_htlc_ourcommit ( struct tracked_output * out ,
static size_t resolve_our_htlc_ourcommit ( const struct chainparams * chainparams ,
struct tracked_output * out ,
const size_t * matches ,
const size_t * matches ,
const struct htlc_stub * htlcs ,
const struct htlc_stub * htlcs ,
u8 * * htlc_scripts )
u8 * * htlc_scripts )
@ -1463,7 +1475,8 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
* - MUST * resolve * the output by spending it using the
* - MUST * resolve * the output by spending it using the
* HTLC - timeout transaction .
* HTLC - timeout transaction .
*/
*/
tx = htlc_timeout_tx ( tmpctx , & out - > txid , out - > outnum ,
tx = htlc_timeout_tx ( tmpctx , chainparams ,
& out - > txid , out - > outnum ,
htlc_amount ,
htlc_amount ,
htlcs [ matches [ i ] ] . cltv_expiry ,
htlcs [ matches [ i ] ] . cltv_expiry ,
to_self_delay [ LOCAL ] , 0 , keyset ) ;
to_self_delay [ LOCAL ] , 0 , keyset ) ;
@ -1778,7 +1791,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
* node ' s ` to_self_delay ` field ) before spending
* node ' s ` to_self_delay ` field ) before spending
* the output .
* the output .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
OUR_UNILATERAL , i ,
OUR_UNILATERAL , i ,
amt ,
amt ,
DELAYED_OUTPUT_TO_US ,
DELAYED_OUTPUT_TO_US ,
@ -1818,7 +1832,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
* node , as ` to_remote ` is considered * resolved *
* node , as ` to_remote ` is considered * resolved *
* by the commitment transaction itself .
* by the commitment transaction itself .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
OUR_UNILATERAL , i ,
OUR_UNILATERAL , i ,
amt ,
amt ,
OUTPUT_TO_THEM ,
OUTPUT_TO_THEM ,
@ -1844,7 +1859,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
* in [ HTLC Output Handling : Local Commitment ,
* in [ HTLC Output Handling : Local Commitment ,
* Local Offers ]
* Local Offers ]
*/
*/
out = new_tracked_output ( & outs , txid ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid ,
tx_blockheight ,
tx_blockheight ,
OUR_UNILATERAL , i ,
OUR_UNILATERAL , i ,
amt ,
amt ,
@ -1852,11 +1868,13 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
NULL , NULL ,
NULL , NULL ,
remote_htlc_sigs ) ;
remote_htlc_sigs ) ;
/* Tells us which htlc to use */
/* Tells us which htlc to use */
which_htlc = resolve_our_htlc_ourcommit ( out , matches ,
which_htlc = resolve_our_htlc_ourcommit ( tx - > chainparams ,
out , matches ,
htlcs ,
htlcs ,
htlc_scripts ) ;
htlc_scripts ) ;
} else {
} else {
out = new_tracked_output ( & outs , txid ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid ,
tx_blockheight ,
tx_blockheight ,
OUR_UNILATERAL , i ,
OUR_UNILATERAL , i ,
amt ,
amt ,
@ -1885,7 +1903,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx,
note_missing_htlcs ( htlc_scripts , htlcs ,
note_missing_htlcs ( htlc_scripts , htlcs ,
tell_if_missing , tell_immediately ) ;
tell_if_missing , tell_immediately ) ;
wait_for_resolved ( outs ) ;
wait_for_resolved ( tx - > chainparams , outs ) ;
}
}
/* We produce individual penalty txs. It's less efficient, but avoids them
/* We produce individual penalty txs. It's less efficient, but avoids them
@ -2085,7 +2103,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
* - Note : this output is considered * resolved * by
* - Note : this output is considered * resolved * by
* the commitment transaction itself .
* the commitment transaction itself .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
THEIR_REVOKED_UNILATERAL ,
THEIR_REVOKED_UNILATERAL ,
i , amt ,
i , amt ,
OUTPUT_TO_US , NULL , NULL , NULL ) ;
OUTPUT_TO_US , NULL , NULL , NULL ) ;
@ -2109,7 +2128,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
* - MUST * resolve * the _remote node ' s main output_ by
* - MUST * resolve * the _remote node ' s main output_ by
* spending it using the revocation private key .
* spending it using the revocation private key .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
THEIR_REVOKED_UNILATERAL , i ,
THEIR_REVOKED_UNILATERAL , i ,
amt ,
amt ,
DELAYED_OUTPUT_TO_THEM ,
DELAYED_OUTPUT_TO_THEM ,
@ -2136,7 +2156,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
* * spend the * commitment tx * once the HTLC timeout has passed .
* * spend the * commitment tx * once the HTLC timeout has passed .
* * spend the * HTLC - success tx * , if the remote node has published it .
* * spend the * HTLC - success tx * , if the remote node has published it .
*/
*/
out = new_tracked_output ( & outs , txid ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid ,
tx_blockheight ,
tx_blockheight ,
THEIR_REVOKED_UNILATERAL , i ,
THEIR_REVOKED_UNILATERAL , i ,
amt ,
amt ,
@ -2146,7 +2167,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
NULL ) ;
NULL ) ;
steal_htlc ( out ) ;
steal_htlc ( out ) ;
} else {
} else {
out = new_tracked_output ( & outs , txid ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid ,
tx_blockheight ,
tx_blockheight ,
THEIR_REVOKED_UNILATERAL , i ,
THEIR_REVOKED_UNILATERAL , i ,
amt ,
amt ,
@ -2168,7 +2190,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
note_missing_htlcs ( htlc_scripts , htlcs ,
note_missing_htlcs ( htlc_scripts , htlcs ,
tell_if_missing , tell_immediately ) ;
tell_if_missing , tell_immediately ) ;
wait_for_resolved ( outs ) ;
wait_for_resolved ( tx - > chainparams , outs ) ;
}
}
static void handle_their_unilateral ( const struct bitcoin_tx * tx ,
static void handle_their_unilateral ( const struct bitcoin_tx * tx ,
@ -2301,7 +2323,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
* - Note : ` to_remote ` is considered * resolved * by the
* - Note : ` to_remote ` is considered * resolved * by the
* commitment transaction itself .
* commitment transaction itself .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
THEIR_UNILATERAL ,
THEIR_UNILATERAL ,
i , amt ,
i , amt ,
OUTPUT_TO_US , NULL , NULL , NULL ) ;
OUTPUT_TO_US , NULL , NULL , NULL ) ;
@ -2328,7 +2351,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
* - Note : ` to_local ` is considered * resolved * by the
* - Note : ` to_local ` is considered * resolved * by the
* commitment transaction itself .
* commitment transaction itself .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
THEIR_UNILATERAL , i ,
THEIR_UNILATERAL , i ,
amt ,
amt ,
DELAYED_OUTPUT_TO_THEM ,
DELAYED_OUTPUT_TO_THEM ,
@ -2350,7 +2374,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
* [ HTLC Output Handling : Remote Commitment ,
* [ HTLC Output Handling : Remote Commitment ,
* Local Offers ]
* Local Offers ]
*/
*/
out = new_tracked_output ( & outs , txid ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid ,
tx_blockheight ,
tx_blockheight ,
THEIR_UNILATERAL , i ,
THEIR_UNILATERAL , i ,
amt ,
amt ,
@ -2362,7 +2387,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
htlcs ,
htlcs ,
htlc_scripts ) ;
htlc_scripts ) ;
} else {
} else {
out = new_tracked_output ( & outs , txid ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid ,
tx_blockheight ,
tx_blockheight ,
THEIR_UNILATERAL , i ,
THEIR_UNILATERAL , i ,
amt ,
amt ,
@ -2385,7 +2411,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx,
note_missing_htlcs ( htlc_scripts , htlcs ,
note_missing_htlcs ( htlc_scripts , htlcs ,
tell_if_missing , tell_immediately ) ;
tell_if_missing , tell_immediately ) ;
wait_for_resolved ( outs ) ;
wait_for_resolved ( tx - > chainparams , outs ) ;
}
}
static void handle_unknown_commitment ( const struct bitcoin_tx * tx ,
static void handle_unknown_commitment ( const struct bitcoin_tx * tx ,
@ -2435,7 +2461,8 @@ static void handle_unknown_commitment(const struct bitcoin_tx *tx,
* - Note : ` to_remote ` is considered * resolved * by the
* - Note : ` to_remote ` is considered * resolved * by the
* commitment transaction itself .
* commitment transaction itself .
*/
*/
out = new_tracked_output ( & outs , txid , tx_blockheight ,
out = new_tracked_output ( tx - > chainparams ,
& outs , txid , tx_blockheight ,
UNKNOWN_UNILATERAL ,
UNKNOWN_UNILATERAL ,
i , amt ,
i , amt ,
OUTPUT_TO_US , NULL , NULL , NULL ) ;
OUTPUT_TO_US , NULL , NULL , NULL ) ;
@ -2477,7 +2504,7 @@ search_done:
wire_sync_write ( REQ_FD , take ( msg ) ) ;
wire_sync_write ( REQ_FD , take ( msg ) ) ;
}
}
wait_for_resolved ( outs ) ;
wait_for_resolved ( tx - > chainparams , outs ) ;
}
}
int main ( int argc , char * argv [ ] )
int main ( int argc , char * argv [ ] )
@ -2538,6 +2565,8 @@ int main(int argc, char *argv[])
master_badmsg ( WIRE_ONCHAIN_INIT , msg ) ;
master_badmsg ( WIRE_ONCHAIN_INIT , msg ) ;
}
}
tx - > chainparams = chainparams_by_chainhash ( & chain_hash ) ;
status_trace ( " feerate_per_kw = %u " , feerate_per_kw ) ;
status_trace ( " feerate_per_kw = %u " , feerate_per_kw ) ;
bitcoin_txid ( tx , & txid ) ;
bitcoin_txid ( tx , & txid ) ;
/* We need to keep tx around, but there's only one: not really a leak */
/* We need to keep tx around, but there's only one: not really a leak */
@ -2561,7 +2590,7 @@ int main(int argc, char *argv[])
outs = tal_arr ( ctx , struct tracked_output * , 0 ) ;
outs = tal_arr ( ctx , struct tracked_output * , 0 ) ;
bitcoin_tx_input_get_txid ( tx , 0 , & tmptxid ) ;
bitcoin_tx_input_get_txid ( tx , 0 , & tmptxid ) ;
new_tracked_output ( & outs , & tmptxid ,
new_tracked_output ( tx - > chainparams , & outs , & tmptxid ,
0 , /* We don't care about funding blockheight */
0 , /* We don't care about funding blockheight */
FUNDING_TRANSACTION ,
FUNDING_TRANSACTION ,
tx - > wtx - > inputs [ 0 ] . index ,
tx - > wtx - > inputs [ 0 ] . index ,
@ -2586,7 +2615,7 @@ int main(int argc, char *argv[])
* [ BOLT # 2 : Channel Close ] ( 02 - peer - protocol . md # channel - close ) ) .
* [ BOLT # 2 : Channel Close ] ( 02 - peer - protocol . md # channel - close ) ) .
*/
*/
if ( is_mutual_close ( tx , scriptpubkey [ LOCAL ] , scriptpubkey [ REMOTE ] ) )
if ( is_mutual_close ( tx , scriptpubkey [ LOCAL ] , scriptpubkey [ REMOTE ] ) )
handle_mutual_close ( & txid , outs ) ;
handle_mutual_close ( tx - > chainparams , & txid , outs ) ;
else {
else {
/* BOLT #5:
/* BOLT #5:
*
*