Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADP-3479] Amend wallet page to host wallet status #4865

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,30 @@ api :: Proxy API
api = Proxy

server
:: Tracer IO String
:: Tracer IO ()
Copy link
Member

@Anviking Anviking Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was curious: did you consider using TVar IO () or I guess TVar IO WalletState? (edit: meant to be used as STM () or STM WalletState, yes, probably that's the better spelling of them)

And taking the latter one step further, we get the question whether we could have DBVar STM WalletState instead of in IO 🤔 cc @HeinrichApfelmus

Copy link
Collaborator Author

@paolino paolino Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the way it is used ATM, the STM composability is not a requirement. We are serving this signal as a long poll over the network. It's also dangerous for the signal producer which could get stuck (in theory) by other concurrent producers. STM () could be better ? But I would not dig into that until we have a real need.
About dangers IIRC there is a fan out in the code that ensure the tracer never locks at the expense of dropping events.

-- ^ Tracer for wallet tip changes
-> Tracer IO String
-> FilePath
-> WalletBootEnv IO
-> WalletResource
-> Server API
server tr dbDir wb r =
server wtc tr dbDir wb r =
listCustomerH r
:<|> queryAddressH r
:<|> createWalletViaMnemonic tr dbDir wb r
:<|> createWalletViaXPub tr dbDir wb r
:<|> createWalletViaMnemonic wtc tr dbDir wb r
:<|> createWalletViaXPub wtc tr dbDir wb r

createWalletViaMnemonic
:: Tracer IO String
:: Tracer IO ()
-- ^ Tracer for wallet tip changes
-> Tracer IO String
-> FilePath
-> WalletBootEnv IO
-> WalletResource
-> PostWalletViaMnemonic
-> Handler NoContent
createWalletViaMnemonic
wtc
tracer
dir
boot
Expand All @@ -99,6 +104,7 @@ createWalletViaMnemonic
initWallet :: WalletResourceM ()
initWallet =
REST.initWallet
wtc
tracer
boot
dir
Expand All @@ -107,13 +113,16 @@ createWalletViaMnemonic
onlyOnWalletIntance resource initWallet $> NoContent

createWalletViaXPub
:: Tracer IO String
:: Tracer IO ()
-- ^ Tracer for wallet tip changes
-> Tracer IO String
-> FilePath
-> WalletBootEnv IO
-> WalletResource
-> PostWalletViaXPub
-> Handler NoContent
createWalletViaXPub
wtc
tracer
dir
boot
Expand All @@ -130,6 +139,7 @@ createWalletViaXPub
Right credentials ->
Right
<$> REST.initWallet
wtc
tracer
boot
dir
Expand Down
16 changes: 10 additions & 6 deletions lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,20 @@ createTheDepositWalletOnDisk _tr dir credentials users action = do

-- | Load an existing wallet from disk.
loadWallet
:: WalletIO.WalletBootEnv IO
:: Tracer IO ()
-- ^ Tracer for wallet tip changes
-> WalletIO.WalletBootEnv IO
-- ^ Environment for the wallet
-> FilePath
-- ^ Path to the wallet database directory
-> WalletResourceM ()
loadWallet bootEnv dir = do
loadWallet wtc bootEnv dir = do
let action
:: (WalletIO.WalletInstance -> IO b) -> IO (Either ErrDatabase b)
action f = findTheDepositWalletOnDisk bootEnv dir $ \case
Right wallet ->
Right
<$> WalletIO.withWalletLoad
<$> WalletIO.withWalletLoad wtc
(WalletIO.WalletEnv bootEnv wallet)
f
Left e -> pure $ Left $ ErrLoadingDatabase e
Expand All @@ -360,7 +362,9 @@ loadWallet bootEnv dir = do

-- | Initialize a new wallet from an 'XPub'.
initWallet
:: Tracer IO String
:: Tracer IO ()
-- ^ Tracer for wallet tip changes
-> Tracer IO String
-- ^ Tracer for logging
-> WalletIO.WalletBootEnv IO
-- ^ Environment for the wallet
Expand All @@ -371,13 +375,13 @@ initWallet
-> Word31
-- ^ Max number of users ?
-> WalletResourceM ()
initWallet tr bootEnv dir credentials users = do
initWallet wtc tr bootEnv dir credentials users = do
let action
:: (WalletIO.WalletInstance -> IO b) -> IO (Either ErrDatabase b)
action f = createTheDepositWalletOnDisk tr dir credentials users $ \case
Just wallet -> do
fmap Right
$ WalletIO.withWalletInit
$ WalletIO.withWalletInit wtc
(WalletIO.WalletEnv bootEnv wallet)
credentials
users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ lg :: (MonadIO m, Show a) => Tracer IO String -> String -> a -> m ()
lg tr p x = liftIO $ traceWith tr $ p <> ": " <> show x

loadDepositWalletFromDisk
:: Tracer IO String
:: Tracer IO ()
-- ^ Tracer for wallet tip changes
-> Tracer IO String
-> FilePath
-> WalletBootEnv IO
-> WalletResource
-> IO ()
loadDepositWalletFromDisk tr dir env resource = do
loadDepositWalletFromDisk wtc tr dir env resource = do
result <- flip runWalletResourceM resource $ do
test <- liftIO $ walletExists dir
when test $ do
lg tr "Loading wallet from" dir
loadWallet env dir
loadWallet wtc env dir
lg tr "Wallet loaded from" dir
pure test
case result of
Expand Down
24 changes: 16 additions & 8 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import Cardano.Wallet.Network.Checkpoints.Policy
import Control.Tracer
( Tracer
, contramap
, traceWith
)
import Data.Bifunctor
( first
Expand Down Expand Up @@ -175,12 +176,14 @@ readWalletState WalletInstance{walletState} =

-- | Initialize a new wallet in the given environment.
withWalletInit
:: WalletEnv IO
:: Tracer IO () -- wallet tip changes
-> WalletEnv IO
-> Credentials
-> Word31
-> (WalletInstance -> IO a)
-> IO a
withWalletInit
wtc
env@WalletEnv
{ bootEnv = WalletBootEnv{genesisData}
, ..
Expand All @@ -194,23 +197,26 @@ withWalletInit
credentials
knownCustomerCount
genesisData
withWalletDBVar env walletState action
withWalletDBVar wtc env walletState action

-- | Load an existing wallet from the given environment.
withWalletLoad
:: WalletEnv IO
:: Tracer IO () -- wallet tip changes
-> WalletEnv IO
-> (WalletInstance -> IO a)
-> IO a
withWalletLoad env@WalletEnv{..} action = do
withWalletLoad wtc env@WalletEnv{..} action = do
walletState <- DBVar.loadDBVar store
withWalletDBVar env walletState action
withWalletDBVar wtc env walletState action

withWalletDBVar
:: WalletEnv IO
:: Tracer IO () -- wallet tip changes
-> WalletEnv IO
-> DBVar.DBVar IO Wallet.DeltaWalletState
-> (WalletInstance -> IO a)
-> IO a
withWalletDBVar
wtc
env@WalletEnv{bootEnv = WalletBootEnv{logger, networkEnv}}
walletState
action = do
Expand All @@ -228,7 +234,7 @@ withWalletDBVar
[ walletTip
, Read.GenesisPoint
]
, rollForward = rollForward w
, rollForward = rollForward w wtc
, rollBackward = rollBackward w
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of showing sync progress this is probably what we want (or practically what we want), but techincally the rollBackward would also update the wallet tip.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I cut that corner for the demo. I feel like we can keep it like this for now.

For the sake of showing sync progress this is probably what we want (or practically what we want), but techincally the rollBackward would also update the wallet tip.

}

Expand Down Expand Up @@ -294,17 +300,19 @@ getAllDeposits w i =

rollForward
:: WalletInstance
-> Tracer IO () -- wallet tip changes
-> NonEmpty (Read.EraValue Read.Block)
-> tip
-> IO ()
rollForward w blocks _nodeTip = do
rollForward w wtc blocks _nodeTip = do
timeFromSlot <- slotResolver w
onWalletState w
$ Delta.update
$ Delta.Replace
. Wallet.rollForwardMany
timeFromSlot
blocks
traceWith wtc ()
x <- readWalletState w
x `seq` pure ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import Cardano.Wallet.Deposit.Read
, TxId
, lessOrEqual
)
import Control.Tracer
( nullTracer
)
import Test.Scenario.Blockchain
( ScenarioEnv
, ada
Expand Down Expand Up @@ -81,7 +84,7 @@ scenarioRestore
:: XPub -> WalletEnv IO -> IO ()
scenarioRestore xpub env = do
let knownCustomerCount = 127
Wallet.withWalletInit env (XPubCredentials xpub) knownCustomerCount $ \w -> do
Wallet.withWalletInit nullTracer env (XPubCredentials xpub) knownCustomerCount $ \w -> do
value <- Wallet.availableBalance w
assert $ value == ada 0
```
Expand All @@ -92,7 +95,7 @@ In order to load the wallet state from a database file and resume operation from
scenarioStart
:: WalletEnv IO -> IO ()
scenarioStart env =
Wallet.withWalletLoad env $ \w -> do
Wallet.withWalletLoad nullTracer env $ \w -> do
value <- Wallet.availableBalance w
assert $ value == ada 0
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import Cardano.Crypto.Wallet
import Cardano.Wallet.Deposit.Pure.State.Creation
( Credentials (..)
)
import Control.Tracer
( nullTracer
)
import Test.Hspec
( SpecWith
, describe
Expand Down Expand Up @@ -62,20 +65,23 @@ scenarios = do
it "1. Assign an address to a customer ID" $ \env -> do
withWalletEnvMock env $ \walletEnv ->
Wallet.withWalletInit
nullTracer
walletEnv
(XPubCredentials $ freshXPub 1)
32
Exchanges.scenarioCreateAddressList

it "4. Create payments to a different wallet" $ \env -> do
withWalletEnvMock env $ \walletEnv ->
Wallet.withWalletInit walletEnv (XPubCredentials xpub) 32
Wallet.withWalletInit nullTracer
walletEnv (XPubCredentials xpub) 32
$ Exchanges.scenarioCreatePayment xprv env mockAddress

describe "Temporary tests" $ do
it "Wallet receives funds that are sent to customer address" $ \env -> do
withWalletEnvMock env $ \walletEnv ->
Wallet.withWalletInit
nullTracer
walletEnv
(XPubCredentials $ freshXPub 0)
8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ withInitializedWallet
-> WalletResourceM a
-> IO (Either ErrWalletResource a)
withInitializedWallet dir f = withWallet $ do
initWallet nullTracer fakeBootEnv dir credentials 0
initWallet nullTracer nullTracer fakeBootEnv dir credentials 0
letItInitialize
f

Expand All @@ -129,7 +129,7 @@ withLoadedWallet
-> WalletResourceM a
-> IO (Either ErrWalletResource a)
withLoadedWallet dir f = withWallet $ do
loadWallet fakeBootEnv dir
loadWallet nullTracer fakeBootEnv dir
letItInitialize
f

Expand Down
8 changes: 7 additions & 1 deletion lib/exe/lib/Cardano/Wallet/Application.hs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ import Cardano.Wallet.UI.Common.Layer
, UILayer
, oobMessages
, sourceOfNewTip
, walletTipChanges
)
import Control.Exception.Extra
( handle
Expand All @@ -208,6 +209,7 @@ import Control.Monad.Trans.Except
)
import Control.Tracer
( Tracer (..)
, nullTracer
, traceWith
)
import Data.Function
Expand Down Expand Up @@ -425,15 +427,16 @@ serveWallet
"deposit-wallet"
Just databaseDir' -> pure databaseDir'
resource <- ContT withResource
ui <- Ui.withUILayer 1 resource
liftIO
$ loadDepositWalletFromDisk
(walletTipChanges >$< oobMessages ui)
( DepositApplicationLog
>$< applicationTracer
)
databaseDir'
bootEnv
resource
ui <- Ui.withUILayer 1 resource
REST.onResourceChange
( \_ -> do
traceWith (oobMessages ui)
Expand Down Expand Up @@ -471,6 +474,7 @@ serveWallet
resource <- ContT withResource
liftIO
$ loadDepositWalletFromDisk
nullTracer
( DepositApplicationLog
>$< applicationTracer
)
Expand Down Expand Up @@ -633,6 +637,7 @@ serveWallet
application =
Server.serve api
$ Deposit.server
nullTracer
(DepositApplicationLog >$< applicationTracer)
databaseDir'
bootEnv
Expand Down Expand Up @@ -668,6 +673,7 @@ serveWallet
application =
Server.serve api
$ DepositUi.serveUI
(walletTipChanges >$< oobMessages ui)
(DepositUIApplicationLog >$< applicationTracer)
ui
bootEnv
Expand Down
9 changes: 5 additions & 4 deletions lib/ui/cardano-wallet-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ library
Cardano.Wallet.UI.Deposit.Server.Payments.Page
Cardano.Wallet.UI.Deposit.Server.Wallet
Cardano.Wallet.UI.Deposit.Types.Payments
Cardano.Wallet.UI.Deposit.Types.Wallet
Cardano.Wallet.UI.Lib.Discretization
Cardano.Wallet.UI.Lib.ListOf
Cardano.Wallet.UI.Lib.Pagination.Map
Expand Down Expand Up @@ -180,16 +181,16 @@ test-suite unit
, cardano-crypto
, cardano-wallet-read
, cardano-wallet-ui
, contra-tracer
, containers
, contra-tracer
, customer-deposit-wallet
, customer-deposit-wallet:rest
, hspec
, mtl
, QuickCheck
, text
, temporary
, text
, time
, customer-deposit-wallet:rest
, customer-deposit-wallet:customer-deposit-wallet

build-tool-depends: hspec-discover:hspec-discover
type: exitcode-stdio-1.0
Expand Down
Loading