Skip to content

UTXO high watermark in db - #1005

Merged
cdecker merged 6 commits into
ElementsProject:masterfrom
rustyrussell:utxo-high-watermark-in-db
Feb 16, 2018
Merged

UTXO high watermark in db#1005
cdecker merged 6 commits into
ElementsProject:masterfrom
rustyrussell:utxo-high-watermark-in-db

Conversation

@rustyrussell

Copy link
Copy Markdown
Contributor

This, I believe, is the proper solution to the problem in #990

Comment thread bitcoin/chainparams.h
const char *cli;
const char *cli_args;
const u64 dust_limit;
const u32 when_lightning_became_cool;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the var name 👍

Comment thread bitcoin/chainparams.c
.cli_args = NULL,
.dust_limit = 546,
/* "Lightning Charge Powers Developers & Blockstream Store" */
.when_lightning_became_cool = 504500,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahahaha

Comment thread wallet/db.c Outdated
"CREATE TABLE htlc_sigs (channelid INTEGER REFERENCES channels(id) ON DELETE CASCADE, signature BLOB);",
"CREATE INDEX channel_idx ON htlc_sigs (channelid)",
/* Need to track high-watermark for wallet utxos. */
"ALTER TABLE vars ADD COLUMN last_processed_block INTEGER;",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. See line 144 for correct way to add new var. with an initial value Note, if not added in migrations, db_get_intvar will return 0.

Each row in TABLE vars is a variable, so we should add a row, not a column. db_set_intvar will automatically add a row, if not present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, will fix, thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't actually need this at all, thanks! I think I prefer all 'vars' defined in the dbmigrations array, rather than allowing them to created on the fly, but that can be done later.

Comment thread wallet/wallet.c Outdated
if (db_get_intvar(w->db, "bip32_max_index", 0) != 0)
return true;

/* Or we could have had a channel terminate unilaterally,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about, if we are a fresh, satoshiless node, somebody opens a channel to us, gives us money, then closes the channel bilaterally? Also, which unilateral is this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be the their_unilateral/to_us output, since it's the only case in which we don't have the secret key integrated into the HD key structure, instead it's a collaboratively generated key. In all other cases onchaind will take care to respend to a key in the hierarchy.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, thank you.

@ZmnSCPxj

Copy link
Copy Markdown
Contributor

Concept ACK, but please see my review.

@cdecker

cdecker commented Feb 15, 2018

Copy link
Copy Markdown
Member

I think this might break onchaind: onchaind relies on witnessing the funding spend, then following the further spends from there. If we see the funding spend, then stop, and restart, then we won't continue tracking the remainder. We could disable the highwater mark updates while an onchaind is running, so that we can restart onchaind with the minimal set of information that we pass in now, but it feels rather hacky.

We already go back 100 from this in case of reorgs, so the block number
itself is sufficient.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
With fallback depending on chainparams: this means the first upgrade
will be slow, but after that it'll be fast.

Fixes: ElementsProject#990
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
As described by @lvaccaro in ElementsProject#990.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
@rustyrussell

Copy link
Copy Markdown
Contributor Author

I think we still get this right? We actually take the earlier of two measurements:

  1. The first channel funding, and
  2. The last block we hadn't already scanned for UTXOs.

We then subtract another 100 blocks for some extra fork protection (which I wish we hadn't initially, since it papered over a number of these bugs!).

I'll add clarifying comments.

We only need to do that if it's possible there's something to find:
either we have an unspent output from a unilateral close, or we've
ever handed out an address.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ZmnSCPxj queried the unilateral close case, so make that clearer.
Christian raise concerns about existing channels, so make it clear
what we're doing there too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
We do a complicated dance because we don't know the current block
height before setting up the topology.

If we're starting at a particular block, we want to go back 100 blocks
before that to cover any reorgs.

If we're not (fresh startup), we still want to go back 100 blocks
because we don't bother handling a reorg which removes all the blocks
we know.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
@rustyrussell
rustyrussell force-pushed the utxo-high-watermark-in-db branch from 6f18a0e to 2ac26dc Compare February 15, 2018 23:08
@ZmnSCPxj

Copy link
Copy Markdown
Contributor

ACK 2ac26dc

@cdecker

cdecker commented Feb 16, 2018

Copy link
Copy Markdown
Member

Ok, didn't see the min() for the scan start.

ACK 2ac26dc

@cdecker
cdecker merged commit bdd11e0 into ElementsProject:master Feb 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants