The Composite of Merkle Trees
Treating single Datums and trees of Datums uniformly.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Composite
ARCANUM SCHOOL: Conjuration // Merkle Trees
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// PLUTUS PATTERN //
module LedgerMonad.Composite where
import PlutusTx.Prelude
import Plutus.V2.Ledger.Api
-- The Recursive Arcane Structure
data CompositeState
= Leaf Integer
| Node [CompositeState]
{-# INLINABLE evaluateState #-}
evaluateState :: CompositeState -> Integer
evaluateState (Leaf val) = val
evaluateState (Node children) = sum (map evaluateState children)
-- The Conjuration Ritual
{-# INLINABLE validateComposite #-}
validateComposite :: CompositeState -> BuiltinData -> ScriptContext -> Bool
validateComposite state _ _ =
let totalMana = evaluateState state
in traceIfFalse "Insufficient composite mana!" (totalMana > 100)
-- Note: sum and map need specific PlutusTx equivalents in production,
-- simplified here for the purity of the incantation.
The Composite pattern manifests when the Conjurer must treat a single drop of mana (a Leaf) and a vast reservoir (a Node tree) with the same invocation. By structuring the Datum as an algebraic data type with recursive branches, the smart contract evaluates complex nested state machines as if they were a singular entity.
Runic Tags:
#composite
#plutus
#conjuration
#merkle