Skip to content

Commit

Permalink
Observe failure to commit unknown UTXO
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Nov 22, 2021
1 parent 10d58bd commit 7e7c907
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion hydra-node/src/Hydra/Chain/Direct.hs
Expand Up @@ -377,7 +377,7 @@ fromPostChainTx TinyWallet{getUtxo, verificationKey} headState cardanoKeys = \ca
[] -> do
pure . Just $ commitTx party Nothing initial
_ ->
throwIO MoreThanOneUtxoCommitted
throwIO (MoreThanOneUtxoCommitted @CardanoTx)
st -> error $ "cannot post CommitTx, invalid state: " <> show st
CollectComTx utxo ->
readTVar headState >>= \case
Expand Down
26 changes: 13 additions & 13 deletions hydra-node/src/Hydra/Ledger/Simple.hs
Expand Up @@ -23,7 +23,7 @@ import qualified Data.Set as Set
import Hydra.Ledger

instance Tx SimpleTx where
type Utxo SimpleTx = Set TxIn
type Utxo SimpleTx = Set SimpleTxIn
type TxId SimpleTx = SimpleId
type AssetId SimpleTx = SimpleId

Expand Down Expand Up @@ -71,19 +71,19 @@ instance FromCBOR SimpleTx where
type SimpleId = Integer

-- |An identifier for a single output of a 'SimpleTx'.
newtype TxIn = TxIn {unTxIn :: Integer}
newtype SimpleTxIn = SimpleTxIn {unSimpleTxIn :: Integer}
deriving stock (Generic)
deriving newtype (Eq, Ord, Show, Num, ToJSON, FromJSON)

instance Arbitrary TxIn where
instance Arbitrary SimpleTxIn where
shrink = genericShrink
arbitrary = genericArbitrary

instance ToCBOR TxIn where
toCBOR (TxIn inId) = toCBOR inId
instance ToCBOR SimpleTxIn where
toCBOR (SimpleTxIn inId) = toCBOR inId

instance FromCBOR TxIn where
fromCBOR = TxIn <$> fromCBOR
instance FromCBOR SimpleTxIn where
fromCBOR = SimpleTxIn <$> fromCBOR

simpleLedger :: Ledger SimpleTx
simpleLedger =
Expand All @@ -99,23 +99,23 @@ simpleLedger =
-- * Builders

utxoRef :: Integer -> Utxo SimpleTx
utxoRef = Set.singleton . TxIn
utxoRef = Set.singleton . SimpleTxIn

utxoRefs :: [Integer] -> Utxo SimpleTx
utxoRefs = Set.fromList . fmap TxIn
utxoRefs = Set.fromList . fmap SimpleTxIn

aValidTx :: Integer -> SimpleTx
aValidTx n = SimpleTx n mempty (utxoRef n)
-- * Generators

listOfCommittedUtxos :: Integer -> Gen [Utxo SimpleTx]
listOfCommittedUtxos numCommits =
pure $ Set.singleton . TxIn <$> [1 .. numCommits]
pure $ Set.singleton . SimpleTxIn <$> [1 .. numCommits]

genSequenceOfValidTransactions :: Utxo SimpleTx -> Gen [SimpleTx]
genSequenceOfValidTransactions initialUtxo = do
n <- fromIntegral <$> getSize
let maxId = if Set.null initialUtxo then 0 else unTxIn (maximum initialUtxo)
let maxId = if Set.null initialUtxo then 0 else unSimpleTxIn (maximum initialUtxo)
numTxs <- choose (1, n)
foldlM newTx (maxId, initialUtxo, mempty) [1 .. numTxs] >>= \(_, _, txs) -> pure (reverse txs)
where
Expand All @@ -124,9 +124,9 @@ genSequenceOfValidTransactions initialUtxo = do
(newMax, ins, outs) <- genInputsAndOutputs maxId utxo
pure (newMax, (utxo Set.\\ ins) `Set.union` outs, SimpleTx txid ins outs : txs)

genInputsAndOutputs :: Integer -> Set TxIn -> Gen (Integer, Set TxIn, Set TxIn)
genInputsAndOutputs :: Integer -> Set SimpleTxIn -> Gen (Integer, Set SimpleTxIn, Set SimpleTxIn)
genInputsAndOutputs maxId utxo = do
ins <- sublistOf (Set.toList utxo)
numOuts <- choose (1, 10)
let outs = fmap (+ maxId) [1 .. numOuts]
pure (maximum outs, Set.fromList ins, Set.fromList $ fmap TxIn outs)
pure (maximum outs, Set.fromList ins, Set.fromList $ fmap SimpleTxIn outs)
8 changes: 5 additions & 3 deletions local-cluster/test/Test/DirectChainSpec.hs
Expand Up @@ -30,7 +30,7 @@ import Hydra.Chain.Direct (
withIOManager,
)
import Hydra.Ledger (Tx)
import Hydra.Ledger.Cardano (genOneUtxoFor)
import Hydra.Ledger.Cardano (CardanoTx, genOneUtxoFor)
import Hydra.Logging (nullTracer, showLogsOnFailure)
import Hydra.Party (Party, deriveParty, generateKey)
import Hydra.Snapshot (Snapshot (..))
Expand Down Expand Up @@ -93,10 +93,12 @@ spec = around showLogsOnFailure $ do
someUtxoB <- generate $ genOneUtxoFor (VKey aliceCardanoVk)

postTx (CommitTx alice (someUtxoA <> someUtxoB))
`shouldThrow` (== MoreThanOneUtxoCommitted)
`shouldThrow` (== MoreThanOneUtxoCommitted @CardanoTx)

postTx (CommitTx alice someUtxoA)
`shouldThrow` (== CannotSpendUtxo someUtxoA)
`shouldThrow` \case
(CannotSpendInput{} :: InvalidTxError CardanoTx) -> True
_ -> False

-- TODO: we need to do some magic to observe the correct Utxo being
-- committed. This is not trivial because we need to generate a tx
Expand Down

0 comments on commit 7e7c907

Please sign in to comment.