Skip to content

Commit

Permalink
[ADP-3492] Retrieve genesis data from disk for the deposit wallet (#4854
Browse files Browse the repository at this point in the history
)

This PR substitute a mock implementation of GenesisData with one loaded
from the disk. Because we do not have persistence yet this is not
creating conflicts at the moment.
This allow us to run the deposit wallet in `preprod` by supplying the
preprod byron genesis file
We also opportunistically fix the UI code to encode correctly addresses
so that testnet addresses are now shown correctly.

- Load `GenesisData` from disk by specifying a filepath in the CLI
arguments
- Use the address encoding lib in the UI to actually show testnet
addresses
- Fix `GenesisData` not propagated correctly in the REST interface

ADP-3492
  • Loading branch information
paolino authored Nov 25, 2024
2 parents a9f941c + b5252c7 commit f1f8390
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 58 deletions.
1 change: 1 addition & 0 deletions lib/benchmarks/exe/latency-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
Nothing -- settings
Nothing -- token metadata server
block0
Nothing
(setupAction networkParameters)

massiveWalletUTxOSize :: Int
Expand Down
1 change: 1 addition & 0 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ library rest
, bytestring
, cardano-addresses
, cardano-crypto
, cardano-ledger-byron
, contra-tracer
, crypto-primitives
, customer-deposit-wallet
Expand Down
23 changes: 14 additions & 9 deletions lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ import Cardano.Wallet.Address.BIP32
( BIP32Path
)
import Cardano.Wallet.Deposit.IO
( WalletPublicIdentity
( WalletBootEnv
, WalletPublicIdentity
, genesisData
)
import Cardano.Wallet.Deposit.IO.Resource
( ErrResourceExists (..)
Expand Down Expand Up @@ -250,12 +252,13 @@ deleteTheDepositWalletOnDisk dir = do

-- | Try to open an existing wallet
findTheDepositWalletOnDisk
:: FilePath
:: WalletBootEnv IO
-> FilePath
-- ^ Path to the wallet database directory
-> (Either ErrLoadingDatabase WalletIO.WalletStore -> IO a)
-- ^ Action to run if the wallet is found
-> IO a
findTheDepositWalletOnDisk dir action = do
findTheDepositWalletOnDisk env dir action = do
ds <- scanDirectoryForDepositPrefix dir
case ds of
[d] -> do
Expand All @@ -265,7 +268,7 @@ findTheDepositWalletOnDisk dir action = do
fromCredentialsAndGenesis
credentials
(fromIntegral @Int users)
Read.mockGenesisDataMainnet
(genesisData env)
store <- newStore
writeS store state
action $ Right store
Expand Down Expand Up @@ -333,7 +336,7 @@ loadWallet
loadWallet bootEnv dir = do
let action
:: (WalletIO.WalletInstance -> IO b) -> IO (Either ErrDatabase b)
action f = findTheDepositWalletOnDisk dir $ \case
action f = findTheDepositWalletOnDisk bootEnv dir $ \case
Right wallet ->
Right
<$> WalletIO.withWalletLoad
Expand Down Expand Up @@ -392,10 +395,12 @@ deleteWallet dir = do
<$> Resource.closeResource resource
liftIO $ deleteTheDepositWalletOnDisk dir

walletExists :: FilePath -> WalletResourceM Bool
walletExists dir = liftIO $ findTheDepositWalletOnDisk dir $ \case
Right _ -> pure True
Left _ -> pure False
walletExists :: FilePath -> IO Bool
walletExists dir = do
r <- scanDirectoryForDepositPrefix dir
case r of
[] -> pure False
_ -> pure True

walletPublicIdentity :: WalletResourceM WalletPublicIdentity
walletPublicIdentity = onWalletInstance WalletIO.walletPublicIdentity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.Wallet.Deposit.REST.Start
( loadDepositWalletFromDisk
Expand Down Expand Up @@ -61,6 +62,7 @@ import Data.Functor.Contravariant
( (>$<)
)

import qualified Cardano.Chain.Genesis as Byron
import qualified Cardano.Wallet.Deposit.Read as Read

lg :: (MonadIO m, Show a) => Tracer IO String -> String -> a -> m ()
Expand All @@ -75,7 +77,7 @@ loadDepositWalletFromDisk
loadDepositWalletFromDisk tr dir env resource = do
result <- runExceptT $ do
exists <- ExceptT $ flip runWalletResourceM resource $ do
test <- walletExists dir
test <- liftIO $ walletExists dir
liftIO $ print test
when test $ do
lg tr "Loading wallet from" dir
Expand Down Expand Up @@ -112,10 +114,19 @@ mockFundTheWallet network resource = flip runWalletResourceM resource $ do
Right () <- liftIO $ postTx network tx
pure ()

newFakeBootEnv :: IO (WalletBootEnv IO)
newFakeBootEnv =
WalletBootEnv
(show >$< stdoutTracer)
Read.mockGenesisDataMainnet
. mapBlock Read.EraValue
<$> newNetworkEnvMock
newFakeBootEnv :: Maybe FilePath -> IO (WalletBootEnv IO)
newFakeBootEnv genesisFile = do
eGenesisData <- runExceptT $ case genesisFile of
Nothing -> ExceptT $ pure $ Right Read.mockGenesisDataMainnet
Just file -> fst <$> Byron.readGenesisData file
print genesisFile
print eGenesisData
print $ Read.getNetworkId <$> eGenesisData
case eGenesisData of
Left e -> error $ show e
Right genesisData' ->
WalletBootEnv
(show >$< stdoutTracer)
genesisData'
. mapBlock Read.EraValue
<$> newNetworkEnvMock
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ spec = do
| dir == fp -> pure ()
Left e -> fail $ show e
Right _ -> fail "Should have failed the query on load"
it "can check if a wallet is present"
it "can check if a wallet is present on disk"
$ inADirectory
$ \dir -> do
r <- withInitializedWallet dir doNothing
onSuccess r $ \_ -> do
presence <- withWallet $ walletExists dir
onSuccess presence $ \p -> p `shouldBe` True
presence <- walletExists dir
presence `shouldBe` True
5 changes: 5 additions & 0 deletions lib/exe/app/cardano-wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import Cardano.Wallet.Application.CLI
, cmdWallet
, cmdWalletCreate
, databaseOption
, depositByronGenesisFileOption
, ekgEnabled
, enableWindowsANSI
, helperTracing
Expand Down Expand Up @@ -241,6 +242,7 @@ data ServeArgs = ServeArgs
, _poolMetadataSourceOpt :: Maybe PoolMetadataSource
, _tokenMetadataSourceOpt :: Maybe TokenMetadataServer
, _logging :: LoggingOptions TracerSeverities
, _depositByronGenesisFile :: Maybe FilePath
}
deriving (Show)

Expand Down Expand Up @@ -268,6 +270,7 @@ cmdServe =
<*> optional poolMetadataSourceOption
<*> optional tokenMetadataSourceOption
<*> loggingOptions tracerSeveritiesOption
<*> optional depositByronGenesisFileOption

exec :: ServeArgs -> IO ()
exec
Expand All @@ -285,6 +288,7 @@ cmdServe =
poolMetadataFetching
tokenMetadataServerURI
logOpt
byronGenesisFileOpt
) = withTracers logOpt $ \tr tracers -> do
withShutdownHandlerMaybe tr enableShutdownHandler $ do
logDebug tr $ MsgServeArgs args
Expand Down Expand Up @@ -321,6 +325,7 @@ cmdServe =
(Settings <$> poolMetadataFetching)
tokenMetadataServerURI
block0
byronGenesisFileOpt
(beforeMainLoop tr)

withShutdownHandlerMaybe :: Trace IO MainLog -> Bool -> IO () -> IO ()
Expand Down
4 changes: 3 additions & 1 deletion lib/exe/lib/Cardano/Wallet/Application.hs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ serveWallet
-- ^ Settings to be set at application start, will be written into DB.
-> Maybe TokenMetadataServer
-> Block
-> Maybe FilePath
-- ^ The genesis block, or some starting point.
-- See also: 'Cardano.Wallet.Primitive.Ledger.Shelley#KnownNetwork'.
-> (URI -> IO ())
Expand All @@ -339,6 +340,7 @@ serveWallet
settings
tokenMetaUri
block0
depositByronGenesisFile
beforeMainLoop = withSNetworkId network $ \sNetwork -> evalContT $ do
let netId = networkIdVal sNetwork
lift $ case blockchainSource of
Expand Down Expand Up @@ -381,7 +383,7 @@ serveWallet
eDepositUiSocket <- bindDepositUiSocket
eDepositSocket <- bindDepositSocket
eShelleySocket <- bindApiSocket
fakeBootEnv <- lift newFakeBootEnv
fakeBootEnv <- lift $ newFakeBootEnv depositByronGenesisFile
callCC $ \exit -> do
case eShelleyUiSocket of
Left err -> do
Expand Down
8 changes: 8 additions & 0 deletions lib/exe/lib/Cardano/Wallet/Application/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module Cardano.Wallet.Application.CLI
, listenShelleyUiOption
, listenDepositUiOption
, listenDepositOption
, depositByronGenesisFileOption
) where

import Prelude hiding
Expand Down Expand Up @@ -1460,6 +1461,13 @@ listenDepositUiOption =
<|>
pure Nothing

-- | [--deposit-byron-genesis-file=FILEPATH]
depositByronGenesisFileOption :: Parser FilePath
depositByronGenesisFileOption = option str $ mempty
<> long "deposit-byron-genesis-file"
<> metavar "FILEPATH"
<> help "Byron genesis file to use for the deposit wallet."

-- | [--ui-random-port]
uiRandomPortOption :: Parser Bool
uiRandomPortOption = flag' False $ mempty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ onClusterStart
Nothing
(Just tokenMetaUrl)
block0
Nothing
(\uri -> k (networkParameters, uri))
`withException` (traceWith tr . MsgServerError)
case end of
Expand Down
3 changes: 0 additions & 3 deletions lib/ui/cardano-wallet-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ library
Cardano.Wallet.UI.Deposit.Server.Deposits.TxIds
Cardano.Wallet.UI.Deposit.Server.Lib
Cardano.Wallet.UI.Deposit.Server.Wallet
Cardano.Wallet.UI.Lib.Address
Cardano.Wallet.UI.Lib.Discretization
Cardano.Wallet.UI.Lib.ListOf
Cardano.Wallet.UI.Lib.Pagination.Map
Expand Down Expand Up @@ -116,8 +115,6 @@ library
, aeson-pretty
, base
, base16-bytestring
, bech32
, bech32-th
, bytestring
, cardano-addresses
, cardano-slotting
Expand Down
8 changes: 4 additions & 4 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Html/Pages/Addresses.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import Prelude
import Cardano.Wallet.Deposit.IO
( WalletPublicIdentity (..)
)
import Cardano.Wallet.Deposit.Pure.API.Address
( encodeAddress
)
import Cardano.Wallet.Deposit.Read
( Address
)
Expand Down Expand Up @@ -45,9 +48,6 @@ import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( WalletPresent (..)
, onWalletPresentH
)
import Cardano.Wallet.UI.Lib.Address
( encodeMainnetAddress
)
import Cardano.Wallet.UI.Lib.ListOf
( ListOf
)
Expand Down Expand Up @@ -95,7 +95,7 @@ customerAddressH copy addr =
truncatableText copy ("address-text-" <> encodedAddr)
$ toHtml encodedAddr
where
encodedAddr = encodeMainnetAddress addr
encodedAddr = encodeAddress addr

addressElementH
:: UTCTime -> UTCTime -> AlertH -> WalletPresent -> Html ()
Expand Down
30 changes: 0 additions & 30 deletions lib/ui/src/Cardano/Wallet/UI/Lib/Address.hs

This file was deleted.

5 changes: 5 additions & 0 deletions run/common/nix/run-wallet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,21 @@ DEPOSIT_WALLET_PORT=${DEPOSIT_WALLET_PORT:=$RANDOM_PORT}
LOCAL_WALLET_LOGS_FILE=./wallet.log
WALLET_LOGS_FILE="${WALLET_LOGS_FILE:=$LOCAL_WALLET_LOGS_FILE}"

DEPOSIT_BYRON_GENESIS="${NODE_CONFIGS}/byron-genesis.json"

echo "Wallet service port: $WALLET_PORT"
echo "Wallet UI port: $WALLET_UI_PORT"
echo "Deposit wallet UI port: $DEPOSIT_WALLET_UI_PORT"
echo "Deposit wallet port: $DEPOSIT_WALLET_PORT"
echo "Deposit Byron genesis file: $DEPOSIT_BYRON_GENESIS"

if [[ $NETWORK == "mainnet" ]]; then
echo "Running wallet in mainnet mode"
NETWORK_OPTION="--mainnet"
else
echo "Running wallet in testnet mode"
NETWORK_OPTION="--testnet ${NODE_CONFIGS}/byron-genesis.json"

fi
# shellcheck disable=SC2086
cabal run --project-dir ../../.. -O0 cardano-wallet-exe:exe:cardano-wallet -- \
Expand All @@ -75,6 +79,7 @@ cabal run --project-dir ../../.. -O0 cardano-wallet-exe:exe:cardano-wallet -- \
--ui-deposit-port "${DEPOSIT_WALLET_UI_PORT}" \
--database "${WALLET_DB}" \
--node-socket "${NODE_SOCKET_PATH}" \
--deposit-byron-genesis-file "${DEPOSIT_BYRON_GENESIS}" \
$NETWORK_OPTION \
--listen-address 0.0.0.0 \
+RTS -N -A16m -qg -qb -RTS

0 comments on commit f1f8390

Please sign in to comment.