Skip to content

Commit

Permalink
Fix genDecrementTx to use correct snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo authored and v0d1ch committed May 7, 2024
1 parent d53d1bd commit f4dc94b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
15 changes: 12 additions & 3 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ import Hydra.Snapshot (
genConfirmedSnapshot,
getSnapshot,
)
import Test.QuickCheck (choose, frequency, oneof, suchThat, vector)
import Test.QuickCheck (choose, frequency, getPositive, oneof, vector)
import Test.QuickCheck.Gen (elements)
import Test.QuickCheck.Modifiers (Positive (Positive))

Expand Down Expand Up @@ -1028,13 +1028,22 @@ genCollectComTx = do
genDecrementTx :: Int -> Gen (ChainContext, OpenState, Tx)
genDecrementTx numParties = do
ctx <- genHydraContextFor numParties
(_, stOpen@OpenState{headId}) <- genStOpen ctx
(u0, stOpen@OpenState{headId}) <- genStOpen ctx
cctx <- pickChainContext ctx
snapshot <- arbitrary `suchThat` (\Snapshot{utxoToDecommit} -> isJust utxoToDecommit)
snapshot <- do
number <- getPositive <$> arbitrary
(utxo, toDecommit) <- splitUTxO u0
pure Snapshot{headId, number, confirmed = [], utxo, utxoToDecommit = Just toDecommit}
signatures <- arbitrary
let openUTxO = getKnownUTxO stOpen
pure (cctx, stOpen, unsafeDecrement cctx headId (ctxHeadParameters ctx) openUTxO snapshot signatures)

splitUTxO :: UTxO -> Gen (UTxO, UTxO)
splitUTxO utxo = do
ix <- choose (0, length utxo)
let (p1, p2) = splitAt ix (UTxO.pairs utxo)
pure (UTxO.fromPairs p1, UTxO.fromPairs p2)

genCloseTx :: Int -> Gen (ChainContext, OpenState, Tx, ConfirmedSnapshot Tx)
genCloseTx numParties = do
ctx <- genHydraContextFor numParties
Expand Down
2 changes: 2 additions & 0 deletions hydra-node/src/Hydra/Snapshot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ data Snapshot tx = Snapshot
-- ^ The set of transactions that lead to 'utxo'
, utxoToDecommit :: Maybe (UTxOType tx)
-- ^ UTxO to be decommitted. Spec: Ûω
-- TODO: what is the difference between Noting and (Just mempty) here?
}
deriving stock (Generic)

Expand Down Expand Up @@ -181,6 +182,7 @@ genConfirmedSnapshot headId minSn utxo sks
-- snapshots
number <- arbitrary `suchThat` (> minSn)
-- TODO: check whether we are fine with this not producing any decommitting utxo ever
-- TODO: use splitUTxO generator
let snapshot = Snapshot{headId, number, utxo, confirmed = [], utxoToDecommit = mempty}
let signatures = aggregate $ fmap (`sign` snapshot) sks
pure $ ConfirmedSnapshot{snapshot, signatures}
Expand Down
1 change: 1 addition & 0 deletions hydra-node/test/Hydra/Chain/Direct/Contract/Decrement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ healthySnapshotNumber = 1

healthySnapshot :: Snapshot Tx
healthySnapshot =
-- TODO: use splitUTxO generator
let (utxoToDecommit', utxo) = splitDecommitUTxO healthyUTxO
in Snapshot
{ headId = mkHeadId testPolicyId
Expand Down

0 comments on commit f4dc94b

Please sign in to comment.