2016-04-24 12:12:18 +02:00
# include "bitcoin/block.h"
2018-06-20 08:47:57 +02:00
# include "bitcoin/feerate.h"
2018-03-04 01:38:49 +01:00
# include "bitcoin/script.h"
2016-04-24 12:12:18 +02:00
# include "bitcoin/tx.h"
2016-04-24 12:07:13 +02:00
# include "bitcoind.h"
# include "chaintopology.h"
2016-11-09 07:44:21 +01:00
# include "jsonrpc.h"
2016-04-24 12:07:13 +02:00
# include "lightningd.h"
# include "log.h"
# include "watch.h"
2016-04-24 12:12:18 +02:00
# include <ccan/array_size/array_size.h>
# include <ccan/asort/asort.h>
2017-11-21 04:30:29 +01:00
# include <ccan/build_assert/build_assert.h>
2016-08-09 05:41:22 +02:00
# include <ccan/io/io.h>
2016-11-07 13:29:02 +01:00
# include <ccan/tal/str/str.h>
2017-12-15 11:17:54 +01:00
# include <common/memleak.h>
2017-08-28 18:04:01 +02:00
# include <common/timeout.h>
2017-08-28 18:02:01 +02:00
# include <common/utils.h>
2016-08-18 06:53:46 +02:00
# include <inttypes.h>
2018-05-06 15:32:01 +02:00
# include <lightningd/channel_control.h>
2018-03-28 12:03:40 +02:00
# include <lightningd/gossip_control.h>
2018-07-16 22:48:38 +02:00
# include <lightningd/param.h>
2016-04-24 12:07:13 +02:00
2017-12-21 12:09:25 +01:00
/* Mutual recursion via timer. */
static void try_extend_tip ( struct chain_topology * topo ) ;
2016-05-09 22:56:09 +02:00
2017-03-02 13:21:49 +01:00
static void next_topology_timer ( struct chain_topology * topo )
2016-05-09 22:56:09 +02:00
{
2017-12-15 11:22:57 +01:00
/* This takes care of its own lifetime. */
2018-05-17 06:08:24 +02:00
notleak ( new_reltimer ( topo - > timers , topo ,
time_from_sec ( topo - > poll_seconds ) ,
2017-12-21 12:09:25 +01:00
try_extend_tip , topo ) ) ;
2016-05-09 22:56:09 +02:00
}
2017-03-02 13:21:49 +01:00
static bool we_broadcast ( const struct chain_topology * topo ,
2017-12-18 07:41:52 +01:00
const struct bitcoin_txid * txid )
2016-05-04 08:41:16 +02:00
{
2017-03-02 13:21:49 +01:00
const struct outgoing_tx * otx ;
2016-05-04 08:41:16 +02:00
2017-03-02 13:21:49 +01:00
list_for_each ( & topo - > outgoing_txs , otx , list ) {
2018-07-04 07:30:02 +02:00
if ( bitcoin_txid_eq ( & otx - > txid , txid ) )
2017-03-02 13:21:49 +01:00
return true ;
2016-05-04 08:41:16 +02:00
}
return false ;
}
2017-12-21 12:09:25 +01:00
static void filter_block_txs ( struct chain_topology * topo , struct block * b )
2016-04-24 12:07:13 +02:00
{
2016-05-04 08:36:19 +02:00
size_t i ;
2017-11-25 13:31:20 +01:00
u64 satoshi_owned ;
2016-04-24 12:07:13 +02:00
2016-04-24 12:18:35 +02:00
/* Now we see if any of those txs are interesting. */
for ( i = 0 ; i < tal_count ( b - > full_txs ) ; i + + ) {
2017-08-18 06:43:53 +02:00
const struct bitcoin_tx * tx = b - > full_txs [ i ] ;
2017-12-18 07:41:52 +01:00
struct bitcoin_txid txid ;
2016-04-24 12:18:35 +02:00
size_t j ;
/* Tell them if it spends a txo we care about. */
2017-01-25 00:35:43 +01:00
for ( j = 0 ; j < tal_count ( tx - > input ) ; j + + ) {
2016-04-24 12:18:35 +02:00
struct txwatch_output out ;
struct txowatch * txo ;
out . txid = tx - > input [ j ] . txid ;
out . index = tx - > input [ j ] . index ;
2017-03-02 13:21:49 +01:00
txo = txowatch_hash_get ( & topo - > txowatches , & out ) ;
2018-04-17 15:20:27 +02:00
if ( txo ) {
wallet_transaction_add ( topo - > wallet , tx , b - > height , i ) ;
2018-02-21 16:57:45 +01:00
txowatch_fire ( txo , tx , j , b ) ;
2018-04-17 15:20:27 +02:00
}
2016-04-24 12:18:35 +02:00
}
2017-11-25 13:31:20 +01:00
satoshi_owned = 0 ;
2017-11-27 16:20:10 +01:00
if ( txfilter_match ( topo - > bitcoind - > ld - > owned_txfilter , tx ) ) {
wallet_extract_owned_outputs ( topo - > bitcoind - > ld - > wallet ,
2018-03-22 00:13:16 +01:00
tx , & b - > height ,
& satoshi_owned ) ;
2017-11-27 16:20:10 +01:00
}
2017-11-25 13:31:20 +01:00
2016-05-04 08:40:37 +02:00
/* We did spends first, in case that tells us to watch tx. */
2016-04-24 12:31:52 +02:00
bitcoin_txid ( tx , & txid ) ;
2017-11-25 13:31:20 +01:00
if ( watching_txid ( topo , & txid ) | | we_broadcast ( topo , & txid ) | |
2018-04-09 14:37:21 +02:00
satoshi_owned ! = 0 ) {
wallet_transaction_add ( topo - > wallet , tx , b - > height , i ) ;
}
2016-04-24 12:18:35 +02:00
}
b - > full_txs = tal_free ( b - > full_txs ) ;
2016-04-24 12:07:13 +02:00
}
2017-03-02 13:21:49 +01:00
size_t get_tx_depth ( const struct chain_topology * topo ,
2018-04-09 17:49:03 +02:00
const struct bitcoin_txid * txid )
2016-04-24 12:07:13 +02:00
{
2018-04-09 17:49:03 +02:00
u32 blockheight = wallet_transaction_height ( topo - > wallet , txid ) ;
2016-04-24 12:07:13 +02:00
2018-04-09 17:49:03 +02:00
if ( blockheight = = 0 )
2016-05-04 08:36:19 +02:00
return 0 ;
2018-04-09 17:49:03 +02:00
return topo - > tip - > height - blockheight + 1 ;
2016-04-24 12:07:13 +02:00
}
2016-11-09 07:44:10 +01:00
struct txs_to_broadcast {
/* We just sent txs[cursor] */
size_t cursor ;
/* These are hex encoded already, for bitcoind_sendrawtx */
const char * * txs ;
2016-11-09 07:44:21 +01:00
2018-01-29 05:46:54 +01:00
/* Command to complete when we're done, if and only if dev-broadcast triggered */
2016-11-09 07:44:21 +01:00
struct command * cmd ;
2016-11-09 07:44:10 +01:00
} ;
2016-11-07 13:34:02 +01:00
/* We just sent the last entry in txs[]. Shrink and send the next last. */
2017-03-02 13:21:49 +01:00
static void broadcast_remainder ( struct bitcoind * bitcoind ,
2016-11-09 07:44:10 +01:00
int exitstatus , const char * msg ,
struct txs_to_broadcast * txs )
2016-05-04 08:33:10 +02:00
{
/* These are expected. */
if ( strstr ( msg , " txn-mempool-conflict " )
| | strstr ( msg , " transaction already in block chain " ) )
2017-03-02 13:21:49 +01:00
log_debug ( bitcoind - > log ,
2016-05-04 08:33:10 +02:00
" Expected error broadcasting tx %s: %s " ,
2016-11-09 07:44:10 +01:00
txs - > txs [ txs - > cursor ] , msg ) ;
2016-11-07 13:30:02 +01:00
else if ( exitstatus )
2017-03-02 13:21:49 +01:00
log_unusual ( bitcoind - > log , " Broadcasting tx %s: %i %s " ,
2016-11-09 07:44:10 +01:00
txs - > txs [ txs - > cursor ] , exitstatus , msg ) ;
2016-05-04 08:33:10 +02:00
2016-11-09 07:44:10 +01:00
txs - > cursor + + ;
if ( txs - > cursor = = tal_count ( txs - > txs ) ) {
2016-11-09 07:44:21 +01:00
if ( txs - > cmd )
command_success ( txs - > cmd , null_response ( txs - > cmd ) ) ;
2016-05-04 08:33:10 +02:00
tal_free ( txs ) ;
return ;
}
2016-11-07 13:34:02 +01:00
/* Broadcast next one. */
2017-06-20 15:34:43 +02:00
bitcoind_sendrawtx ( bitcoind , txs - > txs [ txs - > cursor ] ,
2016-11-09 07:44:10 +01:00
broadcast_remainder , txs ) ;
2016-05-04 08:33:10 +02:00
}
/* FIXME: This is dumb. We can group txs and avoid bothering bitcoind
* if any one tx is in the main chain . */
2017-03-02 13:21:49 +01:00
static void rebroadcast_txs ( struct chain_topology * topo , struct command * cmd )
2016-05-04 08:33:10 +02:00
{
/* Copy txs now (peers may go away, and they own txs). */
size_t num_txs = 0 ;
2016-11-09 07:44:21 +01:00
struct txs_to_broadcast * txs ;
2017-03-02 13:21:49 +01:00
struct outgoing_tx * otx ;
2016-05-04 08:33:10 +02:00
2017-03-02 13:21:49 +01:00
txs = tal ( topo , struct txs_to_broadcast ) ;
2016-11-09 07:44:21 +01:00
txs - > cmd = cmd ;
2016-11-09 07:44:10 +01:00
/* Put any txs we want to broadcast in ->txs. */
txs - > txs = tal_arr ( txs , const char * , 0 ) ;
2017-03-02 13:21:49 +01:00
list_for_each ( & topo - > outgoing_txs , otx , list ) {
2018-04-09 18:48:39 +02:00
if ( wallet_transaction_height ( topo - > wallet , & otx - > txid ) )
2017-03-02 13:21:49 +01:00
continue ;
2016-05-04 08:33:10 +02:00
2017-03-02 13:21:49 +01:00
tal_resize ( & txs - > txs , num_txs + 1 ) ;
txs - > txs [ num_txs ] = tal_strdup ( txs , otx - > hextx ) ;
num_txs + + ;
2016-05-04 08:33:10 +02:00
}
2016-11-09 07:44:10 +01:00
/* Let this do the dirty work. */
txs - > cursor = ( size_t ) - 1 ;
2017-03-02 13:21:49 +01:00
broadcast_remainder ( topo - > bitcoind , 0 , " " , txs ) ;
2016-05-04 08:33:10 +02:00
}
static void destroy_outgoing_tx ( struct outgoing_tx * otx )
{
2017-03-02 13:21:49 +01:00
list_del ( & otx - > list ) ;
2016-11-07 13:29:02 +01:00
}
2018-02-12 11:13:04 +01:00
static void clear_otx_channel ( struct channel * channel , struct outgoing_tx * otx )
2017-03-07 01:55:48 +01:00
{
2018-02-12 11:13:04 +01:00
if ( otx - > channel ! = channel )
fatal ( " channel %p, otx %p has channel %p " , channel , otx , otx - > channel ) ;
otx - > channel = NULL ;
2017-03-07 01:55:48 +01:00
}
2017-03-02 13:21:49 +01:00
static void broadcast_done ( struct bitcoind * bitcoind ,
2016-11-07 13:30:02 +01:00
int exitstatus , const char * msg ,
struct outgoing_tx * otx )
2016-11-07 13:29:02 +01:00
{
2018-02-12 11:13:04 +01:00
/* Channel gone? Stop. */
if ( ! otx - > channel ) {
2017-03-07 01:55:48 +01:00
tal_free ( otx ) ;
return ;
}
2018-02-12 11:13:04 +01:00
/* No longer needs to be disconnected if channel dies. */
tal_del_destructor2 ( otx - > channel , clear_otx_channel , otx ) ;
2018-01-09 11:16:21 +01:00
2016-11-07 13:31:02 +01:00
if ( otx - > failed & & exitstatus ! = 0 ) {
2018-02-12 11:13:04 +01:00
otx - > failed ( otx - > channel , exitstatus , msg ) ;
2016-11-07 13:31:02 +01:00
tal_free ( otx ) ;
} else {
2018-02-12 11:13:04 +01:00
/* For continual rebroadcasting, until channel freed. */
tal_steal ( otx - > channel , otx ) ;
2018-01-10 03:43:23 +01:00
list_add_tail ( & bitcoind - > ld - > topology - > outgoing_txs , & otx - > list ) ;
2016-11-07 13:31:02 +01:00
tal_add_destructor ( otx , destroy_outgoing_tx ) ;
}
2016-05-04 08:33:10 +02:00
}
2017-03-02 13:21:49 +01:00
void broadcast_tx ( struct chain_topology * topo ,
2018-02-12 11:13:04 +01:00
struct channel * channel , const struct bitcoin_tx * tx ,
void ( * failed ) ( struct channel * channel ,
2016-11-07 13:31:02 +01:00
int exitstatus , const char * err ) )
2016-05-04 08:33:10 +02:00
{
2018-02-12 11:13:04 +01:00
/* Channel might vanish: topo owns it to start with. */
2017-03-07 01:55:48 +01:00
struct outgoing_tx * otx = tal ( topo , struct outgoing_tx ) ;
2016-11-07 13:29:02 +01:00
const u8 * rawtx = linearize_tx ( otx , tx ) ;
2016-05-04 08:33:10 +02:00
2018-02-12 11:13:04 +01:00
otx - > channel = channel ;
2016-11-07 13:29:02 +01:00
bitcoin_txid ( tx , & otx - > txid ) ;
2017-01-10 05:49:25 +01:00
otx - > hextx = tal_hex ( otx , rawtx ) ;
2016-11-07 13:31:02 +01:00
otx - > failed = failed ;
2016-11-07 13:29:02 +01:00
tal_free ( rawtx ) ;
2018-02-12 11:13:04 +01:00
tal_add_destructor2 ( channel , clear_otx_channel , otx ) ;
2016-05-04 08:33:10 +02:00
2017-09-12 13:50:10 +02:00
log_add ( topo - > log , " (tx %s) " ,
2018-03-15 05:30:39 +01:00
type_to_string ( tmpctx , struct bitcoin_txid , & otx - > txid ) ) ;
2016-05-04 08:33:10 +02:00
2018-04-09 14:37:21 +02:00
wallet_transaction_add ( topo - > wallet , tx , 0 , 0 ) ;
2017-10-24 04:06:14 +02:00
bitcoind_sendrawtx ( topo - > bitcoind , otx - > hextx , broadcast_done , otx ) ;
2016-05-04 08:33:10 +02:00
}
2017-11-21 04:30:29 +01:00
static const char * feerate_name ( enum feerate feerate )
2016-08-18 06:53:46 +02:00
{
2017-11-21 04:30:29 +01:00
return feerate = = FEERATE_IMMEDIATE ? " Immediate "
: feerate = = FEERATE_NORMAL ? " Normal " : " Slow " ;
}
2017-12-21 12:07:55 +01:00
/* Mutual recursion via timer. */
static void next_updatefee_timer ( struct chain_topology * topo ) ;
2017-11-21 04:30:29 +01:00
/* We sanitize feerates if necessary to put them in descending order. */
static void update_feerates ( struct bitcoind * bitcoind ,
2017-11-21 04:33:22 +01:00
const u32 * satoshi_per_kw ,
2017-11-21 04:30:29 +01:00
struct chain_topology * topo )
{
2017-11-21 04:34:36 +01:00
u32 old_feerates [ NUM_FEERATES ] ;
bool changed = false ;
2018-07-15 18:30:43 +02:00
/* Smoothing factor alpha for simple exponential smoothing. The goal is to
* have the feerate account for 90 percent of the values polled in the last
* 2 minutes . The following will do that in a polling interval
* independent manner . */
double alpha = 1 - pow ( 0.1 , ( double ) topo - > poll_seconds / 120 ) ;
2017-11-21 04:34:36 +01:00
2017-11-21 04:30:29 +01:00
for ( size_t i = 0 ; i < NUM_FEERATES ; i + + ) {
2018-03-20 04:30:21 +01:00
u32 feerate = satoshi_per_kw [ i ] ;
2018-05-17 06:46:19 +02:00
/* Takes into account override_fee_rate */
old_feerates [ i ] = get_feerate ( topo , i ) ;
/* If estimatefee failed, don't do anything. */
if ( ! feerate )
continue ;
2018-07-15 18:30:43 +02:00
/* Smooth the feerate to avoid spikes. */
u32 feerate_smooth = feerate * alpha + old_feerates [ i ] * ( 1 - alpha ) ;
/* But to avoid updating forever, only apply smoothing when its
* effect is more then 10 percent */
2018-07-17 05:39:30 +02:00
if ( abs ( ( int ) feerate - ( int ) feerate_smooth ) > ( 0.1 * feerate ) ) {
2018-07-15 18:30:43 +02:00
feerate = feerate_smooth ;
log_debug ( topo - > log ,
" ...feerate %u smoothed to %u (alpha=%.2f) " ,
satoshi_per_kw [ i ] , feerate , alpha ) ;
}
if ( feerate < feerate_floor ( ) ) {
feerate = feerate_floor ( ) ;
log_debug ( topo - > log ,
" ...feerate %u hit floor %u " ,
satoshi_per_kw [ i ] , feerate ) ;
}
2018-03-20 04:30:21 +01:00
2018-03-20 05:07:30 +01:00
if ( feerate ! = topo - > feerate [ i ] ) {
2018-01-10 05:09:53 +01:00
log_debug ( topo - > log , " %s feerate %u (was %u) " ,
feerate_name ( i ) ,
2018-03-20 04:30:21 +01:00
feerate , topo - > feerate [ i ] ) ;
2018-03-20 05:07:30 +01:00
}
2018-03-20 04:30:21 +01:00
topo - > feerate [ i ] = feerate ;
2017-11-21 04:30:29 +01:00
}
2018-05-17 06:46:19 +02:00
/* Make sure fee rates are in order. */
2017-11-21 04:30:29 +01:00
for ( size_t i = 0 ; i < NUM_FEERATES ; i + + ) {
for ( size_t j = 0 ; j < i ; j + + ) {
if ( topo - > feerate [ j ] < topo - > feerate [ i ] ) {
2018-01-23 17:21:47 +01:00
log_debug ( topo - > log ,
" Feerate %s (%u) above %s (%u) " ,
feerate_name ( i ) , topo - > feerate [ i ] ,
feerate_name ( j ) , topo - > feerate [ j ] ) ;
2017-11-21 04:30:29 +01:00
topo - > feerate [ j ] = topo - > feerate [ i ] ;
}
}
2018-05-17 06:46:19 +02:00
if ( get_feerate ( topo , i ) ! = old_feerates [ i ] )
2017-11-21 04:34:36 +01:00
changed = true ;
2017-11-21 04:30:29 +01:00
}
2017-11-21 04:34:36 +01:00
if ( changed )
notify_feerate_change ( bitcoind - > ld ) ;
2017-12-21 12:07:55 +01:00
next_updatefee_timer ( topo ) ;
2016-08-18 06:53:46 +02:00
}
2017-12-21 12:07:55 +01:00
static void start_fee_estimate ( struct chain_topology * topo )
2016-05-04 08:36:19 +02:00
{
2017-11-21 04:30:29 +01:00
/* FEERATE_IMMEDIATE, FEERATE_NORMAL, FEERATE_SLOW */
const char * estmodes [ ] = { " CONSERVATIVE " , " ECONOMICAL " , " ECONOMICAL " } ;
const u32 blocks [ ] = { 2 , 4 , 100 } ;
BUILD_ASSERT ( ARRAY_SIZE ( blocks ) = = NUM_FEERATES ) ;
2017-12-21 12:07:55 +01:00
/* Once per new block head, update fee estimates. */
bitcoind_estimate_fees ( topo - > bitcoind , blocks , estmodes , NUM_FEERATES ,
update_feerates , topo ) ;
}
static void next_updatefee_timer ( struct chain_topology * topo )
{
/* This takes care of its own lifetime. */
2018-05-17 06:08:24 +02:00
notleak ( new_reltimer ( topo - > timers , topo ,
time_from_sec ( topo - > poll_seconds ) ,
2017-12-21 12:07:55 +01:00
start_fee_estimate , topo ) ) ;
}
2017-12-21 12:10:26 +01:00
/* Once we're run out of new blocks to add, call this. */
static void updates_complete ( struct chain_topology * topo )
{
if ( topo - > tip ! = topo - > prev_tip ) {
/* Tell lightningd about new block. */
notify_new_block ( topo - > bitcoind - > ld , topo - > tip - > height ) ;
/* Tell watch code to re-evaluate all txs. */
watch_topology_changed ( topo ) ;
/* Maybe need to rebroadcast. */
rebroadcast_txs ( topo , NULL ) ;
2018-02-15 23:09:53 +01:00
/* We've processed these UTXOs */
db_set_intvar ( topo - > bitcoind - > ld - > wallet - > db ,
" last_processed_block " , topo - > tip - > height ) ;
2017-12-21 12:10:26 +01:00
topo - > prev_tip = topo - > tip ;
}
/* Try again soon. */
next_topology_timer ( topo ) ;
}
2018-03-02 15:07:13 +01:00
/**
* topo_update_spends - - Tell the wallet about all spent outpoints
*/
static void topo_update_spends ( struct chain_topology * topo , struct block * b )
{
2018-03-28 11:13:43 +02:00
const struct short_channel_id * scid ;
2018-03-02 15:07:13 +01:00
for ( size_t i = 0 ; i < tal_count ( b - > full_txs ) ; i + + ) {
const struct bitcoin_tx * tx = b - > full_txs [ i ] ;
for ( size_t j = 0 ; j < tal_count ( tx - > input ) ; j + + ) {
const struct bitcoin_tx_input * input = & tx - > input [ j ] ;
2018-03-28 11:13:43 +02:00
scid = wallet_outpoint_spend ( topo - > wallet , tmpctx ,
b - > height , & input - > txid ,
input - > index ) ;
if ( scid ) {
2018-03-28 12:03:40 +02:00
gossipd_notify_spend ( topo - > bitcoind - > ld , scid ) ;
2018-03-28 11:13:43 +02:00
tal_free ( scid ) ;
}
2018-03-02 15:07:13 +01:00
}
}
}
2018-03-04 01:38:49 +01:00
static void topo_add_utxos ( struct chain_topology * topo , struct block * b )
{
for ( size_t i = 0 ; i < tal_count ( b - > full_txs ) ; i + + ) {
const struct bitcoin_tx * tx = b - > full_txs [ i ] ;
for ( size_t j = 0 ; j < tal_count ( tx - > output ) ; j + + ) {
const struct bitcoin_tx_output * output = & tx - > output [ j ] ;
if ( is_p2wsh ( output - > script , NULL ) ) {
wallet_utxoset_add ( topo - > wallet , tx , j ,
b - > height , i , output - > script ,
output - > amount ) ;
}
}
}
}
2017-12-21 12:09:25 +01:00
static void add_tip ( struct chain_topology * topo , struct block * b )
2017-12-21 12:07:55 +01:00
{
2017-12-21 12:09:25 +01:00
/* Attach to tip; b is now the tip. */
assert ( b - > height = = topo - > tip - > height + 1 ) ;
b - > prev = topo - > tip ;
topo - > tip - > next = b ;
topo - > tip = b ;
2018-02-19 15:20:51 +01:00
wallet_block_add ( topo - > wallet , b ) ;
2018-02-26 11:36:48 +01:00
2018-03-04 01:38:49 +01:00
topo_add_utxos ( topo , b ) ;
2018-03-02 15:07:13 +01:00
topo_update_spends ( topo , b ) ;
2018-02-26 11:36:48 +01:00
/* Only keep the transactions we care about. */
filter_block_txs ( topo , b ) ;
block_map_add ( & topo - > block_map , b ) ;
2018-06-04 14:46:05 +02:00
topo - > max_blockheight = b - > height ;
2016-04-24 12:07:13 +02:00
}
2017-03-02 13:21:49 +01:00
static struct block * new_block ( struct chain_topology * topo ,
2016-05-04 08:36:19 +02:00
struct bitcoin_block * blk ,
2017-12-21 12:09:25 +01:00
unsigned int height )
2016-04-24 12:07:13 +02:00
{
struct block * b = tal ( topo , struct block ) ;
2017-12-18 07:44:10 +01:00
sha256_double ( & b - > blkid . shad , & blk - > hdr , sizeof ( blk - > hdr ) ) ;
2018-01-25 12:03:50 +01:00
log_debug ( topo - > log , " Adding block %u: %s " ,
height ,
2018-03-15 05:30:39 +01:00
type_to_string ( tmpctx , struct bitcoin_blkid , & b - > blkid ) ) ;
2016-04-24 12:12:18 +02:00
assert ( ! block_map_get ( & topo - > block_map , & b - > blkid ) ) ;
2017-12-21 12:09:25 +01:00
b - > next = NULL ;
b - > prev = NULL ;
2016-04-24 12:07:13 +02:00
2017-12-21 12:09:25 +01:00
b - > height = height ;
2016-04-24 12:07:13 +02:00
2016-04-24 12:12:18 +02:00
b - > hdr = blk - > hdr ;
2016-04-24 12:07:13 +02:00
2016-12-11 23:22:08 +01:00
b - > txnums = tal_arr ( b , u32 , 0 ) ;
2016-04-24 12:18:35 +02:00
b - > full_txs = tal_steal ( b , blk - > tx ) ;
2016-04-24 12:07:13 +02:00
return b ;
}
2017-12-21 12:09:25 +01:00
static void remove_tip ( struct chain_topology * topo )
2017-03-02 13:21:49 +01:00
{
2017-12-21 12:09:25 +01:00
struct block * b = topo - > tip ;
2018-04-10 11:14:50 +02:00
struct bitcoin_txid * txs ;
size_t i , n ;
2017-03-02 13:21:49 +01:00
2017-12-21 12:09:25 +01:00
/* Move tip back one. */
topo - > tip = b - > prev ;
if ( ! topo - > tip )
fatal ( " Initial block %u (%s) reorganized out! " ,
b - > height ,
2018-03-15 05:30:39 +01:00
type_to_string ( tmpctx , struct bitcoin_blkid , & b - > blkid ) ) ;
2016-04-24 12:07:13 +02:00
2018-04-10 11:14:50 +02:00
txs = wallet_transactions_by_height ( b , topo - > wallet , b - > height ) ;
n = tal_count ( txs ) ;
2017-12-21 12:09:25 +01:00
/* Notify that txs are kicked out. */
for ( i = 0 ; i < n ; i + + )
2018-04-10 11:14:50 +02:00
txwatch_fire ( topo , & txs [ i ] , 0 ) ;
2016-04-24 12:07:13 +02:00
2018-02-19 15:20:51 +01:00
wallet_block_remove ( topo - > wallet , b ) ;
2018-03-16 00:23:14 +01:00
block_map_del ( & topo - > block_map , b ) ;
2017-12-21 12:09:25 +01:00
tal_free ( b ) ;
2016-04-24 12:07:13 +02:00
}
2018-02-21 16:06:07 +01:00
static void have_new_block ( struct bitcoind * bitcoind UNUSED ,
2017-12-21 12:09:25 +01:00
struct bitcoin_block * blk ,
struct chain_topology * topo )
2017-03-02 13:21:49 +01:00
{
2018-01-02 10:38:49 +01:00
/* Unexpected predecessor? Free predecessor, refetch it. */
2018-07-04 07:30:02 +02:00
if ( ! bitcoin_blkid_eq ( & topo - > tip - > blkid , & blk - > hdr . prev_hash ) )
2017-12-21 12:09:25 +01:00
remove_tip ( topo ) ;
else
add_tip ( topo , new_block ( topo , blk , topo - > tip - > height + 1 ) ) ;
/* Try for next one. */
try_extend_tip ( topo ) ;
2017-03-02 13:21:49 +01:00
}
2017-12-21 12:09:25 +01:00
static void get_new_block ( struct bitcoind * bitcoind ,
const struct bitcoin_blkid * blkid ,
struct chain_topology * topo )
2016-04-24 12:07:13 +02:00
{
2017-12-21 12:09:25 +01:00
if ( ! blkid ) {
2017-12-21 12:10:26 +01:00
/* No such block, we're done. */
updates_complete ( topo ) ;
2017-12-21 12:09:25 +01:00
return ;
}
bitcoind_getrawblock ( bitcoind , blkid , have_new_block , topo ) ;
2016-04-24 12:07:13 +02:00
}
2017-12-21 12:09:25 +01:00
static void try_extend_tip ( struct chain_topology * topo )
2016-04-24 12:07:13 +02:00
{
2017-12-21 12:09:25 +01:00
bitcoind_getblockhash ( topo - > bitcoind , topo - > tip - > height + 1 ,
get_new_block , topo ) ;
2016-08-09 05:41:22 +02:00
}
2016-04-24 12:07:13 +02:00
2018-02-21 16:06:07 +01:00
static void init_topo ( struct bitcoind * bitcoind UNUSED ,
2016-04-24 12:12:18 +02:00
struct bitcoin_block * blk ,
2017-03-02 13:21:49 +01:00
struct chain_topology * topo )
2016-04-24 12:07:13 +02:00
{
2018-06-04 14:46:05 +02:00
topo - > root = new_block ( topo , blk , topo - > max_blockheight ) ;
2016-05-04 08:36:19 +02:00
block_map_add ( & topo - > block_map , topo - > root ) ;
2017-12-21 12:10:26 +01:00
topo - > tip = topo - > prev_tip = topo - > root ;
2016-04-24 12:07:13 +02:00
2018-03-06 19:30:21 +01:00
/* In case we don't get all the way to updates_complete */
db_set_intvar ( topo - > bitcoind - > ld - > wallet - > db ,
" last_processed_block " , topo - > tip - > height ) ;
2018-01-03 06:26:44 +01:00
io_break ( topo ) ;
2016-04-24 12:07:13 +02:00
}
2017-03-02 13:21:49 +01:00
static void get_init_block ( struct bitcoind * bitcoind ,
2017-12-18 07:44:10 +01:00
const struct bitcoin_blkid * blkid ,
2017-03-02 13:21:49 +01:00
struct chain_topology * topo )
2016-04-24 12:07:13 +02:00
{
2017-03-02 13:21:49 +01:00
bitcoind_getrawblock ( bitcoind , blkid , init_topo , topo ) ;
2016-04-24 12:07:13 +02:00
}
2017-03-02 13:21:49 +01:00
static void get_init_blockhash ( struct bitcoind * bitcoind , u32 blockcount ,
2017-03-02 13:21:49 +01:00
struct chain_topology * topo )
2016-04-24 12:07:13 +02:00
{
2018-04-20 14:44:34 +02:00
/* If bitcoind's current blockheight is below the requested height, just
* go back to that height . This might be a new node catching up , or
* bitcoind is processing a reorg . */
2018-06-04 14:46:05 +02:00
if ( blockcount < topo - > max_blockheight ) {
2018-04-20 14:44:34 +02:00
if ( bitcoind - > ld - > config . rescan < 0 ) {
/* Absolute blockheight, but bitcoind's blockheight isn't there yet */
2018-04-26 07:21:45 +02:00
/* Protect against underflow in subtraction.
* Possible in regtest mode . */
if ( blockcount < 1 )
2018-06-04 14:46:05 +02:00
topo - > max_blockheight = 0 ;
2018-04-26 07:21:45 +02:00
else
2018-06-04 14:46:05 +02:00
topo - > max_blockheight = blockcount - 1 ;
} else if ( topo - > max_blockheight = = UINT32_MAX ) {
2018-04-20 14:44:34 +02:00
/* Relative rescan, but we didn't know the blockheight */
2018-04-26 07:21:45 +02:00
/* Protect against underflow in subtraction.
* Possible in regtest mode . */
if ( blockcount < bitcoind - > ld - > config . rescan )
2018-06-04 14:46:05 +02:00
topo - > max_blockheight = 0 ;
2018-04-26 07:21:45 +02:00
else
2018-06-04 14:46:05 +02:00
topo - > max_blockheight = blockcount - bitcoind - > ld - > config . rescan ;
2018-04-20 14:44:34 +02:00
}
}
2018-02-16 00:07:04 +01:00
2018-02-19 15:20:51 +01:00
/* Rollback to the given blockheight, so we start track
* correctly again */
2018-06-04 14:46:05 +02:00
wallet_blocks_rollback ( topo - > wallet , topo - > max_blockheight ) ;
2018-02-19 15:20:51 +01:00
2018-01-03 06:26:43 +01:00
/* Get up to speed with topology. */
2018-06-04 14:46:05 +02:00
bitcoind_getblockhash ( bitcoind , topo - > max_blockheight ,
2017-03-02 13:21:49 +01:00
get_init_block , topo ) ;
2016-04-24 12:07:13 +02:00
}
2017-03-02 13:21:49 +01:00
u32 get_block_height ( const struct chain_topology * topo )
2016-06-28 23:19:21 +02:00
{
2017-03-02 13:21:49 +01:00
return topo - > tip - > height ;
2016-06-28 23:19:21 +02:00
}
2017-11-21 04:30:29 +01:00
/* We may only have estimate for 2 blocks, for example. Extrapolate. */
2017-11-21 04:33:22 +01:00
static u32 guess_feerate ( const struct chain_topology * topo , enum feerate feerate )
2017-11-21 04:30:29 +01:00
{
size_t i = 0 ;
2017-11-21 04:33:22 +01:00
u32 rate = 0 ;
2017-11-21 04:30:29 +01:00
/* We assume each one is half the previous. */
for ( i = 0 ; i < feerate ; i + + ) {
if ( topo - > feerate [ i ] ) {
log_info ( topo - > log ,
" No fee estimate for %s: basing on %s rate " ,
feerate_name ( feerate ) ,
feerate_name ( i ) ) ;
rate = topo - > feerate [ i ] ;
}
rate / = 2 ;
}
if ( rate = = 0 ) {
rate = topo - > default_fee_rate > > feerate ;
log_info ( topo - > log ,
" No fee estimate for %s: basing on default fee rate " ,
feerate_name ( feerate ) ) ;
}
return rate ;
}
2017-11-21 04:33:22 +01:00
u32 get_feerate ( const struct chain_topology * topo , enum feerate feerate )
2016-08-18 06:53:46 +02:00
{
2018-05-17 06:46:22 +02:00
# if DEVELOPER
if ( topo - > dev_override_fee_rate ) {
2017-03-02 13:21:49 +01:00
log_debug ( topo - > log , " Forcing fee rate, ignoring estimate " ) ;
2018-05-17 06:46:22 +02:00
return topo - > dev_override_fee_rate [ feerate ] ;
}
# endif
if ( topo - > feerate [ feerate ] = = 0 ) {
2017-11-21 04:30:29 +01:00
return guess_feerate ( topo , feerate ) ;
2016-11-06 07:46:58 +01:00
}
2017-11-21 04:30:29 +01:00
return topo - > feerate [ feerate ] ;
2016-08-18 06:53:46 +02:00
}
2017-10-24 04:06:14 +02:00
# if DEVELOPER
2017-11-21 06:26:59 +01:00
static void json_dev_setfees ( struct command * cmd ,
const char * buffer , const jsmntok_t * params )
{
struct chain_topology * topo = cmd - > ld - > topology ;
struct json_result * response ;
2018-05-17 06:46:22 +02:00
if ( ! topo - > dev_override_fee_rate ) {
2017-11-21 06:26:59 +01:00
u32 fees [ NUM_FEERATES ] ;
for ( size_t i = 0 ; i < ARRAY_SIZE ( fees ) ; i + + )
fees [ i ] = get_feerate ( topo , i ) ;
2018-05-17 06:46:22 +02:00
topo - > dev_override_fee_rate = tal_dup_arr ( topo , u32 , fees ,
ARRAY_SIZE ( fees ) , 0 ) ;
2017-11-21 06:26:59 +01:00
}
2018-07-16 22:48:38 +02:00
if ( ! param ( cmd , buffer , params ,
p_opt_def ( " immediate " , json_tok_number ,
& topo - > dev_override_fee_rate [ FEERATE_IMMEDIATE ] ,
topo - > dev_override_fee_rate [ FEERATE_IMMEDIATE ] ) ,
p_opt_def ( " normal " , json_tok_number ,
& topo - > dev_override_fee_rate [ FEERATE_NORMAL ] ,
topo - > dev_override_fee_rate [ FEERATE_NORMAL ] ) ,
p_opt_def ( " slow " , json_tok_number ,
& topo - > dev_override_fee_rate [ FEERATE_SLOW ] ,
topo - > dev_override_fee_rate [ FEERATE_SLOW ] ) ,
NULL ) )
return ;
2017-11-21 06:26:59 +01:00
log_debug ( topo - > log ,
" dev-setfees: fees now %u/%u/%u " ,
2018-05-17 06:46:22 +02:00
topo - > dev_override_fee_rate [ FEERATE_IMMEDIATE ] ,
topo - > dev_override_fee_rate [ FEERATE_NORMAL ] ,
topo - > dev_override_fee_rate [ FEERATE_SLOW ] ) ;
2017-11-21 06:26:59 +01:00
notify_feerate_change ( cmd - > ld ) ;
response = new_json_result ( cmd ) ;
json_object_start ( response , NULL ) ;
json_add_num ( response , " immediate " ,
2018-05-17 06:46:22 +02:00
topo - > dev_override_fee_rate [ FEERATE_IMMEDIATE ] ) ;
2017-11-21 06:26:59 +01:00
json_add_num ( response , " normal " ,
2018-05-17 06:46:22 +02:00
topo - > dev_override_fee_rate [ FEERATE_NORMAL ] ) ;
2017-11-21 06:26:59 +01:00
json_add_num ( response , " slow " ,
2018-05-17 06:46:22 +02:00
topo - > dev_override_fee_rate [ FEERATE_SLOW ] ) ;
2017-11-21 06:26:59 +01:00
json_object_end ( response ) ;
command_success ( cmd , response ) ;
}
static const struct json_command dev_setfees_command = {
" dev-setfees " ,
json_dev_setfees ,
2018-01-25 23:59:00 +01:00
" Set feerate in satoshi-per-kw for {immediate}, {normal} and {slow} (each is optional, when set, separate by spaces) and show the value of those three feerates "
2017-11-21 06:26:59 +01:00
} ;
AUTODATA ( json_command , & dev_setfees_command ) ;
2017-12-15 11:17:54 +01:00
void chaintopology_mark_pointers_used ( struct htable * memtable ,
const struct chain_topology * topo )
{
struct txwatch_hash_iter wit ;
struct txwatch * w ;
struct txowatch_hash_iter owit ;
struct txowatch * ow ;
/* memleak can't see inside hash tables, so do them manually */
for ( w = txwatch_hash_first ( & topo - > txwatches , & wit ) ;
w ;
w = txwatch_hash_next ( & topo - > txwatches , & wit ) )
memleak_scan_region ( memtable , w ) ;
for ( ow = txowatch_hash_first ( & topo - > txowatches , & owit ) ;
ow ;
ow = txowatch_hash_next ( & topo - > txowatches , & owit ) )
memleak_scan_region ( memtable , ow ) ;
}
2017-10-24 04:06:14 +02:00
# endif /* DEVELOPER */
2017-04-27 08:07:50 +02:00
2018-02-12 11:13:04 +01:00
/* On shutdown, channels get deleted last. That frees from our list, so
2017-03-02 13:21:49 +01:00
* do it now instead . */
2018-02-14 02:53:13 +01:00
static void destroy_chain_topology ( struct chain_topology * topo )
2017-03-02 13:21:49 +01:00
{
struct outgoing_tx * otx ;
while ( ( otx = list_pop ( & topo - > outgoing_txs , struct outgoing_tx , list ) ) )
tal_free ( otx ) ;
}
2017-11-01 11:20:40 +01:00
struct chain_topology * new_topology ( struct lightningd * ld , struct log * log )
2016-04-24 12:07:13 +02:00
{
2017-11-01 11:20:40 +01:00
struct chain_topology * topo = tal ( ld , struct chain_topology ) ;
2017-03-02 13:21:49 +01:00
block_map_init ( & topo - > block_map ) ;
list_head_init ( & topo - > outgoing_txs ) ;
txwatch_hash_init ( & topo - > txwatches ) ;
txowatch_hash_init ( & topo - > txowatches ) ;
2017-03-02 13:21:49 +01:00
topo - > log = log ;
topo - > default_fee_rate = 40000 ;
2018-05-17 06:46:19 +02:00
memset ( topo - > feerate , 0 , sizeof ( topo - > feerate ) ) ;
2017-11-01 11:20:40 +01:00
topo - > bitcoind = new_bitcoind ( topo , ld , log ) ;
2018-02-19 15:20:51 +01:00
topo - > wallet = ld - > wallet ;
2018-05-17 06:08:24 +02:00
topo - > poll_seconds = 30 ;
2018-05-17 06:46:22 +02:00
# if DEVELOPER
topo - > dev_override_fee_rate = NULL ;
# endif
2017-03-02 13:21:49 +01:00
return topo ;
}
2017-08-28 18:09:01 +02:00
void setup_topology ( struct chain_topology * topo ,
2017-03-02 13:21:49 +01:00
struct timers * timers ,
2018-06-04 15:00:05 +02:00
u32 min_blockheight , u32 max_blockheight )
2017-03-02 13:21:49 +01:00
{
2017-11-21 04:30:29 +01:00
memset ( & topo - > feerate , 0 , sizeof ( topo - > feerate ) ) ;
2017-03-02 13:21:49 +01:00
topo - > timers = timers ;
2018-02-16 00:07:04 +01:00
2018-06-04 15:00:05 +02:00
topo - > min_blockheight = min_blockheight ;
topo - > max_blockheight = max_blockheight ;
2017-03-02 13:21:49 +01:00
2017-09-28 05:38:05 +02:00
/* Make sure bitcoind is started, and ready */
wait_for_bitcoind ( topo - > bitcoind ) ;
2017-08-28 18:09:01 +02:00
bitcoind_getblockcount ( topo - > bitcoind , get_init_blockhash , topo ) ;
2016-08-09 05:41:22 +02:00
2018-02-14 02:53:13 +01:00
tal_add_destructor ( topo , destroy_chain_topology ) ;
2017-03-02 13:21:49 +01:00
2018-01-03 06:26:44 +01:00
/* Once it gets initial block, it calls io_break() and we return. */
2016-08-09 05:41:22 +02:00
io_loop ( NULL , NULL ) ;
2018-01-03 06:26:44 +01:00
}
void begin_topology ( struct chain_topology * topo )
{
2018-02-16 02:59:45 +01:00
/* Begin fee estimation. */
start_fee_estimate ( topo ) ;
2018-01-03 06:26:44 +01:00
try_extend_tip ( topo ) ;
2016-04-24 12:07:13 +02:00
}