Skip to content

Commit

Permalink
TxTrace: Add decommit value to the head UTxO
Browse files Browse the repository at this point in the history
This prevents negative values in the head outputs since our snapshots
are generated randomly and are not connected to the actual head utxo.
  • Loading branch information
v0d1ch committed May 15, 2024
1 parent 1bd0333 commit d33cfa1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
11 changes: 4 additions & 7 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,10 @@ closeTx scriptRegistry vk closing startSlotNo (endSlotNo, utcTime) openThreadOut
, contesters = []
}

(UTxOHash utxoHashBytes, UTxOHash decommitUTxOHashBytes, snapshotNumber) = case closing of
CloseWithInitialSnapshot{openUtxoHash} -> (openUtxoHash, mempty, 0)
CloseWithConfirmedSnapshot{closeUtxoHash, closeUtxoToDecommitHash, snapshotNumber = sn} -> (closeUtxoHash, closeUtxoToDecommitHash, toInteger sn)

signature = case closing of
CloseWithInitialSnapshot{} -> mempty
CloseWithConfirmedSnapshot{signatures = s} -> toPlutusSignatures s
(UTxOHash utxoHashBytes, UTxOHash decommitUTxOHashBytes, snapshotNumber, signature) = case closing of
CloseWithInitialSnapshot{openUtxoHash} -> (openUtxoHash, mempty, 0, mempty)
CloseWithConfirmedSnapshot{closeUtxoHash, closeUtxoToDecommitHash, snapshotNumber = sn, signatures = s} ->
(closeUtxoHash, closeUtxoToDecommitHash, toInteger sn, toPlutusSignatures s)

contestationDeadline =
addContestationPeriod (posixFromUTCTime utcTime) openContestationPeriod
Expand Down
33 changes: 18 additions & 15 deletions hydra-node/test/Hydra/Chain/Direct/TxTraceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ instance StateModel Model where
|| actor `elem` alreadyContested
)
Fanout{snapshot} ->
headState == Closed -- TODO: gracefully fail in perform instead?
&& snapshot /= latestSnapshot
headState /= Closed -- TODO: gracefully fail in perform instead?
-- TODO: why can't we produce failing action like this here? seed 1761217658
-- headState == Closed && snapshot /= latestSnapshot
_ -> False

nextState :: Model -> Action Model a -> Var a -> Model
Expand Down Expand Up @@ -434,16 +435,18 @@ signedSnapshot ms =

signatures = aggregate [sign sk snapshot | sk <- [Fixture.aliceSk, Fixture.bobSk, Fixture.carolSk]]

addUTxOToDecrement :: Snapshot Tx -> UTxO -> UTxO
addUTxOToDecrement snapshot spendableUTxO = do
let (headUTxO, rest) = splitHeadUTxO spendableUTxO
let headValue = getLovelace headUTxO
-- | Adds a decommit value to the head UTxO. Decrement tx subtracts this value
-- from the Head so we need to make sure it is present in the UTxO since our
-- snapshots are just generated out of thin air.
addDecrementValue :: Snapshot Tx -> UTxO -> UTxO
addDecrementValue snapshot spendableUTxO = do
case utxoToDecommit snapshot of
Nothing -> spendableUTxO
Just toDecommit -> do
let decommitValue = foldMap (txOutValue . snd) (UTxO.pairs toDecommit)
let (headUTxO, rest) = splitHeadUTxO spendableUTxO
let (headIn, headOut) = List.head $ UTxO.pairs headUTxO
let newHeadOutput = UTxO.singleton (headIn, headOut & modifyTxOutValue (<> decommitValue)) <> toDecommit
let newHeadOutput = UTxO.singleton (headIn, headOut & modifyTxOutValue (<> decommitValue))
newHeadOutput <> rest

getLovelace :: UTxO -> Coin
Expand Down Expand Up @@ -504,16 +507,16 @@ openHeadUTxO =
newDecrementTx :: HasCallStack => Actor -> (Snapshot Tx, MultiSignature (Snapshot Tx)) -> AppM Tx
newDecrementTx actor (snapshot, signatures) = do
spendableUTxO <- get
let newSpendableUTxO = addUTxOToDecrement snapshot spendableUTxO
let newSpendableUTxO = addDecrementValue snapshot spendableUTxO
put newSpendableUTxO
either (failure . show) pure $
decrement
(actorChainContext actor)
(mkHeadId Fixture.testPolicyId)
Fixture.testHeadParameters
newSpendableUTxO
snapshot
signatures
decrement
(actorChainContext actor)
(mkHeadId Fixture.testPolicyId)
Fixture.testHeadParameters
newSpendableUTxO
snapshot
signatures

-- | Creates a transaction that closes 'openHeadUTxO' with given the snapshot.
-- NOTE: This uses fixtures for headId, parties (alice, bob, carol),
Expand Down

0 comments on commit d33cfa1

Please sign in to comment.