PR A: 12-state transaction machine + issueInstrument step + SoD matrix
Some checks failed
Code Quality / SonarQube Analysis (pull_request) Failing after 23s
Code Quality / Code Quality Checks (pull_request) Failing after 11s
Security Scan / Dependency Vulnerability Scan (pull_request) Failing after 4s
Security Scan / OWASP ZAP Scan (pull_request) Failing after 5s

Architecture note steps 1, 2, 10 (data model).

- types/transactionState.ts: 12 states, allowed-transition table, SoD matrix
- types/plan.ts: add InstrumentTerms + 'issueInstrument' PlanStep type
- services/planValidation.ts: validate SBLC step (BIC, ISO-4217, sha256,
  YYYY-MM-DD expiry, >0 amount)
- services/stateMachine.ts: transition() enforces legality + SoD + appends
  to transaction_state_transitions
- db/migrations/002: plans.transaction_state (CHECK) +
  transaction_state_transitions append-only table
- tests/unit: 13 + 8 unit tests (31 total, all pass)

No behaviour change yet: coordinator still uses legacy status field.
PRs B-G will migrate execution paths onto the new machine.
This commit is contained in:
Devin
2026-04-22 16:21:36 +00:00
parent b118b2be9c
commit b24a4df983
10 changed files with 634 additions and 18 deletions

View File

@@ -0,0 +1,82 @@
import { describe, it, expect } from "@jest/globals";
import { validatePlan } from "../../src/services/planValidation";
import type { InstrumentTerms, Plan } from "../../src/types/plan";
const goodTerms: InstrumentTerms = {
applicant: "Solace Bank Group PLC",
issuingBankBIC: "SOLBAE22",
beneficiaryBankBIC: "MEBLAEAD", // Emirates Islamic BIC prefix example
beneficiaryName: "Acme Trading LLC",
beneficiaryAccount: "AE070331234567890123456",
amount: 1_000_000,
currency: "USD",
tenor: "90D",
expiryDate: "2026-06-30",
placeOfPresentation: "Dubai, UAE",
governingLaw: "URDG 758",
templateRef: "EIB-SBLC-v3.2",
templateHash:
"a".repeat(64), // dummy sha256
};
function planWith(terms: Partial<InstrumentTerms> | null): Plan {
return {
creator: "solace-ops-01",
steps: [
{
type: "issueInstrument",
amount: terms?.amount ?? 1_000_000,
instrument: terms === null ? undefined : ({ ...goodTerms, ...terms } as InstrumentTerms),
},
],
};
}
describe("validatePlan — issueInstrument step", () => {
it("accepts a well-formed SBLC step", () => {
const result = validatePlan(planWith({}));
expect(result.valid).toBe(true);
expect(result.errors).toHaveLength(0);
});
it("rejects a step missing the instrument object", () => {
const result = validatePlan(planWith(null));
expect(result.valid).toBe(false);
expect(result.errors[0]).toMatch(/missing instrument terms/);
});
it("rejects an invalid BIC", () => {
const result = validatePlan(planWith({ issuingBankBIC: "NOTABIC" }));
expect(result.valid).toBe(false);
expect(result.errors.join("\n")).toMatch(/issuingBankBIC is not a valid BIC/);
});
it("rejects a non-ISO-4217 currency", () => {
const result = validatePlan(planWith({ currency: "usd" }));
expect(result.valid).toBe(false);
expect(result.errors.join("\n")).toMatch(/currency must be ISO 4217/);
});
it("rejects a non-ISO-8601 expiry date", () => {
const result = validatePlan(planWith({ expiryDate: "30-06-2026" }));
expect(result.valid).toBe(false);
expect(result.errors.join("\n")).toMatch(/expiryDate must be YYYY-MM-DD/);
});
it("rejects a non-sha256 template hash", () => {
const result = validatePlan(planWith({ templateHash: "deadbeef" }));
expect(result.valid).toBe(false);
expect(result.errors.join("\n")).toMatch(/templateHash must be 64 hex chars/);
});
it("rejects an instrument with non-positive amount", () => {
const result = validatePlan(planWith({ amount: 0 }));
expect(result.valid).toBe(false);
expect(result.errors.join("\n")).toMatch(/instrument.amount must be > 0/);
});
it("accepts 11-char branched BIC", () => {
const result = validatePlan(planWith({ issuingBankBIC: "SOLBAE22XXX" }));
expect(result.valid).toBe(true);
});
});

View File

@@ -0,0 +1,85 @@
import { describe, it, expect } from "@jest/globals";
import {
ALLOWED_TRANSITIONS,
ROLE_FOR_TRANSITION,
SOD_REQUIRED_TRANSITIONS,
TRANSACTION_STATES,
canTransition,
} from "../../src/types/transactionState";
describe("Transaction state machine (architecture note §8§9)", () => {
it("declares the 12 states from §8.1", () => {
expect(TRANSACTION_STATES).toEqual([
"DRAFT",
"INITIATED",
"PRECONDITIONS_PENDING",
"READY_FOR_PREPARE",
"PREPARED",
"EXECUTING",
"PARTIALLY_EXECUTED",
"VALIDATING",
"COMMITTED",
"ABORTED",
"UNWIND_PENDING",
"CLOSED",
]);
});
describe("§9.1 permitted high-level transitions", () => {
// Each of these is listed in the note; canTransition must accept them.
const legal: Array<[string, string]> = [
["DRAFT", "INITIATED"],
["INITIATED", "PRECONDITIONS_PENDING"],
["PRECONDITIONS_PENDING", "READY_FOR_PREPARE"],
["READY_FOR_PREPARE", "PREPARED"],
["PREPARED", "EXECUTING"],
["EXECUTING", "PARTIALLY_EXECUTED"],
["EXECUTING", "VALIDATING"],
["PARTIALLY_EXECUTED", "VALIDATING"],
["VALIDATING", "COMMITTED"],
["VALIDATING", "ABORTED"],
["ABORTED", "UNWIND_PENDING"],
["COMMITTED", "CLOSED"],
["UNWIND_PENDING", "CLOSED"],
];
it.each(legal)("allows %s -> %s", (from, to) => {
expect(canTransition(from as any, to as any)).toBe(true);
});
// A few illegal edges — explicitly not in §9.1.
const illegal: Array<[string, string]> = [
["DRAFT", "COMMITTED"],
["INITIATED", "EXECUTING"],
["CLOSED", "INITIATED"],
["PREPARED", "COMMITTED"],
["COMMITTED", "ABORTED"],
["ABORTED", "COMMITTED"],
];
it.each(illegal)("rejects %s -> %s", (from, to) => {
expect(canTransition(from as any, to as any)).toBe(false);
});
});
it("CLOSED is a terminal state", () => {
expect(ALLOWED_TRANSITIONS.CLOSED).toEqual([]);
});
describe("segregation-of-duties checkpoints (§13)", () => {
it("flags the four SoD-gated transitions", () => {
expect([...SOD_REQUIRED_TRANSITIONS].sort()).toEqual(
[
"ABORTED->UNWIND_PENDING",
"PREPARED->EXECUTING",
"READY_FOR_PREPARE->PREPARED",
"VALIDATING->COMMITTED",
].sort(),
);
});
it("assigns a role to every SoD-gated transition", () => {
for (const key of SOD_REQUIRED_TRANSITIONS) {
expect(ROLE_FOR_TRANSITION[key]).toBeDefined();
}
});
});
});