Browse Source

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.
ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
7e0b9bd1ab
  1. 18
      lightningd/build_utxos.c
  2. 3
      lightningd/lightningd.c
  3. 1
      lightningd/lightningd.h

18
lightningd/build_utxos.c

@ -26,13 +26,14 @@ static void json_newaddr(struct command *cmd,
struct ripemd160 p2sh;
struct pubkey pubkey;
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 ");
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) {
command_fail(cmd, "Keys generation failure");
return;
@ -48,8 +49,7 @@ static void json_newaddr(struct command *cmd,
sha256(&h, redeemscript, tal_count(redeemscript));
ripemd160(&p2sh, h.u.u8, sizeof(h));
ld->bip32_max_index++;
db_set_intvar(ld->wallet->db, "bip32_max_index", ld->bip32_max_index);
db_set_intvar(ld->wallet->db, "bip32_max_index", bip32_max_index + 1);
json_object_start(response, NULL);
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)
{
struct ext_key ext;
u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
u32 i;
/* 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
return false;
for (i = 0; i < ld->bip32_max_index; i++) {
for (i = 0; i < bip32_max_index; i++) {
u8 *s;
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;
/* We assume two outputs for the weight. */
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);
@ -251,8 +253,10 @@ const struct utxo **build_utxos(const tal_t *ctx,
if (*change_satoshis < dust_limit) {
*change_satoshis = 0;
*change_keyindex = 0;
} else
*change_keyindex = ld->bip32_max_index++;
} else {
*change_keyindex = bip32_max_index + 1;
db_set_intvar(ld->wallet->db, "bip32_max_index", *change_keyindex);
}
return utxos;
}

3
lightningd/lightningd.c

@ -107,7 +107,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx)
list_head_init(&ld->peers);
ld->peer_counter = 0;
ld->bip32_max_index = 0;
ld->dev_debug_subdaemon = NULL;
list_head_init(&ld->utxos);
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 */
ld->wallet = wallet_new(ld, ld->log);
ld->bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
/* Mark ourselves live. */
log_info(ld->log, "Hello world from %s!", version());

1
lightningd/lightningd.h

@ -40,7 +40,6 @@ struct lightningd {
/* Public base for bip32 keys, and max we've ever used. */
struct ext_key *bip32_base;
u32 bip32_max_index;
/* Our bitcoind context. */
struct bitcoind *bitcoind;

Loading…
Cancel
Save