2016-04-24 12:12:18 +02:00
# include "bitcoin/block.h"
# 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-04-24 12:07:13 +02:00
# include <ccan/structeq/structeq.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>
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. */
notleak ( new_reltimer ( topo - > timers , topo , topo - > poll_time ,
2017-12-21 12:09:25 +01:00
try_extend_tip , topo ) ) ;
2016-05-09 22:56:09 +02:00
}
2016-05-04 08:40:37 +02:00
/* FIXME: Remove tx from block when peer done. */
2017-08-18 06:43:53 +02:00
static void add_tx_to_block ( struct block * b ,
const struct bitcoin_tx * tx , const u32 txnum )
2016-04-24 12:18:35 +02:00
{
2017-08-18 06:43:53 +02:00
size_t n = tal_count ( b - > txs ) ;
2016-04-24 12:18:35 +02:00
2017-08-18 06:43:53 +02:00
tal_resize ( & b - > txs , n + 1 ) ;
2016-12-11 23:22:08 +01:00
tal_resize ( & b - > txnums , n + 1 ) ;
2017-08-18 06:43:53 +02:00
b - > txs [ n ] = tal_steal ( b - > txs , tx ) ;
2016-12-11 23:22:08 +01:00
b - > txnums [ n ] = txnum ;
2016-04-24 12:18:35 +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 ) {
if ( structeq ( & otx - > txid , txid ) )
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 ) ;
2016-04-24 12:18:35 +02:00
if ( txo )
2017-08-23 02:58:44 +02:00
txowatch_fire ( topo , txo , tx , j , b ) ;
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 ,
tx , & satoshi_owned ) ;
}
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 ) | |
satoshi_owned ! = 0 )
2017-08-18 06:43:53 +02:00
add_tx_to_block ( b , tx , 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-08-18 06:43:53 +02:00
static const struct bitcoin_tx * tx_in_block ( const struct block * b ,
2017-12-18 07:41:52 +01:00
const struct bitcoin_txid * txid )
2016-04-24 12:07:13 +02:00
{
2017-08-18 06:43:53 +02:00
size_t i , n = tal_count ( b - > txs ) ;
2016-04-24 12:07:13 +02:00
2016-05-04 08:40:37 +02:00
for ( i = 0 ; i < n ; i + + ) {
2017-12-18 07:41:52 +01:00
struct bitcoin_txid this_txid ;
2017-08-18 06:43:53 +02:00
bitcoin_txid ( b - > txs [ i ] , & this_txid ) ;
if ( structeq ( & this_txid , txid ) )
return b - > txs [ i ] ;
2016-04-24 12:07:13 +02:00
}
2017-08-18 06:43:53 +02:00
return NULL ;
2016-04-24 12:07:13 +02:00
}
2016-05-04 08:40:37 +02:00
/* FIXME: Use hash table. */
2017-03-02 13:21:49 +01:00
static struct block * block_for_tx ( const struct chain_topology * topo ,
2017-12-18 07:41:52 +01:00
const struct bitcoin_txid * txid ,
2017-08-18 06:43:53 +02:00
const struct bitcoin_tx * * tx )
2016-04-24 12:07:13 +02:00
{
2016-05-04 08:36:19 +02:00
struct block * b ;
2017-08-18 06:43:53 +02:00
const struct bitcoin_tx * dummy_tx ;
if ( ! tx )
tx = & dummy_tx ;
2016-04-24 12:07:13 +02:00
2016-05-04 08:36:19 +02:00
for ( b = topo - > tip ; b ; b = b - > prev ) {
2017-08-18 06:43:53 +02:00
* tx = tx_in_block ( b , txid ) ;
if ( * tx )
2016-05-04 08:36:19 +02:00
return b ;
2016-04-24 12:07:13 +02:00
}
2016-05-04 08:36:19 +02:00
return NULL ;
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 ,
2017-12-18 07:41:52 +01:00
const struct bitcoin_txid * txid ,
2017-08-18 06:43:53 +02:00
const struct bitcoin_tx * * tx )
2016-04-24 12:07:13 +02:00
{
2016-05-04 08:36:19 +02:00
const struct block * b ;
2016-04-24 12:07:13 +02:00
2017-08-18 06:43:53 +02:00
b = block_for_tx ( topo , txid , tx ) ;
2016-05-04 08:36:19 +02:00
if ( ! b )
return 0 ;
return topo - > tip - > height - b - > height + 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-10-24 04:06:14 +02:00
# if DEVELOPER
2017-03-02 13:21:49 +01:00
if ( topo - > dev_no_broadcast )
2016-11-09 07:44:21 +01:00
return ;
2017-10-24 04:06:14 +02:00
# endif /* DEVELOPER */
2016-11-09 07:44:21 +01: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 ) {
2017-08-18 06:43:53 +02:00
if ( block_for_tx ( topo , & otx - > txid , NULL ) )
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) " ,
2017-12-18 07:41:52 +01:00
type_to_string ( ltmp , struct bitcoin_txid , & otx - > txid ) ) ;
2016-05-04 08:33:10 +02:00
2017-10-24 04:06:14 +02:00
# if DEVELOPER
if ( topo - > dev_no_broadcast ) {
2017-03-02 13:21:49 +01:00
broadcast_done ( topo - > bitcoind , 0 , " dev_no_broadcast " , otx ) ;
2017-10-24 04:06:14 +02:00
return ;
}
# endif
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 ;
2017-11-21 04:30:29 +01:00
for ( size_t i = 0 ; i < NUM_FEERATES ; i + + ) {
2018-01-10 05:09:53 +01:00
if ( satoshi_per_kw [ i ] ! = topo - > feerate [ i ] )
log_debug ( topo - > log , " %s feerate %u (was %u) " ,
feerate_name ( i ) ,
satoshi_per_kw [ i ] , topo - > feerate [ i ] ) ;
2017-11-21 04:34:36 +01:00
old_feerates [ i ] = topo - > feerate [ i ] ;
2017-11-21 04:30:29 +01:00
topo - > feerate [ i ] = satoshi_per_kw [ i ] ;
}
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 ] ;
}
}
2017-11-21 04:34:36 +01:00
if ( topo - > feerate [ i ] ! = old_feerates [ i ] )
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. */
notleak ( new_reltimer ( topo - > timers , topo , topo - > poll_time ,
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 ) ;
}
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
/* Only keep the transactions we care about. */
filter_block_txs ( topo , b ) ;
2016-05-04 08:36:19 +02:00
2017-12-21 12:09:25 +01:00
block_map_add ( & topo - > block_map , b ) ;
/* 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 ;
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 ,
2017-12-18 07:44:10 +01:00
type_to_string ( ltmp , 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
2017-08-18 06:43:53 +02:00
b - > txs = tal_arr ( b , const struct bitcoin_tx * , 0 ) ;
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 ;
size_t i , n = tal_count ( b - > txs ) ;
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 ,
type_to_string ( ltmp , struct bitcoin_blkid , & b - > blkid ) ) ;
2016-04-24 12:07:13 +02:00
2017-12-21 12:09:25 +01:00
/* Notify that txs are kicked out. */
for ( i = 0 ; i < n ; i + + )
txwatch_fire ( topo , b - > txs [ i ] , 0 ) ;
2016-04-24 12:07:13 +02:00
2017-12-21 12:09:25 +01:00
tal_free ( b ) ;
2016-04-24 12:07:13 +02:00
}
2017-12-21 12:09:25 +01:00
static void have_new_block ( struct bitcoind * bitcoind ,
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. */
2017-12-21 12:09:25 +01:00
if ( ! structeq ( & topo - > tip - > blkid , & blk - > hdr . prev_hash ) )
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
2017-03-02 13:21:49 +01:00
static void init_topo ( struct bitcoind * bitcoind ,
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
{
2017-12-21 12:09:25 +01:00
topo - > root = new_block ( topo , blk , topo - > first_blocknum ) ;
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-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-01-03 06:26:43 +01:00
/* This happens if first_blocknum is UINTMAX-1 */
if ( blockcount < topo - > first_blocknum )
topo - > first_blocknum = blockcount ;
2016-08-18 06:55:14 +02:00
2018-01-05 03:38:15 +01:00
/* FIXME: Because we don't handle our root disappearing, we go
* 100 blocks back */
if ( topo - > first_blocknum < 100 )
topo - > first_blocknum = 0 ;
else
topo - > first_blocknum - = 100 ;
2018-01-03 06:26:43 +01:00
/* Get up to speed with topology. */
2017-03-02 13:21:49 +01:00
bitcoind_getblockhash ( bitcoind , topo - > first_blocknum ,
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
{
2017-03-02 13:21:49 +01:00
if ( topo - > override_fee_rate ) {
log_debug ( topo - > log , " Forcing fee rate, ignoring estimate " ) ;
2017-11-21 04:31:31 +01:00
return topo - > override_fee_rate [ feerate ] ;
2017-11-21 04:30:29 +01:00
} else if ( topo - > feerate [ feerate ] = = 0 ) {
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-03-02 13:21:49 +01:00
struct txlocator * locate_tx ( const void * ctx , const struct chain_topology * topo ,
2017-12-18 07:41:52 +01:00
const struct bitcoin_txid * txid )
2016-09-07 23:34:36 +02:00
{
2017-08-18 06:43:53 +02:00
struct block * block = block_for_tx ( topo , txid , NULL ) ;
2016-09-07 23:34:36 +02:00
if ( block = = NULL ) {
return NULL ;
}
struct txlocator * loc = talz ( ctx , struct txlocator ) ;
loc - > blkheight = block - > height ;
2017-08-18 06:43:53 +02:00
size_t i , n = tal_count ( block - > txs ) ;
2016-09-07 23:34:36 +02:00
for ( i = 0 ; i < n ; i + + ) {
2017-12-18 07:41:52 +01:00
struct bitcoin_txid this_txid ;
2017-08-18 06:43:53 +02:00
bitcoin_txid ( block - > txs [ i ] , & this_txid ) ;
if ( structeq ( & this_txid , txid ) ) {
2016-12-11 23:22:08 +01:00
loc - > index = block - > txnums [ i ] ;
return loc ;
2016-09-07 23:34:36 +02:00
}
}
2016-12-11 23:22:08 +01:00
return tal_free ( loc ) ;
2016-09-07 23:34:36 +02:00
}
2017-10-24 04:06:14 +02:00
# if DEVELOPER
2017-03-02 13:21:49 +01:00
void json_dev_broadcast ( struct command * cmd ,
2017-03-02 13:21:49 +01:00
struct chain_topology * topo ,
2017-03-02 13:21:49 +01:00
const char * buffer , const jsmntok_t * params )
2016-11-09 07:44:21 +01:00
{
jsmntok_t * enabletok ;
bool enable ;
2018-01-29 01:34:28 +01:00
if ( ! json_get_params ( cmd , buffer , params ,
2016-11-09 07:44:21 +01:00
" enable " , & enabletok ,
NULL ) ) {
return ;
}
if ( ! json_tok_bool ( buffer , enabletok , & enable ) ) {
2018-01-26 02:03:48 +01:00
command_fail ( cmd , " Enable must be true or false " ) ;
2016-11-09 07:44:21 +01:00
return ;
}
2017-08-28 18:09:01 +02:00
log_debug ( cmd - > ld - > log , " dev-broadcast: broadcast %s " ,
2016-11-09 07:44:21 +01:00
enable ? " enabled " : " disabled " ) ;
2017-08-28 18:09:01 +02:00
cmd - > ld - > topology - > dev_no_broadcast = ! enable ;
2016-11-09 07:44:21 +01:00
/* If enabling, flush and wait. */
if ( enable )
2017-08-28 18:09:01 +02:00
rebroadcast_txs ( cmd - > ld - > topology , cmd ) ;
2016-11-09 07:44:21 +01:00
else
2016-11-11 00:02:04 +01:00
command_success ( cmd , null_response ( cmd ) ) ;
2016-11-09 07:44:21 +01:00
}
2017-04-27 08:07:50 +02:00
static void json_dev_blockheight ( struct command * cmd ,
const char * buffer , const jsmntok_t * params )
{
2017-08-28 18:09:01 +02:00
struct chain_topology * topo = cmd - > ld - > topology ;
2017-04-27 08:07:50 +02:00
struct json_result * response ;
response = new_json_result ( cmd ) ;
json_object_start ( response , NULL ) ;
json_add_num ( response , " blockheight " , get_block_height ( topo ) ) ;
json_object_end ( response ) ;
command_success ( cmd , response ) ;
}
static const struct json_command dev_blockheight = {
" dev-blockheight " ,
json_dev_blockheight ,
2018-01-22 09:55:07 +01:00
" Show current block height "
2017-04-27 08:07:50 +02:00
} ;
AUTODATA ( json_command , & dev_blockheight ) ;
2017-11-21 06:26:59 +01:00
static void json_dev_setfees ( struct command * cmd ,
const char * buffer , const jsmntok_t * params )
{
jsmntok_t * ratetok [ NUM_FEERATES ] ;
struct chain_topology * topo = cmd - > ld - > topology ;
struct json_result * response ;
2018-01-29 01:34:28 +01:00
if ( ! json_get_params ( cmd , buffer , params ,
2017-11-21 06:26:59 +01:00
" ?immediate " , & ratetok [ FEERATE_IMMEDIATE ] ,
" ?normal " , & ratetok [ FEERATE_NORMAL ] ,
" ?slow " , & ratetok [ FEERATE_SLOW ] ,
NULL ) ) {
return ;
}
if ( ! topo - > override_fee_rate ) {
u32 fees [ NUM_FEERATES ] ;
for ( size_t i = 0 ; i < ARRAY_SIZE ( fees ) ; i + + )
fees [ i ] = get_feerate ( topo , i ) ;
topo - > override_fee_rate = tal_dup_arr ( topo , u32 , fees ,
ARRAY_SIZE ( fees ) , 0 ) ;
}
for ( size_t i = 0 ; i < NUM_FEERATES ; i + + ) {
if ( ! ratetok [ i ] )
continue ;
if ( ! json_tok_number ( buffer , ratetok [ i ] ,
& topo - > override_fee_rate [ i ] ) ) {
2018-01-26 02:03:48 +01:00
command_fail ( cmd , " Invalid feerate %.*s " ,
2017-11-21 06:26:59 +01:00
( int ) ( ratetok [ i ] - > end - ratetok [ i ] - > start ) ,
buffer + ratetok [ i ] - > start ) ;
return ;
}
}
log_debug ( topo - > log ,
" dev-setfees: fees now %u/%u/%u " ,
topo - > override_fee_rate [ FEERATE_IMMEDIATE ] ,
topo - > override_fee_rate [ FEERATE_NORMAL ] ,
topo - > override_fee_rate [ FEERATE_SLOW ] ) ;
notify_feerate_change ( cmd - > ld ) ;
response = new_json_result ( cmd ) ;
json_object_start ( response , NULL ) ;
json_add_num ( response , " immediate " ,
topo - > override_fee_rate [ FEERATE_IMMEDIATE ] ) ;
json_add_num ( response , " normal " ,
topo - > override_fee_rate [ FEERATE_NORMAL ] ) ;
json_add_num ( response , " slow " ,
topo - > override_fee_rate [ FEERATE_SLOW ] ) ;
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 ;
2017-11-21 04:31:31 +01:00
topo - > override_fee_rate = NULL ;
2017-11-01 11:20:40 +01:00
topo - > bitcoind = new_bitcoind ( topo , ld , log ) ;
2017-10-24 04:06:14 +02:00
# if DEVELOPER
topo - > dev_no_broadcast = false ;
# endif
2016-04-24 12:07:13 +02:00
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-02-12 11:13:04 +01:00
struct timerel poll_time , u32 first_channel_block )
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 ;
topo - > poll_time = poll_time ;
2018-01-03 06:26:43 +01:00
/* Start one before the block we are interested in (as we won't
* get notifications on txs in that block ) . */
2018-02-12 11:13:04 +01:00
topo - > first_blocknum = first_channel_block - 1 ;
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
}