Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test suite for testing consecutive close/contest transactions #1385

Merged
merged 29 commits into from
Apr 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ea0d7fd
Add simple model of the world to the TxTraceSpec
v0d1ch Mar 26, 2024
a2a14db
Make TxTraceSpec use quickcheck-dynamic
ch1bo Mar 26, 2024
4ae1b87
Add contest coverage
v0d1ch Mar 26, 2024
7ea1caf
Add RunModel instance
v0d1ch Mar 26, 2024
4c4aaf3
Use IO to actually force perform
ch1bo Mar 26, 2024
fd87778
Shorten traces using the precondition
ch1bo Mar 26, 2024
1df02a7
Evaluate healthy close transactions
v0d1ch Mar 26, 2024
0cd3442
Update the model to include list of Snapshots
v0d1ch Mar 27, 2024
efcc68f
Produce arbitrary snapshots
ch1bo Mar 27, 2024
471f109
Generate Close with a snapshot
v0d1ch Mar 27, 2024
bc91619
Fix model update for ProduceSnapshots
ch1bo Mar 28, 2024
35f170b
Move genForParty into Test.Hydra.Fixture
ch1bo Mar 28, 2024
510943e
Add testHeadParameters fixture
ch1bo Mar 28, 2024
335399b
Build a close transaction in TxTraceSpec
ch1bo Mar 28, 2024
d3311f6
Re-order things in TxTraceSpec and fix participation tokens
ch1bo Mar 28, 2024
1f6d930
Simplify the snapshot model and use correctly signed snapshots
ch1bo Mar 28, 2024
1e304a7
Further simplify model
ch1bo Mar 28, 2024
51f4d55
Track UTxO in model and extend Contest action
ch1bo Mar 28, 2024
d19eaad
Build and evaluate contest tx
ch1bo Apr 5, 2024
75c555d
Dry tx validation
ch1bo Apr 5, 2024
668ba43
Filter too old snapshots on close
ch1bo Apr 5, 2024
d63524a
Add observation check to contest
ch1bo Apr 5, 2024
eb3d125
Also filter snapshots after contest
ch1bo Apr 5, 2024
bd8a342
Add actors to Close/Contest actions
ch1bo Apr 5, 2024
1f998f1
Only close/contest with actors still possible
ch1bo Apr 5, 2024
37dd826
ContestObservation in action result
ch1bo Apr 5, 2024
97be612
Add assertion on contesters
ch1bo Apr 5, 2024
3e36262
Fix TxTrace model to allow contest of closing actor
ch1bo Apr 5, 2024
c7171e2
Resolve TODOs and update comments
ch1bo Apr 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Build and evaluate contest tx
ch1bo committed Apr 8, 2024
commit d19eaad56b689fe8b715b55e2edc044eae1670fa
1 change: 1 addition & 0 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Original file line number Diff line number Diff line change
@@ -534,6 +534,7 @@ contest ::
HeadId ->
ContestationPeriod ->
ConfirmedSnapshot Tx ->
-- | Current slot and posix time to be used as the contestation time.
PointInTime ->
Either ContestTxError Tx
contest ctx spendableUTxO headId contestationPeriod confirmedSnapshot pointInTime = do
58 changes: 44 additions & 14 deletions hydra-node/test/Hydra/Chain/Direct/TxTraceSpec.hs
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import Hydra.Cardano.Api.Pretty (renderTxWithUTxO)
import Hydra.Chain.Direct.Contract.Mutation (addParticipationTokens)
import Hydra.Chain.Direct.Fixture qualified as Fixture
import Hydra.Chain.Direct.ScriptRegistry (ScriptRegistry, genScriptRegistry, registryUTxO)
import Hydra.Chain.Direct.State (ChainContext (..), close)
import Hydra.Chain.Direct.State (ChainContext (..), close, contest)
import Hydra.Chain.Direct.Tx (headIdToCurrencySymbol, mkHeadId, mkHeadOutput, observeHeadTx)
import Hydra.Chain.Direct.Tx qualified as Tx
import Hydra.ContestationPeriod qualified as CP
@@ -88,23 +88,25 @@ prop_traces =

prop_runActions :: Actions Model -> Property
prop_runActions actions =
monadicIO $
monadicIO $ do
print actions
void (runActions actions)

-- * Model

data Model = Model
{ snapshots :: [SnapshotNumber]
, headState :: State
, utxo :: Var UTxO
, utxoV :: Var UTxO
-- ^ Last known, spendable UTxO.
}
deriving (Show)

data State
= Open
| Closed
| Final
deriving (Show)
deriving (Show, Eq)

instance StateModel Model where
data Action Model a where
@@ -121,12 +123,15 @@ instance StateModel Model where
case headState of
Open ->
oneof
[ Some . ProduceSnapshots <$> arbitrary
[ -- TODO: non-continuous snapshot numbers
Some . ProduceSnapshots <$> arbitrary
, Some . Close <$> elements (0 : snapshots)
]
Closed ->
oneof
[ Some . Contest <$> elements (0 : snapshots) -- FIXME: needs to depend on currently closed snapshot
[ -- NOTE: Adding 0 to have elements always pass, but precondition
-- would disallow contest with 0.
Some . Contest <$> elements (0 : snapshots) -- FIXME: needs to depend on currently closed snapshot
, pure $ Some Fanout
]
Final -> pure $ Some Stop
@@ -137,20 +142,21 @@ instance StateModel Model where
Model
{ snapshots = []
, headState = Open
, utxo = mkVar 1 -- TODO: what does '1' mean here?
, utxoV = mkVar 1 -- TODO: what does '1' mean here?
}

nextState :: Model -> Action Model a -> Var a -> Model
nextState m Stop _ = m
nextState m t result =
case t of
ProduceSnapshots snapshots -> m{snapshots = snapshots}
Close{} -> m{headState = Closed, utxo = result}
Close{} -> m{headState = Closed, utxoV = result}
Contest{} -> m{headState = Closed}
Fanout -> m{headState = Final}

precondition :: Model -> Action Model a -> Bool
precondition Model{headState = Final} Stop = False
precondition Model{headState} (Contest sn) = headState == Closed && sn /= 0
precondition _ _ = True

instance HasVariables Model where
@@ -164,11 +170,7 @@ deriving instance Show (Action Model a)

instance RunModel Model IO where
perform :: Model -> Action Model a -> LookUp IO -> IO a
perform m action _lookup = do
case headState m of
Open -> putStrLn "=========OPEN======="
_ -> pure ()

perform Model{utxoV} action lookupVar = do
putStrLn $ "performing action: " <> show action

case action of
@@ -192,10 +194,25 @@ instance RunModel Model IO where
observation -> failure $ "Expected Close observation, but got " <> show observation

pure $ adjustUTxO tx openHeadUTxO
Contest snapshotNumber ->
Contest snapshotNumber -> do
-- FIXME: Implement real contestTx + eval + observation
-- NOTE: Should not happen anymore
when (snapshotNumber == 0) $
failure "Cannot contest initial snapshot"
let utxo = lookupVar utxoV
tx <- newContestTx utxo $ correctlySignedSnapshot snapshotNumber
case evaluateTx tx utxo of
Left err ->
failure $ show err
Right redeemerReport ->
when (any isLeft (Map.elems redeemerReport)) $
failure . toString . unlines $
fromString
<$> [ renderTxWithUTxO utxo tx
, "Some redeemers failed: " <> show redeemerReport
]

pure ()
Fanout -> pure ()
Stop -> pure ()

@@ -272,6 +289,19 @@ newCloseTx snapshot =

upperBound = (0, posixSecondsToUTCTime 0)

newContestTx :: HasCallStack => UTxO -> ConfirmedSnapshot Tx -> IO Tx
newContestTx spendableUTxO snapshot =
either (failure . show) pure $
contest
aliceChainContext
spendableUTxO
(mkHeadId Fixture.testPolicyId)
Fixture.cperiod
snapshot
currentTime
where
currentTime = (0, posixSecondsToUTCTime 0)

-- | Fixture for the chain context of 'alice' on 'testNetworkId'. Uses a generated 'ScriptRegistry'.
-- TODO: move to Hydra.Chain.Direct.Fixture / into a testlib
aliceChainContext :: ChainContext