Skip to content

Commit

Permalink
Implement test MIGRATE_01.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed May 12, 2021
1 parent 06f0c7e commit 3e22086
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ spec = describe "BYRON_MIGRATIONS" $ do
_ ->
error "Unable to compare plans."

describe "BYRON_MIGRATE_01 - \
\After a migration operation successfully completes, the correct \
\amounts eventually become available in the target wallet for an \
\arbitrary number of specified addresses, and the balance of the \
\source wallet is completely depleted."
$ do
testAddressCycling "Random" fixtureRandomWallet 1
testAddressCycling "Random" fixtureRandomWallet 3
testAddressCycling "Random" fixtureRandomWallet 10
testAddressCycling "Icarus" fixtureIcarusWallet 1
testAddressCycling "Icarus" fixtureIcarusWallet 3
testAddressCycling "Icarus" fixtureIcarusWallet 10

describe "BYRON_MIGRATE_05 - I could migrate to any valid address" $ do
forM_ [ ("Byron", emptyRandomWallet)
, ("Icarus", emptyIcarusWallet)
Expand Down Expand Up @@ -275,16 +288,7 @@ spec = describe "BYRON_MIGRATIONS" $ do
expectResponseCode HTTP.status400 r
expectErrorMessage errMsg400ParseError r

it "BYRON_MIGRATE_01 - \
\after a migration operation successfully completes, the correct \
\amount eventually becomes available in the target wallet for arbitrary \
\ number of specified addresses."
$ \ctx -> runResourceT $ do
testAddressCycling ctx 1
testAddressCycling ctx 3
testAddressCycling ctx 10

Hspec.it "BYRON_MIGRATE_01 - \
Hspec.it "BYRON_MIGRATE_XX_big_wallet - \
\ migrate a big wallet requiring more than one tx" $ \ctx -> runResourceT @IO $ do
liftIO $ pendingWith "Migration endpoints temporarily disabled."
-- NOTE
Expand Down Expand Up @@ -375,7 +379,7 @@ spec = describe "BYRON_MIGRATIONS" $ do
((`shouldBe` (Just 100)) . Map.lookup 10_000_000_000)
]

it "BYRON_MIGRATE_01 - \
it "BYRON_MIGRATE_XX - \
\a migration operation removes all funds from the source wallet."
$ \ctx -> forM_ [fixtureRandomWallet, fixtureIcarusWallet]
$ \fixtureByronWallet -> runResourceT $ do
Expand Down Expand Up @@ -591,58 +595,64 @@ spec = describe "BYRON_MIGRATIONS" $ do
| otherwise =
pure ()

testAddressCycling ctx addrNum =
forM_ [fixtureRandomWallet, fixtureIcarusWallet]
$ \fixtureByronWallet -> runResourceT $ do
liftIO $ pendingWith "Migration endpoints temporarily disabled."
testAddressCycling sourceWalletType mkSourceWallet targetAddressCount = do
let title = mconcat
[ "Migration from "
, sourceWalletType
, " wallet to target address count: "
, show targetAddressCount
, "."
]
it title $ \ctx -> runResourceT $ do

-- Restore a Byron wallet with funds, to act as a source wallet:
sourceWallet <- fixtureByronWallet ctx
let originalBalance =
view (#balance . #available . #getQuantity) sourceWallet
sourceWallet <- mkSourceWallet ctx
let sourceBalance =
view (#balance. #available . #getQuantity) sourceWallet

-- Create an empty target wallet:
targetWallet <- emptyWallet ctx
addrs <- listAddresses @n ctx targetWallet
let addrIds =
map (\(ApiTypes.ApiAddress theid _ _) -> theid) $
take addrNum addrs
targetAddresses <- listAddresses @n ctx targetWallet
let targetAddressIds = take targetAddressCount targetAddresses <&>
(\(ApiTypes.ApiAddress addrId _ _) -> addrId)

-- Calculate the expected migration fee:
r0 <- request @(ApiWalletMigrationPlan n) ctx
(Link.createMigrationPlan @'Byron sourceWallet) Default Empty
verify r0
[ expectResponseCode HTTP.status200
-- Create a migration plan:
response0 <- request @(ApiWalletMigrationPlan n) ctx
(Link.createMigrationPlan @'Byron sourceWallet) Default
(Json [json|{addresses: #{targetAddressIds}}|])
verify response0
[ expectResponseCode HTTP.status202
, expectField #totalFee (.> Quantity 0)
]
let expectedFee =
getFromResponse (#totalFee . #getQuantity) r0
let balanceLeftover =
getFromResponse (#balanceLeftover . #ada . #getQuantity) r0
let expectedFee = getFromResponse
(#totalFee . #getQuantity) response0
let balanceLeftover = getFromResponse
(#balanceLeftover . #ada . #getQuantity) response0

-- Perform a migration from the source wallet to the target wallet:
r1 <- request @[ApiTransaction n] ctx
response1 <- request @[ApiTransaction n] ctx
(Link.migrateWallet @'Byron sourceWallet)
Default
(Json [json|
{ passphrase: #{fixturePassphrase}
, addresses: #{addrIds}
, addresses: #{targetAddressIds}
}|])
verify r1
verify response1
[ expectResponseCode HTTP.status202
, expectField id (`shouldSatisfy` (not . null))
]

-- Check that funds become available in the target wallet:
let expectedBalance =
originalBalance - expectedFee - balanceLeftover
eventually "Wallet has expectedBalance" $ do
r2 <- request @ApiWallet ctx
-- Check that funds have become available in the target wallet:
let expectedTargetBalance =
sourceBalance - expectedFee - balanceLeftover
eventually "Target wallet has expected balance." $ do
response2 <- request @ApiWallet ctx
(Link.getWallet @'Shelley targetWallet) Default Empty
verify r2
verify response2
[ expectField
(#balance . #available)
(`shouldBe` Quantity expectedBalance)
(#balance . #available)
(`shouldBe` Quantity expectedTargetBalance)
, expectField
(#balance . #total)
(`shouldBe` Quantity expectedBalance)
(#balance . #total)
(`shouldBe` Quantity expectedTargetBalance)
]
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,16 @@ spec = describe "SHELLEY_MIGRATIONS" $ do
error "Unable to compare plans."

describe "SHELLEY_MIGRATE_01 - \
\after a migration operation successfully completes, the correct \
\amount eventually becomes available in the target wallet for arbitrary \
\ number of specified addresses. Balance of source wallet = 0."
\After a migration operation successfully completes, the correct \
\amounts eventually become available in the target wallet for an \
\arbitrary number of specified addresses, and the balance of the \
\source wallet is completely depleted."
$ do
testAddressCycling 1
testAddressCycling 3
testAddressCycling 10
testAddressCycling 1
testAddressCycling 3
testAddressCycling 10

it "SHELLEY_MIGRATE_01_big_wallet - \
it "SHELLEY_MIGRATE_XX_big_wallet - \
\ migrate a big wallet requiring more than one tx" $ \ctx -> runResourceT @IO $ do
liftIO $ pendingWith "Migration endpoints temporarily disabled."

Expand Down Expand Up @@ -573,67 +574,71 @@ spec = describe "SHELLEY_MIGRATIONS" $ do
| otherwise =
pure ()

testAddressCycling addrNum =
it ("Migration from Shelley wallet to " ++ show addrNum ++ " addresses")
$ \ctx -> runResourceT $ do
liftIO $ pendingWith "Migration endpoints temporarily disabled."
testAddressCycling targetAddressCount = do
let title = mconcat
[ "Migration from Shelley wallet to target address count: "
, show targetAddressCount
, "."
]
it title $ \ctx -> runResourceT $ do

-- Restore a Shelley wallet with funds, to act as a source wallet:
sourceWallet <- fixtureWallet ctx
let originalBalance =
view (#balance. #available . #getQuantity)
sourceWallet
let sourceBalance =
view (#balance. #available . #getQuantity) sourceWallet

-- Create an empty target wallet:
targetWallet <- emptyWallet ctx
addrs <- listAddresses @n ctx targetWallet
let addrIds =
map (\(ApiTypes.ApiAddress theid _ _) -> theid) $
take addrNum addrs
targetAddresses <- listAddresses @n ctx targetWallet
let targetAddressIds = take targetAddressCount targetAddresses <&>
(\(ApiTypes.ApiAddress addrId _ _) -> addrId)

-- Calculate the expected migration fee:
r0 <- request @(ApiWalletMigrationPlan n) ctx
(Link.createMigrationPlan @'Shelley sourceWallet) Default Empty
verify r0
[ expectResponseCode HTTP.status200
-- Create a migration plan:
response0 <- request @(ApiWalletMigrationPlan n) ctx
(Link.createMigrationPlan @'Shelley sourceWallet) Default
(Json [json|{addresses: #{targetAddressIds}}|])
verify response0
[ expectResponseCode HTTP.status202
, expectField #totalFee (.> Quantity 0)
]
let expectedFee = getFromResponse (#totalFee . #getQuantity) r0
let expectedFee =
getFromResponse (#totalFee . #getQuantity) response0

-- Perform a migration from the source wallet to the target wallet:
r1 <- request @[ApiTransaction n] ctx
response1 <- request @[ApiTransaction n] ctx
(Link.migrateWallet @'Shelley sourceWallet)
Default
(Json [json|
{ passphrase: #{fixturePassphrase}
, addresses: #{addrIds}
, addresses: #{targetAddressIds}
}|])
verify r1
verify response1
[ expectResponseCode HTTP.status202
, expectField id (`shouldSatisfy` (not . null))
]

-- Check that funds become available in the target wallet:
let expectedBalance = originalBalance - expectedFee
eventually "Wallet has expectedBalance" $ do
r2 <- request @ApiWallet ctx
-- Check that funds have become available in the target wallet:
let expectedTargetBalance = sourceBalance - expectedFee
eventually "Target wallet has expected balance." $ do
response2 <- request @ApiWallet ctx
(Link.getWallet @'Shelley targetWallet) Default Empty
verify r2
verify response2
[ expectField
(#balance . #available)
(`shouldBe` Quantity expectedBalance)
(#balance . #available)
(`shouldBe` Quantity expectedTargetBalance)
, expectField
(#balance . #total)
(`shouldBe` Quantity expectedBalance)
(#balance . #total)
(`shouldBe` Quantity expectedTargetBalance)
]

-- Verify sourceWallet has balance 0
r3 <- request @ApiWallet ctx
-- Check that the source wallet has a balance of zero:
response3 <- request @ApiWallet ctx
(Link.getWallet @'Shelley sourceWallet) Default Empty
verify r3
verify response3
[ expectField
(#balance . #available)
(`shouldBe` Quantity 0)
(#balance . #available)
(`shouldBe` Quantity 0)
, expectField
(#balance . #total)
(`shouldBe` Quantity 0)
(#balance . #total)
(`shouldBe` Quantity 0)
]

0 comments on commit 3e22086

Please sign in to comment.