feat(pay): 收款模型改为单地址+唯一金额(#34/34A Phase A-C)

从"每单唯一 HD 地址"改为"单个固定收款地址 + 每单唯一金额",归集成本 O(订单数)→O(1)。
- store: pay_orders 加 user_ref/expect_amount(唯一金额)/matched_tx_id;新 orphan_payments 表;
  ActiveOrderByUser(同用户单订单)、AmountRecentlyUsed(迟到窗口内金额不复用)、TxHandled(幂等)、
  RecordOrphan。去掉每单派生游标。
- pay: CreateOrder(userRef,sku,priceMicro)——同用户单订单校验 + 分配唯一金额(base+随机微尾数[1,9999]、
  cooldown 内不复用),address 恒为收款地址。
- tron: Transfer 加 BlockTs(区块时间秒),取 block_timestamp。
- watcher: 单地址取到账,按"金额==expect && block_ts>建单"匹配 → paid;不匹配的到账 → orphan;幂等。
- httpapi: POST /order 加 user_ref,同用户重复 → 409;main 收款地址=PAY_RECEIVE_ADDRESS 或 xpub index0。
- 测试:唯一金额/同地址、同用户单订单、精确匹配、付错成孤儿、迟到不误配新单、付款早于建单不匹配、
  超时、幂等、409,全绿。README 更新为单地址模型+API(user_ref/精确金额/orphan)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-09 15:15:38 +08:00
parent 543a54c606
commit c5949a595a
11 changed files with 535 additions and 240 deletions
+136 -58
View File
@@ -9,81 +9,159 @@ import (
"github.com/wangjia/pangolin/pay/internal/tron"
)
type mockFetcher struct{ m map[string][]tron.Transfer }
const recvAddr = "TRecv00000000000000000000000000000A"
func (f *mockFetcher) IncomingTransfers(_ context.Context, addr string) ([]tron.Transfer, error) {
return f.m[addr], nil
type mockFetcher struct{ transfers []tron.Transfer }
func (f *mockFetcher) IncomingTransfers(_ context.Context, _ string) ([]tron.Transfer, error) {
return f.transfers, nil
}
func newPending(t *testing.T, st *store.Store, orderNo, addr string, amount int64, expires time.Time) {
func memStore(t *testing.T) *store.Store {
t.Helper()
st, err := store.Open(":memory:")
if err != nil {
t.Fatalf("store: %v", err)
}
t.Cleanup(func() { _ = st.Close() })
return st
}
func seed(t *testing.T, st *store.Store, no string, amount int64, created time.Time) {
t.Helper()
o := &store.Order{
OrderNo: orderNo, SKU: "pro", ExpectAmount: amount, Address: addr,
Status: store.StatusPending, CreatedAt: time.Unix(1_700_000_000, 0), ExpiresAt: expires,
OrderNo: no, UserRef: "u", SKU: "pro", ExpectAmount: amount, Address: recvAddr,
Status: store.StatusPending, CreatedAt: created, ExpiresAt: created.Add(time.Hour),
}
if err := st.CreateOrder(context.Background(), o); err != nil {
t.Fatalf("seed order: %v", err)
t.Fatalf("seed: %v", err)
}
}
func TestWatcherMarksPaidOnSufficientTransfer(t *testing.T) {
st, _ := store.Open(":memory:")
t.Cleanup(func() { _ = st.Close() })
func TestWatcherMatchesByAmountAndTime(t *testing.T) {
st := memStore(t)
ctx := context.Background()
now := time.Unix(1_700_000_100, 0)
created := now.Add(-5 * time.Minute)
seed(t, st, "PAY1", 5_000017, created)
seed(t, st, "PAY2", 5_000018, created)
newPending(t, st, "PAY1", "TADDR1", 5_000000, now.Add(time.Hour))
fetch := &mockFetcher{m: map[string][]tron.Transfer{}}
w := New(st, fetch, nil)
w.now = func() time.Time { return now }
// No transfer yet -> stays pending.
if err := w.Tick(ctx); err != nil {
t.Fatalf("tick1: %v", err)
}
if o, _ := st.GetOrder(ctx, "PAY1"); o.Status != store.StatusPending {
t.Fatalf("want pending, got %s", o.Status)
}
// Underpayment -> still pending.
fetch.m["TADDR1"] = []tron.Transfer{{TxID: "tx-under", To: "TADDR1", Value: 4_000000}}
_ = w.Tick(ctx)
if o, _ := st.GetOrder(ctx, "PAY1"); o.Status != store.StatusPending {
t.Fatalf("underpay should stay pending, got %s", o.Status)
}
// Sufficient payment -> paid, tx recorded.
fetch.m["TADDR1"] = []tron.Transfer{{TxID: "tx-ok", To: "TADDR1", Value: 5_000000}}
_ = w.Tick(ctx)
o, _ := st.GetOrder(ctx, "PAY1")
if o.Status != store.StatusPaid || o.TxID != "tx-ok" {
t.Fatalf("want paid/tx-ok, got %s/%s", o.Status, o.TxID)
}
// Idempotent: another tick with same transfer doesn't error or flip anything.
if err := w.Tick(ctx); err != nil {
t.Fatalf("idempotent tick: %v", err)
}
o, _ = st.GetOrder(ctx, "PAY1")
if o.Status != store.StatusPaid || o.TxID != "tx-ok" {
t.Fatalf("idempotency broken: %s/%s", o.Status, o.TxID)
}
}
func TestWatcherExpiresOverdue(t *testing.T) {
st, _ := store.Open(":memory:")
t.Cleanup(func() { _ = st.Close() })
ctx := context.Background()
now := time.Unix(1_700_000_100, 0)
newPending(t, st, "OLD", "TADDR2", 1_000000, now.Add(-time.Minute)) // already overdue
w := New(st, &mockFetcher{m: map[string][]tron.Transfer{}}, nil)
fetch := &mockFetcher{transfers: []tron.Transfer{
{TxID: "tx1", To: recvAddr, Value: 5_000017, BlockTs: created.Add(time.Minute).Unix()},
}}
w := New(st, fetch, recvAddr, nil)
w.now = func() time.Time { return now }
if err := w.Tick(ctx); err != nil {
t.Fatalf("tick: %v", err)
}
if o, _ := st.GetOrder(ctx, "OLD"); o.Status != store.StatusExpired {
t.Fatalf("want expired, got %s", o.Status)
o1, _ := st.GetOrder(ctx, "PAY1")
if o1.Status != store.StatusPaid || o1.TxID != "tx1" {
t.Fatalf("PAY1 %s/%s", o1.Status, o1.TxID)
}
o2, _ := st.GetOrder(ctx, "PAY2")
if o2.Status != store.StatusPending {
t.Fatalf("PAY2 should stay pending, got %s", o2.Status)
}
if err := w.Tick(ctx); err != nil { // idempotent
t.Fatalf("tick2: %v", err)
}
o1, _ = st.GetOrder(ctx, "PAY1")
if o1.Status != store.StatusPaid || o1.TxID != "tx1" {
t.Fatal("idempotency broken")
}
}
func TestWatcherWrongAmountIsOrphan(t *testing.T) {
st := memStore(t)
ctx := context.Background()
now := time.Unix(1_700_000_100, 0)
created := now.Add(-5 * time.Minute)
seed(t, st, "PAY1", 5_000017, created)
fetch := &mockFetcher{transfers: []tron.Transfer{
{TxID: "tx-wrong", To: recvAddr, Value: 5_000000, BlockTs: created.Add(time.Minute).Unix()},
}}
w := New(st, fetch, recvAddr, nil)
w.now = func() time.Time { return now }
_ = w.Tick(ctx)
o, _ := st.GetOrder(ctx, "PAY1")
if o.Status != store.StatusPending {
t.Fatalf("PAY1 should stay pending, got %s", o.Status)
}
if h, _ := st.TxHandled(ctx, "tx-wrong"); !h {
t.Fatal("wrong-amount payment should be recorded as orphan")
}
}
func TestWatcherLatePaymentDoesNotMatchNewOrder(t *testing.T) {
// Order1 (amount 5_000017) expired; a NEW order (amount 5_000018) is now active
// on the SAME address. A late payment of the OLD amount must NOT match the new
// order (different amount) -> orphan.
st := memStore(t)
ctx := context.Background()
now := time.Unix(1_700_000_500, 0)
seed(t, st, "PAY2", 5_000018, now.Add(-time.Minute))
fetch := &mockFetcher{transfers: []tron.Transfer{
{TxID: "tx-late", To: recvAddr, Value: 5_000017, BlockTs: now.Unix()},
}}
w := New(st, fetch, recvAddr, nil)
w.now = func() time.Time { return now }
_ = w.Tick(ctx)
o2, _ := st.GetOrder(ctx, "PAY2")
if o2.Status != store.StatusPending {
t.Fatalf("PAY2 must not be matched by a wrong-amount late payment, got %s", o2.Status)
}
if h, _ := st.TxHandled(ctx, "tx-late"); !h {
t.Fatal("late payment should be orphan")
}
}
func TestWatcherIgnoresPaymentBeforeOrder(t *testing.T) {
// A payment whose block time is BEFORE the order was created must not match
// (guards address reuse: prior balance / old tx).
st := memStore(t)
ctx := context.Background()
now := time.Unix(1_700_000_500, 0)
created := now.Add(-2 * time.Minute)
seed(t, st, "PAY1", 5_000017, created)
fetch := &mockFetcher{transfers: []tron.Transfer{
{TxID: "tx-old", To: recvAddr, Value: 5_000017, BlockTs: created.Add(-time.Minute).Unix()},
}}
w := New(st, fetch, recvAddr, nil)
w.now = func() time.Time { return now }
_ = w.Tick(ctx)
o, _ := st.GetOrder(ctx, "PAY1")
if o.Status != store.StatusPending {
t.Fatalf("payment before order must not match, got %s", o.Status)
}
if h, _ := st.TxHandled(ctx, "tx-old"); !h {
t.Fatal("pre-order payment should be orphan")
}
}
func TestWatcherExpires(t *testing.T) {
st := memStore(t)
ctx := context.Background()
now := time.Unix(1_700_000_500, 0)
o := &store.Order{
OrderNo: "OLD", UserRef: "u", SKU: "pro", ExpectAmount: 1, Address: recvAddr,
Status: store.StatusPending, CreatedAt: now.Add(-time.Hour), ExpiresAt: now.Add(-time.Minute),
}
_ = st.CreateOrder(ctx, o)
w := New(st, &mockFetcher{}, recvAddr, nil)
w.now = func() time.Time { return now }
_ = w.Tick(ctx)
got, _ := st.GetOrder(ctx, "OLD")
if got.Status != store.StatusExpired {
t.Fatalf("want expired, got %s", got.Status)
}
}