- 纠正国密链提交方法为自有 sendTransaction(4 参),eth_sendRawTransaction 在国密链禁用 - 新增 GmSSL 3.2.0 环回真 TLCP 握手自测:双证书、国密套件、应用数据往返、负向期望拒绝特征 - 落地 build_optee.sh TA 签名密钥三级硬拒绝,缺 SDK 时判据仍可被执行到 - 新增 FISCO 真机对拍契约:缺环境显式 SKIPPED 并逐项列出,环境齐备只报 NEEDS_IMPL - 同步 catalog 守卫 / 集成文档 / CLAUDE.md,主门禁 15 条改为 16 条
451 lines
19 KiB
Python
451 lines
19 KiB
Python
#!/usr/bin/env python3
|
||
"""链上层(阶段 2 形状层)端到端桩自测 —— 主门禁第 14 条。
|
||
|
||
# 判的是什么
|
||
|
||
本机没有 FISCO BCOS 节点,也没有国密模组。这里用 Python 假 bcos-rpc
|
||
跑完整链路,判的是**框架自己的契约**,不是 FISCO 的实现:
|
||
|
||
1. 链上对象规范编码:确定性、字段敏感、域分隔、SM3 摘要;
|
||
2. 配置纪律:`sm_crypto` 必须显式 true、`client_tlcp=true` 是合法声明
|
||
# (生产路径的 TLCP 准入由 `flush --transport tlcp` 负责)、缺字段即拒;
|
||
3. outbox:幂等(同键不新增不覆盖)、fail-open(链挂仍入队)、补链不重发;
|
||
4. JSON-RPC 判定:HTTP 200 + error 字段必须判失败、缺 result / 串 id
|
||
都不能当成成功、方法名与 3.x 源码一致;
|
||
5. 准入:fail-closed(缓存缺失 / 过期 / 硬规则失败 / 非成员一律拒绝);
|
||
6. 每条负向带**期望的拒绝特征**且必须匹配 —— 只判「失败」分不清
|
||
按设计拒绝和碰巧崩了。
|
||
|
||
# ★ 不判什么(诚实边界)
|
||
|
||
`flush` 发出去的是**链上对象规范字节**,不是已签名交易:FISCO 国密交易
|
||
RLP 结构与 Tongsuo TLCP 握手 FFI 仍未落地。SM2 签名本身已在本轮用
|
||
OpenSSL 真做(见 `tools/selftest-chain-gm.py`),但签完还要打包成交易。
|
||
所以这里不宣称「能上链」,只宣称补链与传输准入契约成立。
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import shutil
|
||
import socket
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
import threading
|
||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||
|
||
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
BIN = os.path.join(REPO, "target", "debug", "mpc")
|
||
|
||
PASS = 0
|
||
FAIL = 0
|
||
|
||
|
||
def check(name, cond, detail=""):
|
||
global PASS, FAIL
|
||
if cond:
|
||
print(" ok", name)
|
||
PASS += 1
|
||
else:
|
||
print("FAIL", name, "--", str(detail)[:300])
|
||
FAIL += 1
|
||
|
||
|
||
def run(*args):
|
||
p = subprocess.run(
|
||
[BIN, *args], capture_output=True, text=True, timeout=40, cwd=REPO
|
||
)
|
||
return p.returncode, p.stdout.strip(), p.stderr.strip()
|
||
|
||
|
||
def expect_reject(name, res, needle):
|
||
rc, out, err = res
|
||
all_text = out + "\n" + err
|
||
check(name, rc != 0 and needle in all_text, f"rc={rc} needle={needle!r} got={all_text[:220]!r}")
|
||
|
||
|
||
class RpcState:
|
||
def __init__(self):
|
||
self.mode = "ok"
|
||
self.calls = []
|
||
self.lock = threading.Lock()
|
||
|
||
def set_mode(self, mode):
|
||
with self.lock:
|
||
self.mode = mode
|
||
self.calls = []
|
||
|
||
|
||
STATE = RpcState()
|
||
|
||
|
||
class Handler(BaseHTTPRequestHandler):
|
||
protocol_version = "HTTP/1.1"
|
||
|
||
def log_message(self, *args):
|
||
pass
|
||
|
||
def do_POST(self):
|
||
n = int(self.headers.get("Content-Length", "0"))
|
||
raw = self.rfile.read(n)
|
||
try:
|
||
req = json.loads(raw)
|
||
except Exception:
|
||
req = {}
|
||
with STATE.lock:
|
||
STATE.calls.append((req.get("method"), req.get("params"), req.get("id")))
|
||
mode = STATE.mode
|
||
rid = req.get("id", 1)
|
||
body = {"jsonrpc": "2.0", "id": rid}
|
||
if mode == "ok":
|
||
body["result"] = "0xdeadbeef"
|
||
elif mode == "error":
|
||
body["error"] = {"code": -32000, "message": "invalid signature"}
|
||
elif mode == "null":
|
||
body["result"] = None
|
||
elif mode == "missing_result":
|
||
pass
|
||
elif mode == "bad_id":
|
||
body["id"] = rid + 1000
|
||
body["result"] = "0xdeadbeef"
|
||
else:
|
||
raise ValueError(mode)
|
||
payload = json.dumps(body).encode()
|
||
self.send_response(200)
|
||
self.send_header("Content-Type", "application/json")
|
||
self.send_header("Content-Length", str(len(payload)))
|
||
self.send_header("Connection", "close")
|
||
self.end_headers()
|
||
self.wfile.write(payload)
|
||
|
||
|
||
def write(path, obj):
|
||
with open(path, "w", encoding="utf-8") as f:
|
||
json.dump(obj, f)
|
||
|
||
|
||
def config(port, **over):
|
||
c = {
|
||
"rpc_endpoint": f"127.0.0.1:{port}",
|
||
"group_id": "group0",
|
||
"chain_id": "chain0",
|
||
"sm_crypto": True,
|
||
"client_tlcp": False,
|
||
}
|
||
c.update(over)
|
||
return c
|
||
|
||
|
||
def receipt(**over):
|
||
r = {
|
||
"order_id": "order-1",
|
||
"decision_id": "dec-1",
|
||
"result_hash": "06" * 32,
|
||
"input_hash": "07" * 32,
|
||
"user_data": "08" * 64,
|
||
"attest": {
|
||
"user_data_ok": True,
|
||
"meas_ok": True,
|
||
"tcb_ok": True,
|
||
"chip_sig": False,
|
||
},
|
||
"link": "p2p",
|
||
"link_binding": "09" * 32,
|
||
"signatures": [],
|
||
}
|
||
r.update(over)
|
||
return r
|
||
|
||
|
||
def main():
|
||
if not os.path.exists(BIN):
|
||
print("缺少二进制:", BIN, "先跑 cargo build -p mpc-cli")
|
||
return 2
|
||
|
||
tmp = tempfile.mkdtemp(prefix="mpc-chain-selftest-")
|
||
srv = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||
port = srv.server_address[1]
|
||
t = threading.Thread(target=srv.serve_forever, daemon=True)
|
||
t.start()
|
||
try:
|
||
p = lambda n: os.path.join(tmp, n)
|
||
cfg_ok = p("config-ok.json")
|
||
cfg_down = p("config-down.json")
|
||
write(cfg_ok, config(port))
|
||
# 端口 1 上不会有服务:模拟链挂,连接会立刻被拒。
|
||
write(cfg_down, config(1))
|
||
rec = p("receipt.json")
|
||
write(rec, receipt())
|
||
|
||
# --- 1. 规范编码与摘要 ---
|
||
rc, out, _ = run("chain", "digest", "--config", cfg_ok, "--kind", "receipt", "--file", rec)
|
||
d = None
|
||
try:
|
||
d = json.loads(out)
|
||
except Exception:
|
||
pass
|
||
check("digest 输出合法 JSON 且 rc=0", rc == 0 and isinstance(d, dict), (rc, out))
|
||
check("hashSuite=sm3", bool(d) and d.get("hashSuite") == "sm3", d)
|
||
check("digest 是 64 位 hex", bool(d) and len(d.get("digest", "")) == 64, d)
|
||
check("canonicalBytes > 0 且 canonicalHex 长度吻合",
|
||
bool(d) and d.get("canonicalBytes", 0) > 0
|
||
and len(d.get("canonicalHex", "")) == 2 * d["canonicalBytes"], d)
|
||
|
||
rc2, out2, _ = run("chain", "digest", "--config", cfg_ok, "--kind", "receipt", "--file", rec)
|
||
check("同一份对象两次摘要一致", rc2 == 0 and out2 == out, (rc2, out2))
|
||
|
||
rec2 = p("receipt-changed.json")
|
||
write(rec2, receipt(link="relay"))
|
||
_, out3, _ = run("chain", "digest", "--config", cfg_ok, "--kind", "receipt", "--file", rec2)
|
||
try:
|
||
d3 = json.loads(out3)
|
||
except Exception:
|
||
d3 = {}
|
||
check("改 link 字段后摘要变化(记录实际路径不是策略)",
|
||
d3.get("digest") != (d or {}).get("digest"), (d, d3))
|
||
|
||
other_kinds = {
|
||
"member": {
|
||
"member_id": "alice", "sign_pubkey": "0102", "platform": "hygon-csv",
|
||
"host_pubkeys": ["aabb"], "active": True, "expires_at": 4102444800,
|
||
},
|
||
"order": {
|
||
"order_id": "order-1", "participants": ["alice", "bob"],
|
||
"catalog_id": "psi-v1", "ir_hash": "01" * 32,
|
||
"input_hashes": ["02" * 32], "min_tcb": 7,
|
||
"session_pubkey": "04" * 65, "policy_bundle_hash": "05" * 32,
|
||
"deadline": 4102444800,
|
||
},
|
||
"bundle": {
|
||
"bundle_hash": "03" * 32, "issuer": "site-a",
|
||
"valid_from": 1, "valid_until": 4102444800, "revoked": False,
|
||
},
|
||
}
|
||
digests = set()
|
||
for kind, body in other_kinds.items():
|
||
f = p(f"{kind}.json")
|
||
write(f, body)
|
||
rc, out, _ = run("chain", "digest", "--config", cfg_ok, "--kind", kind, "--file", f)
|
||
try:
|
||
v = json.loads(out)
|
||
except Exception:
|
||
v = {}
|
||
check(f"{kind} 能算出摘要", rc == 0 and len(v.get("digest", "")) == 64, (rc, out))
|
||
digests.add(v.get("digest"))
|
||
digests.add((d or {}).get("digest"))
|
||
check("四类对象摘要互不相同(域分隔 + 类型标签生效)", len(digests) == 4, digests)
|
||
|
||
# --- 2. 配置纪律:每条负向带期望特征 ---
|
||
bad_missing = p("config-missing.json")
|
||
c = config(port)
|
||
c.pop("sm_crypto")
|
||
write(bad_missing, c)
|
||
expect_reject("配置缺 sm_crypto 即拒绝且点名字段",
|
||
run("chain", "digest", "--config", bad_missing, "--kind", "receipt", "--file", rec),
|
||
"sm_crypto")
|
||
|
||
nogm = p("config-nogm.json")
|
||
write(nogm, config(port, sm_crypto=False))
|
||
expect_reject("sm_crypto=false 拒绝且说明 keccak256 原因",
|
||
run("chain", "digest", "--config", nogm, "--kind", "receipt", "--file", rec),
|
||
"keccak256")
|
||
|
||
tlcp = p("config-tlcp.json")
|
||
write(tlcp, config(port, client_tlcp=True))
|
||
# 用户拍板要做 TLCP:配置层不再拒这个声明;真正的 fail-closed
|
||
# 在 flush 的传输准入,下面单独判,避免「配置能过 = 通道已通」。
|
||
rc, out, _ = run("chain", "digest", "--config", tlcp, "--kind", "receipt", "--file", rec)
|
||
check("client_tlcp=true 是合法声明(配置层不再误拒)",
|
||
rc == 0 and json.loads(out).get("hashSuite") == "sm3", (rc, out))
|
||
expect_reject("flush 不带 --transport 必须拒绝(传输形态没有默认值)",
|
||
run("chain", "flush", "--config", cfg_ok,
|
||
"--outbox", p("outbox-notransport.db")),
|
||
"缺少必填参数 --transport")
|
||
expect_reject("flush --transport tlcp 因缺双证书配置而拒绝",
|
||
run("chain", "flush", "--config", tlcp,
|
||
"--outbox", p("outbox-tlcp.db"), "--transport", "tlcp"),
|
||
"双证书")
|
||
|
||
bad_ep = p("config-ep.json")
|
||
write(bad_ep, config(port, rpc_endpoint="http://127.0.0.1:20200"))
|
||
expect_reject("endpoint 带 scheme 被拒",
|
||
run("chain", "digest", "--config", bad_ep, "--kind", "receipt", "--file", rec),
|
||
"host:port")
|
||
|
||
expect_reject("未知参数直接报错不静默忽略",
|
||
run("chain", "status", "--outbox", p("x.db"), "--bogus", "1"),
|
||
"未知参数")
|
||
|
||
# --- 3. outbox:幂等 / fail-open / 回读 ---
|
||
box = p("outbox.db")
|
||
rc, out, _ = run("chain", "submit", "--config", cfg_down, "--outbox", box, "--file", rec)
|
||
try:
|
||
s1 = json.loads(out)
|
||
except Exception:
|
||
s1 = {}
|
||
check("链挂时 submit 仍成功(fail-open,链不是执行前提)",
|
||
rc == 0 and s1.get("outcome") == "added", (rc, out))
|
||
|
||
rc, out, _ = run("chain", "submit", "--config", cfg_down, "--outbox", box, "--file", rec)
|
||
try:
|
||
s2 = json.loads(out)
|
||
except Exception:
|
||
s2 = {}
|
||
check("同 order_id+decision_id 重复提交 → already_present",
|
||
rc == 0 and s2.get("outcome") == "already_present", (rc, out))
|
||
|
||
rc, out, _ = run("chain", "status", "--outbox", box)
|
||
try:
|
||
st = json.loads(out)
|
||
except Exception:
|
||
st = {}
|
||
check("status 只回读 1 条且为 Pending(重启回读)",
|
||
rc == 0 and len(st.get("entries", [])) == 1
|
||
and st["entries"][0]["state"] == "Pending" and st.get("pending") == 1, (rc, out))
|
||
|
||
rec_b = p("receipt-dec2.json")
|
||
write(rec_b, receipt(decision_id="dec-2"))
|
||
run("chain", "submit", "--config", cfg_down, "--outbox", box, "--file", rec_b)
|
||
_, out, _ = run("chain", "status", "--outbox", box)
|
||
st = json.loads(out)
|
||
check("不同 decision_id 是另一条(幂等键是复合的)", len(st["entries"]) == 2, st)
|
||
|
||
# payload 被覆盖 = 补链写出与当时承诺不一致的账,必须不发生。
|
||
changed = p("receipt-overwrite.json")
|
||
write(changed, receipt(link="relay"))
|
||
run("chain", "submit", "--config", cfg_down, "--outbox", box, "--file", changed)
|
||
_, out, _ = run("chain", "status", "--outbox", box)
|
||
st = json.loads(out)
|
||
e0 = [e for e in st["entries"] if e["key"] == "receipt:order-1:dec-1"][0]
|
||
check("重复提交不覆盖已有 payload", e0["payloadBytes"] == (d or {}).get("canonicalBytes"), e0)
|
||
|
||
# --- 4. flush:链挂 → 失败不吞、保持 Pending ---
|
||
rc, out, _ = run("chain", "flush", "--config", cfg_down, "--outbox", box, "--transport", "stub")
|
||
try:
|
||
fr = json.loads(out)
|
||
except Exception:
|
||
fr = {}
|
||
check("链挂时 flush 不致命(fail-open)", rc == 0, (rc, out))
|
||
check("失败原因带出且条目仍 Pending",
|
||
fr.get("remaining", 0) >= 2
|
||
and any("连 bcos-rpc 失败" in why for _, why in fr.get("failed", [])),
|
||
fr)
|
||
# ★ 回归探针:删掉 .no_proxy() 后,回环端口会被系统代理接走并返回
|
||
# 502,链挂原因就变成代理的错误而不是「连不上 bcos-rpc」——
|
||
# 生产里这等于把这一跳的端点偷偷换掉了。
|
||
check("链挂原因不是代理 502(no_proxy 生效,端点未被转走)",
|
||
all("502" not in why for _, why in fr.get("failed", [])), fr)
|
||
_, out, _ = run("chain", "status", "--outbox", box)
|
||
st = json.loads(out)
|
||
check("失败后 attempts 递增且 lastError 非空",
|
||
all(e["attempts"] >= 1 and e["lastError"] for e in st["entries"]), st)
|
||
|
||
# --- 5. 假 JSON-RPC:正常补链 + 不重发 ---
|
||
STATE.set_mode("ok")
|
||
rc, out, _ = run("chain", "flush", "--config", cfg_ok, "--outbox", box, "--transport", "stub")
|
||
fr = json.loads(out)
|
||
check("假链恢复后补链成功", rc == 0 and fr.get("confirmed") == 2 and fr.get("remaining") == 0, fr)
|
||
check("flush 明确上报走的是明文桩(不是悄悄走 TLCP)",
|
||
fr.get("transport") == "plaintext-stub", fr)
|
||
check("桩方法仍是 eth_sendRawTransaction 但明确标注为桩(不冒充真链入口)",
|
||
fr.get("method") == "eth_sendRawTransaction"
|
||
and fr.get("methodIsStub") is True, fr)
|
||
check("★ 输出同时给出国密链真提交方法 sendTransaction(4 参)",
|
||
fr.get("guomiSubmitMethod") == "sendTransaction", fr)
|
||
check("★ 守卫确认国密链禁用 eth_sendRawTransaction(web3 端点 secp256k1+keccak256)",
|
||
fr.get("guomiChainRejectsStubMethod") is True, fr)
|
||
check("输出如实标注 payload 不是已签名交易",
|
||
fr.get("payload") == "canonical-bytes-not-signed-tx", fr)
|
||
check("服务端只收到 2 次(每条一次)", len(STATE.calls) == 2, STATE.calls)
|
||
check("请求参数带 0x 前缀的 payload",
|
||
all(isinstance(c[1], list) and str(c[1][0]).startswith("0x") for c in STATE.calls),
|
||
STATE.calls)
|
||
|
||
STATE.set_mode("ok")
|
||
rc, out, _ = run("chain", "flush", "--config", cfg_ok, "--outbox", box, "--transport", "stub")
|
||
fr = json.loads(out)
|
||
check("第二次 flush 不重复发送(attempted=0)",
|
||
rc == 0 and fr.get("attempted") == 0 and len(STATE.calls) == 0, (fr, STATE.calls))
|
||
|
||
# --- 6. JSON-RPC 失败形态:每条带期望特征 ---
|
||
for mode, needle in [
|
||
("error", "invalid signature"),
|
||
("null", "返回 null"),
|
||
("missing_result", "缺少 result"),
|
||
("bad_id", "串包"),
|
||
]:
|
||
# 每个场景用一个新 outbox,保证有 Pending 条目可发。
|
||
b = p(f"outbox-{mode}.db")
|
||
run("chain", "submit", "--config", cfg_ok, "--outbox", b, "--file", rec)
|
||
STATE.set_mode(mode)
|
||
rc, out, _ = run("chain", "flush", "--config", cfg_ok, "--outbox", b, "--transport", "stub")
|
||
try:
|
||
fr = json.loads(out)
|
||
except Exception:
|
||
fr = {}
|
||
failed_text = " ".join(str(x) for x in fr.get("failed", []))
|
||
check(f"{mode} 响应不被当成成功,且带出期望特征 {needle!r}",
|
||
rc == 0 and fr.get("confirmed", 1) == 0
|
||
and fr.get("remaining", 0) == 1 and needle in failed_text,
|
||
(rc, out))
|
||
_, out, _ = run("chain", "status", "--outbox", b)
|
||
st = json.loads(out)
|
||
check(f"{mode} 后条目仍是 Pending(不写假绿)",
|
||
st["entries"][0]["state"] == "Pending", st)
|
||
|
||
# --- 7. 准入:fail-closed ---
|
||
cache = p("cache.json")
|
||
write(cache, {"members": ["alice", "bob"], "hard_rules_ok": True, "expires_at": 2000})
|
||
rc, out, _ = run("chain", "admit", "--cache", cache, "--now", "1000",
|
||
"--site", "alice", "--participants", "alice,bob")
|
||
check("缓存新鲜且全是成员 → allow", rc == 0 and "allow" in out, (rc, out))
|
||
expect_reject("缓存过期即拒(含边界 now=expires_at)",
|
||
run("chain", "admit", "--cache", cache, "--now", "2000",
|
||
"--site", "alice", "--participants", "alice,bob"),
|
||
"过期")
|
||
expect_reject("非成员站点拒绝",
|
||
run("chain", "admit", "--cache", cache, "--now", "1000",
|
||
"--site", "eve", "--participants", "alice,bob"),
|
||
"成员表中")
|
||
expect_reject("参与方含非成员拒绝",
|
||
run("chain", "admit", "--cache", cache, "--now", "1000",
|
||
"--site", "alice", "--participants", "alice,eve"),
|
||
"不是联盟成员")
|
||
|
||
hard = p("cache-hard.json")
|
||
write(hard, {"members": ["alice", "bob"], "hard_rules_ok": False, "expires_at": 2000})
|
||
expect_reject("硬规则未通过拒绝",
|
||
run("chain", "admit", "--cache", hard, "--now", "1000",
|
||
"--site", "alice", "--participants", "alice,bob"),
|
||
"硬规则")
|
||
|
||
no_exp = p("cache-no-exp.json")
|
||
write(no_exp, {"members": ["alice", "bob"], "hard_rules_ok": True})
|
||
expect_reject("缓存缺 expires_at 拒绝(不当永不过期)",
|
||
run("chain", "admit", "--cache", no_exp, "--now", "1000",
|
||
"--site", "alice", "--participants", "alice,bob"),
|
||
"永不过期")
|
||
|
||
# --- 8. outbox 坏行不静默跳过 ---
|
||
broken = p("outbox-broken.db")
|
||
with open(broken, "w", encoding="utf-8") as f:
|
||
f.write("v1|broken|line\n")
|
||
expect_reject("outbox 坏行整体拒绝(不悄悄丢审计记录)",
|
||
run("chain", "status", "--outbox", broken), "格式损坏")
|
||
|
||
finally:
|
||
srv.shutdown()
|
||
srv.server_close()
|
||
shutil.rmtree(tmp, ignore_errors=True)
|
||
|
||
print()
|
||
print(f"判据 {PASS + FAIL} 条:通过 {PASS},失败 {FAIL}")
|
||
if FAIL:
|
||
return 1
|
||
print("全部通过")
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|