{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Hydra.HeadLogic.CommitPhaseSpec (spec) where
import Hydra.Prelude
import Test.Hydra.HeadLogic.Validated (checkedSnapshot, checkedTransaction)
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.Sequence qualified as Seq
import Hydra.Agda.OffChainReference qualified as Agda
import Hydra.HeadLogic.Program (Instruction (VerifySnapshot), Program, foldProgram)
import Hydra.HeadLogic.Snapshot.Verification (VerifiedSnapshot, verifyCandidate, verifySignedSnapshot)
import Hydra.HeadLogic.State (CoordinatedHeadState (..), HeadState (Open), OpenState (..), SeenSnapshot (LastSeenSnapshot))
import Hydra.HeadLogic.State.Coordinated qualified as Coordinated
import Hydra.HeadLogicSpec (inOpenState, testSnapshot, withAccumulators)
import Hydra.Ledger.Simple (SimpleChainState (..), SimpleTx (..))
import Hydra.Node.State (NodeState (headState))
import Hydra.Tx (UTxOType)
import Hydra.Tx.Crypto (aggregate, getVerificationKey, sign, verifyMultiSignatureBytes)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (ConfirmedSnapshot), Snapshot (..))
import Test.Hydra.Ledger.Simple (utxoRef)
import Test.Hydra.Tx.Fixture (alice, aliceSk)
spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"commit ownership phases" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"restores selection and signed authority independently, including legacy certificates without a deposit id" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[(Maybe SimpleId, SimpleId, SimpleId, Maybe (Set SimpleTxOut),
Maybe SimpleId)]
-> ((Maybe SimpleId, SimpleId, SimpleId, Maybe (Set SimpleTxOut),
Maybe SimpleId)
-> IO ())
-> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Maybe SimpleId
selected, SimpleId
version, SimpleId
signedVersion, Maybe (Set SimpleTxOut)
claim, Maybe SimpleId
depositIdentity) | Maybe SimpleId
selected <- [Maybe SimpleId
forall a. Maybe a
Nothing, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
7], SimpleId
version <- [SimpleId
0 .. SimpleId
2], SimpleId
signedVersion <- [SimpleId
0 .. SimpleId
2], Maybe (Set SimpleTxOut)
claim <- [Maybe (Set SimpleTxOut)
forall a. Maybe a
Nothing, Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just Set SimpleTxOut
forall a. Monoid a => a
mempty, Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just Set SimpleTxOut
UTxOType SimpleTx
deposited], Maybe SimpleId
depositIdentity <- [Maybe SimpleId
forall a. Maybe a
Nothing, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
7]] (((Maybe SimpleId, SimpleId, SimpleId, Maybe (Set SimpleTxOut),
Maybe SimpleId)
-> IO ())
-> IO ())
-> ((Maybe SimpleId, SimpleId, SimpleId, Maybe (Set SimpleTxOut),
Maybe SimpleId)
-> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \(Maybe SimpleId
selected, SimpleId
version, SimpleId
signedVersion, Maybe (Set SimpleTxOut)
claim, Maybe SimpleId
depositIdentity) -> do
let signed :: Snapshot SimpleTx
signed = Snapshot SimpleTx
snapshot{version = fromInteger signedVersion, utxoToCommit = claim, depositTxId = depositIdentity}
before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{version = fromInteger version, currentDepositTxId = selected, confirmedSnapshot = ConfirmedSnapshot signed mempty}}
expected :: HsCommitPhase
expected = Bool -> SimpleId -> SimpleId -> Bool -> HsCommitPhase
Agda.commitPhaseRef (Maybe SimpleId -> Bool
forall a. Maybe a -> Bool
isJust Maybe SimpleId
selected) SimpleId
version SimpleId
signedVersion (Maybe (Set SimpleTxOut) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (Set SimpleTxOut)
claim)
Runtime SimpleTx -> Maybe Text
forall tx. Runtime tx -> Maybe Text
commitPhase (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before) Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just (HsCommitPhase -> Text
forall b a. (Show a, IsString b) => a -> b
show HsCommitPhase
expected)
[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 -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (if Bool
settleFirst then String
"retires authority when settlement precedes confirmation" else String
"confirms selection and retires authority on settlement") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let accept :: Command SimpleTx
accept = ValidatedSnapshot SimpleTx -> Seq SimpleTx -> Command SimpleTx
forall tx. ValidatedSnapshot tx -> Seq tx -> Command tx
Coordinated.acceptSnapshot (Snapshot SimpleTx -> ValidatedSnapshot SimpleTx
checkedSnapshot Snapshot SimpleTx
snapshot) Seq SimpleTx
forall a. Monoid a => a
mempty
([StateChanged SimpleTx]
_, Runtime SimpleTx
accepted) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open) Command SimpleTx
accept
Candidate SimpleTx
owned <- IO (Candidate SimpleTx)
-> (Candidate SimpleTx -> IO (Candidate SimpleTx))
-> Maybe (Candidate SimpleTx)
-> IO (Candidate SimpleTx)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> IO (Candidate SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"missing candidate") Candidate SimpleTx -> IO (Candidate SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Candidate SimpleTx) -> IO (Candidate SimpleTx))
-> Maybe (Candidate SimpleTx) -> IO (Candidate SimpleTx)
forall a b. (a -> b) -> a -> b
$ Runtime SimpleTx -> Maybe (Candidate SimpleTx)
forall tx. Runtime tx -> Maybe (Candidate tx)
Coordinated.candidate Runtime SimpleTx
accepted
VerifiedCandidate SimpleTx
evidence <- Program
SimpleTx
(Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
-> IO (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall a. Program SimpleTx a -> IO a
verifyOnly (Candidate SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
SimpleTx
(Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall tx.
Candidate tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either (LogicError tx) (VerifiedCandidate tx))
verifyCandidate Candidate SimpleTx
owned (MultiSignature (Snapshot SimpleTx)
-> Program
SimpleTx
(Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)))
-> MultiSignature (Snapshot SimpleTx)
-> Program
SimpleTx
(Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
forall a b. (a -> b) -> a -> b
$ [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]) IO (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx))
-> (Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> IO (VerifiedCandidate SimpleTx))
-> IO (VerifiedCandidate SimpleTx)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (LogicError SimpleTx -> IO (VerifiedCandidate SimpleTx))
-> (VerifiedCandidate SimpleTx -> IO (VerifiedCandidate SimpleTx))
-> Either (LogicError SimpleTx) (VerifiedCandidate SimpleTx)
-> IO (VerifiedCandidate SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (VerifiedCandidate SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (VerifiedCandidate SimpleTx))
-> (LogicError SimpleTx -> String)
-> LogicError SimpleTx
-> IO (VerifiedCandidate SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicError SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show) VerifiedCandidate SimpleTx -> IO (VerifiedCandidate SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
let confirm :: Command SimpleTx
confirm = VerifiedCandidate SimpleTx -> Command SimpleTx
forall tx. VerifiedCandidate tx -> Command tx
Coordinated.confirmSnapshot VerifiedCandidate SimpleTx
evidence
settle :: Command SimpleTx
settle = ChainStateType SimpleTx
-> SnapshotVersion
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> Command SimpleTx
forall tx.
ChainStateType tx
-> SnapshotVersion -> TxIdType tx -> UTxOType tx -> Command tx
Coordinated.recordCommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) SnapshotVersion
1 SimpleId
TxIdType SimpleTx
7 UTxOType SimpleTx
deposited
ending :: [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
ending
| Bool
settleFirst = [(Command SimpleTx
settle, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", Set SimpleTxOut
UTxOType SimpleTx
deposited), (Command SimpleTx
confirm, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", Set SimpleTxOut
UTxOType SimpleTx
deposited)]
| Bool
otherwise = [(Command SimpleTx
confirm, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
7, Text
"AwaitingCommitSettlement", Set SimpleTxOut
forall a. Monoid a => a
mempty), (Command SimpleTx
settle, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", Set SimpleTxOut
UTxOType SimpleTx
deposited)]
spend :: SimpleTx
spend = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
100 UTxOType SimpleTx
deposited (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
100)
OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps OpenState SimpleTx
open ([(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ())
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
forall a b. (a -> b) -> a -> b
$
[(Command SimpleTx
accept, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
7, Text
"CommitSelected", Set SimpleTxOut
forall a. Monoid a => a
mempty)]
[(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
forall a. Semigroup a => a -> a -> a
<> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
ending
[(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
forall a. Semigroup a => a -> a -> a
<> [ (ValidatedTransaction SimpleTx -> Command SimpleTx
forall tx. ValidatedTransaction tx -> Command tx
Coordinated.applyTransaction (UTxOType SimpleTx -> SimpleTx -> ValidatedTransaction SimpleTx
checkedTransaction UTxOType SimpleTx
deposited SimpleTx
spend), Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
100)
, (Command SimpleTx
settle, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
100)
]
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"keeps adopted authority without selection across clearing and ordinary transactions" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
VerifiedSnapshot SimpleTx
evidence <- Snapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx)
verified Snapshot SimpleTx
snapshot
let tx :: SimpleTx
tx = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
100 Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
100)
OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps
OpenState SimpleTx
open
[ (VerifiedSnapshot SimpleTx -> Command SimpleTx
forall tx. VerifiedSnapshot tx -> Command tx
Coordinated.adoptSnapshot VerifiedSnapshot SimpleTx
evidence, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"AwaitingCommitSettlement", Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty)
, (SnapshotNumber -> Command SimpleTx
forall tx. SnapshotNumber -> Command tx
Coordinated.clearLocalState SnapshotNumber
1, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"AwaitingCommitSettlement", Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty)
, (ValidatedTransaction SimpleTx -> Command SimpleTx
forall tx. ValidatedTransaction tx -> Command tx
Coordinated.applyTransaction (UTxOType SimpleTx -> SimpleTx -> ValidatedTransaction SimpleTx
checkedTransaction Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty SimpleTx
tx), Maybe SimpleId
forall a. Maybe a
Nothing, Text
"AwaitingCommitSettlement", SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
100)
, (ChainStateType SimpleTx
-> SnapshotVersion
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> Command SimpleTx
forall tx.
ChainStateType tx
-> SnapshotVersion -> TxIdType tx -> UTxOType tx -> Command tx
Coordinated.recordCommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) SnapshotVersion
1 SimpleId
TxIdType SimpleTx
7 UTxOType SimpleTx
deposited, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", Set SimpleTxOut
UTxOType SimpleTx
deposited Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
100)
]
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retires a dropped claim only when the replacement snapshot is confirmed" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{currentDepositTxId = Just 7, confirmedSnapshot = ConfirmedSnapshot snapshot mempty, seenSnapshot = LastSeenSnapshot 1}}
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
VerifiedSnapshot SimpleTx
evidence <- Snapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx)
verified Snapshot SimpleTx
replacement
OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps
OpenState SimpleTx
before
[ (ValidatedSnapshot SimpleTx -> Seq SimpleTx -> Command SimpleTx
forall tx. ValidatedSnapshot tx -> Seq tx -> Command tx
Coordinated.acceptSnapshot (Snapshot SimpleTx -> ValidatedSnapshot SimpleTx
checkedSnapshot Snapshot SimpleTx
replacement) Seq SimpleTx
forall a. Monoid a => a
mempty, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"AwaitingCommitSettlement", Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty)
, (VerifiedSnapshot SimpleTx -> Command SimpleTx
forall tx. VerifiedSnapshot tx -> Command tx
Coordinated.adoptSnapshot VerifiedSnapshot SimpleTx
evidence, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty)
]
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves another selection when adoption retires an older claim" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{currentDepositTxId = Just 8, confirmedSnapshot = ConfirmedSnapshot snapshot mempty}}
VerifiedSnapshot SimpleTx
evidence <- Snapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx)
verified (Snapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx))
-> Snapshot SimpleTx -> IO (VerifiedSnapshot 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
2 SnapshotVersion
0 [] Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty
OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps OpenState SimpleTx
before [(VerifiedSnapshot SimpleTx -> Command SimpleTx
forall tx. VerifiedSnapshot tx -> Command tx
Coordinated.adoptSnapshot VerifiedSnapshot SimpleTx
evidence, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
8, Text
"CommitSelected", Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty)]
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retires old commit authority on a newer decrement while preserving selection" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{currentDepositTxId = Just 8, confirmedSnapshot = ConfirmedSnapshot snapshot mempty}}
OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps OpenState SimpleTx
before [(ChainStateType SimpleTx
-> SnapshotVersion -> UTxOType SimpleTx -> Command SimpleTx
forall tx.
ChainStateType tx -> SnapshotVersion -> UTxOType tx -> Command tx
Coordinated.recordDecommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) SnapshotVersion
1 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
9), SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
8, Text
"CommitSelected", Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty)]
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves a newer selection on duplicate and older increment observations" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let before :: OpenState SimpleTx
before = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{currentDepositTxId = Just 7, confirmedSnapshot = ConfirmedSnapshot snapshot mempty, seenSnapshot = LastSeenSnapshot 1}}
proposal :: Snapshot SimpleTx
proposal = 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
2 SnapshotVersion
1 [] UTxOType SimpleTx
deposited){utxoToCommit = Just (utxoRef 8), depositTxId = Just 8}
OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps
OpenState SimpleTx
before
[ (ChainStateType SimpleTx
-> SnapshotVersion
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> Command SimpleTx
forall tx.
ChainStateType tx
-> SnapshotVersion -> TxIdType tx -> UTxOType tx -> Command tx
Coordinated.recordCommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
3) SnapshotVersion
1 SimpleId
TxIdType SimpleTx
7 UTxOType SimpleTx
deposited, Maybe SimpleId
forall a. Maybe a
Nothing, Text
"NoCommit", UTxOType SimpleTx
deposited)
, (ValidatedSnapshot SimpleTx -> Seq SimpleTx -> Command SimpleTx
forall tx. ValidatedSnapshot tx -> Seq tx -> Command tx
Coordinated.acceptSnapshot (Snapshot SimpleTx -> ValidatedSnapshot SimpleTx
checkedSnapshot Snapshot SimpleTx
proposal) Seq SimpleTx
forall a. Monoid a => a
mempty, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
8, Text
"CommitSelected", UTxOType SimpleTx
deposited)
, (ChainStateType SimpleTx
-> SnapshotVersion
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> Command SimpleTx
forall tx.
ChainStateType tx
-> SnapshotVersion -> TxIdType tx -> UTxOType tx -> Command tx
Coordinated.recordCommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
4) SnapshotVersion
1 SimpleId
TxIdType SimpleTx
7 UTxOType SimpleTx
deposited, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
8, Text
"CommitSelected", UTxOType SimpleTx
deposited)
, (ChainStateType SimpleTx
-> SnapshotVersion
-> TxIdType SimpleTx
-> UTxOType SimpleTx
-> Command SimpleTx
forall tx.
ChainStateType tx
-> SnapshotVersion -> TxIdType tx -> UTxOType tx -> Command tx
Coordinated.recordCommitFinalization (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
2) SnapshotVersion
0 SimpleId
TxIdType SimpleTx
7 UTxOType SimpleTx
deposited, SimpleId -> Maybe SimpleId
forall a. a -> Maybe a
Just SimpleId
8, Text
"CommitSelected", UTxOType SimpleTx
deposited)
]
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"does not force the transaction ledger to advance or inspect commit selection" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let ([StateChanged SimpleTx]
_, Runtime SimpleTx
next) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
open) (Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx))
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ ValidatedSnapshot SimpleTx -> Seq SimpleTx -> Command SimpleTx
forall tx. ValidatedSnapshot tx -> Seq tx -> Command tx
Coordinated.acceptSnapshot (Snapshot SimpleTx -> ValidatedSnapshot SimpleTx
checkedSnapshot Snapshot SimpleTx
snapshot) (SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton (SimpleTx -> Seq SimpleTx) -> SimpleTx -> Seq SimpleTx
forall a b. (a -> b) -> a -> b
$ Text -> SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"commit phase demands transactions")
Runtime SimpleTx -> Maybe Text
forall tx. Runtime tx -> Maybe Text
commitPhase Runtime SimpleTx
next Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"CommitSelected"
checkSteps :: OpenState SimpleTx -> [(Coordinated.Command SimpleTx, Maybe Integer, Text, UTxOType SimpleTx)] -> Expectation
checkSteps :: OpenState SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, UTxOType SimpleTx)]
-> IO ()
checkSteps OpenState SimpleTx
initial = IO (Runtime SimpleTx) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Runtime SimpleTx) -> IO ())
-> ([(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> IO (Runtime SimpleTx))
-> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Runtime SimpleTx
-> (Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)
-> IO (Runtime SimpleTx))
-> Runtime SimpleTx
-> [(Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)]
-> IO (Runtime SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Runtime SimpleTx
-> (Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)
-> IO (Runtime SimpleTx)
step (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
initial)
where
step :: Runtime SimpleTx
-> (Command SimpleTx, Maybe SimpleId, Text, Set SimpleTxOut)
-> IO (Runtime SimpleTx)
step Runtime SimpleTx
runtime (Command SimpleTx
command, Maybe SimpleId
selected, Text
phase, Set SimpleTxOut
utxo) = do
let ([StateChanged SimpleTx]
events, Runtime SimpleTx
updated) = Runtime SimpleTx
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall tx.
Runtime tx -> Command tx -> ([StateChanged tx], Runtime tx)
Coordinated.step Runtime SimpleTx
runtime Command SimpleTx
command
current :: CoordinatedHeadState SimpleTx
current = Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
updated
replayed :: Runtime SimpleTx
replayed = (Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx)
-> Runtime SimpleTx -> [StateChanged SimpleTx] -> Runtime SimpleTx
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx)
-> Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx)
-> Runtime SimpleTx -> StateChanged SimpleTx -> Runtime SimpleTx)
-> (StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx)
-> Runtime SimpleTx
-> StateChanged SimpleTx
-> Runtime SimpleTx
forall a b. (a -> b) -> a -> b
$ UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent UTxOType SimpleTx
deposited) Runtime SimpleTx
runtime [StateChanged SimpleTx]
events
CoordinatedHeadState SimpleTx
current.currentDepositTxId Maybe SimpleId -> Maybe SimpleId -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleId
selected
CoordinatedHeadState SimpleTx
current.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Set SimpleTxOut
utxo
Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
replayed CoordinatedHeadState SimpleTx
-> CoordinatedHeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` CoordinatedHeadState SimpleTx
current
CoordinatedHeadState SimpleTx
restored <- (DecoderError -> IO (CoordinatedHeadState SimpleTx))
-> (CoordinatedHeadState SimpleTx
-> IO (CoordinatedHeadState SimpleTx))
-> Either DecoderError (CoordinatedHeadState SimpleTx)
-> IO (CoordinatedHeadState SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (CoordinatedHeadState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (CoordinatedHeadState SimpleTx))
-> (DecoderError -> String)
-> DecoderError
-> IO (CoordinatedHeadState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DecoderError -> String
forall b a. (Show a, IsString b) => a -> b
show) CoordinatedHeadState SimpleTx -> IO (CoordinatedHeadState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either DecoderError (CoordinatedHeadState SimpleTx)
-> IO (CoordinatedHeadState SimpleTx))
-> Either DecoderError (CoordinatedHeadState SimpleTx)
-> IO (CoordinatedHeadState SimpleTx)
forall a b. (a -> b) -> a -> b
$ ByteString -> Either DecoderError (CoordinatedHeadState SimpleTx)
forall a. FromCBOR a => ByteString -> Either DecoderError a
decodeFull' (ByteString -> Either DecoderError (CoordinatedHeadState SimpleTx))
-> ByteString
-> Either DecoderError (CoordinatedHeadState SimpleTx)
forall a b. (a -> b) -> a -> b
$ CoordinatedHeadState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' CoordinatedHeadState SimpleTx
current
Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect Runtime SimpleTx
updated [(Text, Maybe Text)] -> [(Text, Maybe Text)] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Runtime SimpleTx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
initial{coordinatedHeadState = restored})
Runtime SimpleTx -> Maybe Text
forall tx. Runtime tx -> Maybe Text
commitPhase Runtime SimpleTx
updated Maybe Text -> Maybe Text -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
phase
Runtime SimpleTx -> IO (Runtime SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Runtime SimpleTx
updated
commitPhase :: Coordinated.Runtime tx -> Maybe Text
commitPhase :: forall tx. Runtime tx -> Maybe Text
commitPhase = Maybe (Maybe Text) -> Maybe Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe Text) -> Maybe Text)
-> (Runtime tx -> Maybe (Maybe Text)) -> Runtime tx -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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))
-> (Runtime tx -> [(Text, Maybe Text)])
-> Runtime tx
-> Maybe (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Runtime tx -> [(Text, Maybe Text)]
forall tx. Runtime tx -> [(Text, Maybe Text)]
Coordinated.inspect
verified :: Snapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx)
verified :: Snapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx)
verified Snapshot SimpleTx
value =
Program
SimpleTx (Either VerificationError (VerifiedSnapshot SimpleTx))
-> IO (Either VerificationError (VerifiedSnapshot SimpleTx))
forall a. Program SimpleTx a -> IO a
verifyOnly ([VerificationKey HydraKey]
-> Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx)
-> Program
SimpleTx (Either VerificationError (VerifiedSnapshot SimpleTx))
forall tx.
IsTx tx =>
[VerificationKey HydraKey]
-> Snapshot tx
-> MultiSignature (Snapshot tx)
-> Program tx (Either VerificationError (VerifiedSnapshot tx))
verifySignedSnapshot [Secret (SigningKey HydraKey) -> VerificationKey HydraKey
forall s k. HasVerificationKey s k => s -> VerificationKey k
getVerificationKey Secret (SigningKey HydraKey)
aliceSk] Snapshot SimpleTx
value (MultiSignature (Snapshot SimpleTx)
-> Program
SimpleTx (Either VerificationError (VerifiedSnapshot SimpleTx)))
-> MultiSignature (Snapshot SimpleTx)
-> Program
SimpleTx (Either VerificationError (VerifiedSnapshot SimpleTx))
forall a b. (a -> b) -> a -> b
$ [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
value]) IO (Either VerificationError (VerifiedSnapshot SimpleTx))
-> (Either VerificationError (VerifiedSnapshot SimpleTx)
-> IO (VerifiedSnapshot SimpleTx))
-> IO (VerifiedSnapshot SimpleTx)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (VerificationError -> IO (VerifiedSnapshot SimpleTx))
-> (VerifiedSnapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx))
-> Either VerificationError (VerifiedSnapshot SimpleTx)
-> IO (VerifiedSnapshot SimpleTx)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> IO (VerifiedSnapshot SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (VerifiedSnapshot SimpleTx))
-> (VerificationError -> String)
-> VerificationError
-> IO (VerifiedSnapshot SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VerificationError -> String
forall b a. (Show a, IsString b) => a -> b
show) VerifiedSnapshot SimpleTx -> IO (VerifiedSnapshot SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
verifyOnly :: Program SimpleTx a -> IO a
verifyOnly :: forall a. Program SimpleTx a -> IO a
verifyOnly = (forall x. Instruction SimpleTx x -> IO x)
-> Program SimpleTx a -> IO a
forall (m :: * -> *) tx a.
Monad m =>
(forall x. Instruction tx x -> m x) -> Program tx a -> m a
foldProgram ((forall x. Instruction SimpleTx x -> IO x)
-> Program SimpleTx a -> IO a)
-> (forall x. Instruction SimpleTx x -> IO x)
-> Program SimpleTx a
-> IO a
forall a b. (a -> b) -> a -> b
$ \case
VerifySnapshot [VerificationKey HydraKey]
parties MultiSignature (Snapshot SimpleTx)
signatures ByteString
bytes -> Either VerificationError () -> IO (Either VerificationError ())
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either VerificationError () -> IO (Either VerificationError ()))
-> Either VerificationError () -> IO (Either VerificationError ())
forall a b. (a -> b) -> a -> b
$ [VerificationKey HydraKey]
-> MultiSignature (Snapshot SimpleTx)
-> ByteString
-> Either VerificationError ()
forall a.
[VerificationKey HydraKey]
-> MultiSignature a -> ByteString -> Either VerificationError ()
verifyMultiSignatureBytes [VerificationKey HydraKey]
parties MultiSignature (Snapshot SimpleTx)
signatures ByteString
bytes
Instruction SimpleTx x
_ -> String -> IO x
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unexpected primitive while verifying a test certificate"
open :: OpenState SimpleTx
open :: OpenState SimpleTx
open = case ([Party] -> NodeState SimpleTx
inOpenState [Party
alice]).headState of
Open OpenState SimpleTx
value -> OpenState SimpleTx
value
HeadState SimpleTx
_ -> Text -> OpenState SimpleTx
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"fixture does not open a Head"
deposited :: UTxOType SimpleTx
deposited :: UTxOType SimpleTx
deposited = SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
7
snapshot :: Snapshot SimpleTx
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 7}