store: widen payments.kind to accept 'tip' so tips are stored
insert_payment has allowed kind = "tip" since the tip path landed, but the
payments table's CHECK only permitted invoice/receipt, so every tip insert
failed ("CHECK constraint failed: kind IN ('invoice','receipt')"). That made
the outbound tip row impossible and silently broke the inbound path too:
ingest_tip returned early and neither the row nor the [tip] chat line was
written.
- recreate the table with the widened CHECK on open, copying rows verbatim
(new stores get it from the schema directly)
- outbound tip() records kind = "tip" with the tip's memo, matching ingest_tip
- tests: outbound row, inbound round trip, pre-tip store migration
Refs kanban t_ecf204ad
This commit is contained in:
parent
24b5c93c43
commit
186ec7436d
2 changed files with 183 additions and 6 deletions
92
src/node.rs
92
src/node.rs
|
|
@ -624,7 +624,7 @@ impl Node {
|
|||
}
|
||||
|
||||
/// Pay selected friend via wallet RPC, then signed receipt.
|
||||
pub async fn tip(&self, friend_pk: &[u8], atomic: &str, _memo: &str) -> Result<(), String> {
|
||||
pub async fn tip(&self, friend_pk: &[u8], atomic: &str, memo: &str) -> Result<(), String> {
|
||||
if !self.wallet.configured() {
|
||||
return Err("wallet not connected — set ONIONWIRE_WALLET_RPC".into());
|
||||
}
|
||||
|
|
@ -646,20 +646,23 @@ impl Node {
|
|||
.map_err(|e| e.to_string())?;
|
||||
let rcp = self.sign_receipt(&txid, atomic, &address)?;
|
||||
let rcp_pt = pay::encode_receipt(&rcp);
|
||||
let tip = self.sign_tip(&txid, atomic, &address, _memo)?;
|
||||
let tip = self.sign_tip(&txid, atomic, &address, memo)?;
|
||||
let tip_pt = pay::encode_tip(&tip);
|
||||
self.send_once(friend_pk, &rcp_pt).await?;
|
||||
self.send_once(friend_pk, &tip_pt).await?;
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
// The user just *spent* this money: the row is a tip, matching the
|
||||
// inbound `ingest_tip` direction. The receipt frame above is the
|
||||
// recipient's copy of the payment, not the local one.
|
||||
store
|
||||
.insert_payment(
|
||||
friend_pk,
|
||||
PaymentWrite {
|
||||
dir: "out",
|
||||
kind: "receipt",
|
||||
kind: "tip",
|
||||
amount_atomic: atomic,
|
||||
address: &address,
|
||||
memo: "",
|
||||
memo,
|
||||
txid: Some(&txid),
|
||||
verified: true,
|
||||
},
|
||||
|
|
@ -679,7 +682,7 @@ impl Node {
|
|||
.append_message(
|
||||
friend_pk,
|
||||
"out",
|
||||
pay::tip_chat_line(atomic, _memo, false).as_bytes(),
|
||||
pay::tip_chat_line(atomic, memo, false).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
|
|
@ -1902,5 +1905,84 @@ mod tests {
|
|||
"the new invite still names the same identity"
|
||||
);
|
||||
}
|
||||
|
||||
/// An outbound tip must be recorded as a *tip*. The receipt row is the
|
||||
/// recipient's view of a receipt frame; for money the user just spent the
|
||||
/// wallet history wants the tip they paid. `ingest_tip` already stores
|
||||
/// `kind = "tip"` for the inbound direction.
|
||||
#[tokio::test]
|
||||
async fn outbound_tip_records_a_tip_payment_row() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let node = offline_node_with_wallet(
|
||||
&dir.path().join("bob"),
|
||||
Arc::new(DialOk),
|
||||
mock_wallet().await,
|
||||
);
|
||||
let peer = Keys::generate();
|
||||
let addr = xmr_addr();
|
||||
{
|
||||
let store = node.store.lock().unwrap();
|
||||
befriend(&store, &peer.identity_pk, &peer.prekey_pk, "peer.onion");
|
||||
let prf = crate::profile::sign(&peer.identity_sk, "peer", "", &addr, 1)
|
||||
.expect("sign peer profile");
|
||||
assert!(store.apply_profile(&peer.identity_pk, &prf).unwrap());
|
||||
}
|
||||
|
||||
node.tip(&peer.identity_pk, "500000000000", "lunch")
|
||||
.await
|
||||
.expect("tip");
|
||||
|
||||
let store = node.store.lock().unwrap();
|
||||
let rows = store.list_payments(&peer.identity_pk).expect("payments");
|
||||
assert_eq!(rows.len(), 1, "one row for one outbound tip");
|
||||
assert_eq!(rows[0].dir, "out");
|
||||
assert_eq!(rows[0].kind, "tip", "an outbound tip is stored as a tip");
|
||||
assert_eq!(rows[0].amount_atomic, "500000000000");
|
||||
assert_eq!(rows[0].txid.as_deref(), Some("abc123"));
|
||||
assert_eq!(rows[0].memo, "lunch");
|
||||
}
|
||||
|
||||
/// Same storage for the other direction: a tip signed by the peer lands as
|
||||
/// a `kind = "tip"` row plus the `[tip]` line. This is the path the
|
||||
/// payments CHECK constraint had been rejecting.
|
||||
#[tokio::test]
|
||||
async fn an_inbound_tip_is_recorded_and_shown() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let node = offline_node(&dir.path().join("bob"), Arc::new(NoDial));
|
||||
let peer = Keys::generate();
|
||||
befriend(
|
||||
&node.store.lock().unwrap(),
|
||||
&peer.identity_pk,
|
||||
&peer.prekey_pk,
|
||||
"peer.onion",
|
||||
);
|
||||
let tip = pay::sign_tip(
|
||||
&peer.identity_sk,
|
||||
"abc123",
|
||||
"500000000000",
|
||||
&xmr_addr(),
|
||||
"lunch",
|
||||
1,
|
||||
)
|
||||
.expect("sign peer tip");
|
||||
|
||||
node.ingest_tip(&peer.identity_pk, &pay::encode_tip(&tip))
|
||||
.await
|
||||
.expect("inbound tip must be stored");
|
||||
|
||||
assert_eq!(
|
||||
chat_lines(&node, &peer),
|
||||
vec![(
|
||||
"in".to_string(),
|
||||
"[tip] 0.5 XMR — lunch (pending)".to_string()
|
||||
)],
|
||||
"the receiver's chat shows the tip that arrived"
|
||||
);
|
||||
let rows = node.store.lock().unwrap().list_payments(&peer.identity_pk);
|
||||
let rows = rows.expect("payments");
|
||||
assert_eq!(rows.len(), 1, "one row for one inbound tip");
|
||||
assert_eq!(rows[0].dir, "in");
|
||||
assert_eq!(rows[0].kind, "tip");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
97
src/store.rs
97
src/store.rs
|
|
@ -261,7 +261,7 @@ impl Store {
|
|||
id INTEGER PRIMARY KEY,
|
||||
friend_id INTEGER NOT NULL REFERENCES friends(id),
|
||||
dir TEXT NOT NULL CHECK(dir IN ('in','out')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt','tip')),
|
||||
amount_atomic TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
memo TEXT NOT NULL DEFAULT '',
|
||||
|
|
@ -365,6 +365,51 @@ impl Store {
|
|||
"payload",
|
||||
"ALTER TABLE outbox ADD COLUMN payload BLOB",
|
||||
)?;
|
||||
// `insert_payment` has accepted `kind = "tip"` since the tip path
|
||||
// landed, but the table's CHECK only allowed invoice/receipt, so every
|
||||
// tip insert (inbound *and* outbound) failed the constraint and the
|
||||
// frame was dropped. SQLite cannot alter a CHECK, so rebuild.
|
||||
self.widen_payment_kinds()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Widen `payments.kind` to accept `'tip'` on stores created before the
|
||||
/// tip path. The table is rebuilt in place because SQLite has no
|
||||
/// `ALTER TABLE ... DROP CONSTRAINT`; ids and rows are preserved verbatim.
|
||||
fn widen_payment_kinds(&self) -> Result<()> {
|
||||
let schema: Option<String> = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'payments'",
|
||||
[],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
let Some(schema) = schema else {
|
||||
return Ok(());
|
||||
};
|
||||
if schema.contains("'tip'") {
|
||||
return Ok(());
|
||||
}
|
||||
self.conn.execute_batch(
|
||||
"ALTER TABLE payments RENAME TO payments_pre_tip;
|
||||
CREATE TABLE payments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
friend_id INTEGER NOT NULL REFERENCES friends(id),
|
||||
dir TEXT NOT NULL CHECK(dir IN ('in','out')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt','tip')),
|
||||
amount_atomic TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
memo TEXT NOT NULL DEFAULT '',
|
||||
txid TEXT,
|
||||
verified INTEGER NOT NULL DEFAULT 0,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
INSERT INTO payments (id, friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at)
|
||||
SELECT id, friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at
|
||||
FROM payments_pre_tip;
|
||||
DROP TABLE payments_pre_tip;",
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -1940,6 +1985,56 @@ mod at_rest {
|
|||
}
|
||||
}
|
||||
|
||||
/// The old `payments.kind` CHECK allowed only invoice/receipt, so every
|
||||
/// `kind = "tip"` insert (inbound and outbound) failed the constraint.
|
||||
/// Opening an existing store must widen it without losing the rows in it.
|
||||
#[test]
|
||||
fn a_store_predating_tip_payments_is_widened_on_open() {
|
||||
const ADDR: &str = "4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge";
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let db_path = dir.path().join("onionwire.db");
|
||||
write_legacy_plaintext_store(&db_path, "tip-pass");
|
||||
{
|
||||
let conn = Connection::open(&db_path).unwrap();
|
||||
let schema: String = conn
|
||||
.query_row(
|
||||
"SELECT sql FROM sqlite_master WHERE name = 'payments'",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!schema.contains("'tip'"), "fixture is the pre-tip table");
|
||||
conn.execute(
|
||||
"INSERT INTO payments (friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at)
|
||||
VALUES (1, 'out', 'invoice', '1000000000000', ?1, 'm', NULL, 0, 7)",
|
||||
params![ADDR],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "tip-pass").expect("open+upgrade");
|
||||
let rows = store.list_payments(&LEGACY_FRIEND_PK).unwrap();
|
||||
assert_eq!(rows.len(), 1, "the pre-tip row survives the rebuild");
|
||||
assert_eq!(rows[0].kind, "invoice");
|
||||
assert_eq!(rows[0].amount_atomic, "1000000000000");
|
||||
assert_eq!(rows[0].created_at, 7, "rows are copied verbatim");
|
||||
|
||||
store
|
||||
.insert_payment(
|
||||
&LEGACY_FRIEND_PK,
|
||||
PaymentWrite {
|
||||
dir: "in",
|
||||
kind: "tip",
|
||||
amount_atomic: "500000000000",
|
||||
address: ADDR,
|
||||
memo: "lunch",
|
||||
txid: Some("abc123"),
|
||||
verified: false,
|
||||
},
|
||||
)
|
||||
.expect("a migrated store accepts the tip kind");
|
||||
}
|
||||
|
||||
/// `ratchet_sessions` keys on `friend_id`, and nothing in the crate deletes
|
||||
/// friends yet, so the cascade is exercised with the raw statement a future
|
||||
/// friend-removal path would run.
|
||||
|
|
|
|||
Loading…
Reference in a new issue