The Memento of Chronomancy
Preserving historical UTxO states within the Datum for later restoration.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Memento
ARCANUM SCHOOL: Chronomancy // State Reversion
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// PLUTUS PATTERN //
module LedgerMonad.Memento where
import PlutusTx.Prelude
import Plutus.V2.Ledger.Api
-- The Chrono-Anchor (Memento)
data StateSnapshot = StateSnapshot
{ snapMana :: Integer
, snapPosition :: Integer
}
PlutusTx.unstableMakeIsData ''StateSnapshot
-- The Originator State
data ContractState = ContractState
{ currentMana :: Integer
, currentPosition :: Integer
, memento :: Maybe StateSnapshot
}
PlutusTx.unstableMakeIsData ''ContractState
data ChronoRedeemer = SaveState | RestoreState | Mutate Integer Integer
PlutusTx.unstableMakeIsData ''ChronoRedeemer
{-# INLINABLE mementoValidator #-}
mementoValidator :: ContractState -> ChronoRedeemer -> ScriptContext -> Bool
mementoValidator datum SaveState _ =
-- Requires checking that the continuing output has memento = Just (StateSnapshot currentMana currentPosition)
True
mementoValidator datum RestoreState _ =
case memento datum of
Nothing -> traceError "No temporal anchor found!"
Just _ -> True -- Requires checking that continuing output restores the snapshot values
mementoValidator _ (Mutate _ _) _ = True
Because the UTxO model consumes state permanently, reversing a bad mutation is normally impossible without hard forks. The Memento pattern is a Chronomantic trick: the current state explicitly caches a historical snapshot of itself inside its own Datum. When disaster strikes, a specific RestoreState Redeemer can be invoked to overwrite the current variables with the cached Memento, effectively rewinding time for that specific smart contract.
Runic Tags:
#memento
#plutus
#chronomancy
#state-machine