feat: 重做协作台账状态机并支持充值核账
This commit is contained in:
+96
-14
@@ -41,8 +41,15 @@ def _strip_tail(body: bytes) -> bytes:
|
||||
return body
|
||||
|
||||
|
||||
def _decode_taf_value(ins: TafInputStream, dtype: int, depth: int = 0) -> object:
|
||||
"""递归解码单个 TAF 值"""
|
||||
def _decode_taf_value(
|
||||
ins: TafInputStream,
|
||||
dtype: int,
|
||||
depth: int = 0,
|
||||
*,
|
||||
max_depth: int = 5,
|
||||
keep_bytes: bool = False,
|
||||
) -> object:
|
||||
"""递归解码单个 TAF 值。"""
|
||||
if dtype == TafType.ZERO:
|
||||
return 0
|
||||
if dtype in (TafType.INT8, TafType.INT16, TafType.INT32, TafType.INT64):
|
||||
@@ -66,9 +73,21 @@ def _decode_taf_value(ins: TafInputStream, dtype: int, depth: int = 0) -> object
|
||||
m = {}
|
||||
for _ in range(cnt):
|
||||
_, kt = ins.read_head()
|
||||
k = _decode_taf_value(ins, kt, depth + 1)
|
||||
k = _decode_taf_value(
|
||||
ins,
|
||||
kt,
|
||||
depth + 1,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
_, vt = ins.read_head()
|
||||
v = _decode_taf_value(ins, vt, depth + 1)
|
||||
v = _decode_taf_value(
|
||||
ins,
|
||||
vt,
|
||||
depth + 1,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
m[k] = v
|
||||
return m
|
||||
if dtype == TafType.LIST:
|
||||
@@ -76,19 +95,40 @@ def _decode_taf_value(ins: TafInputStream, dtype: int, depth: int = 0) -> object
|
||||
items = []
|
||||
for _ in range(cnt):
|
||||
_, it = ins.read_head()
|
||||
items.append(_decode_taf_value(ins, it, depth + 1))
|
||||
items.append(
|
||||
_decode_taf_value(
|
||||
ins,
|
||||
it,
|
||||
depth + 1,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
)
|
||||
return items
|
||||
if dtype == TafType.SIMPLE_LIST:
|
||||
ins.read_head() # element type INT8
|
||||
ln = ins._read_int_len()
|
||||
if keep_bytes:
|
||||
return ins.buf.read(ln)
|
||||
return f"<bytes {ln}B>"
|
||||
if dtype == TafType.STRUCT_BEGIN:
|
||||
return _decode_taf_struct(ins, depth + 1)
|
||||
return _decode_taf_struct(
|
||||
ins,
|
||||
depth + 1,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
return f"<0x{dtype:02x}>"
|
||||
|
||||
|
||||
def _decode_taf_struct(ins: TafInputStream, depth: int = 0) -> dict:
|
||||
"""解码一个 TAF 结构体 (已消费 STRUCT_BEGIN) → 有序字典"""
|
||||
def _decode_taf_struct(
|
||||
ins: TafInputStream,
|
||||
depth: int = 0,
|
||||
*,
|
||||
max_depth: int = 5,
|
||||
keep_bytes: bool = False,
|
||||
) -> dict:
|
||||
"""解码一个 TAF 结构体 (已消费 STRUCT_BEGIN) → 有序字典。"""
|
||||
fields = {}
|
||||
while True:
|
||||
try:
|
||||
@@ -99,10 +139,17 @@ def _decode_taf_struct(ins: TafInputStream, depth: int = 0) -> dict:
|
||||
ins.read_head()
|
||||
break
|
||||
ins.read_head()
|
||||
# 限制递归深度
|
||||
if depth < 5:
|
||||
# 默认只解 5 层,避免日志/通用接口被超深结构拖垮;
|
||||
# queryAllocation 这类需要完整商品字段的调用方显式传 max_depth。
|
||||
if depth < max_depth:
|
||||
try:
|
||||
val = _decode_taf_value(ins, dtype, depth)
|
||||
val = _decode_taf_value(
|
||||
ins,
|
||||
dtype,
|
||||
depth,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
val = f"<decode_err:0x{dtype:02x}>"
|
||||
else:
|
||||
@@ -215,11 +262,16 @@ def _decode_wup_body(body: bytes) -> dict:
|
||||
return result
|
||||
|
||||
|
||||
def decode_struct_bytes(data: bytes) -> dict:
|
||||
def decode_struct_bytes(
|
||||
data: bytes,
|
||||
*,
|
||||
max_depth: int = 5,
|
||||
keep_bytes: bool = False,
|
||||
) -> dict:
|
||||
"""把裸结构体字节解成通用 dict(``tagN`` → 值);非结构体或解析失败返回 {}。
|
||||
|
||||
WSS 与 HTTP 两条通道都要用「未逐字段建模」的通用解码,统一放在这里,
|
||||
避免两边各写一份。
|
||||
避免两边各写一份。``max_depth`` 默认保持 5 层;需要完整商品字段时显式传大值。
|
||||
"""
|
||||
if not isinstance(data, bytes) or not data:
|
||||
return {}
|
||||
@@ -229,11 +281,41 @@ def decode_struct_bytes(data: bytes) -> dict:
|
||||
if dtype != TafType.STRUCT_BEGIN:
|
||||
return {}
|
||||
ins.read_head()
|
||||
return _decode_taf_struct(ins)
|
||||
return _decode_taf_struct(
|
||||
ins,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return {}
|
||||
|
||||
|
||||
def decode_value_bytes(
|
||||
data: bytes,
|
||||
*,
|
||||
max_depth: int = 40,
|
||||
keep_bytes: bool = True,
|
||||
) -> object:
|
||||
"""把裸 TAF 值(LIST / STRUCT / MAP 等,含 tag+type 头)解成通用对象。
|
||||
|
||||
``queryAllocation`` 的商品列表放在 SIMPLE_LIST 里,内容本身又是一个 TAF LIST;
|
||||
这里专门给这种嵌套结构用,默认保留字节、放宽深度。
|
||||
"""
|
||||
if not isinstance(data, bytes) or not data:
|
||||
return None
|
||||
ins = TafInputStream(data)
|
||||
try:
|
||||
_tag, dtype = ins.read_head()
|
||||
return _decode_taf_value(
|
||||
ins,
|
||||
dtype,
|
||||
max_depth=max_depth,
|
||||
keep_bytes=keep_bytes,
|
||||
)
|
||||
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
return None
|
||||
|
||||
|
||||
def _truncate(obj, max_str=80):
|
||||
"""截断过长的值,使日志紧凑"""
|
||||
if isinstance(obj, str):
|
||||
|
||||
Reference in New Issue
Block a user