Files
agents-marketplace/tests/unit/tx-submit-live.test.ts
jelisaveta-talbot 0b93aee1ec test: clarify M1-F-D-defer status + add LIVECBOR_DEBUG env
While checking whether last night's localUPLCEval: false fix unblocks
the 5 tx-submit-live tests skipped under TODO(M1-F-D-defer), found
that those tests still crash — but at a DIFFERENT spot than before.

Original UPLC issue (now fixed in production via 7ea7328 + fd24598):
- lucid's CML WASM UPLC interpreter falsely rejected our Plutus V3
  Submit/Claim/Accept txs. End-to-end chat + TTS lifecycle now works
  on Vector testnet by routing eval to Ogmios.

New / remaining test-side issue:
- Even with a proper evaluateTransaction mock response, lucid's
  applyUPLCEvalProvider → CML TransactionBuilder.set_exunits still
  panics with wasm "unreachable" inside the test fixture. The
  production path doesn't hit this (real Ogmios + real wallet UTxOs).
  Whatever the test-mock + CML interaction trips, it's separate from
  the original UPLC bug.

Refinements landed
- liveCbor.ts: opt-in LIVECBOR_DEBUG=1 env that prints the full inner
  err + WASM stack from rethrowAsTxError, so the next investigation
  doesn't need to re-add this scaffolding.
- tx-submit-live.test.ts:
  - setupSubmitHappyPath() now mocks "evaluateTransaction" with a
    proper EvalRedeemer-shaped response (useful baseline for any
    future un-skip attempt; not enough on its own to make the tests
    pass).
  - The first skipped test's TODO comment updated to record what's
    fixed in production vs. what remains as a test-infra bug.

Test counts unchanged (3 pass, 5 still skipped); production path
still verified end-to-end on testnet. Task M1-F-D-defer stays pending
but the original UPLC-interpreter blocker is gone.
2026-05-11 09:09:49 +02:00

311 lines
14 KiB
TypeScript

/**
* tx-submit-live.test.ts — RED phase tests for buildSubmitTx() with LiveOgmiosProvider
*
* The live path produces real Cardano CBOR spending the Claimed escrow UTxO.
* Redeemer = Submit (Constr1, variant 1 of EscrowRedeemer).
* submitted_at = validity upper bound; result_receipt_hash = sha256(canonical(signedReceipt)).
*
* Existing MockChainProvider tests in tx-submit.test.ts are NOT modified.
*
* M1-F-4 RED — tests fail until Catherine implements the live CBOR path.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { createHash } from "crypto";
import { LiveOgmiosProvider } from "../../packages/shared/src/chain/LiveOgmiosProvider.js";
import { buildSubmitTx } from "../../packages/shared/src/tx/escrow/submit.js";
import { TxConstructionError } from "../../packages/shared/src/tx/types.js";
import { encodeEscrowDatum, decodeEscrowDatum } from "../../packages/shared/src/cbor/EscrowDatum.js";
import { buildSupplierWalletKey } from "../fixtures/supplier-side/wallet-keys.js";
import {
buildClaimedEscrowUtxo,
DELIVER_BY,
} from "../fixtures/buyer-side/sample-escrow-utxos.js";
import { canonicalize } from "../../packages/shared/src/cbor/canonical.js";
// ─── Fixtures ──────────────────────────────────────────────────────────────────
const ESCROW_TX_HASH = "f".repeat(64);
const ESCROW_INDEX = 1;
const COLLATERAL_LOVELACE = 5_000_000;
const VALID_RECEIPT_HASH = "a".repeat(64);
const mockFetch = vi.fn();
beforeEach(() => {
mockFetch.mockReset();
});
function rpcOk<T>(result: T) {
return { ok: true, json: async () => ({ jsonrpc: "2.0", result }) };
}
function makeOgmiosUtxo(txId: string, index: number, address: string, lovelace: number, datumHex?: string) {
return { transaction: { id: txId }, index, address, value: { ada: { lovelace } }, datum: datumHex ?? null, datumHash: null, script: null };
}
function protocolParamsResponse() {
return rpcOk({
minFeeCoefficient: 44, minFeeConstant: { ada: { lovelace: 155381 } },
maxTransactionSize: { bytes: 16384 }, maxValueSize: { bytes: 5000 },
stakeCredentialDeposit: { ada: { lovelace: 2000000 } },
stakePoolDeposit: { ada: { lovelace: 500000000 } },
prices: { memory: "0.0577", steps: "0.0000721" },
maxExecutionUnitsPerTransaction: { memory: 14000000, cpu: 10000000000 },
coinsPerUtxoByte: { ada: { lovelace: 4310 } }, collateralPercentage: 150, maxCollateralInputs: 3,
plutusCostModels: { "plutus:v1": {}, "plutus:v2": {}, "plutus:v3": {} },
monetaryExpansion: "0.003", treasuryExpansion: "0.2",
minStakePoolCost: { ada: { lovelace: 340000000 } },
minFeeReferenceScripts: { base: 15 },
governanceActionDeposit: { ada: { lovelace: 100000000000 } },
delegateRepresentativeDeposit: { ada: { lovelace: 500000000 } },
});
}
function buildLiveChain(): LiveOgmiosProvider {
return new LiveOgmiosProvider({ ogmiosUrl: "http://ogmios:1337", fetch: mockFetch });
}
function setupSubmitHappyPath(escrowDatumHex: string): void {
const supplier = buildSupplierWalletKey();
const claimedUtxo = buildClaimedEscrowUtxo();
const escrowAddress = claimedUtxo.address;
mockFetch.mockImplementation(async (_url: string, init?: RequestInit) => {
const body = JSON.parse((init?.body as string) ?? "{}");
const method: string = body.method ?? "";
if (method === "queryLedgerState/protocolParameters") return protocolParamsResponse();
if (method === "queryLedgerState/utxo") {
if ((body.params ?? {}).outputReferences) {
return rpcOk([makeOgmiosUtxo(ESCROW_TX_HASH, ESCROW_INDEX, escrowAddress, 4_000_000, escrowDatumHex)]);
}
return rpcOk([makeOgmiosUtxo("c".repeat(64), 0, supplier.address, COLLATERAL_LOVELACE)]);
}
if (method === "queryNetwork/tip") {
return rpcOk({ slot: Math.floor((DELIVER_BY - 30_000) / 1000), id: "a".repeat(64) });
}
// localUPLCEval: false delegates exec-unit calc to Ogmios's
// evaluateTransaction. The provider has a fallback when this is missing
// but its synthesized budget can exceed lucid's bundled preset maxes
// → CML set_exunits WASM panic. Return conservative budgets that fit
// comfortably within PROTOCOL_PARAMETERS_DEFAULT's
// maxExecutionUnitsPerTransaction.
if (method === "evaluateTransaction") {
return rpcOk([
{ validator: { purpose: "spend", index: 0 }, budget: { memory: 100_000, cpu: 50_000_000 } },
]);
}
if (method === "submitTransaction") return rpcOk({ transaction: { id: "d".repeat(64) } });
return rpcOk({});
});
}
// ─── A. CBOR shape ─────────────────────────────────────────────────────────────
describe("buildSubmitTx() [live] — CBOR shape", () => {
it.skip("returns real Cardano CBOR starting with 83 or 84", async () => {
// TODO(M1-F-D-defer): the ORIGINAL UPLC-WASM crash (CML interpreting
// the Plutus V3 script ourselves) was fixed in production by passing
// localUPLCEval: false to txBuilder.complete() — lucid now delegates
// eval to Ogmios. End-to-end chat + TTS lifecycle works on Vector
// testnet (commits 7ea7328 + fd24598). However this test still crashes
// at a DIFFERENT spot: lucid's applyUPLCEvalProvider → CML
// TransactionBuilder.set_exunits → wasm "unreachable" panic, even with
// a proper evaluateTransaction mock response. The test-side mock +
// CML interaction has its own bug separate from the production-path
// fix. Defer until we either (a) bisect the CML version or (b) rewrite
// these tests to assert at a layer above lucid's set_exunits.
const claimedUtxo = buildClaimedEscrowUtxo();
setupSubmitHappyPath(claimedUtxo.datumHex!);
const chain = buildLiveChain();
const supplier = buildSupplierWalletKey();
const result = await buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: VALID_RECEIPT_HASH,
});
const firstByte = result.txCborHex.slice(0, 2).toLowerCase();
expect(["83", "84"]).toContain(firstByte);
});
it.skip("txCborHex is non-empty hex string", async () => {
// TODO(M1-F-D-defer): UPLC wasm crashes on this test's lucid cost-models setup.
// Production path uses real Ogmios cost models and works on testnet.
// Defer until we plumb usePresetProtocolParameters into the test-side LucidContext.
const claimedUtxo = buildClaimedEscrowUtxo();
setupSubmitHappyPath(claimedUtxo.datumHex!);
const chain = buildLiveChain();
const supplier = buildSupplierWalletKey();
const result = await buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: VALID_RECEIPT_HASH,
});
expect(/^[0-9a-f]+$/i.test(result.txCborHex)).toBe(true);
expect(result.txCborHex.length).toBeGreaterThan(0);
});
});
// ─── B. Datum state transition ────────────────────────────────────────────────
describe("buildSubmitTx() [live] — datum fields in continuing output", () => {
it.skip("continuing output datum contains the receipt hash", async () => {
// TODO(M1-F-D-defer): UPLC wasm crashes on this test's lucid cost-models setup.
// Production path uses real Ogmios cost models and works on testnet.
// Defer until we plumb usePresetProtocolParameters into the test-side LucidContext.
const claimedUtxo = buildClaimedEscrowUtxo();
setupSubmitHappyPath(claimedUtxo.datumHex!);
const chain = buildLiveChain();
const supplier = buildSupplierWalletKey();
const result = await buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: VALID_RECEIPT_HASH,
});
// The receipt hash bytes must appear in the tx CBOR (as part of the inline datum)
expect(result.txCborHex).toContain(VALID_RECEIPT_HASH);
});
});
// ─── C. Receipt hash derivation ───────────────────────────────────────────────
describe("buildSubmitTx() [live] — receipt hash in EscrowDatum", () => {
it.skip("result_receipt_hash in new datum equals the receiptHash parameter", async () => {
// TODO(M1-F-D-defer): UPLC wasm crashes on this test's lucid cost-models setup.
// Production path uses real Ogmios cost models and works on testnet.
// Defer until we plumb usePresetProtocolParameters into the test-side LucidContext.
const claimedUtxo = buildClaimedEscrowUtxo();
setupSubmitHappyPath(claimedUtxo.datumHex!);
const chain = buildLiveChain();
const supplier = buildSupplierWalletKey();
const preComputedHash = createHash("sha256")
.update(canonicalize({ receipt: "test-receipt", signature: "sig-bytes" }))
.digest("hex");
const result = await buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: preComputedHash,
});
// The hash must be embedded in the tx CBOR
expect(result.txCborHex).toContain(preComputedHash);
});
});
// ─── D. Rejection paths ────────────────────────────────────────────────────────
describe("buildSubmitTx() [live] — rejection paths", () => {
it("throws TxConstructionError('receipt hash must be 32 bytes') for invalid receiptHash", async () => {
const chain = buildLiveChain();
const supplier = buildSupplierWalletKey();
await expect(
buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: "not-64-chars",
}),
).rejects.toMatchObject({ reason: "receipt hash must be 32 bytes" });
// Must throw before any fetch
expect(mockFetch).not.toHaveBeenCalled();
});
it("throws TxConstructionError('wrong state') when escrow.state = Submitted (double submit)", async () => {
// Build a Submitted datum
const claimedUtxo = buildClaimedEscrowUtxo();
const claimedDatum = decodeEscrowDatum(claimedUtxo.datumHex!);
const submittedDatumHex = encodeEscrowDatum({
...claimedDatum,
state: "Submitted" as const,
submitted_at: DELIVER_BY - 20_000,
result_receipt_hash: "a".repeat(64),
});
setupSubmitHappyPath(submittedDatumHex);
const chain = buildLiveChain();
const supplier = buildSupplierWalletKey();
await expect(
buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: VALID_RECEIPT_HASH,
}),
).rejects.toThrow(TxConstructionError);
});
it("throws TxConstructionError('supplier signature mismatch') when caller pkh != datum.supplier_pkh", async () => {
const claimedUtxo = buildClaimedEscrowUtxo();
setupSubmitHappyPath(claimedUtxo.datumHex!);
const chain = buildLiveChain();
const buyer = buildBuyerWalletKey(); // wrong key
await expect(
buildSubmitTx({
chain,
supplierKey: buyer,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: VALID_RECEIPT_HASH,
}),
).rejects.toMatchObject({ reason: "supplier signature mismatch" });
});
it.skip("throws TxConstructionError('submit after deliver_by') when tip >= deliver_by", async () => {
// TODO(M1-F-D-defer): UPLC wasm crashes on this test's lucid cost-models setup.
// On the live path tipMs = Date.now() (not tip slot * 1000). The tip slot is set
// to (DELIVER_BY+60_000)/1000 but Date.now() (May 2026) < DELIVER_BY (Jun 2026),
// so the pre-check passes and the UPLC evaluator crashes. Fix requires either fake
// timers with deliver_by in the past OR using real Ogmios cost models.
// Defer until we plumb usePresetProtocolParameters into the test-side LucidContext.
const claimedUtxo = buildClaimedEscrowUtxo();
const escrowDatumHex = claimedUtxo.datumHex!;
const supplier = buildSupplierWalletKey();
mockFetch.mockImplementation(async (_url: string, init?: RequestInit) => {
const body = JSON.parse((init?.body as string) ?? "{}");
const method: string = body.method ?? "";
if (method === "queryLedgerState/protocolParameters") return protocolParamsResponse();
if (method === "queryLedgerState/utxo") {
if ((body.params ?? {}).outputReferences) {
return rpcOk([makeOgmiosUtxo(ESCROW_TX_HASH, ESCROW_INDEX, "addr_escrow", 4_000_000, escrowDatumHex)]);
}
return rpcOk([makeOgmiosUtxo("c".repeat(64), 0, supplier.address, COLLATERAL_LOVELACE)]);
}
// Tip AFTER deliver_by
if (method === "queryNetwork/tip") {
return rpcOk({ slot: Math.floor((DELIVER_BY + 60_000) / 1000), id: "a".repeat(64) });
}
return rpcOk({});
});
const chain = buildLiveChain();
await expect(
buildSubmitTx({
chain,
supplierKey: supplier,
escrowRef: { txHash: ESCROW_TX_HASH, index: ESCROW_INDEX },
receiptHash: VALID_RECEIPT_HASH,
}),
).rejects.toMatchObject({ reason: "submit after deliver_by" });
});
});
// Need this import at top-level scope for the rejection test
import { buildBuyerWalletKey } from "../fixtures/buyer-side/wallet-keys.js";