Skip to content

Commit

Permalink
Merge #1900
Browse files Browse the repository at this point in the history
1900: Testing byron txs and migrations in Shelley r=piotr-iohk a=piotr-iohk

# Issue Number

#1844, #1675

# Overview

- 66e5151
  Enable more integration tests for byron transactions, migrations, addresses and HW wallets in shelley
  
- d773d95
  Add some special Byron and Icarus wallets to genesis for testing migration scenarios
  
- 834923e
  Use regular HSpec.it for tests that use explicit/special mnemonics, otherwise in case they fail, on repeat there will be a 409 as such wallet would exist
  
- 18d1c34
  Don't run Byron tx test on Jormungandr

  



# Comments

<!-- Additional comments or screenshots to attach if any -->

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Assign the PR to a corresponding milestone
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: Piotr Stachyra <[email protected]>
  • Loading branch information
iohk-bors[bot] and Piotr Stachyra authored Jul 21, 2020
2 parents 5161dde + f25977b commit 5d10245
Show file tree
Hide file tree
Showing 10 changed files with 752 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .buildkite/rebuild.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ buildStep dryRun bk nightly = do
titled "Build"
(build Fast (["--test", "--no-run-tests"] ++ cabalFlags)) .&&.
titled "Test"
(timeout 45 (test Fast Serial cabalFlags .&&. test Fast Parallel cabalFlags)) .&&.
(timeout 60 (test Fast Serial cabalFlags .&&. test Fast Parallel cabalFlags)) .&&.
titled "Checking golden test files"
(checkUnclean dryRun "lib/core/test/data")
where
Expand Down
1 change: 1 addition & 0 deletions lib/core-integration/cardano-wallet-core-integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ library
Test.Integration.Scenario.API.Byron.Addresses
Test.Integration.Scenario.API.Byron.Transactions
Test.Integration.Scenario.API.Byron.Migrations
Test.Integration.Scenario.API.Byron.TransactionsShelley
Test.Integration.Scenario.API.Byron.Network
Test.Integration.Scenario.API.Shelley.Addresses
Test.Integration.Scenario.API.Shelley.HWWallets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ spec :: forall n t.
, EncodeAddress n
, PaymentAddress n IcarusKey
) => SpecWith (Context t)
spec = do
spec = describe "BYRON_HWWALLETS" $ do
it "HW_WALLETS_01 - Restoration from account public key preserves funds" $ \ctx -> do
wSrc <- fixtureIcarusWallet ctx
-- create wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Api.Types as ApiTypes
import qualified Data.Map.Strict as Map
import qualified Network.HTTP.Types.Status as HTTP

import qualified Test.Hspec as Hspec

spec :: forall n t.
( DecodeAddress n
Expand All @@ -99,7 +99,7 @@ spec :: forall n t.
, PaymentAddress n IcarusKey
, PaymentAddress n ByronKey
) => SpecWith (Context t)
spec = do
spec = describe "BYRON_MIGRATIONS" $ do
it "BYRON_CALCULATE_01 - \
\for non-empty wallet calculated fee is > zero."
$ \ctx -> forM_ [fixtureRandomWallet, fixtureIcarusWallet]
Expand All @@ -124,7 +124,7 @@ spec = do
, expectErrorMessage (errMsg403NothingToMigrate $ w ^. walletId)
]

it "BYRON_CALCULATE_02 - \
Hspec.it "BYRON_CALCULATE_02 - \
\Cannot calculate fee for wallet with dust, that cannot be migrated."
$ \ctx -> do
-- NOTE
Expand Down Expand Up @@ -207,7 +207,7 @@ spec = do
testAddressCycling ctx 3
testAddressCycling ctx 10

it "BYRON_MIGRATE_01 - \
Hspec.it "BYRON_MIGRATE_01xxx - \
\ migrate a big wallet requiring more than one tx" $ \ctx -> do
-- NOTE
-- Special mnemonic for which 500 legacy funds are attached to in the
Expand All @@ -227,8 +227,10 @@ spec = do
"passphrase": #{fixturePassphrase},
"style": "random"
} |]
(_, wOld) <- unsafeRequest @ApiByronWallet ctx
(Link.postWallet @'Byron) payloadRestore
r <- request @ApiByronWallet ctx (Link.postWallet @'Byron) Default payloadRestore
verify r [ expectResponseCode @IO HTTP.status201 ]
let wOld = getFromResponse id r

eventually "wallet balance greater than 0" $ do
request @ApiByronWallet ctx
(Link.getWallet @'Byron wOld)
Expand Down Expand Up @@ -263,9 +265,7 @@ spec = do
(Link.migrateWallet @'Byron wOld)
Default
payloadMigrate >>= flip verify
[ expectResponseCode @IO HTTP.status202
, expectField id ((`shouldBe` 2). length)
]
[ expectResponseCode @IO HTTP.status202 ]

-- Check that funds become available in the target wallet:
let expectedBalance = originalBalance - expectedFee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import Test.Integration.Framework.TestData
import qualified Cardano.Wallet.Api.Link as Link
import qualified Data.Text as T
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Hspec as Hspec

data TestCase a = TestCase
{ query :: T.Text
Expand All @@ -72,9 +73,9 @@ spec :: forall n t.
( DecodeAddress n
, DecodeStakeAddress n
) => SpecWith (Context t)
spec = do
spec = describe "BYRON_TX_COMMON" $ do

it "BYRON_RESTORE_08 - Icarus wallet with high indexes" $ \ctx -> do
Hspec.it "BYRON_RESTORE_08 - Icarus wallet with high indexes" $ \ctx -> do
-- NOTE
-- Special Icarus mnemonic where address indexes are all after the index
-- 500. Because we don't have the whole history, restoring sequential
Expand All @@ -98,27 +99,27 @@ spec = do
, expectField (#balance . #available) (`shouldBe` Quantity faucetAmt)
]

it "BYRON_RESTORE_09 - Ledger wallet" $ \ctx -> do
-- NOTE
-- Special legacy wallets where addresses have been generated from a
-- seed derived using the auxiliary method used by Ledger.
let mnemonics =
[ "vague" , "wrist" , "poet" , "crazy" , "danger" , "dinner"
, "grace" , "home" , "naive" , "unfold" , "april" , "exile"
, "relief" , "rifle" , "ranch" , "tone" , "betray" , "wrong"
] :: [T.Text]
let payload = Json [json| {
"name": "Ledger Wallet",
"mnemonic_sentence": #{mnemonics},
"passphrase": #{fixturePassphrase},
"style": "ledger"
} |]

r <- request @ApiByronWallet ctx (Link.postWallet @'Byron) Default payload
verify r
[ expectResponseCode @IO HTTP.status201
, expectField (#balance . #available) (`shouldBe` Quantity faucetAmt)
]
-- it "BYRON_RESTORE_09 - Ledger wallet" $ \ctx -> do
-- -- NOTE
-- -- Special legacy wallets where addresses have been generated from a
-- -- seed derived using the auxiliary method used by Ledger.
-- let mnemonics =
-- [ "vague" , "wrist" , "poet" , "crazy" , "danger" , "dinner"
-- , "grace" , "home" , "naive" , "unfold" , "april" , "exile"
-- , "relief" , "rifle" , "ranch" , "tone" , "betray" , "wrong"
-- ] :: [T.Text]
-- let payload = Json [json| {
-- "name": "Ledger Wallet",
-- "mnemonic_sentence": #{mnemonics},
-- "passphrase": #{fixturePassphrase},
-- "style": "ledger"
-- } |]
--
-- r <- request @ApiByronWallet ctx (Link.postWallet @'Byron) Default payload
-- verify r
-- [ expectResponseCode @IO HTTP.status201
-- , expectField (#balance . #available) (`shouldBe` Quantity faucetAmt)
-- ]

it "BYRON_TX_LIST_01 - 0 txs on empty Byron wallet"
$ \ctx -> forM_ [emptyRandomWallet, emptyIcarusWallet] $ \emptyByronWallet -> do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Test.Integration.Scenario.API.Byron.TransactionsShelley
( spec
) where

import Prelude

import Cardano.Wallet.Api.Types
( ApiByronWallet
, ApiFee
, ApiTransaction
, ApiWallet
, DecodeAddress
, DecodeStakeAddress
, EncodeAddress
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.Types
( Direction (..), TxStatus (..) )
import Control.Monad
( forM_ )
import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Network.HTTP.Types.Method
( Method )
import Numeric.Natural
( Natural )
import Test.Hspec
( SpecWith, describe )
import Test.Hspec.Expectations.Lifted
( shouldBe )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
( Context
, Headers (..)
, Payload (..)
, between
, eventually
, expectField
, expectResponseCode
, expectSuccess
, faucetAmt
, faucetUtxoAmt
, fixtureIcarusWallet
, fixturePassphrase
, fixtureRandomWallet
, fixtureWallet
, getFromResponse
, json
, listAddresses
, request
, verify
, (.>=)
)
import Test.Integration.Framework.Request
( RequestException )

import qualified Cardano.Wallet.Api.Link as Link
import qualified Network.HTTP.Types.Status as HTTP

spec :: forall n t.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
) => SpecWith (Context t)
spec = do
describe "BYRON_TRANS_SHELLEY_01 - Single Output Transaction with non-Shelley witnesses" $
forM_ [(fixtureRandomWallet, "Byron wallet"), (fixtureIcarusWallet, "Icarus wallet")] $
\(srcFixture,name) -> it name $ \ctx -> do

(wByron, wShelley) <- (,) <$> srcFixture ctx <*> fixtureWallet ctx
addrs <- listAddresses @n ctx wShelley

let amt = 1
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}]
}|]

rFeeEst <- request @ApiFee ctx
(Link.getTransactionFee @'Byron wByron) Default payload
verify rFeeEst
[ expectSuccess
, expectResponseCode HTTP.status202
]
let (Quantity feeEstMin) = getFromResponse #estimatedMin rFeeEst
let (Quantity feeEstMax) = getFromResponse #estimatedMax rFeeEst

r <- postTx ctx
(wByron, Link.createTransaction @'Byron, fixturePassphrase)
wShelley
amt
verify r
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#amount . #getQuantity) $
between (feeEstMin + amt, feeEstMax + amt)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` Pending)
]

ra <- request @ApiByronWallet ctx (Link.getWallet @'Byron wByron) Default Empty
verify ra
[ expectSuccess
, expectField (#balance . #total) $
between
( Quantity (faucetAmt - feeEstMax - amt)
, Quantity (faucetAmt - feeEstMin - amt)
)
, expectField
(#balance . #available)
(.>= Quantity (faucetAmt - faucetUtxoAmt))
]

eventually "wByron and wShelley balances are as expected" $ do
rb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wShelley) Default Empty
expectField
(#balance . #getApiT . #available)
(`shouldBe` Quantity (faucetAmt + amt)) rb

ra2 <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wByron) Default Empty
expectField
(#balance . #available)
(`shouldBe` Quantity (faucetAmt - feeEstMax - amt)) ra2
where

postTx
:: Context t
-> (wal, wal -> (Method, Text), Text)
-> ApiWallet
-> Natural
-> IO (HTTP.Status, Either RequestException (ApiTransaction n))
postTx ctx (wSrc, postTxEndp, pass) wDest amt = do
addrs <- listAddresses @n ctx wDest
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}],
"passphrase": #{pass}
}|]
r <- request @(ApiTransaction n) ctx (postTxEndp wSrc) Default payload
expectResponseCode HTTP.status202 r
return r
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Api.Types as ApiTypes
import qualified Data.Map.Strict as Map
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Hspec as Hspec

spec :: forall n t.
( DecodeAddress n
Expand Down Expand Up @@ -120,7 +121,7 @@ spec = do
, expectErrorMessage (errMsg403NothingToMigrate $ w ^. walletId)
]

it "SHELLEY_CALCULATE_02 - \
Hspec.it "SHELLEY_CALCULATE_02 - \
\Cannot calculate fee for wallet with dust, that cannot be migrated."
$ \ctx -> do
-- NOTE
Expand Down Expand Up @@ -167,7 +168,7 @@ spec = do
testAddressCycling 3
testAddressCycling 10

it "SHELLEY_MIGRATE_01_big_wallet - \
Hspec.it "SHELLEY_MIGRATE_01_big_wallet - \
\ migrate a big wallet requiring more than one tx" $ \ctx -> do
-- NOTE
-- Special mnemonic for which 200 shelley funds are attached to in the
Expand Down Expand Up @@ -269,7 +270,7 @@ spec = do
, expectErrorMessage (errMsg403NothingToMigrate srcId)
]

it "SHELLEY_MIGRATE_02 - \
Hspec.it "SHELLEY_MIGRATE_02 - \
\migrating wallet with dust should fail."
$ \ctx -> do
-- NOTE
Expand Down
Loading

0 comments on commit 5d10245

Please sign in to comment.