Skip to content

Commit

Permalink
Merge #1731
Browse files Browse the repository at this point in the history
1731: Foundational work to get a cluster of pools for integration testing r=KtorZ a=KtorZ

# Issue Number

<!-- Put here a reference to the issue this PR relates to and which requirements it tackles -->

N/A

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->
- 5a263bc
  📍 **move content of 'Test.Utils.Ports' to 'Cardano.Wallet.Network.Ports'**
  
- 6ed9efc
  📍 **rework the shelley launcher code so that we can start a cluster of nodes**
  using 'withCluster ... ... 0' will result in the same behavior as before. Right now, pools would
exists as process but not on chain. In order to exist on chain, pools need to be registered, which
will be done in a separate commit.

- 1a8b720
  📍 **fix database path**
  
- 6745ea9
  📍 **Regenerate nix**
  
- 6b7b947
  📍 **assign sensible default ordering to 'listAllTransactions'**
  
# Comments

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

This should make it possible to run a cluster of pools as simply as:

```hs
        let numberOfPools = 3
        withCluster tr Info numberOfPools $ \socketPath block0 (gp,vData) -> ...
```

So far, the pools won't have any _stake_ and won't therefore be any useful.. I'll give them some stake in another PR. 

<!-- 
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: KtorZ <[email protected]>
Co-authored-by: IOHK <[email protected]>
  • Loading branch information
3 people authored Jun 8, 2020
2 parents 269d273 + 19559c4 commit 016cbe3
Show file tree
Hide file tree
Showing 15 changed files with 287 additions and 204 deletions.
2 changes: 1 addition & 1 deletion lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ listAllTransactions
-> w
-> IO [ApiTransaction n]
listAllTransactions ctx w =
listTransactions ctx w Nothing Nothing Nothing
listTransactions ctx w Nothing Nothing (Just Descending)

listTransactions
:: forall n t w. (DecodeAddress n, HasType (ApiT WalletId) w)
Expand Down
1 change: 1 addition & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ library
, persistent-template
, process
, random
, random-shuffle
, retry
, safe
, scientific
Expand Down
26 changes: 25 additions & 1 deletion lib/core/src/Cardano/Wallet/Network/Ports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ module Cardano.Wallet.Network.Ports
-- * Helpers
, waitForPort
, unsafePortNumber
, findPort
, randomUnusedTCPPorts
) where

import Prelude

import Control.Monad
( filterM )
import Control.Monad.IO.Class
( liftIO )
import Control.Retry
( RetryPolicyM, retrying )
import Data.List
( isInfixOf )
( isInfixOf, sort )
import Data.Streaming.Network
( bindRandomPortTCP )
import Data.Word
Expand All @@ -54,6 +58,8 @@ import Network.Socket
, socket
, tupleToHostAddress
)
import System.Random.Shuffle
( shuffleM )
import UnliftIO.Exception
( bracket, throwIO, try )

Expand Down Expand Up @@ -113,3 +119,21 @@ unsafePortNumber = \case
SockAddrInet p _ -> p
SockAddrInet6 p _ _ _ -> p
SockAddrUnix _ -> error "unsafePortNumber: no port for unix sockets."

-- | Get a list of random TCPv4 ports that currently do not have any servers
-- listening on them. It may return less than the requested number of ports.
--
-- Note that this method of allocating ports is subject to race
-- conditions. Production code should use better methods such as passing a
-- listening socket to the child process.
randomUnusedTCPPorts :: Int -> IO [Int]
randomUnusedTCPPorts count = do
usablePorts <- shuffleM [1024..49151]
sort <$> filterM unused (take count usablePorts)
where
unused = fmap not . isPortOpen . simpleSockAddr (127,0,0,1) . fromIntegral

-- | Returen a single TCP port that was unused at the time this function was
-- called.
findPort :: IO Int
findPort = head <$> randomUnusedTCPPorts 1
2 changes: 0 additions & 2 deletions lib/jormungandr/cardano-wallet-jormungandr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ test-suite jormungandr-integration
, monad-control
, persistent
, process
, random-shuffle
, retry
, safe
, servant
Expand Down Expand Up @@ -281,7 +280,6 @@ test-suite jormungandr-integration
Test.Integration.Jormungandr.Scenario.CLI.StakePools
Test.Integration.Jormungandr.Scenario.CLI.Transactions
Test.Integration.Jormungandr.Scenario.CLI.Port
Test.Utils.Ports

benchmark latency
default-language:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import Cardano.Wallet.Network
)
import Cardano.Wallet.Network.BlockHeaders
( emptyBlockHeaders )
import Cardano.Wallet.Network.Ports
( randomUnusedTCPPorts )
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..), Passphrase (..) )
import Cardano.Wallet.Primitive.Types
Expand Down Expand Up @@ -122,8 +124,6 @@ import Test.Hspec
)
import Test.QuickCheck
( Arbitrary (..), generate, vector )
import Test.Utils.Ports
( randomUnusedTCPPorts )

import qualified Cardano.Wallet.Jormungandr.Api.Client as Jormungandr
import qualified Data.ByteString as BS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Cardano.Launcher
( Command (..), StdStream (..), withBackendProcess )
import Cardano.Wallet.Api.Types
( ApiWallet )
import Cardano.Wallet.Network.Ports
( findPort )
import Cardano.Wallet.Primitive.Types
( SyncProgress (..) )
import Control.Exception
Expand Down Expand Up @@ -68,8 +70,6 @@ import Test.Integration.Framework.DSL
)
import Test.Utils.Paths
( getTestData )
import Test.Utils.Ports
( findPort )

import qualified Data.Text as T
import qualified Data.Text.IO as TIO
Expand Down
43 changes: 0 additions & 43 deletions lib/jormungandr/test/integration/Test/Utils/Ports.hs

This file was deleted.

1 change: 1 addition & 0 deletions lib/shelley/cardano-wallet-shelley.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ library
, cardano-wallet-cli
, cardano-wallet-core
, cardano-wallet-launcher
, cardano-wallet-test-utils
, cborg
, containers
, contra-tracer
Expand Down
Loading

0 comments on commit 016cbe3

Please sign in to comment.