{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Hydra.HeadLogic.SnapshotDecommitSpec (spec) where
import Hydra.HeadLogic.Cardano (cardanoSnapshotLimits)
import Hydra.HeadLogic.Snapshot.Cached qualified as Cached
import Hydra.Prelude
import Test.Hydra.Prelude (Spec, describe, it, shouldBe, shouldSatisfy)
import Cardano.Binary (decodeFull', serialize')
import Data.Map.Strict qualified as Map
import Data.Secret (Secret)
import Data.Sequence qualified as Seq
import Hydra.API.ClientInput qualified as Client
import Hydra.Agda.OffChainReference qualified as Agda
import Hydra.Chain (ChainEvent (Observation), OnChainTx (OnDecrementTx))
import Hydra.HeadLogic.Error (LogicError (RequireFailed), RequirementFailure (InvalidMultisignature))
import Hydra.HeadLogic.Snapshot.Request (DecommitStatus (..), nextSnapshotRequest)
import Hydra.HeadLogic.State (CoordinatedHeadState (..), HeadState (Open), OpenState (..), SeenSnapshot (SeenSnapshot))
import Hydra.HeadLogic.State.Coordinated qualified as Coordinated
import Hydra.Ledger.Simple (SimpleChainState (SimpleChainState), SimpleTx (..), simpleLedger)
import Hydra.Network.Message (Message (..), NetworkEvent (ReceivedMessage))
import Hydra.Node.HeadLogic qualified as Head
import Hydra.Node.State (NodeState (..), initialChainTime)
import Hydra.Tx (UTxOType)
import Hydra.Tx.Accumulator qualified as Accumulator
import Hydra.Tx.Crypto (HydraKey, SigningKey, aggregateInOrder, sign)
import Hydra.Tx.Party (Party)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (ConfirmedSnapshot), Snapshot (..), getSnapshot)
import Test.Hydra.HeadLogic.Scenario qualified as Scenario
import Test.Hydra.Ledger.Simple (utxoRef)
import Test.Hydra.Tx.Fixture (alice, aliceSk, bob, bobSk, carol, carolSk, testHeadId)
spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"snapshot decommit retention" (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
"carries the decommit into the next round when ReqSn overtakes ReqDec" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
let outcome :: Outcome SimpleTx
outcome = NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
confirmed Party
bob (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
ordinary
afterTx :: NodeState SimpleTx
afterTx = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
confirmed Outcome SimpleTx
outcome
expected :: Message SimpleTx
expected = SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
2 [SimpleId
TxIdType SimpleTx
20] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
Outcome SimpleTx -> [Message SimpleTx]
requests Outcome SimpleTx
outcome [Message SimpleTx] -> [Message SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [Message SimpleTx
expected]
NodeState SimpleTx
requestedAgain <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
afterTx Party
bob Message SimpleTx
expected
NodeState SimpleTx
confirmedAgain <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requestedAgain
let current :: CoordinatedHeadState SimpleTx
current = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmedAgain).coordinatedHeadState
CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit
(ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot CoordinatedHeadState SimpleTx
current.confirmedSnapshot).number SnapshotNumber -> SnapshotNumber -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SnapshotNumber
2
(ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot CoordinatedHeadState SimpleTx
current.confirmedSnapshot).utxo Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
20
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"retains the candidate separately until confirmation, including delayed ReqDec" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
let current :: CoordinatedHeadState SimpleTx
current = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
requested).coordinatedHeadState
CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleTx
forall a. Maybe a
Nothing
SimpleId -> Map SimpleId SimpleTx -> Maybe SimpleTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SimpleId
10 CoordinatedHeadState SimpleTx
current.allTxs Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit
NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
requested (NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
requested Party
alice (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec SimpleTx
decommit) NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
requested
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
let confirmedState :: CoordinatedHeadState SimpleTx
confirmedState = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState
CoordinatedHeadState SimpleTx
confirmedState.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit
TxIdType SimpleTx
-> Map (TxIdType SimpleTx) SimpleTx -> Maybe SimpleTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxIdType SimpleTx
10 CoordinatedHeadState SimpleTx
confirmedState.allTxs Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleTx
forall a. Maybe a
Nothing
NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
confirmed (NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
confirmed Party
alice (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec SimpleTx
decommit) NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
confirmed
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"keeps a different reservation until confirmation then promotes the candidate" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let other :: SimpleTx
other = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)
NodeState SimpleTx
reserved <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
bob (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec SimpleTx
other
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
reserved Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
requested).coordinatedHeadState.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
other
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
requested).coordinatedHeadState.localUTxO UTxOType SimpleTx -> UTxOType SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` UTxOType SimpleTx
forall a. Monoid a => a
mempty
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"a rejected final signature cannot promote the payload or release a reservation" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let other :: SimpleTx
other = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)
NodeState SimpleTx
reserved <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
bob (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec SimpleTx
other
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
reserved Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
Snapshot SimpleTx
snapshot <- NodeState SimpleTx -> IO (Snapshot SimpleTx)
candidateOf NodeState SimpleTx
requested
NodeState SimpleTx
withAlice <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
requested Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ Signature (Snapshot SimpleTx) -> SnapshotNumber -> Message SimpleTx
forall tx. Signature (Snapshot tx) -> SnapshotNumber -> Message tx
AckSn (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) SnapshotNumber
1
NodeState SimpleTx
withBob <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
withAlice Party
bob (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ Signature (Snapshot SimpleTx) -> SnapshotNumber -> Message SimpleTx
forall tx. Signature (Snapshot tx) -> SnapshotNumber -> Message tx
AckSn (Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
bobSk Snapshot SimpleTx
snapshot) SnapshotNumber
1
let rejected :: Outcome SimpleTx
rejected = NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
withBob Party
carol (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ Signature (Snapshot SimpleTx) -> SnapshotNumber -> Message SimpleTx
forall tx. Signature (Snapshot tx) -> SnapshotNumber -> Message tx
AckSn (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) SnapshotNumber
1
Outcome SimpleTx
rejected Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case Head.Error (RequireFailed InvalidMultisignature{}) -> Bool
True; Outcome SimpleTx
_ -> Bool
False
NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
withBob Outcome SimpleTx
rejected NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
withBob
let current :: CoordinatedHeadState SimpleTx
current = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
withBob).coordinatedHeadState
CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
other
SimpleId -> Map SimpleId SimpleTx -> Maybe SimpleTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup SimpleId
10 CoordinatedHeadState SimpleTx
current.allTxs Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"duplicate and expired ReqTx cannot evict the candidate's transaction" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
[TTL] -> (TTL -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [TTL
0, TTL
2] ((TTL -> IO ()) -> IO ()) -> (TTL -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \TTL
ttl -> do
let duplicate :: Outcome SimpleTx
duplicate = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
Head.update (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
requested (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
Head.NetworkInput TTL
ttl (NetworkEvent (Message SimpleTx) -> Input SimpleTx)
-> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ Party -> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall msg. Party -> msg -> NetworkEvent msg
ReceivedMessage Party
alice (Message SimpleTx -> NetworkEvent (Message SimpleTx))
-> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
decommit
Outcome SimpleTx
duplicate Outcome SimpleTx -> Outcome SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Head.Continue [] []
NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
requested Outcome SimpleTx
duplicate NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
requested
IO (NodeState SimpleTx) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (NodeState SimpleTx) -> IO ())
-> IO (NodeState SimpleTx) -> IO ()
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"settlement before the final signature retires the retained transaction" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
let observation :: Input SimpleTx
observation = ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Head.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ OnChainTx SimpleTx
-> ChainStateType SimpleTx -> ChainEvent SimpleTx
forall tx. OnChainTx tx -> ChainStateType tx -> ChainEvent tx
Observation (HeadId
-> SnapshotVersion
-> UTxOType SimpleTx
-> SnapshotNumber
-> MultiSignature (Snapshot SimpleTx)
-> OnChainTx SimpleTx
forall tx.
HeadId
-> SnapshotVersion
-> UTxOType tx
-> SnapshotNumber
-> MultiSignature (Snapshot tx)
-> OnChainTx tx
OnDecrementTx HeadId
testHeadId SnapshotVersion
1 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
10) SnapshotNumber
1 MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty) (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
1)
outcome :: Outcome SimpleTx
outcome = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
Head.update (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
requested Input SimpleTx
observation
NodeState SimpleTx
observed <- NodeState SimpleTx -> Outcome SimpleTx -> IO (NodeState SimpleTx)
accept NodeState SimpleTx
requested Outcome SimpleTx
outcome
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
observed).coordinatedHeadState.version SnapshotVersion -> SnapshotVersion -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SnapshotVersion
1
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
observed
let current :: CoordinatedHeadState SimpleTx
current = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState
CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleTx
forall a. Maybe a
Nothing
TxIdType SimpleTx
-> Map (TxIdType SimpleTx) SimpleTx -> Maybe SimpleTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxIdType SimpleTx
10 CoordinatedHeadState SimpleTx
current.allTxs Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleTx
forall a. Maybe a
Nothing
CoordinatedHeadState SimpleTx
current.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2
let next :: Outcome SimpleTx
next = NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
confirmed Party
bob (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
ordinary
Outcome SimpleTx -> [Message SimpleTx]
requests Outcome SimpleTx
next [Message SimpleTx] -> [Message SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
1 SnapshotNumber
2 [SimpleId
TxIdType SimpleTx
20] Maybe SimpleTx
forall a. Maybe a
Nothing Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing]
String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"settlement releases reserved inputs" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
[(String, Maybe SimpleTx)]
-> ((String, Maybe SimpleTx) -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String
"without a reservation", Maybe SimpleTx
forall a. Maybe a
Nothing), (String
"with the same reservation", SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit), (String
"with a different reservation", SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
otherDecommit)] (((String, Maybe SimpleTx) -> Spec) -> Spec)
-> ((String, Maybe SimpleTx) -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \(String
reservationLabel, Maybe SimpleTx
reservation) ->
[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 ->
[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
observedCertificate ->
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
reservationLabel String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", settlement " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> (if Bool
settleFirst then String
"before" else String
"after") String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" acknowledgements, certificate " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Bool -> String
forall b a. (Show a, IsString b) => a -> b
show Bool
observedCertificate) (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
let start :: NodeState SimpleTx
start = UTxOType SimpleTx -> NodeState SimpleTx
initialWith (UTxOType SimpleTx -> NodeState SimpleTx)
-> UTxOType SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3
pending :: SimpleTx
pending = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
30 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
30)
NodeState SimpleTx
reserved <- IO (NodeState SimpleTx)
-> (SimpleTx -> IO (NodeState SimpleTx))
-> Maybe SimpleTx
-> IO (NodeState SimpleTx)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NodeState SimpleTx
start) (NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
start Party
bob (Message SimpleTx -> IO (NodeState SimpleTx))
-> (SimpleTx -> Message SimpleTx)
-> SimpleTx
-> IO (NodeState SimpleTx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec) Maybe SimpleTx
reservation
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
reserved Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
Snapshot SimpleTx
candidate <- NodeState SimpleTx -> IO (Snapshot SimpleTx)
candidateOf NodeState SimpleTx
requested
NodeState SimpleTx
withPending <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
requested Party
bob (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
pending
let signatures :: MultiSignature (Snapshot SimpleTx)
signatures = Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder ([(Party, Signature (Snapshot SimpleTx))]
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Party
p, Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
candidate) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [(Party, Secret (SigningKey HydraKey))]
signers]) [Party]
Scenario.scenarioParties
observation :: Input SimpleTx
observation = ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
Head.ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ OnChainTx SimpleTx
-> ChainStateType SimpleTx -> ChainEvent SimpleTx
forall tx. OnChainTx tx -> ChainStateType tx -> ChainEvent tx
Observation (HeadId
-> SnapshotVersion
-> UTxOType SimpleTx
-> SnapshotNumber
-> MultiSignature (Snapshot SimpleTx)
-> OnChainTx SimpleTx
forall tx.
HeadId
-> SnapshotVersion
-> UTxOType tx
-> SnapshotNumber
-> MultiSignature (Snapshot tx)
-> OnChainTx tx
OnDecrementTx HeadId
testHeadId SnapshotVersion
1 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
10) SnapshotNumber
1 (if Bool
observedCertificate then MultiSignature (Snapshot SimpleTx)
signatures else MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty)) (ChainSlot -> SimpleChainState
SimpleChainState ChainSlot
1)
settle :: NodeState SimpleTx -> IO (NodeState SimpleTx)
settle NodeState SimpleTx
node = NodeState SimpleTx -> Outcome SimpleTx -> IO (NodeState SimpleTx)
accept NodeState SimpleTx
node (Outcome SimpleTx -> IO (NodeState SimpleTx))
-> Outcome SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
Head.update (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node Input SimpleTx
observation
acknowledge :: NodeState SimpleTx -> IO (NodeState SimpleTx)
acknowledge NodeState SimpleTx
node = (NodeState SimpleTx
-> (Party, Secret (SigningKey HydraKey))
-> IO (NodeState SimpleTx))
-> NodeState SimpleTx
-> [(Party, Secret (SigningKey HydraKey))]
-> IO (NodeState SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM (\NodeState SimpleTx
current (Party
p, Secret (SigningKey HydraKey)
sk) -> NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
current Party
p (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ Signature (Snapshot SimpleTx) -> SnapshotNumber -> Message SimpleTx
forall tx. Signature (Snapshot tx) -> SnapshotNumber -> Message tx
AckSn (Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
candidate) Snapshot SimpleTx
candidate.number) NodeState SimpleTx
node [(Party, Secret (SigningKey HydraKey))]
signers
NodeState SimpleTx
final <-
if Bool
settleFirst
then do
NodeState SimpleTx
settled <- NodeState SimpleTx -> IO (NodeState SimpleTx)
settle NodeState SimpleTx
withPending
if Bool
observedCertificate then NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NodeState SimpleTx
settled else NodeState SimpleTx -> IO (NodeState SimpleTx)
acknowledge NodeState SimpleTx
settled
else NodeState SimpleTx -> IO (NodeState SimpleTx)
acknowledge NodeState SimpleTx
withPending IO (NodeState SimpleTx)
-> (NodeState SimpleTx -> IO (NodeState SimpleTx))
-> IO (NodeState SimpleTx)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NodeState SimpleTx -> IO (NodeState SimpleTx)
settle
let current :: CoordinatedHeadState SimpleTx
current = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
final).coordinatedHeadState
CoordinatedHeadState SimpleTx
current.version SnapshotVersion -> SnapshotVersion -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SnapshotVersion
1
CoordinatedHeadState SimpleTx
current.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleTx
forall a. Maybe a
Nothing
CoordinatedHeadState SimpleTx
current.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
30
CoordinatedHeadState SimpleTx
current.localTxs Seq SimpleTx -> Seq SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton SimpleTx
pending
(ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot CoordinatedHeadState SimpleTx
current.confirmedSnapshot).utxo Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
3
NodeState SimpleTx
spent <- NodeState SimpleTx -> Outcome SimpleTx -> IO (NodeState SimpleTx)
accept NodeState SimpleTx
final (Outcome SimpleTx -> IO (NodeState SimpleTx))
-> Outcome SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
final Party
bob (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
ordinary
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
spent).coordinatedHeadState.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
20 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
30
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"side-loading and clearing local work preserve the authorised payload" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
let signed :: ConfirmedSnapshot SimpleTx
signed = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState.confirmedSnapshot
outcome :: Outcome SimpleTx
outcome = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
Head.update (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
confirmed (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
Head.ClientInput (ClientInput SimpleTx -> Input SimpleTx)
-> ClientInput SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ ConfirmedSnapshot SimpleTx -> ClientInput SimpleTx
forall tx. ConfirmedSnapshot tx -> ClientInput tx
Client.SideLoadSnapshot ConfirmedSnapshot SimpleTx
signed
NodeState SimpleTx
cleared <- NodeState SimpleTx -> Outcome SimpleTx -> IO (NodeState SimpleTx)
accept NodeState SimpleTx
confirmed Outcome SimpleTx
outcome
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
cleared).coordinatedHeadState.decommitTx Maybe SimpleTx -> Maybe SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
cleared).coordinatedHeadState.allTxs Map (TxIdType SimpleTx) SimpleTx
-> Map (TxIdType SimpleTx) SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Map (TxIdType SimpleTx) SimpleTx
forall a. Monoid a => a
mempty
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"does not request a round when a legacy checkpoint lacks the authorised payload" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
let open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed
old :: NodeState SimpleTx
old = NodeState SimpleTx
confirmed{headState = Open open{coordinatedHeadState = open.coordinatedHeadState{decommitTx = Nothing}}}
outcome :: Outcome SimpleTx
outcome = NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
old Party
bob (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
ordinary
after :: NodeState SimpleTx
after = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
old Outcome SimpleTx
outcome
Outcome SimpleTx -> [Message SimpleTx]
requests Outcome SimpleTx
outcome [Message SimpleTx] -> [Message SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` []
(NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
after).coordinatedHeadState.seenSnapshot SeenSnapshot SimpleTx -> SeenSnapshot SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` OpenState SimpleTx
open.coordinatedHeadState.seenSnapshot
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"rejects missing or mismatched carry proposals before retaining a request marker" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
NodeState SimpleTx
requested <- NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
initial Party
alice (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotVersion
-> SnapshotNumber
-> [TxIdType SimpleTx]
-> Maybe SimpleTx
-> Maybe (TxIdType SimpleTx)
-> Message SimpleTx
forall tx.
SnapshotVersion
-> SnapshotNumber
-> [TxIdType tx]
-> Maybe tx
-> Maybe (TxIdType tx)
-> Message tx
ReqSn SnapshotVersion
0 SnapshotNumber
1 [] (SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
decommit) Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
NodeState SimpleTx
confirmed <- NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
requested
let snapshot :: Snapshot SimpleTx
snapshot = ConfirmedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. IsTx tx => ConfirmedSnapshot tx -> Snapshot tx
getSnapshot (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState.confirmedSnapshot
other :: SimpleTx
other = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)
[DecommitStatus SimpleTx]
-> (DecommitStatus SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit, SimpleTx -> DecommitStatus SimpleTx
forall tx. tx -> DecommitStatus tx
AwaitingInputs SimpleTx
other, SimpleTx -> DecommitStatus SimpleTx
forall tx. tx -> DecommitStatus tx
ReadyToDecommit SimpleTx
other, SimpleTx -> DecommitStatus SimpleTx
forall tx. tx -> DecommitStatus tx
CarryDecommit SimpleTx
other] ((DecommitStatus SimpleTx -> IO ()) -> IO ())
-> (DecommitStatus SimpleTx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \DecommitStatus SimpleTx
status ->
Maybe (SnapshotRequest SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust (SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
nextSnapshotRequest SnapshotLimits
cardanoSnapshotLimits SnapshotVersion
0 Snapshot SimpleTx
snapshot Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
status Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing) Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool
False
Maybe (SnapshotRequest SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust (SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
nextSnapshotRequest SnapshotLimits
cardanoSnapshotLimits SnapshotVersion
0 Snapshot SimpleTx
snapshot Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing (SimpleTx -> DecommitStatus SimpleTx
forall tx. tx -> DecommitStatus tx
CarryDecommit SimpleTx
decommit) Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing) Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool
True
Maybe (SnapshotRequest SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust (SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
nextSnapshotRequest SnapshotLimits
cardanoSnapshotLimits SnapshotVersion
1 Snapshot SimpleTx
snapshot Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing) Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool
True
SnapshotRequest SimpleTx
stale <- IO (SnapshotRequest SimpleTx)
-> (SnapshotRequest SimpleTx -> IO (SnapshotRequest SimpleTx))
-> Maybe (SnapshotRequest SimpleTx)
-> IO (SnapshotRequest SimpleTx)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> IO (SnapshotRequest SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no stale proposal") SnapshotRequest SimpleTx -> IO (SnapshotRequest SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (SnapshotRequest SimpleTx) -> IO (SnapshotRequest SimpleTx))
-> Maybe (SnapshotRequest SimpleTx)
-> IO (SnapshotRequest SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
nextSnapshotRequest SnapshotLimits
cardanoSnapshotLimits SnapshotVersion
0 Snapshot SimpleTx
snapshot{utxoToDecommit = Nothing} Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing
let ([StateChanged SimpleTx]
events, Runtime SimpleTx
runtime) = 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 -> Runtime SimpleTx)
-> OpenState SimpleTx -> Runtime SimpleTx
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed) (Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx))
-> Command SimpleTx -> ([StateChanged SimpleTx], Runtime SimpleTx)
forall a b. (a -> b) -> a -> b
$ SnapshotRequest SimpleTx -> Command SimpleTx
forall tx. SnapshotRequest tx -> Command tx
Coordinated.requestSnapshot SnapshotRequest SimpleTx
stale
[StateChanged SimpleTx]
events [StateChanged SimpleTx] -> [StateChanged SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` []
Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
runtime CoordinatedHeadState SimpleTx
-> CoordinatedHeadState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
confirmed).coordinatedHeadState
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"agrees with Agda on bounded payload selection and the carry obligation" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[Bool] -> (Bool -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Bool
False, Bool
True] ((Bool -> IO ()) -> IO ()) -> (Bool -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Bool
sameVersion ->
[[SimpleId]] -> ([SimpleId] -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[], [SimpleId
1], [SimpleId
2], [SimpleId
1, SimpleId
2]] (([SimpleId] -> IO ()) -> IO ()) -> ([SimpleId] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[SimpleId]
outputs ->
[[SimpleId]] -> ([SimpleId] -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[], [SimpleId
1], [SimpleId
2], [SimpleId
1, SimpleId
2]] (([SimpleId] -> IO ()) -> IO ()) -> ([SimpleId] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[SimpleId]
reservedOutputs ->
[[[SimpleId]]] -> ([[SimpleId]] -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [[], [[SimpleId
1]], [[SimpleId
2], [SimpleId
1]], [[SimpleId
1, SimpleId
2], [SimpleId
2]]] (([[SimpleId]] -> IO ()) -> IO ())
-> ([[SimpleId]] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[[SimpleId]]
registryOutputs -> do
let toUTxO :: [SimpleId] -> Set SimpleTxOut
toUTxO = [Item (Set SimpleTxOut)] -> Set SimpleTxOut
forall l. IsList l => [Item l] -> l
fromList ([Item (Set SimpleTxOut)] -> Set SimpleTxOut)
-> ([SimpleId] -> [Item (Set SimpleTxOut)])
-> [SimpleId]
-> Set SimpleTxOut
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SimpleId -> Item (Set SimpleTxOut))
-> [SimpleId] -> [Item (Set SimpleTxOut)]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SimpleId -> Item (Set SimpleTxOut)
forall a. Num a => SimpleId -> a
fromInteger
snapshot :: Snapshot SimpleTx
snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
Scenario.initialSnapshot{utxoToDecommit = Just $ toUTxO outputs}
reserved :: SimpleTx
reserved = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
0 Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty ([SimpleId] -> Set SimpleTxOut
toUTxO [SimpleId]
reservedOutputs)
registry :: [SimpleTx]
registry = [SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
index Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty ([SimpleId] -> Set SimpleTxOut
toUTxO [SimpleId]
refs) | (SimpleId
index, [SimpleId]
refs) <- [SimpleId] -> [[SimpleId]] -> [(SimpleId, [SimpleId])]
forall a b. [a] -> [b] -> [(a, b)]
zip [SimpleId
1 ..] [[SimpleId]]
registryOutputs]
open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
initial
base :: OpenState SimpleTx
base = OpenState SimpleTx
open{coordinatedHeadState = open.coordinatedHeadState{version = if sameVersion then 0 else 1, decommitTx = reserved <$ guard sameVersion, allTxs = Map.fromList [(tx.txSimpleId, tx) | tx <- registry]}}
runtime :: Runtime SimpleTx
runtime = UTxOType SimpleTx
-> StateChanged SimpleTx -> Runtime SimpleTx -> Runtime SimpleTx
forall tx.
IsChainState tx =>
UTxOType tx -> StateChanged tx -> Runtime tx -> Runtime tx
Coordinated.replayEvent Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty (HeadId
-> Maybe (Snapshot SimpleTx)
-> MultiSignature (Snapshot SimpleTx)
-> StateChanged SimpleTx
forall tx.
HeadId
-> Maybe (Snapshot tx)
-> MultiSignature (Snapshot tx)
-> StateChanged tx
Head.SnapshotConfirmed HeadId
testHeadId (Snapshot SimpleTx -> Maybe (Snapshot SimpleTx)
forall a. a -> Maybe a
Just Snapshot SimpleTx
snapshot) MultiSignature (Snapshot SimpleTx)
forall a. Monoid a => a
mempty) (Runtime SimpleTx -> Runtime SimpleTx)
-> Runtime SimpleTx -> Runtime SimpleTx
forall a b. (a -> b) -> a -> b
$ OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
base
actual :: CoordinatedHeadState SimpleTx
actual = Runtime SimpleTx -> CoordinatedHeadState SimpleTx
forall tx. Runtime tx -> CoordinatedHeadState tx
Coordinated.currentState Runtime SimpleTx
runtime
expected :: Maybe SimpleId
expected = Bool -> [SimpleId] -> [SimpleId] -> [[SimpleId]] -> Maybe SimpleId
Agda.carriedDecommitPayloadRef Bool
sameVersion [SimpleId]
outputs [SimpleId]
reservedOutputs [[SimpleId]]
registryOutputs
((.txSimpleId) (SimpleTx -> SimpleId) -> Maybe SimpleTx -> Maybe SimpleId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CoordinatedHeadState SimpleTx
actual.decommitTx) Maybe SimpleId -> Maybe SimpleId -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Maybe SimpleId
expected
[DecommitStatus SimpleTx]
-> (DecommitStatus SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit, SimpleTx -> DecommitStatus SimpleTx
forall tx. tx -> DecommitStatus tx
CarryDecommit SimpleTx
reserved] ((DecommitStatus SimpleTx -> IO ()) -> IO ())
-> (DecommitStatus SimpleTx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \DecommitStatus SimpleTx
status -> do
let matching :: Bool
matching = case DecommitStatus SimpleTx
status of CarryDecommit{} -> Bool -> Bool
not ([SimpleId] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [SimpleId]
outputs) Bool -> Bool -> Bool
&& [SimpleId]
outputs [SimpleId] -> [SimpleId] -> Bool
forall a. Eq a => a -> a -> Bool
== [SimpleId]
reservedOutputs; DecommitStatus SimpleTx
_ -> Bool
False
proposal :: Maybe (SnapshotRequest SimpleTx)
proposal = SnapshotLimits
-> SnapshotVersion
-> Snapshot SimpleTx
-> Seq SimpleTx
-> PendingDeposits SimpleTx
-> Maybe (TxIdType SimpleTx)
-> DecommitStatus SimpleTx
-> Maybe (SettlingCommit SimpleTx)
-> Maybe (SnapshotRequest SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> SnapshotVersion
-> Snapshot tx
-> Seq tx
-> PendingDeposits tx
-> Maybe (TxIdType tx)
-> DecommitStatus tx
-> Maybe (SettlingCommit tx)
-> Maybe (SnapshotRequest tx)
nextSnapshotRequest SnapshotLimits
cardanoSnapshotLimits (if Bool
sameVersion then SnapshotVersion
0 else SnapshotVersion
1) Snapshot SimpleTx
snapshot Seq SimpleTx
forall a. Monoid a => a
mempty Map SimpleId (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe SimpleId
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
status Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing
Maybe (SnapshotRequest SimpleTx) -> Bool
forall a. Maybe a -> Bool
isJust Maybe (SnapshotRequest SimpleTx)
proposal Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Bool -> Bool -> Bool
Agda.carriedDecommitRequestRef Bool
sameVersion Bool
True Bool
matching
where
otherDecommit :: SimpleTx
otherDecommit = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
11 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
11)
initial :: NodeState SimpleTx
initial :: NodeState SimpleTx
initial = UTxOType SimpleTx -> NodeState SimpleTx
initialWith (UTxOType SimpleTx -> NodeState SimpleTx)
-> UTxOType SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2
initialWith :: UTxOType SimpleTx -> NodeState SimpleTx
initialWith :: UTxOType SimpleTx -> NodeState SimpleTx
initialWith UTxOType SimpleTx
utxo =
let base :: Snapshot SimpleTx
base :: Snapshot SimpleTx
base = Snapshot SimpleTx
Scenario.initialSnapshot{utxo, accumulator = Accumulator.buildFromUTxO @SimpleTx utxo, appliedAccumulator = Accumulator.buildFromUTxO @SimpleTx utxo}
signed :: MultiSignature (Snapshot SimpleTx)
signed = Map Party (Signature (Snapshot SimpleTx))
-> [Party] -> MultiSignature (Snapshot SimpleTx)
forall k a. Ord k => Map k (Signature a) -> [k] -> MultiSignature a
aggregateInOrder ([(Party, Signature (Snapshot SimpleTx))]
-> Map Party (Signature (Snapshot SimpleTx))
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Party
p, Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
base) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [(Party, Secret (SigningKey HydraKey))]
signers]) [Party]
Scenario.scenarioParties
open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
Scenario.initialNode
in NodeState SimpleTx
Scenario.initialNode{headState = Open open{coordinatedHeadState = open.coordinatedHeadState{localUTxO = utxo, confirmedSnapshot = ConfirmedSnapshot base signed}}}
decommit, ordinary :: SimpleTx
decommit :: SimpleTx
decommit = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
10 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
1) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
10)
ordinary :: SimpleTx
ordinary = SimpleId -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx SimpleId
20 (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
2) (SimpleId -> UTxOType SimpleTx
utxoRef SimpleId
20)
signers :: [(Party, Secret (SigningKey HydraKey))]
signers :: [(Party, Secret (SigningKey HydraKey))]
signers = [(Party
alice, Secret (SigningKey HydraKey)
aliceSk), (Party
bob, Secret (SigningKey HydraKey)
bobSk), (Party
carol, Secret (SigningKey HydraKey)
carolSk)]
run :: NodeState SimpleTx -> Party -> Message SimpleTx -> Head.Outcome SimpleTx
run :: NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
node Party
sender Message SimpleTx
msg = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
Head.update (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node (Input SimpleTx -> Outcome SimpleTx)
-> Input SimpleTx -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
Head.NetworkInput TTL
2 (NetworkEvent (Message SimpleTx) -> Input SimpleTx)
-> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ Party -> Message SimpleTx -> NetworkEvent (Message SimpleTx)
forall msg. Party -> msg -> NetworkEvent msg
ReceivedMessage Party
sender Message SimpleTx
msg
deliver :: NodeState SimpleTx -> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver :: NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
node Party
sender Message SimpleTx
msg = NodeState SimpleTx -> Outcome SimpleTx -> IO (NodeState SimpleTx)
accept NodeState SimpleTx
node (Outcome SimpleTx -> IO (NodeState SimpleTx))
-> Outcome SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> Party -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
node Party
sender Message SimpleTx
msg
accept :: NodeState SimpleTx -> Head.Outcome SimpleTx -> IO (NodeState SimpleTx)
accept :: NodeState SimpleTx -> Outcome SimpleTx -> IO (NodeState SimpleTx)
accept NodeState SimpleTx
node = \case
outcome :: Outcome SimpleTx
outcome@(Head.Continue [StateChanged SimpleTx]
events [Effect SimpleTx]
_) -> do
NodeState SimpleTx
next <- (NodeState SimpleTx
-> StateChanged SimpleTx -> IO (NodeState SimpleTx))
-> NodeState SimpleTx
-> [StateChanged SimpleTx]
-> IO (NodeState SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM NodeState SimpleTx
-> StateChanged SimpleTx -> IO (NodeState SimpleTx)
replay NodeState SimpleTx
node [StateChanged SimpleTx]
events
NodeState SimpleTx
next NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
Head.aggregateState NodeState SimpleTx
node Outcome SimpleTx
outcome
NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NodeState SimpleTx
next
Outcome SimpleTx
outcome -> String -> IO (NodeState SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> IO (NodeState SimpleTx))
-> String -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ Outcome SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Outcome SimpleTx
outcome
where
replay :: NodeState SimpleTx -> Head.StateChanged SimpleTx -> IO (NodeState SimpleTx)
replay :: NodeState SimpleTx
-> StateChanged SimpleTx -> IO (NodeState SimpleTx)
replay NodeState SimpleTx
before StateChanged SimpleTx
event = do
StateChanged SimpleTx
decoded <- (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' (StateChanged SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' StateChanged SimpleTx
event)
StateChanged SimpleTx
decoded StateChanged SimpleTx -> StateChanged SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` StateChanged SimpleTx
event
let next :: NodeState SimpleTx
next = NodeState SimpleTx -> StateChanged SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> StateChanged tx -> NodeState tx
Head.aggregateNodeState NodeState SimpleTx
before StateChanged SimpleTx
decoded
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' (NodeState SimpleTx -> ByteString
forall a. ToCBOR a => a -> ByteString
serialize' NodeState SimpleTx
next)
NodeState SimpleTx
restored NodeState SimpleTx -> NodeState SimpleTx -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` NodeState SimpleTx
next
NodeState SimpleTx -> IO (NodeState SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NodeState SimpleTx
restored
candidateOf :: NodeState SimpleTx -> IO (Snapshot SimpleTx)
candidateOf :: NodeState SimpleTx -> IO (Snapshot SimpleTx)
candidateOf NodeState SimpleTx
node = case (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
node).coordinatedHeadState.seenSnapshot of
SeenSnapshot CachedSnapshot SimpleTx
cached Map Party (Signature (Snapshot SimpleTx))
_ -> Snapshot SimpleTx -> IO (Snapshot SimpleTx)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Snapshot SimpleTx -> IO (Snapshot SimpleTx))
-> Snapshot SimpleTx -> IO (Snapshot SimpleTx)
forall a b. (a -> b) -> a -> b
$ CachedSnapshot SimpleTx -> Snapshot SimpleTx
forall tx. CachedSnapshot tx -> Snapshot tx
Cached.snapshot CachedSnapshot SimpleTx
cached
SeenSnapshot SimpleTx
_ -> String -> IO (Snapshot SimpleTx)
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no candidate snapshot"
confirm :: NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm :: NodeState SimpleTx -> IO (NodeState SimpleTx)
confirm NodeState SimpleTx
node = do
Snapshot SimpleTx
candidate <- NodeState SimpleTx -> IO (Snapshot SimpleTx)
candidateOf NodeState SimpleTx
node
(NodeState SimpleTx
-> (Party, Secret (SigningKey HydraKey))
-> IO (NodeState SimpleTx))
-> NodeState SimpleTx
-> [(Party, Secret (SigningKey HydraKey))]
-> IO (NodeState SimpleTx)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM (\NodeState SimpleTx
current (Party
p, Secret (SigningKey HydraKey)
sk) -> NodeState SimpleTx
-> Party -> Message SimpleTx -> IO (NodeState SimpleTx)
deliver NodeState SimpleTx
current Party
p (Message SimpleTx -> IO (NodeState SimpleTx))
-> Message SimpleTx -> IO (NodeState SimpleTx)
forall a b. (a -> b) -> a -> b
$ Signature (Snapshot SimpleTx) -> SnapshotNumber -> Message SimpleTx
forall tx. Signature (Snapshot tx) -> SnapshotNumber -> Message tx
AckSn (Secret (SigningKey HydraKey)
-> Snapshot SimpleTx -> Signature (Snapshot SimpleTx)
forall a.
SignableRepresentation a =>
Secret (SigningKey HydraKey) -> a -> Signature a
sign Secret (SigningKey HydraKey)
sk Snapshot SimpleTx
candidate) Snapshot SimpleTx
candidate.number) NodeState SimpleTx
node [(Party, Secret (SigningKey HydraKey))]
signers
requests :: Head.Outcome SimpleTx -> [Message SimpleTx]
requests :: Outcome SimpleTx -> [Message SimpleTx]
requests = \case
Head.Continue [StateChanged SimpleTx]
_ [Effect SimpleTx]
effects -> [Message SimpleTx
message | Head.NetworkEffect message :: Message SimpleTx
message@ReqSn{} <- [Effect SimpleTx]
effects]
Outcome SimpleTx
_ -> []