mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 23:24:27 +01:00
wallet: Always use the DB backed bip32_max_index
We were loading it on startup and updating as we went. Removing caching to reduce chances of becoming desynchronized.
This commit is contained in:
committed by
Rusty Russell
parent
257ecf6222
commit
7e0b9bd1ab
@@ -26,13 +26,14 @@ static void json_newaddr(struct command *cmd,
|
|||||||
struct ripemd160 p2sh;
|
struct ripemd160 p2sh;
|
||||||
struct pubkey pubkey;
|
struct pubkey pubkey;
|
||||||
u8 *redeemscript;
|
u8 *redeemscript;
|
||||||
|
u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
|
||||||
|
|
||||||
if (ld->bip32_max_index == BIP32_INITIAL_HARDENED_CHILD) {
|
if (bip32_max_index == BIP32_INITIAL_HARDENED_CHILD) {
|
||||||
command_fail(cmd, "Keys exhausted ");
|
command_fail(cmd, "Keys exhausted ");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bip32_key_from_parent(ld->bip32_base, ld->bip32_max_index,
|
if (bip32_key_from_parent(ld->bip32_base, bip32_max_index,
|
||||||
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
|
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
|
||||||
command_fail(cmd, "Keys generation failure");
|
command_fail(cmd, "Keys generation failure");
|
||||||
return;
|
return;
|
||||||
@@ -48,8 +49,7 @@ static void json_newaddr(struct command *cmd,
|
|||||||
sha256(&h, redeemscript, tal_count(redeemscript));
|
sha256(&h, redeemscript, tal_count(redeemscript));
|
||||||
ripemd160(&p2sh, h.u.u8, sizeof(h));
|
ripemd160(&p2sh, h.u.u8, sizeof(h));
|
||||||
|
|
||||||
ld->bip32_max_index++;
|
db_set_intvar(ld->wallet->db, "bip32_max_index", bip32_max_index + 1);
|
||||||
db_set_intvar(ld->wallet->db, "bip32_max_index", ld->bip32_max_index);
|
|
||||||
|
|
||||||
json_object_start(response, NULL);
|
json_object_start(response, NULL);
|
||||||
json_add_string(response, "address",
|
json_add_string(response, "address",
|
||||||
@@ -71,6 +71,7 @@ static bool can_spend(struct lightningd *ld, const u8 *script,
|
|||||||
u32 *index, bool *output_is_p2sh)
|
u32 *index, bool *output_is_p2sh)
|
||||||
{
|
{
|
||||||
struct ext_key ext;
|
struct ext_key ext;
|
||||||
|
u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
|
||||||
u32 i;
|
u32 i;
|
||||||
|
|
||||||
/* If not one of these, can't be for us. */
|
/* If not one of these, can't be for us. */
|
||||||
@@ -81,7 +82,7 @@ static bool can_spend(struct lightningd *ld, const u8 *script,
|
|||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (i = 0; i < ld->bip32_max_index; i++) {
|
for (i = 0; i < bip32_max_index; i++) {
|
||||||
u8 *s;
|
u8 *s;
|
||||||
|
|
||||||
if (bip32_key_from_parent(ld->bip32_base, i,
|
if (bip32_key_from_parent(ld->bip32_base, i,
|
||||||
@@ -218,6 +219,7 @@ const struct utxo **build_utxos(const tal_t *ctx,
|
|||||||
struct tracked_utxo *utxo;
|
struct tracked_utxo *utxo;
|
||||||
/* We assume two outputs for the weight. */
|
/* We assume two outputs for the weight. */
|
||||||
u64 satoshi_in = 0, weight = (4 + (8 + 22) * 2 + 4) * 4;
|
u64 satoshi_in = 0, weight = (4 + (8 + 22) * 2 + 4) * 4;
|
||||||
|
u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
|
||||||
|
|
||||||
tal_add_destructor2(utxos, destroy_utxos, ld);
|
tal_add_destructor2(utxos, destroy_utxos, ld);
|
||||||
|
|
||||||
@@ -251,8 +253,10 @@ const struct utxo **build_utxos(const tal_t *ctx,
|
|||||||
if (*change_satoshis < dust_limit) {
|
if (*change_satoshis < dust_limit) {
|
||||||
*change_satoshis = 0;
|
*change_satoshis = 0;
|
||||||
*change_keyindex = 0;
|
*change_keyindex = 0;
|
||||||
} else
|
} else {
|
||||||
*change_keyindex = ld->bip32_max_index++;
|
*change_keyindex = bip32_max_index + 1;
|
||||||
|
db_set_intvar(ld->wallet->db, "bip32_max_index", *change_keyindex);
|
||||||
|
}
|
||||||
|
|
||||||
return utxos;
|
return utxos;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,7 +107,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
|
|||||||
|
|
||||||
list_head_init(&ld->peers);
|
list_head_init(&ld->peers);
|
||||||
ld->peer_counter = 0;
|
ld->peer_counter = 0;
|
||||||
ld->bip32_max_index = 0;
|
|
||||||
ld->dev_debug_subdaemon = NULL;
|
ld->dev_debug_subdaemon = NULL;
|
||||||
list_head_init(&ld->utxos);
|
list_head_init(&ld->utxos);
|
||||||
htlc_end_map_init(&ld->htlc_ends);
|
htlc_end_map_init(&ld->htlc_ends);
|
||||||
@@ -257,8 +256,6 @@ int main(int argc, char *argv[])
|
|||||||
/* Initialize wallet, now that we are in the correct directory */
|
/* Initialize wallet, now that we are in the correct directory */
|
||||||
ld->wallet = wallet_new(ld, ld->log);
|
ld->wallet = wallet_new(ld, ld->log);
|
||||||
|
|
||||||
ld->bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
|
|
||||||
|
|
||||||
/* Mark ourselves live. */
|
/* Mark ourselves live. */
|
||||||
log_info(ld->log, "Hello world from %s!", version());
|
log_info(ld->log, "Hello world from %s!", version());
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,6 @@ struct lightningd {
|
|||||||
|
|
||||||
/* Public base for bip32 keys, and max we've ever used. */
|
/* Public base for bip32 keys, and max we've ever used. */
|
||||||
struct ext_key *bip32_base;
|
struct ext_key *bip32_base;
|
||||||
u32 bip32_max_index;
|
|
||||||
|
|
||||||
/* Our bitcoind context. */
|
/* Our bitcoind context. */
|
||||||
struct bitcoind *bitcoind;
|
struct bitcoind *bitcoind;
|
||||||
|
|||||||
Reference in New Issue
Block a user