When a business operation needs to atomically span two Sub-Chains (e.g., an asset moves from the logistics chain to the finance chain), HieraChain uses the Two-Phase Commit (2PC) protocol to guarantee atomicity. Either both chains commit the change, or both roll back β no partial state is possible.
Example real-world trigger: An inventory item is transferred between departments. The source chain records a deduct event; the destination chain records a receive event. Both must succeed or neither applies.
Flow Diagram β Happy Path
sequenceDiagram
autonumber
participant HM as ποΈ HierarchyManager
participant TM as π CrossChainTransactionManager
participant SRC as π¦ Source SubChain
participant DST as π¦ Destination SubChain
HM->>TM: initiate_cross_chain_transaction(src, dst, payload)
TM->>TM: Create CrossChainTransaction (UUID, state=PENDING)
rect rgb(0, 0, 0, 0)
Note over TM,DST: PHASE 1 β PREPARE
TM->>SRC: prepare_transaction(tx_id, payload, is_source=True)
SRC->>SRC: Lock resources, validate payload
SRC-->>TM: True β
TM->>DST: prepare_transaction(tx_id, payload, is_source=False)
DST->>DST: Verify capacity to accept
DST-->>TM: True β
TM->>TM: state = PREPARED
end
rect rgb(0, 0, 0, 0)
Note over TM,DST: PHASE 2 β COMMIT
TM->>SRC: commit_transaction(tx_id)
SRC-->>TM: True β
TM->>DST: commit_transaction(tx_id)
DST-->>TM: True β
TM->>TM: state = COMMITTED
end
TM-->>HM: tx_id (COMMITTED)
Flow Diagram β Failure Paths
sequenceDiagram
autonumber
participant TM as π CrossChainTransactionManager
participant SRC as π¦ Source SubChain
participant DST as π¦ Destination SubChain
rect rgb(0, 0, 0, 0)
Note over TM,DST: SCENARIO A β Phase 1 Prepare Fails
TM->>SRC: prepare_transaction(tx_id, payload)
SRC-->>TM: True β
TM->>DST: prepare_transaction(tx_id, payload)
DST-->>TM: False β (capacity / validation fail)
TM->>TM: state = PENDING β rollback triggered
TM->>SRC: rollback_transaction(tx_id)
TM->>DST: rollback_transaction(tx_id)
TM->>TM: state = ROLLED_BACK β οΈ
end
rect rgb(0, 0, 0, 0)
Note over TM,DST: SCENARIO B β Phase 2 Partial Commit Fails
TM->>SRC: commit_transaction(tx_id)
SRC-->>TM: True β
TM->>DST: commit_transaction(tx_id)
DST-->>TM: Exception β
TM->>TM: state = FAILED β
Note over TM: Manual reconciliation required<br/>Inspect logs for partial state
end
Transaction State Machine
flowchart LR
P["PENDING"] --> PR["PREPARED"]
PR --> C["COMMITTED β "]
PR --> RB["ROLLED_BACK β οΈ"]
P --> F["FAILED β"]
PR --> F
Step-by-Step Breakdown
Step
Description
1. Initiate
HierarchyManager creates a CrossChainTransaction with UUID and state=PENDING