Files
vupay/app/task/ton_parse_test.go
admin a950fd2ead
Docker Build (Gitea) / Build and Push Docker Image (push) Successful in 47s
rebrand: BEpusdt → VUPAY
展示层与标识符全量改名:
- module github.com/v03413/bepusdt → git.viaeon.com/admin/vupay(含全部 import)
- 二进制/镜像/容器/DB 名 bepusdt → vupay(Dockerfile、workflow IMAGE、compose、docs)
- 显示名 BEpusdt → VUPAY(前端 title、后端 banner、version 命令、通知文案)
- 默认路径 /var/lib/bepusdt → /var/lib/vupay,/var/log/bepusdt → /var/log/vupay
- Telegram 通知的频道按钮原来指向上游 t.me 群,改成站点链接(否则改名后是死链)

刻意保留(运行语义,改了会坏):
- api_type 取值 "epusdt" / "epusdt_order"(库里存量数据按此匹配)
- 表前缀 bep_(bep_order / bep_conf / …,改需 RENAME TABLE 迁移)
- 签名函数 EpusdtSign 与 handler 包名 epusdt(对外协议语义,非品牌)
- 所有加密/会话派生的字面量原样不动

验证:gofmt 通过、go build ./... 通过、全仓 grep -i bepusdt 归零
2026-11-25 09:06:06 +08:00

125 lines
4.2 KiB
Go

package task
import (
"testing"
"git.viaeon.com/admin/vupay/app/model"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
tgo "github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/tvm/cell"
)
var (
tonTestSrc = address.NewAddress(0, 0, make([]byte, 32))
tonTestDst = address.NewAddress(0, 0, append(make([]byte, 31), 1))
)
func tonTestTx(body *cell.Cell, nano uint64, dsc any) *tlb.Transaction {
tx := &tlb.Transaction{AccountAddr: tonTestDst.Data(), Now: 1700000000, Hash: []byte{0xab}, Description: dsc}
tx.IO.In = &tlb.Message{MsgType: tlb.MsgTypeInternal, Msg: &tlb.InternalMessage{
SrcAddr: tonTestSrc,
DstAddr: tonTestDst,
Amount: tlb.FromNanoTONU(nano),
Body: body,
}}
return tx
}
func tonTestOrdinary(success bool) tlb.TransactionDescriptionOrdinary {
return tlb.TransactionDescriptionOrdinary{
ComputePhase: tlb.ComputePhase{Phase: tlb.ComputePhaseVM{Success: success}},
ActionPhase: &tlb.ActionPhase{Success: success},
}
}
func tonTestJettonBody(op uint32, amount uint64) *cell.Cell {
return cell.BeginCell().
MustStoreUInt(uint64(op), 32).
MustStoreUInt(1, 64).
MustStoreCoins(amount).
MustStoreAddr(tonTestSrc).
EndCell()
}
func TestParseTonTransfer(t *testing.T) {
empty := cell.BeginCell().EndCell()
skipped := tlb.TransactionDescriptionOrdinary{
ComputePhase: tlb.ComputePhase{Phase: tlb.ComputePhaseSkipped{}},
Aborted: true,
}
returned := skipped
returned.BouncePhase = &tlb.BouncePhase{Phase: tlb.BouncePhaseOk{}}
noFunds := skipped
noFunds.BouncePhase = &tlb.BouncePhase{Phase: tlb.BouncePhaseNoFunds{}}
bouncedIn := tonTestTx(empty, 1e9, tonTestOrdinary(true))
bouncedIn.IO.In.Msg.(*tlb.InternalMessage).Bounced = true
external := tonTestTx(empty, 1e9, tonTestOrdinary(true))
external.IO.In.Msg = &tlb.ExternalMessage{}
noIn := tonTestTx(empty, 1e9, tonTestOrdinary(true))
noIn.IO.In = nil
cases := []struct {
name string
tx *tlb.Transaction
ok bool
}{
{"plain", tonTestTx(empty, 1e9, tonTestOrdinary(true)), true},
{"uninit account", tonTestTx(empty, 1e9, skipped), true},
{"uninit account, bounce unpaid", tonTestTx(empty, 1e9, noFunds), true},
{"returned to sender", tonTestTx(empty, 1e9, returned), false},
{"zero amount", tonTestTx(empty, 0, tonTestOrdinary(true)), false},
{"with body", tonTestTx(tonTestJettonBody(tonOpInternalTransfer, 1), 1e9, tonTestOrdinary(true)), false},
{"bounced inbound", bouncedIn, false},
{"external inbound", external, false},
{"no inbound", noIn, false},
{"tick tock", tonTestTx(empty, 1e9, tlb.TransactionDescriptionTickTock{}), false},
}
for _, c := range cases {
got, ok := (&ton{}).parseTonTransfer(c.tx, 100)
if ok != c.ok {
t.Errorf("%s: ok = %v, want %v", c.name, ok, c.ok)
}
if !ok {
continue
}
if got.TradeType != model.TonGram || got.Amount.String() != "1" || got.BlockNum != 100 {
t.Errorf("%s: unexpected transfer %+v", c.name, got)
}
if got.RecvAddress != tonTestDst.Bounce(false).String() || got.FromAddress != tonTestSrc.Bounce(false).String() {
t.Errorf("%s: unexpected address %s <- %s", c.name, got.RecvAddress, got.FromAddress)
}
}
}
func TestParseTonInternalTransfer(t *testing.T) {
shard := &tgo.BlockIDExt{Workchain: 0}
cases := []struct {
name string
tx *tlb.Transaction
ok bool
}{
{"jetton received", tonTestTx(tonTestJettonBody(tonOpInternalTransfer, 2_500_000), 1, tonTestOrdinary(true)), true},
{"other op", tonTestTx(tonTestJettonBody(0x7362d09c, 2_500_000), 1, tonTestOrdinary(true)), false},
{"execution failed", tonTestTx(tonTestJettonBody(tonOpInternalTransfer, 2_500_000), 1, tonTestOrdinary(false)), false},
{"zero amount", tonTestTx(tonTestJettonBody(tonOpInternalTransfer, 0), 1, tonTestOrdinary(true)), false},
{"empty body", tonTestTx(cell.BeginCell().EndCell(), 1, tonTestOrdinary(true)), false},
}
for _, c := range cases {
got, ok := (&ton{}).parseInternalTransfer(shard, c.tx, 100)
if ok != c.ok {
t.Errorf("%s: ok = %v, want %v", c.name, ok, c.ok)
}
if !ok {
continue
}
if got.TradeType != model.UsdtTon || got.Amount.String() != "2.5" || got.RecvAddress != tonTestDst.Bounce(false).String() {
t.Errorf("%s: unexpected transfer %+v", c.name, got)
}
}
}