core-lightning/lightningd/bitcoind.h

164 lines
4.7 KiB
C
Raw Normal View History

#ifndef LIGHTNING_LIGHTNINGD_BITCOIND_H
#define LIGHTNING_LIGHTNINGD_BITCOIND_H
#include "config.h"
#include <bitcoin/chainparams.h>
#include <bitcoin/tx.h>
#include <ccan/list/list.h>
#include <ccan/strmap/strmap.h>
struct bitcoin_blkid;
struct bitcoin_tx_output;
struct block;
struct feerate_est;
struct lightningd;
struct ripemd160;
struct bitcoin_tx;
struct bitcoin_block;
struct bitcoind {
/* Where to do logging. */
struct logger *log;
/* Main lightningd structure */
struct lightningd *ld;
/* Is our Bitcoin backend synced? If not, we retry. */
bool synced;
/* Ignore results, we're shutting down. */
bool shutdown;
struct list_head pending_getfilteredblock;
/* Map each method to a plugin, so we can have multiple plugins
* handling different functionalities. */
STRMAP(struct plugin *) pluginsmap;
};
/* A single outpoint in a filtered block */
struct filteredblock_outpoint {
struct bitcoin_outpoint outpoint;
u32 txindex;
const u8 *scriptPubKey;
struct amount_sat amount;
};
/* A struct representing a block with most of the parts filtered out. */
struct filteredblock {
struct bitcoin_blkid id;
u32 height;
struct bitcoin_blkid prev_hash;
struct filteredblock_outpoint **outpoints;
};
struct bitcoind *new_bitcoind(const tal_t *ctx,
struct lightningd *ld,
struct logger *log);
#define bitcoind_estimate_fees(ctx, bitcoind_, cb, arg) \
bitcoind_estimate_fees_((ctx), (bitcoind_), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct lightningd *, \
u32, \
const struct feerate_est *), \
(arg))
void bitcoind_estimate_fees_(const tal_t *ctx,
struct bitcoind *bitcoind,
void (*cb)(struct lightningd *ld,
u32 feerate_floor,
const struct feerate_est *feerates,
void *arg),
void *cb_arg);
/* If ctx is freed, cb won't be called! */
void bitcoind_sendrawtx_(const tal_t *ctx,
struct bitcoind *bitcoind,
const char *id_prefix TAKES,
const char *hextx,
bool allowhighfees,
void (*cb)(struct bitcoind *bitcoind,
bool success, const char *msg, void *),
void *arg);
#define bitcoind_sendrawtx(ctx, bitcoind_, id_prefix, hextx, allowhighfees, cb, arg) \
bitcoind_sendrawtx_((ctx), (bitcoind_), (id_prefix), (hextx), \
(allowhighfees), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
bool, const char *), \
(arg))
void bitcoind_getfilteredblock_(const tal_t *ctx,
struct bitcoind *bitcoind, u32 height,
void (*cb)(struct bitcoind *bitcoind,
const struct filteredblock *fb,
void *arg),
void *arg);
#define bitcoind_getfilteredblock(ctx, bitcoind_, height, cb, arg) \
bitcoind_getfilteredblock_((ctx), (bitcoind_), \
(height), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
const struct filteredblock *), \
(arg))
void bitcoind_getchaininfo_(const tal_t *ctx,
struct bitcoind *bitcoind,
feat(bitcoind): pass the current known block height When core lightning is asking the information about the blockchain with `getchaininfo` command lightningd know already the information about the min and max block height. the problem is when we have a smarter Bitcoin backend that is able to switch between different clients in some cases is helpful give the information about current known height by lightningd and pass it down to the plugin. In this way, the plugin knows what is the correct known height from lightnind, and can try to fix some problems if any exit. This is particularly useful when you are syncing a new backend from scratch like https://github.com/cloudhead/nakamoto and we avoid returning the lower height from the known, and avoid the crash of core lightning. With this information, the plugin can start to sync the chain and return the answer back only when the chain is in sync with the current status of lightningd. Another reason to add this field and not wait the correct block in core lightning itself is because Bitcoin Core is extremely slow to sync up, so the question here is, how long should we wait? The time depends on various factors. With this approach of informing the plugin about the height, in some cases, you can start the syncing but move the execution to another backend until the previous one is ready. The problem I want to solve is that I don't want to be left in the dark when we run `getchaininfo`, and I want to have the opportunity to wait for the blockchain sync or decide to dispatch the request elsewhere. Changelog-Added: Pass the current known block height down to the getchaininfo call. Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-04-15 16:53:47 +02:00
const u32 height,
void (*cb)(struct bitcoind *bitcoind,
const char *chain,
u32 headercount,
u32 blockcount,
bool ibd,
void *),
void *cb_arg);
#define bitcoind_getchaininfo(ctx, bitcoind_, height_, cb, arg) \
bitcoind_getchaininfo_((ctx), (bitcoind_), (height_), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
const char *, u32, u32, \
bool), \
(arg))
void bitcoind_getrawblockbyheight_(const tal_t *ctx,
struct bitcoind *bitcoind,
u32 height,
void (*cb)(struct bitcoind *bitcoind,
u32 height,
struct bitcoin_blkid *blkid,
struct bitcoin_block *blk,
void *arg),
void *arg);
#define bitcoind_getrawblockbyheight(ctx, bitcoind_, height_, cb, arg) \
bitcoind_getrawblockbyheight_((ctx), (bitcoind_), (height_), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
u32, \
struct bitcoin_blkid *, \
struct bitcoin_block *),\
(arg))
void bitcoind_getutxout_(const tal_t *ctx,
struct bitcoind *bitcoind,
const struct bitcoin_outpoint *outpoint,
void (*cb)(struct bitcoind *,
const struct bitcoin_tx_output *,
void *),
void *arg);
#define bitcoind_getutxout(ctx, bitcoind_, outpoint_, cb, arg) \
bitcoind_getutxout_((ctx), (bitcoind_), (outpoint_), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
const struct bitcoin_tx_output *),\
(arg))
void bitcoind_check_commands(struct bitcoind *bitcoind);
#endif /* LIGHTNING_LIGHTNINGD_BITCOIND_H */