{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Hydra.HeadLogic.ReplaySpec (spec) where
import Hydra.Prelude
import Test.Hydra.Prelude (Expectation, Spec, describe, it, shouldBe)
import Cardano.Binary (decodeFull', serialize')
import Control.Monad (foldM)
import Data.List qualified as List
import Data.Map.Strict qualified as Map
import Hydra.HeadLogic.Outcome (StateChanged (..))
import Hydra.HeadLogic.Snapshot.Cached qualified as Cached
import Hydra.HeadLogic.State (CoordinatedHeadState (..), FinalizedSnapshot (..), HeadState (..), OpenState (..), SeenSnapshot (..))
import Hydra.HeadLogic.State.Coordinated qualified as Coordinated
import Hydra.HeadLogic.State.Node qualified as Machine
import Hydra.HeadLogicSpec (inOpenState, testSnapshot, withAccumulators)
import Hydra.Ledger.Simple (SimpleChainState (..), SimpleTx (..))
import Hydra.Node.State (Deposit (..), DepositStatus (..), NodeState (..), TrackedDeposit (..), initialChainTime)
import Hydra.Tx.Crypto (aggregate, sign)
import Hydra.Tx.IsTx (IsTx (..))
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..), Snapshot (..), SnapshotVersion)
import Test.Hydra.Ledger.Simple (utxoRef)
import Test.Hydra.Tx.Fixture (alice, aliceSk, testHeadId)
spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"commit and deposit replay" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
[Bool] -> (Bool -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> Spec) -> Spec) -> (Bool -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \Bool
settleFirst ->
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (if Bool
settleFirst then String
"restores settlement before snapshot confirmation" else String
"restores approval before settlement without making the deposit spendable early") (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let signature :: Signature (Snapshot SimpleTx)
signature = Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk Snapshot SimpleTx
snapshot
signatures :: MultiSignature (Snapshot SimpleTx)
signatures = [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Signature (Snapshot SimpleTx)
signature]
confirmed :: ConfirmedSnapshot SimpleTx
confirmed = Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot Snapshot SimpleTx
snapshot MultiSignature (Snapshot SimpleTx)
signatures
settled :: StateChanged SimpleTx
settled = ChainStateType SimpleTx
-> HeadId
-> SnapshotVersion
-> TxIdType SimpleTx
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> SnapshotVersion -> TxIdType tx -> StateChanged tx
CommitFinalized (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) HeadId
testHeadId SnapshotVersion
1 Integer
TxIdType SimpleTx
depositId
spend :: SimpleTx
spend = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
100 Set SimpleTxOut
UTxOType SimpleTx
deposited (Integer -> UTxOType SimpleTx
utxoRef Integer
100)
checkConfirmed :: NodeState SimpleTx -> Expectation
checkConfirmed NodeState SimpleTx
node = do
(NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated NodeState SimpleTx
node).confirmedSnapshot ConfirmedSnapshot SimpleTx
-> ConfirmedSnapshot SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` ConfirmedSnapshot SimpleTx
confirmed
(NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated NodeState SimpleTx
node).seenSnapshot SeenSnapshot SimpleTx -> SeenSnapshot SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SnapshotNumber -> SeenSnapshot SimpleTx
forall tx. SnapshotNumber -> SeenSnapshot tx
LastSeenSnapshot SnapshotNumber
1
checkSettled :: NodeState SimpleTx -> Expectation
checkSettled NodeState SimpleTx
node = do
NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Map Integer (Deposit SimpleTx)
forall a. Monoid a => a
mempty
Integer
-> Map Integer (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Integer
depositId NodeState SimpleTx
node.deposits Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` TrackedDeposit SimpleTx -> Maybe (TrackedDeposit SimpleTx)
forall a. a -> Maybe a
Just (Deposit SimpleTx
-> ChainSlot -> Maybe ChainSlot -> TrackedDeposit SimpleTx
forall tx.
Deposit tx -> ChainSlot -> Maybe ChainSlot -> TrackedDeposit tx
TrackedDeposit Deposit SimpleTx
active ChainSlot
1 (ChainSlot -> Maybe ChainSlot
forall a. a -> Maybe a
Just ChainSlot
3))
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
deposited SnapshotVersion
1 NodeState SimpleTx
node
checkFinalized :: NodeState SimpleTx -> Expectation
checkFinalized NodeState SimpleTx
node = do
NodeState SimpleTx -> Expectation
checkConfirmed NodeState SimpleTx
node
NodeState SimpleTx -> Expectation
checkSettled NodeState SimpleTx
node
(NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated NodeState SimpleTx
node).finalizedCommit Maybe (FinalizedSnapshot SimpleTx)
-> Maybe (FinalizedSnapshot SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` FinalizedSnapshot SimpleTx -> Maybe (FinalizedSnapshot SimpleTx)
forall a. a -> Maybe a
Just (ConfirmedSnapshot SimpleTx
-> ChainSlot -> FinalizedSnapshot SimpleTx
forall tx.
ConfirmedSnapshot tx -> ChainSlot -> FinalizedSnapshot tx
FinalizedSnapshot ConfirmedSnapshot SimpleTx
confirmed ChainSlot
3)
ending :: [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
ending
| Bool
settleFirst =
[
( StateChanged SimpleTx
settled
, \NodeState SimpleTx
node -> do
NodeState SimpleTx -> Expectation
checkSettled NodeState SimpleTx
node
case (NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated NodeState SimpleTx
node).seenSnapshot of
SeenSnapshot{CachedSnapshot SimpleTx
cachedSnapshot :: CachedSnapshot SimpleTx
$sel:cachedSnapshot:NoSeenSnapshot :: forall tx. SeenSnapshot tx -> CachedSnapshot tx
cachedSnapshot} -> CachedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. CachedSnapshot tx -> Snapshot tx
Cached.snapshot CachedSnapshot SimpleTx
cachedSnapshot Snapshot SimpleTx -> Snapshot SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Snapshot SimpleTx
snapshot
SeenSnapshot SimpleTx
seen -> String -> Expectation
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Expectation) -> String -> Expectation
forall a b. (a -> b) -> a -> b
$ String
"expected a snapshot collecting signatures, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> SeenSnapshot SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show SeenSnapshot SimpleTx
seen
)
, (HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId (Snapshot SimpleTx -> Maybe (Snapshot SimpleTx)
forall a. a -> Maybe a
Just Snapshot SimpleTx
snapshot) MultiSignature (Snapshot SimpleTx)
signatures, NodeState SimpleTx -> Expectation
checkFinalized)
]
| Bool
otherwise =
[ (HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing MultiSignature (Snapshot SimpleTx)
signatures, \NodeState SimpleTx
node -> NodeState SimpleTx -> Expectation
checkConfirmed NodeState SimpleTx
node Expectation -> Expectation -> Expectation
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node)
,
( HeadId -> UTxOType SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> UTxOType tx -> StateChanged tx
CommitApproved HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
deposited
, \NodeState SimpleTx
node -> do
NodeState SimpleTx -> Expectation
checkConfirmed NodeState SimpleTx
node
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Deposit SimpleTx -> Map Integer (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton Integer
depositId Deposit SimpleTx
active
)
, (StateChanged SimpleTx
settled, NodeState SimpleTx -> Expectation
checkFinalized)
]
[(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation
checkReplay ([(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation)
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation
forall a b. (a -> b) -> a -> b
$
[
( StateChanged SimpleTx
recorded
, \NodeState SimpleTx
node -> do
Integer
-> Map Integer (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Integer
depositId NodeState SimpleTx
node.deposits Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` TrackedDeposit SimpleTx -> Maybe (TrackedDeposit SimpleTx)
forall a. a -> Maybe a
Just (Deposit SimpleTx
-> ChainSlot -> Maybe ChainSlot -> TrackedDeposit SimpleTx
forall tx.
Deposit tx -> ChainSlot -> Maybe ChainSlot -> TrackedDeposit tx
TrackedDeposit Deposit SimpleTx
inactive ChainSlot
1 Maybe ChainSlot
forall a. Maybe a
Nothing)
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
)
,
( StateChanged SimpleTx
activated
, \NodeState SimpleTx
node -> do
NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Deposit SimpleTx -> Map Integer (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton Integer
depositId Deposit SimpleTx
active
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
)
, (Snapshot SimpleTx
-> Seq SimpleTx
-> Maybe (TxIdType SimpleTx)
-> StateChanged SimpleTx
forall tx.
Snapshot tx -> Seq tx -> Maybe (TxIdType tx) -> StateChanged tx
SnapshotRequested Snapshot SimpleTx
snapshot Seq SimpleTx
forall a. Monoid a => a
mempty (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId), Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0)
, (SnapshotNumber
-> Party -> Signature (Snapshot SimpleTx) -> StateChanged SimpleTx
forall tx.
SnapshotNumber
-> Party -> Signature (Snapshot tx) -> StateChanged tx
PartySignedSnapshot SnapshotNumber
1 Party
alice Signature (Snapshot SimpleTx)
signature, Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0)
]
[(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
forall a. Semigroup a => a -> a -> a
<> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
ending
[(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
forall a. Semigroup a => a -> a -> a
<> [ (SimpleTx -> StateChanged SimpleTx
forall tx. tx -> StateChanged tx
TransactionReceived SimpleTx
spend, Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
deposited SnapshotVersion
1)
, (HeadId -> SimpleTx -> StateChanged SimpleTx
forall tx. HeadId -> tx -> StateChanged tx
TransactionAppliedToLocalUTxO HeadId
testHeadId SimpleTx
spend, Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing (Integer -> UTxOType SimpleTx
utxoRef Integer
100) SnapshotVersion
1)
,
(StateChanged SimpleTx
settled, Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing (Integer -> UTxOType SimpleTx
utxoRef Integer
100) SnapshotVersion
1)
]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"restores expiry, recovery and rollback in the registry and selected deposit" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let expired :: Deposit SimpleTx
expired = Deposit SimpleTx
active{status = Expired}
[(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation
checkReplay
[ (StateChanged SimpleTx
recorded, \NodeState SimpleTx
node -> NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Deposit SimpleTx -> Map Integer (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton Integer
depositId Deposit SimpleTx
inactive)
, (StateChanged SimpleTx
activated, Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0)
,
( TxIdType SimpleTx
-> UTCTime -> Deposit SimpleTx -> StateChanged SimpleTx
forall tx. TxIdType tx -> UTCTime -> Deposit tx -> StateChanged tx
DepositExpired Integer
TxIdType SimpleTx
depositId UTCTime
initialChainTime Deposit SimpleTx
expired
, \NodeState SimpleTx
node -> do
NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Deposit SimpleTx -> Map Integer (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton Integer
depositId Deposit SimpleTx
expired
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
depositId) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
)
,
( ChainStateType SimpleTx
-> HeadId
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> TxIdType tx -> UTxOType tx -> StateChanged tx
DepositRecovered (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) HeadId
testHeadId Integer
TxIdType SimpleTx
depositId Set SimpleTxOut
UTxOType SimpleTx
deposited
, \NodeState SimpleTx
node -> do
NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Map Integer (Deposit SimpleTx)
forall a. Monoid a => a
mempty
Integer
-> Map Integer (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Integer
depositId NodeState SimpleTx
node.deposits Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` TrackedDeposit SimpleTx -> Maybe (TrackedDeposit SimpleTx)
forall a. a -> Maybe a
Just (Deposit SimpleTx
-> ChainSlot -> Maybe ChainSlot -> TrackedDeposit SimpleTx
forall tx.
Deposit tx -> ChainSlot -> Maybe ChainSlot -> TrackedDeposit tx
TrackedDeposit Deposit SimpleTx
expired ChainSlot
1 (ChainSlot -> Maybe ChainSlot
forall a. a -> Maybe a
Just ChainSlot
3))
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
)
,
( ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2)
, \NodeState SimpleTx
node -> do
NodeState SimpleTx
node.pendingDeposits Map Integer (Deposit SimpleTx)
-> Map Integer (Deposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Deposit SimpleTx -> Map Integer (Deposit SimpleTx)
forall k a. k -> a -> Map k a
Map.singleton Integer
depositId Deposit SimpleTx
expired
Integer
-> Map Integer (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Integer
depositId NodeState SimpleTx
node.deposits Maybe (TrackedDeposit SimpleTx)
-> Maybe (TrackedDeposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` TrackedDeposit SimpleTx -> Maybe (TrackedDeposit SimpleTx)
forall a. a -> Maybe a
Just (Deposit SimpleTx
-> ChainSlot -> Maybe ChainSlot -> TrackedDeposit SimpleTx
forall tx.
Deposit tx -> ChainSlot -> Maybe ChainSlot -> TrackedDeposit tx
TrackedDeposit Deposit SimpleTx
expired ChainSlot
1 Maybe ChainSlot
forall a. Maybe a
Nothing)
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
)
,
( ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
0)
, \NodeState SimpleTx
node -> do
NodeState SimpleTx
node.deposits Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
-> Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Map (TxIdType SimpleTx) (TrackedDeposit SimpleTx)
forall a. Monoid a => a
mempty
Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node
)
]
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retains a signed commit through expiry, recovery and rollback until a replacement is confirmed" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let expired :: Deposit SimpleTx
expired = Deposit SimpleTx
active{status = Expired}
signatures :: MultiSignature (Snapshot SimpleTx)
signatures = [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk Snapshot SimpleTx
snapshot]
replacement :: Snapshot SimpleTx
replacement = SnapshotNumber
-> SnapshotVersion
-> [SimpleTx]
-> UTxOType SimpleTx
-> Snapshot SimpleTx
forall tx.
IsTx tx =>
SnapshotNumber
-> SnapshotVersion -> [tx] -> UTxOType tx -> Snapshot tx
testSnapshot SnapshotNumber
2 SnapshotVersion
0 [] UTxOType SimpleTx
forall a. Monoid a => a
mempty
phase :: Text -> NodeState SimpleTx -> Expectation
phase :: Text -> NodeState SimpleTx -> Expectation
phase Text
expected NodeState SimpleTx
node = case NodeState SimpleTx
node.headState of
Open OpenState SimpleTx
open -> Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Text -> [(Text, Maybe Text)] -> Maybe (Maybe Text)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
List.lookup Text
"Commits" ([(Text, Maybe Text)] -> Maybe (Maybe Text))
-> [(Text, Maybe Text)] -> Maybe (Maybe Text)
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect (Runtime SimpleTx -> [(Text, Maybe Text)])
-> Runtime SimpleTx -> [(Text, Maybe Text)]
forall a b. (a -> b) -> a -> b
$ OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open) Maybe Text -> Maybe Text -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
expected
HeadState SimpleTx
_ -> String -> Expectation
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"expected an open Head"
awaiting :: NodeState SimpleTx -> Expectation
awaiting = Text -> NodeState SimpleTx -> Expectation
phase Text
"AwaitingCommitSettlement"
[(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation
checkReplay
[ (StateChanged SimpleTx
recorded, Text -> NodeState SimpleTx -> Expectation
phase Text
"NoCommit")
, (StateChanged SimpleTx
activated, Text -> NodeState SimpleTx -> Expectation
phase Text
"CommitSelected")
, (HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId (Snapshot SimpleTx -> Maybe (Snapshot SimpleTx)
forall a. a -> Maybe a
Just Snapshot SimpleTx
snapshot) MultiSignature (Snapshot SimpleTx)
signatures, NodeState SimpleTx -> Expectation
awaiting)
, (TxIdType SimpleTx
-> UTCTime -> Deposit SimpleTx -> StateChanged SimpleTx
forall tx. TxIdType tx -> UTCTime -> Deposit tx -> StateChanged tx
DepositExpired Integer
TxIdType SimpleTx
depositId UTCTime
initialChainTime Deposit SimpleTx
expired, NodeState SimpleTx -> Expectation
awaiting)
, (ChainStateType SimpleTx
-> HeadId
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId -> TxIdType tx -> UTxOType tx -> StateChanged tx
DepositRecovered (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) HeadId
testHeadId Integer
TxIdType SimpleTx
depositId Set SimpleTxOut
UTxOType SimpleTx
deposited, \NodeState SimpleTx
node -> NodeState SimpleTx -> Expectation
awaiting NodeState SimpleTx
node Expectation -> Expectation -> Expectation
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SnapshotVersion
0 NodeState SimpleTx
node)
, (ChainStateType SimpleTx -> StateChanged SimpleTx
forall tx. ChainStateType tx -> StateChanged tx
ChainRolledBack (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2), NodeState SimpleTx -> Expectation
awaiting)
, (Snapshot SimpleTx
-> Seq SimpleTx
-> Maybe (TxIdType SimpleTx)
-> StateChanged SimpleTx
forall tx.
Snapshot tx -> Seq tx -> Maybe (TxIdType tx) -> StateChanged tx
SnapshotRequested Snapshot SimpleTx
replacement Seq SimpleTx
forall a. Monoid a => a
mempty Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing, NodeState SimpleTx -> Expectation
awaiting)
, (HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
SnapshotConfirmed HeadId
testHeadId Maybe (Snapshot SimpleTx)
forall a. Maybe a
Nothing ([Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
aliceSk Snapshot SimpleTx
replacement]), Text -> NodeState SimpleTx -> Expectation
phase Text
"NoCommit")
]
where
depositId :: Integer
depositId = Integer
7
deposited :: UTxOType SimpleTx
deposited = Integer -> UTxOType SimpleTx
utxoRef Integer
7
inactive :: Deposit SimpleTx
inactive = HeadId
-> UTxOType SimpleTx
-> UTCTime
-> UTCTime
-> DepositStatus
-> Deposit SimpleTx
forall tx.
HeadId
-> UTxOType tx -> UTCTime -> UTCTime -> DepositStatus -> Deposit tx
Deposit HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
deposited UTCTime
initialChainTime (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
3600 UTCTime
initialChainTime) DepositStatus
Inactive
active :: Deposit SimpleTx
active = Deposit SimpleTx
inactive{status = Active}
recorded :: StateChanged SimpleTx
recorded = ChainStateType SimpleTx
-> HeadId
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> UTCTime
-> UTCTime
-> StateChanged SimpleTx
forall tx.
ChainStateType tx
-> HeadId
-> TxIdType tx
-> UTxOType tx
-> UTCTime
-> UTCTime
-> StateChanged tx
DepositRecorded (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
1) HeadId
testHeadId Integer
TxIdType SimpleTx
depositId Set SimpleTxOut
UTxOType SimpleTx
deposited Deposit SimpleTx
inactive.created Deposit SimpleTx
inactive.deadline
activated :: StateChanged SimpleTx
activated = TxIdType SimpleTx
-> UTCTime -> Deposit SimpleTx -> StateChanged SimpleTx
forall tx. TxIdType tx -> UTCTime -> Deposit tx -> StateChanged tx
DepositActivated Integer
TxIdType SimpleTx
depositId UTCTime
initialChainTime Deposit SimpleTx
active
snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => Snapshot tx -> Snapshot tx
withAccumulators (Snapshot SimpleTx -> Snapshot SimpleTx)
-> Snapshot SimpleTx -> Snapshot SimpleTx
forall a b. (a -> b) -> a -> b
$ (SnapshotNumber
-> SnapshotVersion
-> [SimpleTx]
-> UTxOType SimpleTx
-> Snapshot SimpleTx
forall tx.
IsTx tx =>
SnapshotNumber
-> SnapshotVersion -> [tx] -> UTxOType tx -> Snapshot tx
testSnapshot SnapshotNumber
1 SnapshotVersion
0 [] Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty){utxoToCommit = Just deposited, depositTxId = Just depositId}
checkReplay :: [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)] -> Expectation
checkReplay :: [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation
checkReplay = IO (Runtime SimpleTx) -> Expectation
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Runtime SimpleTx) -> Expectation)
-> ([(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> IO (Runtime SimpleTx))
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> Expectation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Runtime SimpleTx
-> (StateChanged SimpleTx, NodeState SimpleTx -> Expectation)
-> IO (Runtime SimpleTx))
-> Runtime SimpleTx
-> [(StateChanged SimpleTx, NodeState SimpleTx -> Expectation)]
-> IO (Runtime SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Runtime SimpleTx
-> (StateChanged SimpleTx, NodeState SimpleTx -> Expectation)
-> IO (Runtime SimpleTx)
step (NodeState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => NodeState tx -> Runtime tx
Machine.start (NodeState SimpleTx -> Runtime SimpleTx)
-> NodeState SimpleTx -> Runtime SimpleTx
forall a b. (a -> b) -> a -> b
$ [Party] -> NodeState SimpleTx
inOpenState [Party
alice])
where
step :: Machine.Runtime SimpleTx -> (StateChanged SimpleTx, NodeState SimpleTx -> Expectation) -> IO (Machine.Runtime SimpleTx)
step :: Runtime SimpleTx
-> (StateChanged SimpleTx, NodeState SimpleTx -> Expectation)
-> IO (Runtime SimpleTx)
step Runtime SimpleTx
runtime (StateChanged SimpleTx
event, NodeState SimpleTx -> Expectation
check) = do
NodeState SimpleTx
restored <- (DecoderError -> IO (NodeState SimpleTx))
-> (NodeState SimpleTx -> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (NodeState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (NodeState SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (NodeState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx))
-> Either DecoderError (NodeState SimpleTx)
-> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (NodeState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (NodeState SimpleTx))
-> ByteString -> Either DecoderError (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' (NodeState SimpleTx -> ByteString)
-> NodeState SimpleTx -> ByteString
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
runtime
StateChanged SimpleTx
retained <- (DecoderError -> IO (StateChanged SimpleTx))
-> (StateChanged SimpleTx -> IO (StateChanged SimpleTx))
-> Either DecoderError (StateChanged SimpleTx)
-> IO (StateChanged SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (StateChanged SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (StateChanged SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (StateChanged SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) StateChanged SimpleTx -> IO (StateChanged SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (StateChanged SimpleTx)
-> IO (StateChanged SimpleTx))
-> Either DecoderError (StateChanged SimpleTx)
-> IO (StateChanged SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (StateChanged SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (StateChanged SimpleTx))
-> ByteString -> Either DecoderError (StateChanged SimpleTx)
forall a b. (a -> b) -> a -> b
$ StateChanged SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' StateChanged SimpleTx
event
let next :: Runtime SimpleTx
next = Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
Runtime tx -> StateChanged tx -> Runtime tx
Machine.step Runtime SimpleTx
runtime StateChanged SimpleTx
event
node :: NodeState SimpleTx
node = Runtime SimpleTx -> NodeState SimpleTx
forall tx. Runtime tx -> NodeState tx
Machine.currentState Runtime SimpleTx
next
NodeState SimpleTx -> [StateChanged SimpleTx] -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> [StateChanged tx] -> NodeState tx
Machine.applyEvents ([Party] -> NodeState SimpleTx
inOpenState [Party
alice]) [NodeState SimpleTx -> StateChanged SimpleTx
forall tx. NodeState tx -> StateChanged tx
Checkpoint NodeState SimpleTx
restored, StateChanged SimpleTx
retained] NodeState SimpleTx -> NodeState SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` NodeState SimpleTx
node
NodeState SimpleTx -> Expectation
check NodeState SimpleTx
node
Runtime SimpleTx -> IO (Runtime SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Runtime SimpleTx
next
coordinated :: NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated :: NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated NodeState SimpleTx
node = case NodeState SimpleTx
node.headState of
Open OpenState SimpleTx
open -> OpenState SimpleTx
open.coordinatedHeadState
HeadState SimpleTx
observed -> Text -> CoordinatedHeadState SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error (Text -> CoordinatedHeadState SimpleTx)
-> Text -> CoordinatedHeadState SimpleTx
forall a b. (a -> b) -> a -> b
$ Text
"expected an open head, got " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HeadState SimpleTx -> Text
forall b a. (Show a, IsString b) => a -> b
show HeadState SimpleTx
observed
checkLocal :: Maybe Integer -> UTxOType SimpleTx -> SnapshotVersion -> NodeState SimpleTx -> Expectation
checkLocal :: Maybe Integer
-> UTxOType SimpleTx
-> SnapshotVersion
-> NodeState SimpleTx
-> Expectation
checkLocal Maybe Integer
selected UTxOType SimpleTx
utxo SnapshotVersion
version NodeState SimpleTx
node = do
let current :: CoordinatedHeadState SimpleTx
current = NodeState SimpleTx -> CoordinatedHeadState SimpleTx
coordinated NodeState SimpleTx
node
CoordinatedHeadState SimpleTx
current.currentDepositTxId Maybe Integer -> Maybe Integer -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Maybe Integer
selected
CoordinatedHeadState SimpleTx
current.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Set SimpleTxOut
UTxOType SimpleTx
utxo
CoordinatedHeadState SimpleTx
current.version SnapshotVersion -> SnapshotVersion -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SnapshotVersion
version