From d2f81388619f54f686b9dc87e9ff51d20785f88f Mon Sep 17 00:00:00 2001 From: Johannes Lund Date: Wed, 26 Jun 2019 18:35:44 +0200 Subject: [PATCH] Rework postTx tests to expect exceptions from encoder and add a test case for unbalanced transactions --- .../Cardano/Wallet/Jormungandr/NetworkSpec.hs | 62 +++++++++++++++---- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs b/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs index 30122813c75..6fdb7971ec3 100644 --- a/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs +++ b/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs @@ -23,7 +23,6 @@ import Cardano.Wallet.Jormungandr.Primitive.Types import Cardano.Wallet.Network ( ErrGetBlock (..) , ErrNetworkTip (..) - , ErrPostTx (..) , NetworkLayer (..) , defaultRetryPolicy , waitForConnection @@ -70,7 +69,6 @@ import Test.Hspec , beforeAll , describe , it - , pendingWith , shouldBe , shouldReturn , shouldSatisfy @@ -169,6 +167,7 @@ spec = do $ beforeAll startNode' $ afterAll killNode $ do it "empty tx succeeds" $ \(_, nw) -> do + -- Would be rejected eventually. let signedEmpty = (Tx [] [], []) runExceptT (postTx nw signedEmpty) `shouldReturn` Right () @@ -176,13 +175,18 @@ spec = do let signed = (txNonEmpty, [pkWitness]) runExceptT (postTx nw signed) `shouldReturn` Right () - it "more inputs than witnesses" $ \(_, nw) -> do + it "unbalanced tx (surplus) succeeds" $ \(_, nw) -> do + -- Jormungandr will eventually reject txs that are not perfectly + -- balanced though. + let signed = (unbalancedTx, [pkWitness]) + runExceptT (postTx nw signed) `shouldReturn` Right () + + it "more inputs than witnesses - encoder throws" $ \(_, nw) -> do let signed = (txNonEmpty, []) - let err = Left $ ErrPostTxBadRequest "" - runExceptT (postTx nw signed) `shouldReturn` err + runExceptT (postTx nw signed) `shouldThrow` someException it "more witnesses than inputs - fine apparently" $ \(_, nw) -> do - -- Becase of how signed txs are encoded: + -- Because of how signed txs are encoded: -- n :: Word8 -- m :: Word8 -- in_0 .. in_n :: [TxIn] @@ -192,9 +196,10 @@ spec = do -- this should in practice be like appending bytes to the end of -- the message. let signed = (txNonEmpty, [pkWitness, pkWitness, pkWitness]) - runExceptT (postTx nw signed) `shouldReturn` Right () + runExceptT (postTx nw signed) `shouldThrow` someException it "no input, one output" $ \(_, nw) -> do + -- Would be rejected eventually. let tx = (Tx [] [ (TxOut $ unsafeDecodeAddress proxy "ca1qwunuat6snw60g99ul6qvte98fja\ @@ -203,13 +208,20 @@ spec = do ], []) runExceptT (postTx nw tx) `shouldReturn` Right () - it "fails when addresses and hashes have wrong length" $ \(_, nw) -> do - pendingWith "We need to handle errors in Jormungandr.Binary" - let tx = (Tx [] [ TxOut (Address "") (Coin 1227362560) ], []) - let err = Left $ ErrPostTxBadRequest "" - runExceptT (postTx nw tx) `shouldReturn` err + it "throws when addresses and hashes have wrong length" $ \(_, nw) -> do + let out = TxOut (Address "") (Coin 1227362560) + let tx = (Tx [] [out] , []) + runExceptT (postTx nw tx) `shouldThrow` someException + it "encoder throws an exception if tx is invalid (eg too many inputs)" $ + \(_, nw) -> do + let inp = (inputs txNonEmpty) !! 0 + let out = (outputs txNonEmpty) !! 0 + let tx = (Tx (replicate 300 inp) (replicate 3 out), []) + runExceptT (postTx nw tx) `shouldThrow` someException where + someException = (const True :: SomeException -> Bool) + url :: BaseUrl url = BaseUrl Http "localhost" 8081 "/api" @@ -269,3 +281,29 @@ spec = do } ] } + + unbalancedTx :: Tx + unbalancedTx = Tx + { inputs = + [ (TxIn + { inputId = Hash $ unsafeFromHex + "666984dec4bc0ff1888be97bfe0694a9\ + \6b35c58d025405ead51d5cc72a3019f4" + , inputIx = 0 + }, Coin 100) + ] + , outputs = + [ TxOut + { address = unsafeDecodeAddress proxy + "ca1q0u7k6ltp3e52pch47rhdkld2gdv\ + \gu26rwyqh02csu3ah3384f2nvhlk7a6" + , coin = Coin 5 + } + , TxOut + { address = unsafeDecodeAddress proxy + "ca1qwunuat6snw60g99ul6qvte98fja\ + \le2k0uu5mrymylqz2ntgzs6vs386wxd" + , coin = Coin 5 + } + ] + }