{-# LANGUAGE OverloadedRecordDot #-}
{-# OPTIONS_GHC -Wno-ambiguous-fields #-}
module Hydra.HeadLogic.CapacitySpec (spec) where
import Hydra.HeadLogic.Cardano (cardanoSnapshotLimits)
import Hydra.HeadLogic.Program (change)
import Hydra.HeadLogic.Rules.Input qualified as Input
import Hydra.HeadLogic.Workflow qualified as Workflow
import Hydra.Prelude hiding (local)
import Test.Hydra.HeadLogic.Interpreter (evaluateResult, testInterpreterConfig)
import Test.Hydra.Prelude (Spec, describe, it, shouldBe, shouldSatisfy)
import Data.Map.Strict qualified as Map
import Data.Sequence qualified as Seq
import Data.Set qualified as Set
import Hydra.API.ClientInput qualified as Client
import Hydra.Agda.OffChainReference qualified as Agda
import Hydra.HeadLogic.Error (LogicError (RequireFailed, SideLoadSnapshotFailed), RequirementFailure (ReqSnUTxOSetTooLarge), SideLoadRequirementFailure (SideLoadUTxOSetTooLarge))
import Hydra.HeadLogic.Input (Input (ClientInput, NetworkInput))
import Hydra.HeadLogic.Interpreter qualified as Interpreter
import Hydra.HeadLogic.Limits (SnapshotLimits (..))
import Hydra.HeadLogic.Outcome (Effect (NetworkEffect), Outcome (Continue, Error), StateChanged (DecommitInvalid, DecommitRecorded, SnapshotRequestDecided, SnapshotRequested))
import Hydra.HeadLogic.Rules.Coordinated (sendSnapshot)
import Hydra.HeadLogic.Rules.Environment (RuleEnvironment (snapshotLimits))
import Hydra.HeadLogic.Rules.Transactions (applyTransactionsProgram, pruneTransactions)
import Hydra.HeadLogic.Snapshot.Capacity (checkSnapshotCapacity)
import Hydra.HeadLogic.Snapshot.Request (DecommitStatus (CarryDecommit, NoDecommit), SettlingCommit (ContinueCommit), nextSnapshotRequest, requestDepositTxId, requestMessage, requestTxs)
import Hydra.HeadLogic.State (CoordinatedHeadState (..), HeadState (Open), OpenState (..), SeenSnapshot (RequestedSnapshot), mkSeenSnapshot)
import Hydra.HeadLogic.State.Coordinated qualified as Coordinated
import Hydra.Ledger.Simple (SimpleTx (..), SimpleTxOut (..), simpleLedger)
import Hydra.Network.Message (Message (AckSn, ReqDec, ReqSn, ReqTx), NetworkEvent (ReceivedMessage))
import Hydra.Node.HeadLogic (aggregateState, update)
import Hydra.Node.HeadLogic.Configuration qualified as Configuration
import Hydra.Node.State (Deposit (..), DepositStatus (Active), NodeState (headState), PendingDeposits, initialChainTime)
import Hydra.Tx (HeadId (UnsafeHeadId), applyTxTo, txId)
import Hydra.Tx.Accumulator (AccumulatorTooLarge (..), maxAccumulatorSize)
import Hydra.Tx.Snapshot (ConfirmedSnapshot (ConfirmedSnapshot), Snapshot (..))
import Test.Hydra.HeadLogic.Capacity qualified as Capacity
import Test.Hydra.HeadLogic.Scenario qualified as Scenario
import Test.Hydra.Ledger.Simple (utxoRef, utxoRefs)
import Test.Hydra.Tx.Fixture (alice, testHeadId)
spec :: Spec
spec :: Spec
spec = String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"snapshot capacity" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"confirms the independent merge after pruning" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
Either Text ()
Capacity.capacityRegression Either Text () -> (Either Text () -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Either Text () -> Bool
forall a b. Either a b -> Bool
isRight
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"checks both fanout alternatives, counting overlapping references once" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
Integer
Agda.accumulatorCapacityRef Integer -> Integer -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
maxAccumulatorSize
[([Integer], [Integer], [Integer])]
-> (([Integer], [Integer], [Integer]) -> Expectation)
-> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [([Integer]
a, [Integer]
c, [Integer]
d) | [Integer]
a <- [Integer] -> [[Integer]]
forall a. [a] -> [[a]]
subsequences [Integer
1, Integer
2], [Integer]
c <- [Integer] -> [[Integer]]
forall a. [a] -> [[a]]
subsequences [Integer
2, Integer
3], [Integer]
d <- [Integer] -> [[Integer]]
forall a. [a] -> [[a]]
subsequences [Integer
3, Integer
4]] ((([Integer], [Integer], [Integer]) -> Expectation) -> Expectation)
-> (([Integer], [Integer], [Integer]) -> Expectation)
-> Expectation
forall a b. (a -> b) -> a -> b
$ \([Integer]
a, [Integer]
c, [Integer]
d) ->
Either AccumulatorTooLarge () -> Bool
forall a b. Either a b -> Bool
isRight (forall tx.
IsTx tx =>
SnapshotLimits
-> UTxOType tx
-> Maybe (UTxOType tx)
-> Maybe (UTxOType tx)
-> Either AccumulatorTooLarge ()
checkSnapshotCapacity @SimpleTx SnapshotLimits
cardanoSnapshotLimits (Int -> Set SimpleTxOut
padding Int
3 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> [Integer] -> Set SimpleTxOut
refs [Integer]
a) (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just (Set SimpleTxOut -> Maybe (Set SimpleTxOut))
-> Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer]
c) (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just (Set SimpleTxOut -> Maybe (Set SimpleTxOut))
-> Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer]
d))
Bool -> Bool -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> [Integer] -> [Integer] -> [Integer] -> Bool
Agda.capacityFitsRef Integer
3 [Integer]
a [Integer]
c [Integer]
d
[(Int, [Integer], [Integer], [Integer])]
-> ((Int, [Integer], [Integer], [Integer]) -> Expectation)
-> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int
limit, [Integer]
a, [Integer]
c, [Integer]
d) | Int
limit <- [Int
0 .. Int
4], [Integer]
a <- [Integer] -> [[Integer]]
forall a. [a] -> [[a]]
subsequences [Integer
1, Integer
2], [Integer]
c <- [Integer] -> [[Integer]]
forall a. [a] -> [[a]]
subsequences [Integer
2, Integer
3], [Integer]
d <- [Integer] -> [[Integer]]
forall a. [a] -> [[a]]
subsequences [Integer
3, Integer
4]] (((Int, [Integer], [Integer], [Integer]) -> Expectation)
-> Expectation)
-> ((Int, [Integer], [Integer], [Integer]) -> Expectation)
-> Expectation
forall a b. (a -> b) -> a -> b
$ \(Int
limit, [Integer]
a, [Integer]
c, [Integer]
d) ->
Either AccumulatorTooLarge () -> Bool
forall a b. Either a b -> Bool
isRight (forall tx.
IsTx tx =>
SnapshotLimits
-> UTxOType tx
-> Maybe (UTxOType tx)
-> Maybe (UTxOType tx)
-> Either AccumulatorTooLarge ()
checkSnapshotCapacity @SimpleTx (Int -> SnapshotLimits
SnapshotLimits Int
limit) ([Integer] -> Set SimpleTxOut
refs [Integer]
a) (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just (Set SimpleTxOut -> Maybe (Set SimpleTxOut))
-> Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer]
c) (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just (Set SimpleTxOut -> Maybe (Set SimpleTxOut))
-> Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer]
d))
Bool -> Bool -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> [Integer] -> [Integer] -> [Integer] -> Bool
Agda.capacityFitsRef (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
limit) [Integer]
a [Integer]
c [Integer]
d
[(Int, Int)] -> ((Int, Int) -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int
a, Int
b) | Int
a <- [Int
maxAccumulatorSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
maxAccumulatorSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1], Int
b <- [Int
maxAccumulatorSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
maxAccumulatorSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1]] (((Int, Int) -> Expectation) -> Expectation)
-> ((Int, Int) -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \(Int
a, Int
b) ->
Either AccumulatorTooLarge () -> Bool
forall a b. Either a b -> Bool
isRight (forall tx.
IsTx tx =>
SnapshotLimits
-> UTxOType tx
-> Maybe (UTxOType tx)
-> Maybe (UTxOType tx)
-> Either AccumulatorTooLarge ()
checkSnapshotCapacity @SimpleTx SnapshotLimits
cardanoSnapshotLimits Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just (Set SimpleTxOut -> Maybe (Set SimpleTxOut))
-> Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer
1 .. Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
a]) (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just (Set SimpleTxOut -> Maybe (Set SimpleTxOut))
-> Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer
1 .. Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
b]))
Bool -> Bool -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Integer -> Bool
Agda.snapshotCapacityRef (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
a) (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
b)
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"agrees with Agda selection and pruning at configured and deployed limits" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
[(Bool, Int, [Integer], [SimpleTx])]
-> ((Bool, Int, [Integer], [SimpleTx]) -> Expectation)
-> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
padded, Int
limit, [Integer]
commit, [SimpleTx]
txs) | Bool
padded <- [Bool
False, Bool
True], Int
limit <- [Int
3, Int
4], [Integer]
commit <- [[], [Integer
8]], Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Integer] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Integer]
commit Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
limit, [SimpleTx]
txs <- (Int -> [[SimpleTx]]) -> [Int] -> [[SimpleTx]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int -> [SimpleTx] -> [[SimpleTx]]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
`replicateM` [SimpleTx]
palette) [Int
0 .. Int
3]] (((Bool, Int, [Integer], [SimpleTx]) -> Expectation)
-> Expectation)
-> ((Bool, Int, [Integer], [SimpleTx]) -> Expectation)
-> Expectation
forall a b. (a -> b) -> a -> b
$ \(Bool
padded, Int
limit, [Integer]
commit, [SimpleTx]
txs) -> do
let limits :: SnapshotLimits
limits = if Bool
padded then SnapshotLimits
cardanoSnapshotLimits else Int -> SnapshotLimits
SnapshotLimits Int
limit
active :: Set SimpleTxOut
active = (if Bool
padded then Int -> Set SimpleTxOut
padding Int
limit else Set SimpleTxOut
forall a. Monoid a => a
mempty) Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> [Integer] -> Set SimpleTxOut
refs [Integer
1, Integer
2, Integer
3]
base :: Snapshot SimpleTx
base = Snapshot SimpleTx
Scenario.initialSnapshot{utxo = active}
held :: Set SimpleTxOut
held = [Integer] -> Set SimpleTxOut
refs [Integer]
commit
deposit :: Deposit SimpleTx
deposit = HeadId
-> UTxOType SimpleTx
-> UTCTime
-> UTCTime
-> DepositStatus
-> Deposit SimpleTx
forall tx.
HeadId
-> UTxOType tx -> UTCTime -> UTCTime -> DepositStatus -> Deposit tx
Deposit HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
held UTCTime
initialChainTime (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
100 UTCTime
initialChainTime) DepositStatus
Active
settling :: Maybe (SettlingCommit SimpleTx)
settling = if [Integer] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Integer]
commit then Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing else SettlingCommit SimpleTx -> Maybe (SettlingCommit SimpleTx)
forall a. a -> Maybe a
Just (SettlingCommit SimpleTx -> Maybe (SettlingCommit SimpleTx))
-> SettlingCommit SimpleTx -> Maybe (SettlingCommit SimpleTx)
forall a b. (a -> b) -> a -> b
$ (TxIdType SimpleTx, Deposit SimpleTx) -> SettlingCommit SimpleTx
forall tx. (TxIdType tx, Deposit tx) -> SettlingCommit tx
ContinueCommit (Integer
TxIdType SimpleTx
9, Deposit SimpleTx
deposit)
spentInputs :: [[Integer]]
spentInputs = (SimpleTx -> [Integer]) -> [SimpleTx] -> [[Integer]]
forall a b. (a -> b) -> [a] -> [b]
map (Set SimpleTxOut -> [Integer]
identifiers (Set SimpleTxOut -> [Integer])
-> (SimpleTx -> Set SimpleTxOut) -> SimpleTx -> [Integer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (.txInputs)) [SimpleTx]
txs
outputs :: [[Integer]]
outputs = (SimpleTx -> [Integer]) -> [SimpleTx] -> [[Integer]]
forall a b. (a -> b) -> [a] -> [b]
map (Set SimpleTxOut -> [Integer]
identifiers (Set SimpleTxOut -> [Integer])
-> (SimpleTx -> Set SimpleTxOut) -> SimpleTx -> [Integer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (.txOutputs)) [SimpleTx]
txs
request :: Maybe (SnapshotRequest SimpleTx)
request = 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
limits SnapshotVersion
0 Snapshot SimpleTx
base ([SimpleTx] -> Seq SimpleTx
forall a. [a] -> Seq a
Seq.fromList [SimpleTx]
txs) Map Integer (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit Maybe (SettlingCommit SimpleTx)
settling
pruning :: Program SimpleTx ()
pruning = do
Seq SimpleTx
kept <- SnapshotLimits
-> ChainSlot
-> Maybe (UTxOType SimpleTx)
-> Maybe (UTxOType SimpleTx)
-> UTxOType SimpleTx
-> Seq SimpleTx
-> Program SimpleTx (Seq SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> ChainSlot
-> Maybe (UTxOType tx)
-> Maybe (UTxOType tx)
-> UTxOType tx
-> Seq tx
-> Program tx (Seq tx)
pruneTransactions SnapshotLimits
limits ChainSlot
0 (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just Set SimpleTxOut
held) Maybe (Set SimpleTxOut)
Maybe (UTxOType SimpleTx)
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
active ([SimpleTx] -> Seq SimpleTx
forall a. [a] -> Seq a
Seq.fromList [SimpleTx]
txs)
StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change (StateChanged SimpleTx -> Program SimpleTx ())
-> StateChanged SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx
-> Seq SimpleTx
-> Maybe (TxIdType SimpleTx)
-> StateChanged SimpleTx
forall tx.
Snapshot tx -> Seq tx -> Maybe (TxIdType tx) -> StateChanged tx
SnapshotRequested Snapshot SimpleTx
base Seq SimpleTx
kept Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
expected :: [Integer]
expected = Integer
-> [Integer]
-> [Integer]
-> [Integer]
-> [[Integer]]
-> [[Integer]]
-> [Integer]
Agda.capacityPruneRef (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
limit) [Integer
1, Integer
2, Integer
3] [Integer]
commit [] [[Integer]]
spentInputs [[Integer]]
outputs
Bool -> Expectation -> Expectation
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Either (SimpleTx, ValidationError) (Set SimpleTxOut) -> Bool
forall a b. Either a b -> Bool
isRight (Either (SimpleTx, ValidationError) (Set SimpleTxOut) -> Bool)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut) -> Bool
forall a b. (a -> b) -> a -> b
$ (Outcome SimpleTx
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> (Either (SimpleTx, ValidationError) (Set SimpleTxOut)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either
(Outcome SimpleTx)
(Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text -> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a t. (HasCallStack, IsText t) => t -> a
error (Text -> Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> (Outcome SimpleTx -> Text)
-> Outcome SimpleTx
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Outcome SimpleTx -> Text
forall b a. (Show a, IsString b) => a -> b
show) Either (SimpleTx, ValidationError) (Set SimpleTxOut)
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a. a -> a
id (Either
(Outcome SimpleTx)
(Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either
(Outcome SimpleTx)
(Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either (SimpleTx, ValidationError) (Set SimpleTxOut)
forall a b. (a -> b) -> a -> b
$ InterpreterConfig SimpleTx
-> Program
SimpleTx (Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either
(Outcome SimpleTx)
(Either (SimpleTx, ValidationError) (Set SimpleTxOut))
forall tx a.
InterpreterConfig tx -> Program tx a -> Either (Outcome tx) a
evaluateResult (Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx. IsTx tx => Ledger tx -> InterpreterConfig tx
testInterpreterConfig Ledger SimpleTx
simpleLedger) (Program
SimpleTx (Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either
(Outcome SimpleTx)
(Either (SimpleTx, ValidationError) (Set SimpleTxOut)))
-> Program
SimpleTx (Either (SimpleTx, ValidationError) (Set SimpleTxOut))
-> Either
(Outcome SimpleTx)
(Either (SimpleTx, ValidationError) (Set SimpleTxOut))
forall a b. (a -> b) -> a -> b
$ ChainSlot
-> UTxOType SimpleTx
-> [SimpleTx]
-> Program
SimpleTx (Either (SimpleTx, ValidationError) (UTxOType SimpleTx))
forall tx.
IsTx tx =>
ChainSlot
-> UTxOType tx
-> [tx]
-> Program tx (Either (tx, ValidationError) (UTxOType tx))
applyTransactionsProgram ChainSlot
0 Set SimpleTxOut
UTxOType SimpleTx
active [SimpleTx]
txs) (Expectation -> Expectation) -> Expectation -> Expectation
forall a b. (a -> b) -> a -> b
$
(SnapshotRequest SimpleTx -> Integer)
-> Maybe (SnapshotRequest SimpleTx) -> Maybe Integer
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer)
-> (SnapshotRequest SimpleTx -> Int)
-> SnapshotRequest SimpleTx
-> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SimpleTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([SimpleTx] -> Int)
-> (SnapshotRequest SimpleTx -> [SimpleTx])
-> SnapshotRequest SimpleTx
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SnapshotRequest SimpleTx -> [SimpleTx]
forall tx. SnapshotRequest tx -> [tx]
requestTxs) Maybe (SnapshotRequest SimpleTx)
request
Maybe Integer -> Maybe Integer -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Maybe Integer
forall a. a -> Maybe a
Just (Integer
-> [Integer] -> [Integer] -> [[Integer]] -> [[Integer]] -> Integer
Agda.capacityPrefixRef (Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
limit) [Integer
1, Integer
2, Integer
3] [Integer]
commit [[Integer]]
spentInputs [[Integer]]
outputs)
case InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Environment -> Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx.
IsTx tx =>
Environment -> Ledger tx -> InterpreterConfig tx
Configuration.interpreterConfig (Int -> Environment
Scenario.environment Int
0) Ledger SimpleTx
simpleLedger) Program SimpleTx ()
pruning of
Continue [SnapshotRequested Snapshot SimpleTx
_ Seq SimpleTx
kept Maybe (TxIdType SimpleTx)
_] [] ->
Seq SimpleTx -> [SimpleTx]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Seq SimpleTx
kept [SimpleTx] -> [SimpleTx] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [SimpleTx
tx | (Integer
index, SimpleTx
tx) <- [Integer] -> [SimpleTx] -> [(Integer, SimpleTx)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Integer
1 ..] [SimpleTx]
txs, Integer
index Integer -> [Integer] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Integer]
expected]
Outcome SimpleTx
_ -> String -> Expectation
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"pruning does not produce its retained sequence"
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"uses configured limits for follower requests and both decommit entry paths" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let split :: SimpleTx
split = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef Integer
1) ([Integer] -> Set SimpleTxOut
refs [Integer
4, Integer
5])
run :: SnapshotLimits
-> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx
run SnapshotLimits
limits NodeState SimpleTx
node Input SimpleTx
input =
InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Environment -> Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx.
IsTx tx =>
Environment -> Ledger tx -> InterpreterConfig tx
Configuration.interpreterConfig (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger) (Program SimpleTx () -> Outcome SimpleTx)
-> Program SimpleTx () -> Outcome SimpleTx
forall a b. (a -> b) -> a -> b
$
Workflow SimpleTx InputTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run
( Args SimpleTx -> Workflow SimpleTx InputTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx InputTopology ()
Input.workflow
Input.Args
{ $sel:env:Args :: RuleEnvironment
env = (Environment -> RuleEnvironment
Configuration.ruleEnvironment (Int -> Environment
Scenario.environment Int
1)){snapshotLimits = limits}
, $sel:now:Args :: UTCTime
now = UTCTime
initialChainTime
, $sel:nodeState:Args :: NodeState SimpleTx
nodeState = NodeState SimpleTx
node
, $sel:input:Args :: Input SimpleTx
input = Input SimpleTx
input
}
)
received :: NodeState SimpleTx
received = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
Scenario.initialNode (Outcome SimpleTx -> NodeState SimpleTx)
-> Outcome SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotLimits
-> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx
run SnapshotLimits
cardanoSnapshotLimits NodeState SimpleTx
Scenario.initialNode (TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
NetworkInput TTL
0 (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
split)
request :: Input SimpleTx
request = TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
NetworkInput TTL
0 (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
$ 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 -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
split] Maybe SimpleTx
forall a. Maybe a
Nothing Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
SnapshotLimits
-> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx
run (Int -> SnapshotLimits
SnapshotLimits Int
1) NodeState SimpleTx
received Input SimpleTx
request Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` LogicError SimpleTx -> Outcome SimpleTx
forall tx. LogicError tx -> Outcome tx
Error (RequirementFailure SimpleTx -> LogicError SimpleTx
forall tx. RequirementFailure tx -> LogicError tx
RequireFailed (RequirementFailure SimpleTx -> LogicError SimpleTx)
-> RequirementFailure SimpleTx -> LogicError SimpleTx
forall a b. (a -> b) -> a -> b
$ Int -> Int -> RequirementFailure SimpleTx
forall tx. Int -> Int -> RequirementFailure tx
ReqSnUTxOSetTooLarge Int
2 Int
1)
SnapshotLimits
-> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx
run (Int -> SnapshotLimits
SnapshotLimits Int
2) NodeState SimpleTx
received Input SimpleTx
request Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` \case Continue [StateChanged SimpleTx]
_ [Effect SimpleTx]
effects -> (Effect SimpleTx -> Bool) -> [Effect SimpleTx] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\case NetworkEffect AckSn{} -> Bool
True; Effect SimpleTx
_ -> Bool
False) [Effect SimpleTx]
effects; Outcome SimpleTx
_ -> Bool
False
[Input SimpleTx] -> (Input SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (SimpleTx -> [Input SimpleTx]
inputs SimpleTx
split) ((Input SimpleTx -> Expectation) -> Expectation)
-> (Input SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Input SimpleTx
input -> do
SnapshotLimits
-> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx
run (Int -> SnapshotLimits
SnapshotLimits Int
1) NodeState SimpleTx
Scenario.initialNode Input SimpleTx
input Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Outcome SimpleTx -> Bool
rejectedDecommit
SnapshotLimits
-> NodeState SimpleTx -> Input SimpleTx -> Outcome SimpleTx
run (Int -> SnapshotLimits
SnapshotLimits Int
2) NodeState SimpleTx
Scenario.initialNode Input SimpleTx
input Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` (Bool -> Bool
not (Bool -> Bool)
-> (Outcome SimpleTx -> Bool) -> Outcome SimpleTx -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Outcome SimpleTx -> Bool
rejectedDecommit)
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"checks configured side-load limits at ingress and before the core forces snapshot evidence" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let snapshot :: Snapshot SimpleTx
snapshot = Snapshot SimpleTx
Scenario.initialSnapshot{confirmed = error "side-load guard forces snapshot payload"}
input :: ClientInput SimpleTx
input = ConfirmedSnapshot SimpleTx -> ClientInput SimpleTx
forall tx. ConfirmedSnapshot tx -> ClientInput tx
Client.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
snapshot (Text -> MultiSignature (Snapshot SimpleTx)
forall a t. (HasCallStack, IsText t) => t -> a
error Text
"side-load guard forces certificate")
config :: InterpreterConfig SimpleTx
config = Environment -> Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx.
IsTx tx =>
Environment -> Ledger tx -> InterpreterConfig tx
Configuration.interpreterConfig (Int -> Environment
Scenario.environment Int
0) Ledger SimpleTx
simpleLedger
rules :: RuleEnvironment
rules = (Environment -> RuleEnvironment
Configuration.ruleEnvironment (Environment -> RuleEnvironment) -> Environment -> RuleEnvironment
forall a b. (a -> b) -> a -> b
$ Int -> Environment
Scenario.environment Int
0){snapshotLimits = SnapshotLimits 0}
Either AccumulatorTooLarge (ClientInput SimpleTx)
-> Either AccumulatorTooLarge ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (SnapshotLimits
-> ClientInput SimpleTx
-> Either AccumulatorTooLarge (ClientInput SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> ClientInput tx -> Either AccumulatorTooLarge (ClientInput tx)
Client.validateClientInputWith (Int -> SnapshotLimits
SnapshotLimits Int
0) ClientInput SimpleTx
input) Either AccumulatorTooLarge ()
-> Either AccumulatorTooLarge () -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` AccumulatorTooLarge -> Either AccumulatorTooLarge ()
forall a b. a -> Either a b
Left (Int -> Int -> AccumulatorTooLarge
AccumulatorTooLarge Int
1 Int
0)
Either AccumulatorTooLarge (ClientInput SimpleTx)
-> Either AccumulatorTooLarge ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ClientInput SimpleTx
-> Either AccumulatorTooLarge (ClientInput SimpleTx)
forall tx.
IsTx tx =>
ClientInput tx -> Either AccumulatorTooLarge (ClientInput tx)
Client.validateClientInput ClientInput SimpleTx
input) Either AccumulatorTooLarge ()
-> Either AccumulatorTooLarge () -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` () -> Either AccumulatorTooLarge ()
forall a b. b -> Either a b
Right ()
InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram
InterpreterConfig SimpleTx
config
( Workflow SimpleTx InputTopology () -> Program SimpleTx ()
forall {vertex} tx (topology :: Topology vertex) result.
Workflow tx topology result -> Program tx result
Workflow.run (Workflow SimpleTx InputTopology () -> Program SimpleTx ())
-> Workflow SimpleTx InputTopology () -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$
Args SimpleTx -> Workflow SimpleTx InputTopology ()
forall tx.
IsChainState tx =>
Args tx -> Workflow tx InputTopology ()
Input.workflow
Input.Args
{ $sel:env:Args :: RuleEnvironment
env = RuleEnvironment
rules
, $sel:now:Args :: UTCTime
now = UTCTime
initialChainTime
, $sel:nodeState:Args :: NodeState SimpleTx
nodeState = NodeState SimpleTx
Scenario.initialNode
, $sel:input:Args :: Input SimpleTx
input = ClientInput SimpleTx -> Input SimpleTx
forall tx. ClientInput tx -> Input tx
ClientInput ClientInput SimpleTx
input
}
)
Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` LogicError SimpleTx -> Outcome SimpleTx
forall tx. LogicError tx -> Outcome tx
Error (SideLoadRequirementFailure SimpleTx -> LogicError SimpleTx
forall tx. SideLoadRequirementFailure tx -> LogicError tx
SideLoadSnapshotFailed (SideLoadRequirementFailure SimpleTx -> LogicError SimpleTx)
-> SideLoadRequirementFailure SimpleTx -> LogicError SimpleTx
forall a b. (a -> b) -> a -> b
$ Int -> Int -> SideLoadRequirementFailure SimpleTx
forall tx. Int -> Int -> SideLoadRequirementFailure tx
SideLoadUTxOSetTooLarge Int
1 Int
0)
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"lets a new deposit use space freed by a merge and defers one that does not fit" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let base :: Snapshot SimpleTx
base = Snapshot SimpleTx
Scenario.initialSnapshot{utxo = padding 3 <> refs [1, 2, 3]}
merge :: SimpleTx
merge = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
2 ([Integer] -> Set SimpleTxOut
refs [Integer
2, Integer
3]) (Integer -> UTxOType SimpleTx
utxoRef Integer
6)
deposit :: Set SimpleTxOut -> PendingDeposits SimpleTx
deposit :: Set SimpleTxOut -> PendingDeposits SimpleTx
deposit Set SimpleTxOut
u = TxIdType SimpleTx -> Deposit SimpleTx -> PendingDeposits SimpleTx
forall k a. k -> a -> Map k a
Map.singleton Integer
TxIdType SimpleTx
9 (Deposit SimpleTx -> PendingDeposits SimpleTx)
-> Deposit SimpleTx -> PendingDeposits SimpleTx
forall a b. (a -> b) -> a -> b
$ HeadId
-> UTxOType SimpleTx
-> UTCTime
-> UTCTime
-> DepositStatus
-> Deposit SimpleTx
forall tx.
HeadId
-> UTxOType tx -> UTCTime -> UTCTime -> DepositStatus -> Deposit tx
Deposit HeadId
testHeadId Set SimpleTxOut
UTxOType SimpleTx
u UTCTime
initialChainTime (NominalDiffTime -> UTCTime -> UTCTime
addUTCTime NominalDiffTime
100 UTCTime
initialChainTime) DepositStatus
Active
select :: Map Integer (Deposit SimpleTx)
-> [SimpleTx] -> Maybe (SnapshotRequest SimpleTx)
select Map Integer (Deposit SimpleTx)
pending [SimpleTx]
txs = 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
base ([SimpleTx] -> Seq SimpleTx
forall a. [a] -> Seq a
Seq.fromList [SimpleTx]
txs) Map Integer (Deposit SimpleTx)
PendingDeposits SimpleTx
pending Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing
(SnapshotRequest SimpleTx -> Maybe Integer)
-> Maybe (SnapshotRequest SimpleTx) -> Maybe (Maybe Integer)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SnapshotRequest SimpleTx -> Maybe Integer
SnapshotRequest SimpleTx -> Maybe (TxIdType SimpleTx)
forall tx. SnapshotRequest tx -> Maybe (TxIdType tx)
requestDepositTxId (Map Integer (Deposit SimpleTx)
-> [SimpleTx] -> Maybe (SnapshotRequest SimpleTx)
select (Set SimpleTxOut -> PendingDeposits SimpleTx
deposit (Set SimpleTxOut -> PendingDeposits SimpleTx)
-> Set SimpleTxOut -> PendingDeposits SimpleTx
forall a b. (a -> b) -> a -> b
$ Integer -> UTxOType SimpleTx
utxoRef Integer
8) [SimpleTx
merge]) Maybe (Maybe Integer) -> Maybe (Maybe Integer) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Maybe Integer -> Maybe (Maybe Integer)
forall a. a -> Maybe a
Just (Integer -> Maybe Integer
forall a. a -> Maybe a
Just Integer
9)
(SnapshotRequest SimpleTx -> Maybe Integer)
-> Maybe (SnapshotRequest SimpleTx) -> Maybe (Maybe Integer)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SnapshotRequest SimpleTx -> Maybe Integer
SnapshotRequest SimpleTx -> Maybe (TxIdType SimpleTx)
forall tx. SnapshotRequest tx -> Maybe (TxIdType tx)
requestDepositTxId (Map Integer (Deposit SimpleTx)
-> [SimpleTx] -> Maybe (SnapshotRequest SimpleTx)
select (Set SimpleTxOut -> PendingDeposits SimpleTx
deposit (Set SimpleTxOut -> PendingDeposits SimpleTx)
-> Set SimpleTxOut -> PendingDeposits SimpleTx
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer
8, Integer
9]) [SimpleTx
merge]) Maybe (Maybe Integer) -> Maybe (Maybe Integer) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Maybe Integer -> Maybe (Maybe Integer)
forall a. a -> Maybe a
Just Maybe Integer
forall a. Maybe a
Nothing
(SnapshotRequest SimpleTx -> [SimpleTx])
-> Maybe (SnapshotRequest SimpleTx) -> Maybe [SimpleTx]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SnapshotRequest SimpleTx -> [SimpleTx]
forall tx. SnapshotRequest tx -> [tx]
requestTxs (Map Integer (Deposit SimpleTx)
-> [SimpleTx] -> Maybe (SnapshotRequest SimpleTx)
select (Set SimpleTxOut -> PendingDeposits SimpleTx
deposit (Set SimpleTxOut -> PendingDeposits SimpleTx)
-> Set SimpleTxOut -> PendingDeposits SimpleTx
forall a b. (a -> b) -> a -> b
$ [Integer] -> Set SimpleTxOut
refs [Integer
8, Integer
9]) [SimpleTx
merge]) Maybe [SimpleTx] -> Maybe [SimpleTx] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [SimpleTx] -> Maybe [SimpleTx]
forall a. a -> Maybe a
Just [SimpleTx
merge]
(SnapshotRequest SimpleTx -> Maybe Integer)
-> Maybe (SnapshotRequest SimpleTx) -> Maybe (Maybe Integer)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SnapshotRequest SimpleTx -> Maybe Integer
SnapshotRequest SimpleTx -> Maybe (TxIdType SimpleTx)
forall tx. SnapshotRequest tx -> Maybe (TxIdType tx)
requestDepositTxId (Map Integer (Deposit SimpleTx)
-> [SimpleTx] -> Maybe (SnapshotRequest SimpleTx)
select (Set SimpleTxOut -> PendingDeposits SimpleTx
deposit (Set SimpleTxOut -> PendingDeposits SimpleTx)
-> Set SimpleTxOut -> PendingDeposits SimpleTx
forall a b. (a -> b) -> a -> b
$ Integer -> UTxOType SimpleTx
utxoRef Integer
8) []) Maybe (Maybe Integer) -> Maybe (Maybe Integer) -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Maybe Integer -> Maybe (Maybe Integer)
forall a. a -> Maybe a
Just Maybe Integer
forall a. Maybe a
Nothing
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reserves fanout capacity for a carried decommit" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let base :: Snapshot SimpleTx
base = Snapshot SimpleTx
Scenario.initialSnapshot{utxo = padding 3 <> refs [1, 2], utxoToDecommit = Just $ utxoRef 9}
decommit :: SimpleTx
decommit = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
3 (Integer -> UTxOType SimpleTx
utxoRef Integer
8) (Integer -> UTxOType SimpleTx
utxoRef Integer
9)
split :: SimpleTx
split = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef Integer
1) ([Integer] -> Set SimpleTxOut
refs [Integer
4, Integer
5])
(SnapshotRequest SimpleTx -> [SimpleTx])
-> Maybe (SnapshotRequest SimpleTx) -> Maybe [SimpleTx]
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SnapshotRequest SimpleTx -> [SimpleTx]
forall tx. SnapshotRequest tx -> [tx]
requestTxs (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
base (SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton SimpleTx
split) Map Integer (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe Integer
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) Maybe [SimpleTx] -> Maybe [SimpleTx] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [SimpleTx] -> Maybe [SimpleTx]
forall a. a -> Maybe a
Just []
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"keeps competing optimistic transactions for a delayed valid snapshot at TTL zero" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let active :: Set SimpleTxOut
active = Int -> Set SimpleTxOut
padding Int
3 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> [Integer] -> Set SimpleTxOut
refs [Integer
1, Integer
2]
split :: SimpleTx
split = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef Integer
1) ([Integer] -> Set SimpleTxOut
refs [Integer
4, Integer
5])
competing :: SimpleTx
competing = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
2 (Integer -> UTxOType SimpleTx
utxoRef Integer
2) ([Integer] -> Set SimpleTxOut
refs [Integer
6, Integer
7])
open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
Scenario.initialNode
initial :: NodeState SimpleTx
initial = NodeState SimpleTx
Scenario.initialNode{headState = Open open{coordinatedHeadState = open.coordinatedHeadState{localUTxO = active, confirmedSnapshot = ConfirmedSnapshot Scenario.initialSnapshot{utxo = active} mempty}}}
run :: NodeState SimpleTx -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
node Message SimpleTx
message = 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 (Int -> Environment
Scenario.environment Int
1) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
node (TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
NetworkInput TTL
0 (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
message)
receive :: NodeState SimpleTx -> Message SimpleTx -> NodeState SimpleTx
receive NodeState SimpleTx
node Message SimpleTx
message = NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
node (Outcome SimpleTx -> NodeState SimpleTx)
-> Outcome SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
node Message SimpleTx
message
optimistic :: NodeState SimpleTx
optimistic = NodeState SimpleTx -> Message SimpleTx -> NodeState SimpleTx
receive (NodeState SimpleTx -> Message SimpleTx -> NodeState SimpleTx
receive NodeState SimpleTx
initial (Message SimpleTx -> NodeState SimpleTx)
-> Message SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
split) (Message SimpleTx -> NodeState SimpleTx)
-> Message SimpleTx -> NodeState SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqTx SimpleTx
competing
c :: CoordinatedHeadState SimpleTx
c = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
optimistic).coordinatedHeadState
signed :: Outcome SimpleTx
signed = NodeState SimpleTx -> Message SimpleTx -> Outcome SimpleTx
run NodeState SimpleTx
optimistic (Message SimpleTx -> Outcome SimpleTx)
-> Message SimpleTx -> Outcome 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 -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
competing] Maybe SimpleTx
forall a. Maybe a
Nothing Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
after :: CoordinatedHeadState SimpleTx
after = (NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState (NodeState SimpleTx -> OpenState SimpleTx)
-> NodeState SimpleTx -> OpenState SimpleTx
forall a b. (a -> b) -> a -> b
$ NodeState SimpleTx -> Outcome SimpleTx -> NodeState SimpleTx
forall tx.
IsChainState tx =>
NodeState tx -> Outcome tx -> NodeState tx
aggregateState NodeState SimpleTx
optimistic Outcome SimpleTx
signed).coordinatedHeadState
Set SimpleTxOut -> Int
forall a. Set a -> Int
Set.size CoordinatedHeadState SimpleTx
c.localUTxO Int -> Int -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Int
maxAccumulatorSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Integer -> Map Integer SimpleTx -> Maybe SimpleTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (SimpleTx -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
split) CoordinatedHeadState SimpleTx
c.allTxs Maybe SimpleTx -> Maybe SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
split
Integer -> Map Integer SimpleTx -> Maybe SimpleTx
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (SimpleTx -> TxIdType SimpleTx
forall tx. IsTx tx => tx -> TxIdType tx
txId SimpleTx
competing) CoordinatedHeadState SimpleTx
c.allTxs Maybe SimpleTx -> Maybe SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SimpleTx -> Maybe SimpleTx
forall a. a -> Maybe a
Just SimpleTx
competing
Outcome SimpleTx
signed Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` \case Continue [StateChanged SimpleTx]
_ [Effect SimpleTx]
effects -> (Effect SimpleTx -> Bool) -> [Effect SimpleTx] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\case NetworkEffect AckSn{} -> Bool
True; Effect SimpleTx
_ -> Bool
False) [Effect SimpleTx]
effects; Outcome SimpleTx
_ -> Bool
False
CoordinatedHeadState SimpleTx
after.localUTxO Set SimpleTxOut -> Set SimpleTxOut -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` SimpleTx -> UTxOType SimpleTx -> UTxOType SimpleTx
forall tx. IsTx tx => tx -> UTxOType tx -> UTxOType tx
applyTxTo SimpleTx
competing Set SimpleTxOut
UTxOType SimpleTx
active
CoordinatedHeadState SimpleTx
after.localTxs Seq SimpleTx -> Seq SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Seq SimpleTx
forall a. Monoid a => a
mempty
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"rejects a stale or in-flight proposal without recording or broadcasting it" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
SnapshotRequest SimpleTx
request <- 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 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
Scenario.initialSnapshot Seq SimpleTx
forall a. Monoid a => a
mempty Map Integer (Deposit SimpleTx)
PendingDeposits SimpleTx
forall a. Monoid a => a
mempty Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing DecommitStatus SimpleTx
forall tx. DecommitStatus tx
NoDecommit Maybe (SettlingCommit SimpleTx)
forall a. Maybe a
Nothing
let open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
Scenario.initialNode
current :: CoordinatedHeadState SimpleTx
current = OpenState SimpleTx
open.coordinatedHeadState
run :: OpenState SimpleTx -> Outcome SimpleTx
run OpenState SimpleTx
before = 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
$ Runtime SimpleTx -> SnapshotRequest SimpleTx -> Program SimpleTx ()
forall tx.
IsTx tx =>
Runtime tx -> SnapshotRequest tx -> Program tx ()
sendSnapshot (OpenState SimpleTx -> Runtime SimpleTx
forall tx. IsChainState tx => OpenState tx -> Runtime tx
Coordinated.start OpenState SimpleTx
before) SnapshotRequest SimpleTx
request
changedSnapshot :: Snapshot SimpleTx -> OpenState SimpleTx
changedSnapshot :: Snapshot SimpleTx -> OpenState SimpleTx
changedSnapshot Snapshot SimpleTx
snapshot = OpenState SimpleTx
open{coordinatedHeadState = current{confirmedSnapshot = ConfirmedSnapshot snapshot mempty}}
otherHead :: OpenState SimpleTx
otherHead :: OpenState SimpleTx
otherHead = OpenState SimpleTx
open{headId = UnsafeHeadId "another head", coordinatedHeadState = current{confirmedSnapshot = ConfirmedSnapshot Scenario.initialSnapshot{headId = UnsafeHeadId "another head"} mempty}}
stale :: [OpenState SimpleTx]
stale :: [OpenState SimpleTx]
stale =
[ OpenState SimpleTx
open{coordinatedHeadState = current{version = 1}}
, Snapshot SimpleTx -> OpenState SimpleTx
changedSnapshot Snapshot SimpleTx
Scenario.initialSnapshot{number = 1}
, OpenState SimpleTx
otherHead
, OpenState SimpleTx
open{coordinatedHeadState = current{seenSnapshot = RequestedSnapshot 0 1}}
, OpenState SimpleTx
open{coordinatedHeadState = current{seenSnapshot = mkSeenSnapshot Scenario.initialSnapshot{number = 1} mempty}}
]
OpenState SimpleTx -> Outcome SimpleTx
run OpenState SimpleTx
open Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [SnapshotNumber -> StateChanged SimpleTx
forall tx. SnapshotNumber -> StateChanged tx
SnapshotRequestDecided SnapshotNumber
1] [Message SimpleTx -> Effect SimpleTx
forall tx. Message tx -> Effect tx
NetworkEffect (Message SimpleTx -> Effect SimpleTx)
-> Message SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ SnapshotRequest SimpleTx -> Message SimpleTx
forall tx. IsTx tx => SnapshotRequest tx -> Message tx
requestMessage SnapshotRequest SimpleTx
request]
[OpenState SimpleTx]
-> (OpenState SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [OpenState SimpleTx]
stale ((OpenState SimpleTx -> Expectation) -> Expectation)
-> (OpenState SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \OpenState SimpleTx
before -> do
OpenState SimpleTx -> Outcome SimpleTx
run OpenState SimpleTx
before Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [] []
([StateChanged SimpleTx], Runtime SimpleTx)
-> [StateChanged SimpleTx]
forall a b. (a, b) -> a
fst (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
before) (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
request) [StateChanged SimpleTx] -> [StateChanged SimpleTx] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` []
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"checks decommit capacity against both local and confirmed UTxOs on both entry paths" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
let large :: Set SimpleTxOut
large = Int -> Set SimpleTxOut
padding Int
3 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> [Integer] -> Set SimpleTxOut
refs [Integer
1, Integer
2, Integer
3]
small :: Set SimpleTxOut
small = Int -> Set SimpleTxOut
padding Int
3 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> [Integer] -> Set SimpleTxOut
refs [Integer
1, Integer
6]
split :: SimpleTx
split = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef Integer
1) ([Integer] -> Set SimpleTxOut
refs [Integer
4, Integer
5])
noOutputs :: SimpleTx
noOutputs = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef Integer
1) Set SimpleTxOut
UTxOType SimpleTx
forall a. Monoid a => a
mempty
[(Set SimpleTxOut, Set SimpleTxOut)]
-> ((Set SimpleTxOut, Set SimpleTxOut) -> Expectation)
-> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Set SimpleTxOut
large, Set SimpleTxOut
small), (Set SimpleTxOut
small, Set SimpleTxOut
large), (Set SimpleTxOut
large, Set SimpleTxOut
large)] (((Set SimpleTxOut, Set SimpleTxOut) -> Expectation)
-> Expectation)
-> ((Set SimpleTxOut, Set SimpleTxOut) -> Expectation)
-> Expectation
forall a b. (a -> b) -> a -> b
$ \(Set SimpleTxOut
local, Set SimpleTxOut
confirmed) ->
[Input SimpleTx] -> (Input SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (SimpleTx -> [Input SimpleTx]
inputs SimpleTx
split) ((Input SimpleTx -> Expectation) -> Expectation)
-> (Input SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Input SimpleTx
input -> Set SimpleTxOut
-> Set SimpleTxOut -> Input SimpleTx -> Outcome SimpleTx
runAt Set SimpleTxOut
local Set SimpleTxOut
confirmed Input SimpleTx
input Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Outcome SimpleTx -> Bool
rejectedDecommit
[Input SimpleTx] -> (Input SimpleTx -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (SimpleTx -> [Input SimpleTx]
inputs SimpleTx
noOutputs) ((Input SimpleTx -> Expectation) -> Expectation)
-> (Input SimpleTx -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \Input SimpleTx
input -> Set SimpleTxOut
-> Set SimpleTxOut -> Input SimpleTx -> Outcome SimpleTx
runAt Set SimpleTxOut
small Set SimpleTxOut
small Input SimpleTx
input Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` Outcome SimpleTx -> Bool
rejectedDecommit
Set SimpleTxOut
-> Set SimpleTxOut -> Input SimpleTx -> Outcome SimpleTx
runAt Set SimpleTxOut
small Set SimpleTxOut
small (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
$ SimpleTx -> ClientInput SimpleTx
forall tx. tx -> ClientInput tx
Client.Decommit SimpleTx
split) Outcome SimpleTx -> Outcome SimpleTx -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` [StateChanged SimpleTx] -> [Effect SimpleTx] -> Outcome SimpleTx
forall tx. [StateChanged tx] -> [Effect tx] -> Outcome tx
Continue [] [Message SimpleTx -> Effect SimpleTx
forall tx. Message tx -> Effect tx
NetworkEffect (Message SimpleTx -> Effect SimpleTx)
-> Message SimpleTx -> Effect SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> Message SimpleTx
forall tx. tx -> Message tx
ReqDec SimpleTx
split]
Set SimpleTxOut
-> Set SimpleTxOut -> Input SimpleTx -> Outcome SimpleTx
runAt Set SimpleTxOut
small Set SimpleTxOut
small (TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
NetworkInput TTL
0 (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
ReqDec SimpleTx
split)
Outcome SimpleTx -> (Outcome SimpleTx -> Bool) -> Expectation
forall a. (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
`shouldSatisfy` \case Continue [DecommitRecorded{}] [Effect SimpleTx]
_ -> Bool
True; Outcome SimpleTx
_ -> Bool
False
[(Set SimpleTxOut, Set SimpleTxOut, SimpleTx)]
-> ((Set SimpleTxOut, Set SimpleTxOut, SimpleTx) -> Expectation)
-> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Set SimpleTxOut
local, Set SimpleTxOut
confirmed, SimpleTx
tx) | Set SimpleTxOut
local <- [Set SimpleTxOut
large, Set SimpleTxOut
small], Set SimpleTxOut
confirmed <- [Set SimpleTxOut
large, Set SimpleTxOut
small], SimpleTx
tx <- [SimpleTx
noOutputs, SimpleTx
split]] (((Set SimpleTxOut, Set SimpleTxOut, SimpleTx) -> Expectation)
-> Expectation)
-> ((Set SimpleTxOut, Set SimpleTxOut, SimpleTx) -> Expectation)
-> Expectation
forall a b. (a -> b) -> a -> b
$ \(Set SimpleTxOut
local, Set SimpleTxOut
confirmed, SimpleTx
tx) ->
Bool -> Bool
not (Outcome SimpleTx -> Bool
rejectedDecommit (Outcome SimpleTx -> Bool) -> Outcome SimpleTx -> Bool
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut
-> Set SimpleTxOut -> Input SimpleTx -> Outcome SimpleTx
runAt Set SimpleTxOut
local Set SimpleTxOut
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
ClientInput (ClientInput SimpleTx -> Input SimpleTx)
-> ClientInput SimpleTx -> Input SimpleTx
forall a b. (a -> b) -> a -> b
$ SimpleTx -> ClientInput SimpleTx
forall tx. tx -> ClientInput tx
Client.Decommit SimpleTx
tx)
Bool -> Bool -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Integer -> Integer -> Integer -> Bool
Agda.decommitCapacityRef (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut -> Int
forall a. Set a -> Int
Set.size SimpleTx
tx.txOutputs) (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut -> Int
forall a. Set a -> Int
Set.size (Set SimpleTxOut -> Int) -> Set SimpleTxOut -> Int
forall a b. (a -> b) -> a -> b
$ SimpleTx -> UTxOType SimpleTx -> UTxOType SimpleTx
forall tx. IsTx tx => tx -> UTxOType tx -> UTxOType tx
applyTxTo SimpleTx
tx Set SimpleTxOut
UTxOType SimpleTx
local) (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut -> Int
forall a. Set a -> Int
Set.size (Set SimpleTxOut -> Int) -> Set SimpleTxOut -> Int
forall a b. (a -> b) -> a -> b
$ SimpleTx -> UTxOType SimpleTx -> UTxOType SimpleTx
forall tx. IsTx tx => tx -> UTxOType tx -> UTxOType tx
applyTxTo SimpleTx
tx Set SimpleTxOut
UTxOType SimpleTx
confirmed)
String -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"matches the extracted pruning predicate for each rejection cause" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$
[(Bool, Bool, Bool)]
-> ((Bool, Bool, Bool) -> Expectation) -> Expectation
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Bool
valid, Bool
disjoint, Bool
fits) | Bool
valid <- [Bool
False, Bool
True], Bool
disjoint <- [Bool
False, Bool
True], Bool
fits <- [Bool
False, Bool
True]] (((Bool, Bool, Bool) -> Expectation) -> Expectation)
-> ((Bool, Bool, Bool) -> Expectation) -> Expectation
forall a b. (a -> b) -> a -> b
$ \(Bool
valid, Bool
disjoint, Bool
fits) -> do
let active :: Set SimpleTxOut
active = Int -> Set SimpleTxOut
padding Int
3 Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> [Integer] -> Set SimpleTxOut
refs [Integer
1, Integer
2]
tx :: SimpleTx
tx = Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef (Integer -> UTxOType SimpleTx) -> Integer -> UTxOType SimpleTx
forall a b. (a -> b) -> a -> b
$ if Bool
valid then Integer
1 else Integer
9) ([Integer] -> Set SimpleTxOut
refs ([Integer] -> Set SimpleTxOut) -> [Integer] -> Set SimpleTxOut
forall a b. (a -> b) -> a -> b
$ if Bool
fits then [Integer
4] else [Integer
4, Integer
5, Integer
6])
commit :: Set SimpleTxOut
commit = if Bool
disjoint then Set SimpleTxOut
forall a. Monoid a => a
mempty else Integer -> UTxOType SimpleTx
utxoRef Integer
4
after :: UTxOType SimpleTx
after = SimpleTx -> UTxOType SimpleTx -> UTxOType SimpleTx
forall tx. IsTx tx => tx -> UTxOType tx -> UTxOType tx
applyTxTo SimpleTx
tx Set SimpleTxOut
UTxOType SimpleTx
active
expected :: Bool
expected = Bool -> Bool -> Integer -> Integer -> Bool
Agda.pendingTxFitsRef Bool
valid Bool
disjoint (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut -> Int
forall a. Set a -> Int
Set.size Set SimpleTxOut
UTxOType SimpleTx
after) (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut -> Int
forall a. Set a -> Int
Set.size (Set SimpleTxOut -> Int) -> Set SimpleTxOut -> Int
forall a b. (a -> b) -> a -> b
$ Set SimpleTxOut
UTxOType SimpleTx
after Set SimpleTxOut -> Set SimpleTxOut -> Set SimpleTxOut
forall a. Semigroup a => a -> a -> a
<> Set SimpleTxOut
commit)
program :: Program SimpleTx ()
program = do
Seq SimpleTx
kept <- SnapshotLimits
-> ChainSlot
-> Maybe (UTxOType SimpleTx)
-> Maybe (UTxOType SimpleTx)
-> UTxOType SimpleTx
-> Seq SimpleTx
-> Program SimpleTx (Seq SimpleTx)
forall tx.
IsTx tx =>
SnapshotLimits
-> ChainSlot
-> Maybe (UTxOType tx)
-> Maybe (UTxOType tx)
-> UTxOType tx
-> Seq tx
-> Program tx (Seq tx)
pruneTransactions SnapshotLimits
cardanoSnapshotLimits ChainSlot
0 (Set SimpleTxOut -> Maybe (Set SimpleTxOut)
forall a. a -> Maybe a
Just Set SimpleTxOut
commit) Maybe (Set SimpleTxOut)
Maybe (UTxOType SimpleTx)
forall a. Maybe a
Nothing Set SimpleTxOut
UTxOType SimpleTx
active (SimpleTx -> Seq SimpleTx
forall a. a -> Seq a
Seq.singleton SimpleTx
tx)
StateChanged SimpleTx -> Program SimpleTx ()
forall tx. StateChanged tx -> Program tx ()
change (StateChanged SimpleTx -> Program SimpleTx ())
-> StateChanged SimpleTx -> Program SimpleTx ()
forall a b. (a -> b) -> a -> b
$ Snapshot SimpleTx
-> Seq SimpleTx
-> Maybe (TxIdType SimpleTx)
-> StateChanged SimpleTx
forall tx.
Snapshot tx -> Seq tx -> Maybe (TxIdType tx) -> StateChanged tx
SnapshotRequested Snapshot SimpleTx
Scenario.initialSnapshot Seq SimpleTx
kept Maybe Integer
Maybe (TxIdType SimpleTx)
forall a. Maybe a
Nothing
case InterpreterConfig SimpleTx
-> Program SimpleTx () -> Outcome SimpleTx
forall tx. InterpreterConfig tx -> Program tx () -> Outcome tx
Interpreter.runProgram (Environment -> Ledger SimpleTx -> InterpreterConfig SimpleTx
forall tx.
IsTx tx =>
Environment -> Ledger tx -> InterpreterConfig tx
Configuration.interpreterConfig (Int -> Environment
Scenario.environment Int
0) Ledger SimpleTx
simpleLedger) Program SimpleTx ()
program of
Continue [SnapshotRequested Snapshot SimpleTx
_ Seq SimpleTx
kept Maybe (TxIdType SimpleTx)
_] [] -> Bool -> Bool
not (Seq SimpleTx -> Bool
forall a. Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq SimpleTx
kept) Bool -> Bool -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` Bool
expected
Outcome SimpleTx
_ -> String -> Expectation
forall a. String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"pruning does not complete"
where
padding :: Int -> Set SimpleTxOut
padding Int
limit = [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) -> (Int -> Integer) -> Int -> SimpleTxOut
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> SimpleTxOut) -> [Int] -> [SimpleTxOut]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int
100 .. Int
100 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
maxAccumulatorSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
limit Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
refs :: [Integer] -> Set SimpleTxOut
refs = [SimpleTxOut] -> Set SimpleTxOut
forall a. Ord a => [a] -> Set a
Set.fromList ([SimpleTxOut] -> Set SimpleTxOut)
-> ([Integer] -> [SimpleTxOut]) -> [Integer] -> Set SimpleTxOut
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> SimpleTxOut) -> [Integer] -> [SimpleTxOut]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> SimpleTxOut
forall a. Num a => Integer -> a
fromInteger
identifiers :: Set SimpleTxOut -> [Integer]
identifiers = (SimpleTxOut -> Integer) -> [SimpleTxOut] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map SimpleTxOut -> Integer
unSimpleTxOut ([SimpleTxOut] -> [Integer])
-> (Set SimpleTxOut -> [SimpleTxOut])
-> Set SimpleTxOut
-> [Integer]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set SimpleTxOut -> [SimpleTxOut]
forall a. Set a -> [a]
Set.toList
palette :: [SimpleTx]
palette = [Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
1 (Integer -> UTxOType SimpleTx
utxoRef Integer
1) ([Integer] -> UTxOType SimpleTx
utxoRefs [Integer
4, Integer
5]), Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
2 ([Integer] -> UTxOType SimpleTx
utxoRefs [Integer
2, Integer
3]) (Integer -> UTxOType SimpleTx
utxoRef Integer
6), Integer -> UTxOType SimpleTx -> UTxOType SimpleTx -> SimpleTx
SimpleTx Integer
3 ([Integer] -> UTxOType SimpleTx
utxoRefs [Integer
4, Integer
5]) (Integer -> UTxOType SimpleTx
utxoRef Integer
7)]
inputs :: SimpleTx -> [Input SimpleTx]
inputs :: SimpleTx -> [Input SimpleTx]
inputs SimpleTx
tx = [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
$ SimpleTx -> ClientInput SimpleTx
forall tx. tx -> ClientInput tx
Client.Decommit SimpleTx
tx, TTL -> NetworkEvent (Message SimpleTx) -> Input SimpleTx
forall tx. TTL -> NetworkEvent (Message tx) -> Input tx
NetworkInput TTL
0 (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
ReqDec SimpleTx
tx]
runAt :: Set SimpleTxOut
-> Set SimpleTxOut -> Input SimpleTx -> Outcome SimpleTx
runAt Set SimpleTxOut
local Set SimpleTxOut
confirmed Input SimpleTx
input =
let open :: OpenState SimpleTx
open = NodeState SimpleTx -> OpenState SimpleTx
Scenario.openState NodeState SimpleTx
Scenario.initialNode
current :: CoordinatedHeadState SimpleTx
current = OpenState SimpleTx
open.coordinatedHeadState{localUTxO = local, confirmedSnapshot = ConfirmedSnapshot Scenario.initialSnapshot{utxo = confirmed} mempty}
in 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 (Int -> Environment
Scenario.environment Int
0) Ledger SimpleTx
simpleLedger UTCTime
initialChainTime NodeState SimpleTx
Scenario.initialNode{headState = Open open{coordinatedHeadState = current}} Input SimpleTx
input
rejectedDecommit :: Outcome SimpleTx -> Bool
rejectedDecommit :: Outcome SimpleTx -> Bool
rejectedDecommit = \case
Continue [DecommitInvalid{}] [] -> Bool
True
Outcome SimpleTx
_ -> Bool
False