{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Hydra.HeadLogic.InputWorkflowSpec (spec) where
import Data.Map.Strict qualified as Map
import Data.Set qualified as Set
import Hydra.API.ClientInput (ClientInput (..))
import Hydra.API.ServerOutput (DecommitInvalidReason (..))
import Hydra.API.ServerOutput qualified as ServerOutput
import Hydra.Agda.OffChainReference qualified as Agda
import Hydra.Chain (ChainEvent (..))
import Hydra.HeadLogic.Cardano (cardanoSnapshotLimits)
import Hydra.HeadLogic.Interpreter qualified as Interpreter
import Hydra.HeadLogic.Rules.Chain.Fanout qualified as ChainFanout
import Hydra.HeadLogic.Rules.Chain.Rollback qualified as Rollback
import Hydra.HeadLogic.Rules.Chain.Settlement qualified as Settlement
import Hydra.HeadLogic.Rules.Client.Decommit qualified as Decommit
import Hydra.HeadLogic.Workflow qualified as Workflow
import Hydra.Ledger (Ledger (..), ValidationError (..))
import Hydra.Ledger.Simple (SimpleTx (..), SimpleTxOut (..), simpleLedger)
import Hydra.Network.Message (Message (..), NetworkEvent (..))
import Hydra.Node.HeadLogic (
CoordinatedHeadState (decommitTx, seenSnapshot),
Effect (ClientEffect, NetworkEffect),
FanoutMode (AutoDrain, AwaitingSelection, DistributingSelection),
FinalizedSnapshot (FinalizedSnapshot),
HeadState (FanoutProgress),
Input (ChainInput, ClientInput, NetworkInput),
LogicError (SideLoadSnapshotFailed),
OpenState (coordinatedHeadState, headId, headSeed, parameters),
Outcome (Continue, Error, Wait),
PartialFanoutState (mode, remainingOutputs),
StateChanged (
CommitFinalized,
DecommitFinalized,
DecommitInvalid,
HeadPartialFannedOut,
SnapshotConfirmed,
decommitInvalidReason,
mode
),
WaitReason (WaitOnNodeInSync),
mkSeenSnapshot,
update,
)
import Hydra.Node.HeadLogic.Configuration qualified as Configuration
import Hydra.Node.State (NodeState (..), initialChainTime)
import Hydra.Prelude
import Hydra.Tx.Accumulator qualified as Accumulator
import Hydra.Tx.Crypto (MultiSignature, aggregate, aggregateInOrder, sign)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (..), Snapshot (..))
import Test.Hydra.HeadLogic.InputFixtures qualified as Inputs
import Test.Hydra.HeadLogic.Interpreter (testInterpreterConfig)
import Test.Hydra.HeadLogic.Scenario qualified as Scenario
import Test.Hydra.Ledger.Simple (utxoRef)
import Test.Hydra.Prelude (
Spec,
describe,
expectationFailure,
it,
shouldBe,
shouldSatisfy,
)
import Test.Hydra.Tx.Fixture (alice, aliceSk, bobSk, carolSk)
spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"bounded Agda input decisions" (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
"checks size before sync admission, and admits chain inputs while catching up" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[(Integer, Bool, Bool)]
-> ((Integer, Bool, Bool) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Integer
kind, Bool
safe, Bool
synced) | Integer
kind <- [Integer
0, Integer
1, Integer
2], Bool
safe <- [Bool
False, Bool
True], Bool
synced <- [Bool
False, Bool
True]] (((Integer, Bool, Bool) -> IO ()) -> IO ())
-> ((Integer, Bool, Bool) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Integer
kind, Bool
safe, Bool
synced) -> do
let node :: NodeState SimpleTx
node = if Bool
synced then NodeState SimpleTx
initial else HeadState SimpleTx
-> TrackedDeposits SimpleTx -> ChainPointTime -> NodeState SimpleTx
forall tx.
HeadState tx
-> TrackedDeposits tx -> ChainPointTime -> NodeState tx
NodeCatchingUp NodeState SimpleTx
initial.headState NodeState SimpleTx
initial.deposits NodeState SimpleTx
initial.chainPointTime
input :: Input SimpleTx
input = case Integer
kind of
Integer
0 -> ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput (ClientInput SimpleTx -> Input SimpleTx)
-> ClientInput SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ if Bool
safe then ClientInput SimpleTx
forall tx. ClientInput tx
Close else ConfirmedSnapshot SimpleTx -> ClientInput SimpleTx
forall tx. ConfirmedSnapshot tx -> ClientInput tx
SideLoadSnapshot (ConfirmedSnapshot SimpleTx -> ClientInput SimpleTx)
-> ConfirmedSnapshot SimpleTx -> ClientInput SimpleTx
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot Snapshot SimpleTx
oversized ([Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate [])
Integer
1 -> TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
NetworkInput TTL
1 (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
Scenario.transactionOne
Integer
_ -> ChainEvent SimpleTx -> Input SimpleTx
forall tx. ChainEvent tx -> Input tx
ChainInput (ChainEvent SimpleTx -> Input SimpleTx)
-> ChainEvent SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ UTCTime -> ChainPointType SimpleTx -> ChainEvent SimpleTx
forall tx. UTCTime -> ChainPointType tx -> ChainEvent tx
Tick UTCTime
initialChainTime ChainSlot
ChainPointType SimpleTx
0
result :: Outcome SimpleTx
result = Environment
-> Ledger SimpleTx
-> UTCTime
-> NodeState SimpleTx
-> Input SimpleTx
-> Outcome SimpleTx
forall tx.
IsChainState tx =>
Environment
-> Ledger tx -> UTCTime -> NodeState tx -> Input tx -> Outcome tx
update Environment
env Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node Input SimpleTx
input
tag :: Integer
tag = case Outcome SimpleTx
result of
Error SideLoadSnapshotFailed{} -> Integer
1
Continue [] [ClientEffect ServerOutput.RejectedInputBecauseUnsynced{}] -> Integer
2
Wait WaitOnNodeInSync{} [] -> Integer
3
Outcome SimpleTx
_ -> Integer
0
Integer
tag Integer -> Integer -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Integer -> Bool -> Bool -> Integer
Agda.inputAdmissionRef Integer
kind Bool
safe Bool
synced
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"preserves client decommit failure priority" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[(Bool, Bool, Bool)] -> ((Bool, Bool, Bool) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
existing, Bool
applies, Bool
outputs) | Bool
existing <- [Bool
False, Bool
True], Bool
applies <- [Bool
False, Bool
True], Bool
outputs <- [Bool
False, Bool
True]] (((Bool, Bool, Bool) -> IO ()) -> IO ())
-> ((Bool, Bool, Bool) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
existing, Bool
applies, Bool
outputs) -> do
let tx :: SimpleTx
tx = if Bool
outputs then SimpleTx
Scenario.transactionOne else SimpleTx
Scenario.transactionOne{txOutputs = mempty}
st :: CoordinatedHeadState SimpleTx
st :: CoordinatedHeadState SimpleTx
st = OpenState SimpleTx
os.coordinatedHeadState{decommitTx = if existing then Just Scenario.transactionTwo else Nothing}
ledger :: Ledger SimpleTx
ledger = (ChainSlot
-> UTxOType SimpleTx
-> [SimpleTx]
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx
forall tx.
(ChainSlot
-> UTxOType tx
-> [tx]
-> Either (tx, ValidationError) (UTxOType tx))
-> Ledger tx
Ledger ((ChainSlot
-> UTxOType SimpleTx
-> [SimpleTx]
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx)
-> (ChainSlot
-> UTxOType SimpleTx
-> [SimpleTx]
-> Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
-> Ledger SimpleTx
forall a b. (a -> b) -> a -> b
$ \ChainSlot
_ UTxOType SimpleTx
utxo [SimpleTx]
_ -> if Bool
applies then Set SimpleTxOut
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. b -> Either a b
Right Set SimpleTxOut
UTxOType SimpleTx
utxo else (SimpleTx, ValidationError)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. a -> Either a b
Left (SimpleTx
tx, Text -> ValidationError
ValidationError Text
"controlled ledger failure")
result :: Outcome SimpleTx
result =
InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
ledger) (Program SimpleTx () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$
Workflow SimpleTx DecommitTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx DecommitTopology () -> Program SimpleTx ())
-> Workflow SimpleTx DecommitTopology () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$
Args SimpleTx -> Workflow SimpleTx DecommitTopology ()
forall tx. IsTx tx => Args tx -> Workflow tx DecommitTopology ()
Decommit.workflow
Decommit.Args
{ $sel:snapshotLimits:Args :: SnapshotLimits
snapshotLimits = SnapshotLimits
cardanoSnapshotLimits
, $sel:headId:Args :: HeadId
headId = OpenState SimpleTx
os.headId
, $sel:currentSlot:Args :: ChainSlot
currentSlot = ChainSlot
0
, $sel:coordinatedHeadState:Args :: CoordinatedHeadState SimpleTx
coordinatedHeadState = CoordinatedHeadState SimpleTx
st
, $sel:decommitTx:Args :: SimpleTx
decommitTx = SimpleTx
tx
}
tag :: Integer
tag = case Outcome SimpleTx
result of
Continue [DecommitInvalid{$sel:decommitInvalidReason:NetworkConnected :: forall tx. StateChanged tx -> DecommitInvalidReason tx
decommitInvalidReason = DecommitAlreadyInFlight{}}] [] -> Integer
0
Continue [DecommitInvalid{$sel:decommitInvalidReason:NetworkConnected :: forall tx. StateChanged tx -> DecommitInvalidReason tx
decommitInvalidReason = DecommitTxInvalid{$sel:validationError:DecommitTxInvalid :: forall tx. DecommitInvalidReason tx -> ValidationError
validationError = ValidationError Text
"controlled ledger failure"}}] [] -> Integer
1
Continue [DecommitInvalid{$sel:decommitInvalidReason:NetworkConnected :: forall tx. StateChanged tx -> DecommitInvalidReason tx
decommitInvalidReason = DecommitTxInvalid{}}] [] -> Integer
2
Continue [] [NetworkEffect ReqDec{}] -> Integer
3
Outcome SimpleTx
_ -> -Integer
1
Integer
tag Integer -> Integer -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Bool -> Bool -> Integer
Agda.clientDecommitRef Bool
existing Bool
applies Bool
outputs
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"adopts exactly the matching signed settlement and always records finalization first" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[(Bool, SnapshotNumber, SnapshotVersion, Bool, Bool)]
-> ((Bool, SnapshotNumber, SnapshotVersion, Bool, Bool) -> IO ())
-> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
increment, SnapshotNumber
observed, SnapshotVersion
newVersion, Bool
matches, Bool
valid) | Bool
increment <- [Bool
False, Bool
True], SnapshotNumber
observed <- [SnapshotNumber
0, SnapshotNumber
1, SnapshotNumber
2], SnapshotVersion
newVersion <- [SnapshotVersion
0, SnapshotVersion
1, SnapshotVersion
2], Bool
matches <- [Bool
False, Bool
True], Bool
valid <- [Bool
False, Bool
True]] (((Bool, SnapshotNumber, SnapshotVersion, Bool, Bool) -> IO ())
-> IO ())
-> ((Bool, SnapshotNumber, SnapshotVersion, Bool, Bool) -> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
increment, SnapshotNumber
observed, SnapshotVersion
newVersion, Bool
matches, Bool
valid) -> do
let utxo :: UTxOType SimpleTx
utxo = Integer -> UTxOType SimpleTx
utxoRef Integer
1
commit :: Maybe (UTxOType SimpleTx)
commit = if Bool
increment Bool -> Bool -> Bool
&& Bool
matches then UTxOType SimpleTx -> Maybe (UTxOType SimpleTx)
forall a. a -> Maybe a
Just (UTxOType SimpleTx -> Maybe (UTxOType SimpleTx))
-> UTxOType SimpleTx -> Maybe (UTxOType SimpleTx)
forall a b. (a -> b) -> a -> b
$ Integer -> UTxOType SimpleTx
utxoRef Integer
42 else Maybe (Set SimpleTxOut)
Maybe (UTxOType SimpleTx)
forall a. Maybe a
Nothing
decommit :: Maybe (UTxOType SimpleTx)
decommit = if Bool -> Bool
not Bool
increment Bool -> Bool -> Bool
&& Bool
matches then UTxOType SimpleTx -> Maybe (UTxOType SimpleTx)
forall a. a -> Maybe a
Just (UTxOType SimpleTx -> Maybe (UTxOType SimpleTx))
-> UTxOType SimpleTx -> Maybe (UTxOType SimpleTx)
forall a b. (a -> b) -> a -> b
$ Integer -> UTxOType SimpleTx
utxoRef Integer
2 else Maybe (Set SimpleTxOut)
Maybe (UTxOType SimpleTx)
forall a. Maybe a
Nothing
(HydraAccumulator
accumulator, HydraAccumulator
appliedAccumulator) = forall tx.
IsTx tx =>
UTxOType tx
-> Maybe (UTxOType tx)
-> Maybe (UTxOType tx)
-> (HydraAccumulator, HydraAccumulator)
Accumulator.buildFromSnapshotUTxOs @SimpleTx Set SimpleTxOut
UTxOType SimpleTx
utxo Maybe (UTxOType SimpleTx)
commit Maybe (UTxOType SimpleTx)
decommit
snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
Scenario.initialSnapshot{number = 1, utxoToCommit = commit, utxoToDecommit = decommit, depositTxId = if isJust commit then Just 42 else Nothing, accumulator, appliedAccumulator}
st :: OpenState SimpleTx
st = OpenState SimpleTx
os{coordinatedHeadState = os.coordinatedHeadState{seenSnapshot = mkSeenSnapshot snapshot mempty}}
signatures :: MultiSignature (Snapshot SimpleTx)
signatures = if Bool
valid then Snapshot SimpleTx -> MultiSignature (Snapshot SimpleTx)
signaturesFor Snapshot SimpleTx
snapshot else [Signature (Snapshot SimpleTx)]
-> MultiSignature (Snapshot SimpleTx)
forall a. [Signature a] -> MultiSignature a
aggregate []
outcome :: Outcome SimpleTx
outcome =
if Bool
increment
then
InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$
Workflow SimpleTx SettlementTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx SettlementTopology () -> Program SimpleTx ())
-> Workflow SimpleTx SettlementTopology () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$
Args SimpleTx -> Workflow SimpleTx SettlementTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx SettlementTopology ()
Settlement.workflow
Settlement.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
Configuration.ruleEnvironment Environment
env
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map Integer (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
st
, $sel:newChainState:Args :: ChainStateType SimpleTx
newChainState = ChainStateType SimpleTx
SimpleChainState
1
, $sel:newVersion:Args :: SnapshotVersion
newVersion = SnapshotVersion
newVersion
, $sel:settlement:Args :: Settlement SimpleTx
settlement = TxIdType SimpleTx -> Settlement SimpleTx
forall tx. TxIdType tx -> Settlement tx
Settlement.Increment Integer
TxIdType SimpleTx
42
, $sel:snapshotNumber:Args :: SnapshotNumber
snapshotNumber = SnapshotNumber
observed
, $sel:signatures:Args :: MultiSignature (Snapshot SimpleTx)
signatures = MultiSignature (Snapshot SimpleTx)
signatures
}
else
InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$
Workflow SimpleTx SettlementTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx SettlementTopology () -> Program SimpleTx ())
-> Workflow SimpleTx SettlementTopology () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$
Args SimpleTx -> Workflow SimpleTx SettlementTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx SettlementTopology ()
Settlement.workflow
Settlement.Args
{ $sel:env:Args :: RuleEnvironment
env = Environment -> RuleEnvironment
Configuration.ruleEnvironment Environment
env
, $sel:pendingDeposits:Args :: PendingDeposits SimpleTx
pendingDeposits = Map Integer (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty
, $sel:openState:Args :: OpenState SimpleTx
openState = OpenState SimpleTx
st
, $sel:newChainState:Args :: ChainStateType SimpleTx
newChainState = ChainStateType SimpleTx
SimpleChainState
1
, $sel:newVersion:Args :: SnapshotVersion
newVersion = SnapshotVersion
newVersion
, $sel:settlement:Args :: Settlement SimpleTx
settlement = UTxOType SimpleTx -> Settlement SimpleTx
forall tx. UTxOType tx -> Settlement tx
Settlement.Decrement (Integer -> UTxOType SimpleTx
utxoRef Integer
2)
, $sel:snapshotNumber:Args :: SnapshotNumber
snapshotNumber = SnapshotNumber
observed
, $sel:signatures:Args :: MultiSignature (Snapshot SimpleTx)
signatures = MultiSignature (Snapshot SimpleTx)
signatures
}
shouldAdopt :: Bool
shouldAdopt = Integer -> Integer -> Integer -> Integer -> Bool -> Bool -> Bool
Agda.adoptSettlementRef (SnapshotNumber -> Integer
forall a. Integral a => a -> Integer
toInteger SnapshotNumber
observed) Integer
1 Integer
0 (SnapshotVersion -> Integer
forall a. Integral a => a -> Integer
toInteger SnapshotVersion
newVersion) Bool
matches Bool
valid
case Outcome SimpleTx
outcome of
Continue [StateChanged SimpleTx]
events [Effect SimpleTx]
_ -> do
[() | SnapshotConfirmed{} <- [StateChanged SimpleTx]
events] [()] -> [()] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [() | Bool
shouldAdopt]
Int -> [StateChanged SimpleTx] -> [StateChanged SimpleTx]
forall a. Int -> [a] -> [a]
take Int
1 [StateChanged SimpleTx]
events [StateChanged SimpleTx]
-> ([StateChanged SimpleTx] -> Bool) -> IO ()
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> IO ()
`shouldSatisfy` \case [CommitFinalized{}] -> Bool
increment; [DecommitFinalized{}] -> Bool -> Bool
not Bool
increment; [StateChanged SimpleTx]
_ -> Bool
False
Outcome SimpleTx
_ -> HasCallStack => String -> IO ()
String -> IO ()
expectationFailure (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Outcome SimpleTx -> String
forall b a. (Show a, IsString b) => a -> b
show Outcome SimpleTx
outcome
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reposts retained settlement only strictly before its observation slot" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[(Bool, Integer)] -> ((Bool, Integer) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
pending, Integer
rollbackSlot) | Bool
pending <- [Bool
False, Bool
True], Integer
rollbackSlot <- [Integer
2, Integer
3, Integer
4]] (((Bool, Integer) -> IO ()) -> IO ())
-> ((Bool, Integer) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
pending, Integer
rollbackSlot) -> do
let snapshot :: Snapshot SimpleTx
snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
Scenario.initialSnapshot{utxoToDecommit = Just $ utxoRef 2}
confirmed :: ConfirmedSnapshot SimpleTx
confirmed = Snapshot SimpleTx
-> MultiSignature (Snapshot SimpleTx) -> ConfirmedSnapshot SimpleTx
forall tx.
Snapshot tx -> MultiSignature (Snapshot tx) -> ConfirmedSnapshot tx
ConfirmedSnapshot Snapshot SimpleTx
snapshot (Snapshot SimpleTx -> MultiSignature (Snapshot SimpleTx)
signaturesFor Snapshot SimpleTx
snapshot)
finalized :: Maybe (FinalizedSnapshot SimpleTx)
finalized = FinalizedSnapshot SimpleTx -> Maybe (FinalizedSnapshot SimpleTx)
forall a. a -> Maybe a
Just (FinalizedSnapshot SimpleTx -> Maybe (FinalizedSnapshot SimpleTx))
-> FinalizedSnapshot SimpleTx -> Maybe (FinalizedSnapshot SimpleTx)
forall a b. (a -> b) -> a -> b
$ ConfirmedSnapshot SimpleTx
-> ChainSlot -> FinalizedSnapshot SimpleTx
forall tx.
ConfirmedSnapshot tx -> ChainSlot -> FinalizedSnapshot tx
FinalizedSnapshot ConfirmedSnapshot SimpleTx
confirmed ChainSlot
3
result :: Outcome SimpleTx
result = InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadSeed
-> HeadId
-> HeadParameters
-> Maybe SimpleTx
-> ConfirmedSnapshot SimpleTx
-> Maybe (FinalizedSnapshot SimpleTx)
-> ChainSlot
-> Program SimpleTx ()
forall tx.
HeadSeed
-> HeadId
-> HeadParameters
-> Maybe tx
-> ConfirmedSnapshot tx
-> Maybe (FinalizedSnapshot tx)
-> ChainSlot
-> Program tx ()
Rollback.maybeRepostDecrementTx OpenState SimpleTx
os.headSeed OpenState SimpleTx
os.headId OpenState SimpleTx
os.parameters (if Bool
pending then SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
Scenario.transactionOne else Maybe SimpleTx
forall a. Maybe a
Nothing) ConfirmedSnapshot SimpleTx
confirmed Maybe (FinalizedSnapshot SimpleTx)
finalized (Integer -> ChainSlot
forall a. Num a => Integer -> a
fromInteger Integer
rollbackSlot)
posted :: Bool
posted = case Outcome SimpleTx
result of Continue [] [Effect SimpleTx
_] -> Bool
True; Outcome SimpleTx
_ -> Bool
False
Bool
posted Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Integer -> Integer -> Bool
Agda.repostFinalizedRef Bool
pending Integer
rollbackSlot Integer
3
String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"continues fanout according to the remaining set and driver mode" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
[(Bool, Integer)] -> ((Bool, Integer) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
remainderEmpty, Integer
modeTag) | Bool
remainderEmpty <- [Bool
False, Bool
True], Integer
modeTag <- [Integer
0, Integer
1, Integer
2]] (((Bool, Integer) -> IO ()) -> IO ())
-> ((Bool, Integer) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Bool
remainderEmpty, Integer
modeTag) -> do
let modes :: [FanoutMode SimpleTx]
modes = [FanoutMode SimpleTx
forall tx. FanoutMode tx
AutoDrain, UTxOType SimpleTx -> FanoutMode SimpleTx
forall tx. UTxOType tx -> FanoutMode tx
DistributingSelection (UTxOType SimpleTx -> FanoutMode SimpleTx)
-> UTxOType SimpleTx -> FanoutMode SimpleTx
forall a b. (a -> b) -> a -> b
$ Integer -> UTxOType SimpleTx
utxoRef Integer
2, FanoutMode SimpleTx
forall tx. FanoutMode tx
AwaitingSelection]
drivers :: [PartialFanoutState SimpleTx]
drivers = [PartialFanoutState SimpleTx
st | (Text
"fanout-driver-observation", NodeInSync{$sel:headState:NodeInSync :: forall tx. NodeState tx -> HeadState tx
headState = FanoutProgress PartialFanoutState SimpleTx
st}, Input SimpleTx
_) <- [(Text, NodeState SimpleTx, Input SimpleTx)]
Inputs.examples]
[PartialFanoutState SimpleTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PartialFanoutState SimpleTx]
drivers Int -> Int -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Int
1
[PartialFanoutState SimpleTx]
-> (PartialFanoutState SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [PartialFanoutState SimpleTx]
drivers ((PartialFanoutState SimpleTx -> IO ()) -> IO ())
-> (PartialFanoutState SimpleTx -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \PartialFanoutState SimpleTx
driver -> do
let st :: PartialFanoutState SimpleTx
st :: PartialFanoutState SimpleTx
st = PartialFanoutState SimpleTx
driver{remainingOutputs = utxoRef 1 <> utxoRef 2 <> utxoRef 3, mode = fromMaybe AwaitingSelection $ listToMaybe $ drop (fromInteger modeTag) modes}
observed :: Set SimpleTxOut
observed = if Bool
remainderEmpty then PartialFanoutState SimpleTx
st.remainingOutputs else Integer -> UTxOType SimpleTx
utxoRef Integer
1
result :: Outcome SimpleTx
result = InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program SimpleTx () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$ PartialFanoutState SimpleTx
-> ChainStateType SimpleTx
-> UTxOType SimpleTx
-> Program SimpleTx ()
forall tx.
IsTx tx =>
PartialFanoutState tx
-> ChainStateType tx -> UTxOType tx -> Program tx ()
ChainFanout.onPartialFanoutChainPartialFanoutTx PartialFanoutState SimpleTx
st ChainStateType SimpleTx
SimpleChainState
2 Set SimpleTxOut
UTxOType SimpleTx
observed
tag :: Integer
tag = case Outcome SimpleTx
result of
Continue [StateChanged SimpleTx]
_ [] -> Integer
0
Continue [HeadPartialFannedOut{$sel:mode:NetworkConnected :: forall tx. StateChanged tx -> FanoutMode tx
mode = DistributingSelection{}}] [Effect SimpleTx
_] -> Integer
2
Continue [StateChanged SimpleTx]
_ [Effect SimpleTx
_] -> Integer
1
Outcome SimpleTx
_ -> -Integer
1
Integer
tag Integer -> Integer -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Bool -> Integer -> Integer
Agda.fanoutContinuationRef Bool
remainderEmpty Integer
modeTag
where
initial :: NodeState SimpleTx
initial = NodeState SimpleTx
Scenario.initialNode
os :: OpenState SimpleTx
os = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
initial
env :: Environment
env = Int -> Environment
Scenario.environment Int
0
oversized :: Snapshot SimpleTx
oversized :: Snapshot SimpleTx
oversized = Snapshot SimpleTx
Scenario.initialSnapshot{utxo = large, accumulator = largeAccumulator, appliedAccumulator = largeAccumulator}
large :: Set SimpleTxOut
large = [SimpleTxOut] -> Set SimpleTxOut
forall a. Ord a => [a] -> Set a
Set.fromList ([SimpleTxOut] -> Set SimpleTxOut)
-> [SimpleTxOut] -> Set SimpleTxOut
forall a b. (a -> b) -> a -> b
$ Integer -> SimpleTxOut
SimpleTxOut (Integer -> SimpleTxOut) -> [Integer] -> [SimpleTxOut]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Integer
0 .. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
Accumulator.maxAccumulatorSize]
largeAccumulator :: HydraAccumulator
largeAccumulator = forall tx. IsTx tx => UTxOType tx -> HydraAccumulator
Accumulator.buildFromUTxO @SimpleTx Set SimpleTxOut
UTxOType SimpleTx
large
signaturesFor :: Snapshot SimpleTx -> MultiSignature (Snapshot SimpleTx)
signaturesFor :: Snapshot SimpleTx -> MultiSignature (Snapshot SimpleTx)
signaturesFor Snapshot SimpleTx
snapshot = 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
snapshot) | (Party
p, Secret (SigningKey HydraKey)
sk) <- [Party]
-> [Secret (SigningKey HydraKey)]
-> [(Party, Secret (SigningKey HydraKey))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Party]
Scenario.scenarioParties [Secret (SigningKey HydraKey)
aliceSk, Secret (SigningKey HydraKey)
bobSk, Secret (SigningKey HydraKey)
carolSk]]) [Party]
Scenario.scenarioParties