Browse Source

wallet: save block height when we first create channel.

This gives us a lower bound on where funding tx could be.

In theory, it could be lower than this if we get a reorganization, but
in practice this is already a 1-block buffer (since we can't get into
current block, only the next one).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
93c589efe8
  1. 8
      lightningd/peer_control.c
  2. 6
      lightningd/peer_htlcs.c
  3. 16
      wallet/test/run-wallet.c
  4. 8
      wallet/wallet.c
  5. 5
      wallet/wallet.h

8
lightningd/peer_control.c

@ -300,7 +300,7 @@ void peer_set_condition(struct peer *peer, enum peer_state old_state,
if (peer_persists(peer)) { if (peer_persists(peer)) {
assert(peer->channel != NULL); assert(peer->channel != NULL);
/* TODO(cdecker) Selectively save updated fields to DB */ /* TODO(cdecker) Selectively save updated fields to DB */
wallet_channel_save(peer->ld->wallet, peer->channel); wallet_channel_save(peer->ld->wallet, peer->channel, 0);
} }
} }
@ -494,7 +494,7 @@ static struct wallet_channel *peer_channel_new(struct wallet *w,
wallet_peer_by_nodeid(w, &peer->id, peer); wallet_peer_by_nodeid(w, &peer->id, peer);
wc->id = 0; wc->id = 0;
wallet_channel_save(w, wc); wallet_channel_save(w, wc, get_block_height(peer->ld->topology));
return wc; return wc;
} }
@ -1707,7 +1707,7 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg)
} }
/* TODO(cdecker) Selectively save updated fields to DB */ /* TODO(cdecker) Selectively save updated fields to DB */
wallet_channel_save(peer->ld->wallet, peer->channel); wallet_channel_save(peer->ld->wallet, peer->channel, 0);
} }
void peer_last_tx(struct peer *peer, struct bitcoin_tx *tx, void peer_last_tx(struct peer *peer, struct bitcoin_tx *tx,
@ -1767,7 +1767,7 @@ static void peer_received_closing_signature(struct peer *peer, const u8 *msg)
/* FIXME: Make sure signature is correct! */ /* FIXME: Make sure signature is correct! */
if (better_closing_fee(peer, tx)) { if (better_closing_fee(peer, tx)) {
/* TODO(cdecker) Selectively save updated fields to DB */ /* TODO(cdecker) Selectively save updated fields to DB */
wallet_channel_save(peer->ld->wallet, peer->channel); wallet_channel_save(peer->ld->wallet, peer->channel, 0);
peer_last_tx(peer, tx, &sig); peer_last_tx(peer, tx, &sig);
} }

6
lightningd/peer_htlcs.c

@ -914,7 +914,7 @@ static bool peer_save_commitsig_received(struct peer *peer, u64 commitnum)
peer->next_index[LOCAL]++; peer->next_index[LOCAL]++;
/* FIXME: Save to database, with sig and HTLCs. */ /* FIXME: Save to database, with sig and HTLCs. */
wallet_channel_save(peer->ld->wallet, peer->channel); wallet_channel_save(peer->ld->wallet, peer->channel, 0);
return true; return true;
} }
@ -931,7 +931,7 @@ static bool peer_save_commitsig_sent(struct peer *peer, u64 commitnum)
peer->next_index[REMOTE]++; peer->next_index[REMOTE]++;
/* FIXME: Save to database, with sig and HTLCs. */ /* FIXME: Save to database, with sig and HTLCs. */
wallet_channel_save(peer->ld->wallet, peer->channel); wallet_channel_save(peer->ld->wallet, peer->channel, 0);
return true; return true;
} }
@ -1243,7 +1243,7 @@ void peer_got_revoke(struct peer *peer, const u8 *msg)
hin = find_htlc_in(&peer->ld->htlcs_in, peer, changed[i].id); hin = find_htlc_in(&peer->ld->htlcs_in, peer, changed[i].id);
local_fail_htlc(hin, failcodes[i], NULL); local_fail_htlc(hin, failcodes[i], NULL);
} }
wallet_channel_save(peer->ld->wallet, peer->channel); wallet_channel_save(peer->ld->wallet, peer->channel, 0);
} }
static void *tal_arr_append_(void **p, size_t size) static void *tal_arr_append_(void **p, size_t size)

16
wallet/test/run-wallet.c

@ -314,7 +314,7 @@ static bool test_channel_crud(const tal_t *ctx)
CHECK(!wallet_err); CHECK(!wallet_err);
/* Variant 1: insert with null for scid, funding_tx_id, channel_info, last_tx */ /* Variant 1: insert with null for scid, funding_tx_id, channel_info, last_tx */
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 1);
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
tal_fmt(w, "Insert into DB: %s", wallet_err)); tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
@ -329,7 +329,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 2: update with scid set */ /* Variant 2: update with scid set */
c1.peer->scid = talz(w, struct short_channel_id); c1.peer->scid = talz(w, struct short_channel_id);
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
tal_fmt(w, "Insert into DB: %s", wallet_err)); tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
@ -345,7 +345,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 3: update with our_satoshi set */ /* Variant 3: update with our_satoshi set */
c1.peer->our_msatoshi = &msat; c1.peer->our_msatoshi = &msat;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
@ -354,7 +354,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 4: update with funding_tx_id */ /* Variant 4: update with funding_tx_id */
c1.peer->funding_txid = hash; c1.peer->funding_txid = hash;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
@ -363,7 +363,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 5: update with channel_info */ /* Variant 5: update with channel_info */
p.channel_info = &ci; p.channel_info = &ci;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
@ -372,7 +372,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 6: update with last_commit_sent */ /* Variant 6: update with last_commit_sent */
p.last_sent_commit = &last_commit; p.last_sent_commit = &last_commit;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
@ -382,7 +382,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 7: update with last_tx (taken from BOLT #3) */ /* Variant 7: update with last_tx (taken from BOLT #3) */
p.last_tx = bitcoin_tx_from_hex(w, "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", strlen("02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220")); p.last_tx = bitcoin_tx_from_hex(w, "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", strlen("02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220"));
p.last_sig = sig; p.last_sig = sig;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,
@ -392,7 +392,7 @@ static bool test_channel_crud(const tal_t *ctx)
/* Variant 8: update and add remote_shutdown_scriptpubkey */ /* Variant 8: update and add remote_shutdown_scriptpubkey */
p.remote_shutdown_scriptpubkey = scriptpubkey; p.remote_shutdown_scriptpubkey = scriptpubkey;
p.local_shutdown_idx = 1337; p.local_shutdown_idx = 1337;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1, 0);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err));
CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB")); CHECK_MSG(c2 = wallet_channel_load(w, c1.id), tal_fmt(w, "Load from DB"));
CHECK_MSG(!wallet_err, CHECK_MSG(!wallet_err,

8
wallet/wallet.c

@ -671,7 +671,9 @@ bool wallet_channel_config_load(struct wallet *w, const u64 id,
return ok; return ok;
} }
void wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ void wallet_channel_save(struct wallet *w, struct wallet_channel *chan,
u32 current_block_height)
{
struct peer *p = chan->peer; struct peer *p = chan->peer;
tal_t *tmpctx = tal_tmpctx(w); tal_t *tmpctx = tal_tmpctx(w);
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
@ -689,8 +691,10 @@ void wallet_channel_save(struct wallet *w, struct wallet_channel *chan){
/* Insert a stub, that we can update, unifies INSERT and UPDATE paths */ /* Insert a stub, that we can update, unifies INSERT and UPDATE paths */
if (chan->id == 0) { if (chan->id == 0) {
stmt = db_prepare(w->db, "INSERT INTO channels (peer_id) VALUES (?);"); assert(current_block_height);
stmt = db_prepare(w->db, "INSERT INTO channels (peer_id,first_blocknum) VALUES (?,?);");
sqlite3_bind_int64(stmt, 1, p->dbid); sqlite3_bind_int64(stmt, 1, p->dbid);
sqlite3_bind_int(stmt, 2, current_block_height);
db_exec_prepared(w->db, stmt); db_exec_prepared(w->db, stmt);
chan->id = sqlite3_last_insert_rowid(w->db->sql); chan->id = sqlite3_last_insert_rowid(w->db->sql);
} }

5
wallet/wallet.h

@ -204,8 +204,11 @@ bool wallet_shachain_load(struct wallet *wallet, u64 id,
* @wallet: the wallet to save into * @wallet: the wallet to save into
* @chan: the instance to store (not const so we can update the unique_id upon * @chan: the instance to store (not const so we can update the unique_id upon
* insert) * insert)
* @current_block_height: current height, minimum block this funding tx could
* be in (only used on initial insert).
*/ */
void wallet_channel_save(struct wallet *w, struct wallet_channel *chan); void wallet_channel_save(struct wallet *w, struct wallet_channel *chan,
u32 current_block_height);
/** /**
* wallet_channel_config_save -- Upsert a channel_config into the database * wallet_channel_config_save -- Upsert a channel_config into the database

Loading…
Cancel
Save