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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue