@@ -795,6 +795,7 @@ struct filteredblock_call {
795795 struct filteredblock_outpoint * * outpoints ;
796796 size_t current_outpoint ;
797797 struct timeabs start_time ;
798+ u32 height ;
798799};
799800
800801/* Declaration for recursion in process_getfilteredblock_step1 */
@@ -831,6 +832,12 @@ static void process_getfilteredblock_step2(struct bitcoind *bitcoind,
831832{
832833 struct filteredblock_outpoint * o ;
833834 struct bitcoin_tx * tx ;
835+
836+ /* If for some reason we couldn't get the block, just report a
837+ * failure. */
838+ if (block == NULL )
839+ return process_getfiltered_block_final (bitcoind , call );
840+
834841 call -> result -> prev_hash = block -> hdr .prev_hash ;
835842
836843 /* Allocate an array containing all the potentially interesting
@@ -876,7 +883,16 @@ static void process_getfilteredblock_step1(struct bitcoind *bitcoind,
876883 const struct bitcoin_blkid * blkid ,
877884 struct filteredblock_call * call )
878885{
886+ /* If we were unable to fetch the block hash (bitcoind doesn't know
887+ * about a block at that height), we can short-circuit and just call
888+ * the callback. */
889+ if (!blkid )
890+ return process_getfiltered_block_final (bitcoind , call );
891+
879892 /* So we have the first piece of the puzzle, the block hash */
893+ call -> result = tal (call , struct filteredblock );
894+ call -> result -> height = call -> height ;
895+ call -> result -> outpoints = tal_arr (call -> result , struct filteredblock_outpoint * , 0 );
880896 call -> result -> id = * blkid ;
881897
882898 /* Now get the raw block to get all outpoints that were created in
@@ -891,24 +907,29 @@ process_getfiltered_block_final(struct bitcoind *bitcoind,
891907 const struct filteredblock_call * call )
892908{
893909 struct filteredblock_call * c , * next ;
894- u32 height = call -> result -> height ;
910+ u32 height = call -> height ;
911+
912+ if (call -> result == NULL )
913+ goto next ;
914+
895915 /* Need to steal so we don't accidentally free it while iterating through the list below. */
896916 struct filteredblock * fb = tal_steal (NULL , call -> result );
897917 list_for_each_safe (& bitcoind -> pending_getfilteredblock , c , next , list ) {
898- if (c -> result -> height == height ) {
918+ if (c -> height == height ) {
899919 c -> cb (bitcoind , fb , c -> arg );
900920 list_del (& c -> list );
901921 tal_free (c );
902922 }
903923 }
904924 tal_free (fb );
905925
926+ next :
906927 /* Nothing to free here, since `*call` was already deleted during the
907928 * iteration above. It was also removed from the list, so no need to
908929 * pop here. */
909930 if (!list_empty (& bitcoind -> pending_getfilteredblock )) {
910931 c = list_top (& bitcoind -> pending_getfilteredblock , struct filteredblock_call , list );
911- bitcoind_getblockhash (bitcoind , c -> result -> height , process_getfilteredblock_step1 , c );
932+ bitcoind_getblockhash (bitcoind , c -> height , process_getfilteredblock_step1 , c );
912933 }
913934}
914935
@@ -925,11 +946,10 @@ void bitcoind_getfilteredblock_(struct bitcoind *bitcoind, u32 height,
925946 bool start = list_empty (& bitcoind -> pending_getfilteredblock );
926947 call -> cb = cb ;
927948 call -> arg = arg ;
928- call -> result = tal ( call , struct filteredblock ) ;
949+ call -> height = height ;
929950 assert (call -> cb != NULL );
930951 call -> start_time = time_now ();
931- call -> result -> height = height ;
932- call -> result -> outpoints = tal_arr (call -> result , struct filteredblock_outpoint * , 0 );
952+ call -> result = NULL ;
933953 call -> current_outpoint = 0 ;
934954
935955 list_add_tail (& bitcoind -> pending_getfilteredblock , & call -> list );
0 commit comments