Skip to content

Commit

Permalink
Merge pull request #1311 from input-output-hk/add-tx-metadata
Browse files Browse the repository at this point in the history
Add tx metadata to label Hydra transactions
  • Loading branch information
ch1bo authored Feb 19, 2024
2 parents a19389f + e8fb5fe commit 8c6e4bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ changes.

## [0.16.0] - UNRELEASED

- Add metadata to identify Hydra protocol transactions created by `hydra-node`.

- **BREAKING** Transaction serialization on hydra-node api and persisted data changed.

- Provide more details about why a command failed. Added the state of the head logic at the point of failure.
Expand Down
2 changes: 1 addition & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,6 @@ renderTxWithUTxO utxo (Tx body _wits) =
Api.TxExtraKeyWitnesses xs -> ("- " <>) . show <$> xs

metadataLines =
[ "== REQUIRED SIGNERS"
[ "== METADATA"
, show (txMetadata content)
]
34 changes: 34 additions & 0 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ instance Arbitrary ClosedThreadOutput where
hydraHeadV1AssetName :: AssetName
hydraHeadV1AssetName = AssetName (fromBuiltin hydraHeadV1)

-- | The metadata label used for identifying Hydra protocol transactions. As
-- suggested by a friendly large language model: The number most commonly
-- associated with "Hydra" is 5, as in the mythological creature Hydra, which
-- had multiple heads, and the number 5 often symbolizes multiplicity or
-- diversity. However, there is no specific numerical association for Hydra
-- smaller than 10000 beyond this mythological reference.
hydraMetadataLabel :: Word64
hydraMetadataLabel = 55555

-- | Create a transaction metadata entry to identify Hydra transactions (for
-- informational purposes).
mkHydraHeadV1TxName :: Text -> TxMetadata
mkHydraHeadV1TxName name =
TxMetadata $ Map.fromList [(hydraMetadataLabel, TxMetaText $ "HydraV1/" <> name)]

-- | Get the metadata entry to identify Hydra transactions (for informational
-- purposes).
getHydraHeadV1TxName :: Tx -> Maybe Text
getHydraHeadV1TxName =
lookupName . txMetadata . getTxBodyContent . getTxBody
where
lookupName = \case
TxMetadataNone -> Nothing
TxMetadataInEra (TxMetadata m) ->
case Map.lookup hydraMetadataLabel m of
Just (TxMetaText name) -> Just name
_ -> Nothing

-- * Create Hydra Head transactions

-- | Create the init transaction from some 'HeadParameters' and a single TxIn
Expand All @@ -139,6 +167,7 @@ initTx networkId seedTxIn participants parameters =
: map (mkInitialOutput networkId seedTxIn) participants
)
& mintTokens (HeadTokens.mkHeadTokenScript seedTxIn) Mint ((hydraHeadV1AssetName, 1) : participationTokens)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "InitTx")
where
participationTokens =
[(onChainIdToAssetName oid, 1) | oid <- participants]
Expand Down Expand Up @@ -202,6 +231,7 @@ commitTx networkId scriptRegistry headId party utxoToCommitWitnessed (initialInp
& addInputs committedTxIns
& addExtraRequiredSigners [vkh]
& addOutputs [commitOutput]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CommitTx")
where
initialWitness =
BuildTxWith $
Expand Down Expand Up @@ -273,6 +303,7 @@ collectComTx networkId scriptRegistry vk headId headParameters (headInput, initi
& addReferenceInputs [commitScriptRef, headScriptRef]
& addOutputs [headOutput]
& addExtraRequiredSigners [verificationKeyHash vk]
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CollectComTx")
where
HeadParameters{parties, contestationPeriod} = headParameters

Expand Down Expand Up @@ -360,6 +391,7 @@ closeTx scriptRegistry vk closing startSlotNo (endSlotNo, utcTime) openThreadOut
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityLowerBound startSlotNo
& setValidityUpperBound endSlotNo
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "CloseTx")
where
OpenThreadOutput
{ openThreadUTxO = (headInput, headOutputBefore)
Expand Down Expand Up @@ -452,6 +484,7 @@ contestTx scriptRegistry vk Snapshot{number, utxo} sig (slotNo, _) closedThreadO
& addOutputs [headOutputAfter]
& addExtraRequiredSigners [verificationKeyHash vk]
& setValidityUpperBound slotNo
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "ContestTx")
where
ClosedThreadOutput
{ closedThreadUTxO = (headInput, headOutputBefore)
Expand Down Expand Up @@ -528,6 +561,7 @@ fanoutTx scriptRegistry utxo (headInput, headOutput) deadlineSlotNo headTokenScr
& addOutputs orderedTxOutsToFanout
& burnTokens headTokenScript Burn headTokens
& setValidityLowerBound (deadlineSlotNo + 1)
& setTxMetadata (TxMetadataInEra $ mkHydraHeadV1TxName "FanoutTx")
where
headWitness =
BuildTxWith $
Expand Down

0 comments on commit 8c6e4bf

Please sign in to comment.