fix: broken SQL statement in wallet db_set_utxo

I discovered this accidentally when using the `tests/plugins/dblog.py`
plugin on another testcase: tests/test_connection.py::test_fail_unconfirmed

There the plugin/hook crashes because it can't execute the statement:
```json
{
  "jsonrpc": "2.0",
  "id": 34,
  "error": {
    "code": -32600,
    "message": "Error while processing db_write: unrecognized token: \"174WHERE\"",
    "traceback": "Traceback (most recent call last):\n  File \"/home/will/projects/lightning.git/contrib/pyln-client/pyln/client/plugin.py\", line 535, in _dispatch_request\n    result = self._exec_func(method.func, request)\n  File \"/home/will/projects/lightning.git/contrib/pyln-client/pyln/client/plugin.py\", line 520, in _exec_func\n    return func(*ba.args, **ba.kwargs)\n  File \"/home/will/projects/lightning.git/tests/plugins/dblog.py\", line 45, in db_write\n    plugin.conn.execute(c)\nsqlite3.OperationalError: unrecognized token: \"174WHERE\"\n"
  }
}
```

Changelog-Fixed: plugin: Regression with SQL statement expansion that could result in invalid statements being passed to the `db_write` hook.
This commit is contained in:
Michael Schmoock 2020-09-24 14:43:31 +02:00 committed by Rusty Russell
parent 711133e3c7
commit 011590b20e
4 changed files with 9 additions and 9 deletions

View File

@ -1053,8 +1053,8 @@ struct db_query db_postgres_queries[] = {
.readonly = true,
},
{
.name = "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?",
.query = "UPDATE outputs SET status=$1, reserved_til=$2WHERE prev_out_tx=$3 AND prev_out_index=$4",
.name = "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?",
.query = "UPDATE outputs SET status=$1, reserved_til=$2 WHERE prev_out_tx=$3 AND prev_out_index=$4",
.placeholders = 4,
.readonly = false,
},
@ -1654,4 +1654,4 @@ struct db_query db_postgres_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */
// SHA256STAMP:fa191e6b54c56b2d4e85a24ca83bc0d41dddec5dbde948e2bc5d627426089ade
// SHA256STAMP:5fa02e1dbf8ea4e155e08671e9f36b66c8a5b8ad04226e6695eacf2ea423b8bd

View File

@ -1053,8 +1053,8 @@ struct db_query db_sqlite3_queries[] = {
.readonly = true,
},
{
.name = "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?",
.query = "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?",
.name = "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?",
.query = "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?",
.placeholders = 4,
.readonly = false,
},
@ -1654,4 +1654,4 @@ struct db_query db_sqlite3_queries[] = {
#endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */
// SHA256STAMP:fa191e6b54c56b2d4e85a24ca83bc0d41dddec5dbde948e2bc5d627426089ade
// SHA256STAMP:5fa02e1dbf8ea4e155e08671e9f36b66c8a5b8ad04226e6695eacf2ea423b8bd

View File

@ -695,7 +695,7 @@ msgid "SELECT prev_out_tx, prev_out_index, value, type, status, keyindex, chann
msgstr ""
#: wallet/wallet.c:417
msgid "UPDATE outputs SET status=?, reserved_til=?WHERE prev_out_tx=? AND prev_out_index=?"
msgid "UPDATE outputs SET status=?, reserved_til=? WHERE prev_out_tx=? AND prev_out_index=?"
msgstr ""
#: wallet/wallet.c:502
@ -1089,4 +1089,4 @@ msgstr ""
#: wallet/test/run-wallet.c:1359
msgid "INSERT INTO channels (id) VALUES (1);"
msgstr ""
# SHA256STAMP:fb0b381451867f44c3f35f2202e741a06ff9f531329facf6e9df302036eff4e4
# SHA256STAMP:c44c0588e8f2139e6530e926acbd3186bbbd65bec871d5274c8480fd8dcbd519

View File

@ -414,7 +414,7 @@ static void db_set_utxo(struct db *db, const struct utxo *utxo)
assert(!utxo->reserved_til);
stmt = db_prepare_v2(
db, SQL("UPDATE outputs SET status=?, reserved_til=?"
db, SQL("UPDATE outputs SET status=?, reserved_til=? "
"WHERE prev_out_tx=? AND prev_out_index=?"));
db_bind_int(stmt, 0, output_status_in_db(utxo->status));
db_bind_int(stmt, 1, utxo->reserved_til);