c5949a595a
从"每单唯一 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>
168 lines
4.9 KiB
Go
168 lines
4.9 KiB
Go
package watcher
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/wangjia/pangolin/pay/internal/store"
|
|
"github.com/wangjia/pangolin/pay/internal/tron"
|
|
)
|
|
|
|
const recvAddr = "TRecv00000000000000000000000000000A"
|
|
|
|
type mockFetcher struct{ transfers []tron.Transfer }
|
|
|
|
func (f *mockFetcher) IncomingTransfers(_ context.Context, _ string) ([]tron.Transfer, error) {
|
|
return f.transfers, nil
|
|
}
|
|
|
|
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: 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: %v", err)
|
|
}
|
|
}
|
|
|
|
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)
|
|
|
|
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)
|
|
}
|
|
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)
|
|
}
|
|
}
|