-
Notifications
You must be signed in to change notification settings - Fork 942
Store onchaind state in the database and reduce rescan on startup #1398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fb8af75
3b3f62f
961e55b
2bdbcc4
57f9129
e147967
84376f8
ea97404
155335e
01cb08a
17b3286
47c4e74
8ef2e8a
ddd53f1
fc2b6c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
#include <lightningd/log.h> | ||
#include <lightningd/peer_control.h> | ||
#include <lightningd/peer_htlcs.h> | ||
#include <onchaind/gen_onchain_wire.h> | ||
#include <string.h> | ||
|
||
#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF | ||
|
@@ -2277,3 +2278,77 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx, | |
return txids; | ||
} | ||
|
||
void wallet_channeltxs_add(struct wallet *w, struct channel *chan, | ||
const int type, const struct bitcoin_txid *txid, | ||
const u32 input_num, const u32 blockheight) | ||
{ | ||
sqlite3_stmt *stmt; | ||
stmt = db_prepare(w->db, "INSERT INTO channeltxs (" | ||
" channel_id" | ||
", type" | ||
", transaction_id" | ||
", input_num" | ||
", blockheight" | ||
") VALUES (?, ?, ?, ?, ?);"); | ||
sqlite3_bind_int(stmt, 1, chan->dbid); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the 'id' database field used at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an autoincrement field that is used in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is declared https://www.sqlite.org/autoinc.html
Admittedly the condition of reaching 2^63 is extremely unlikely, and if we neglect that, we can generally think of Indeed, later on it pretty much admits that the only difference between |
||
sqlite3_bind_int(stmt, 2, type); | ||
sqlite3_bind_sha256(stmt, 3, &txid->shad.sha); | ||
sqlite3_bind_int(stmt, 4, input_num); | ||
sqlite3_bind_int(stmt, 5, blockheight); | ||
|
||
db_exec_prepared(w->db, stmt); | ||
} | ||
|
||
u32 *wallet_onchaind_channels(struct wallet *w, | ||
const tal_t *ctx) | ||
{ | ||
sqlite3_stmt *stmt; | ||
size_t count = 0; | ||
u32 *channel_ids = tal_arr(ctx, u32, 0); | ||
stmt = db_prepare(w->db, "SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;"); | ||
sqlite3_bind_int(stmt, 1, WIRE_ONCHAIN_INIT); | ||
|
||
while (sqlite3_step(stmt) == SQLITE_ROW) { | ||
count++; | ||
tal_resize(&channel_ids, count); | ||
channel_ids[count-1] = sqlite3_column_int64(stmt, 0); | ||
} | ||
|
||
return channel_ids; | ||
} | ||
|
||
struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx, | ||
u32 channel_id) | ||
{ | ||
sqlite3_stmt *stmt; | ||
size_t count = 0; | ||
struct channeltx *res = tal_arr(ctx, struct channeltx, 0); | ||
stmt = db_prepare(w->db, | ||
"SELECT" | ||
" c.type" | ||
", c.blockheight" | ||
", t.rawtx" | ||
", c.input_num" | ||
", c.blockheight - t.blockheight + 1 AS depth" | ||
", t.id as txid " | ||
"FROM channeltxs c " | ||
"JOIN transactions t ON t.id == c.transaction_id " | ||
"WHERE channel_id = ? " | ||
"ORDER BY c.id ASC;"); | ||
sqlite3_bind_int(stmt, 1, channel_id); | ||
|
||
while (sqlite3_step(stmt) == SQLITE_ROW) { | ||
count++; | ||
tal_resize(&res, count); | ||
|
||
res[count-1].channel_id = channel_id; | ||
res[count-1].type = sqlite3_column_int(stmt, 0); | ||
res[count-1].blockheight = sqlite3_column_int(stmt, 1); | ||
res[count-1].tx = sqlite3_column_tx(ctx, stmt, 2); | ||
res[count-1].input_num = sqlite3_column_int(stmt, 3); | ||
res[count-1].depth = sqlite3_column_int(stmt, 4); | ||
sqlite3_column_sha256(stmt, 5, &res[count-1].txid.shad.sha); | ||
} | ||
|
||
return res; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,7 @@ struct channel_stats { | |
u64 out_msatoshi_offered, out_msatoshi_fulfilled; | ||
}; | ||
|
||
struct onchaindtx { | ||
struct channeltx { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this particular change can be put in the previous commit instead? |
||
u32 channel_id; | ||
int type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
u32 blockheight; | ||
|
@@ -846,4 +846,23 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx, | |
struct wallet *w, | ||
const u32 blockheight); | ||
|
||
/** | ||
* Store transactions of interest in the database to replay on restart | ||
*/ | ||
void wallet_channeltxs_add(struct wallet *w, struct channel *chan, | ||
const int type, const struct bitcoin_txid *txid, | ||
const u32 input_num, const u32 blockheight); | ||
|
||
/** | ||
* List channels for which we had an onchaind running | ||
*/ | ||
u32 *wallet_onchaind_channels(struct wallet *w, | ||
const tal_t *ctx); | ||
|
||
/** | ||
* Get transactions that we'd like to replay for a channel. | ||
*/ | ||
struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx, | ||
u32 channel_id); | ||
|
||
#endif /* LIGHTNING_WALLET_WALLET_H */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent comma usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I started using your comma placement, which is much nicer, half way through and forgot to fixup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another wrinkle of comma placement, is that one notices that SQL consistently, uses longer command keywords, than the keywords for clauses:
SELECT
vsWHERE
vsORDER
vsFROM
;INSERT
vsVALUES
;UPDATE
vsWHERE
vsSET
, etc.This has the advantage of never requiring spaces before the end of strings being concatenated, since everything after the initial command keyword gets an extra space before it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a bit much imho, with the spacing that I added we actually get the same spacing as if we'd written it in the shell (
.schema
will show excessive spaces in theCREATE
statement for example, which is hard to read).