Skip to content

Commit

Permalink
extend API with 'forceResync' operation
Browse files Browse the repository at this point in the history
- Added a 'forceResync' link
- Extended the servant Client
- Provide an initial straightforward implementation re-using 'rollbackBlocks'
  • Loading branch information
KtorZ committed Jan 15, 2020
1 parent 92c2aa0 commit 206affc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module Cardano.Wallet
, updateWalletPassphrase
, walletSyncProgress
, fetchRewardBalance
, rollbackBlocks
, ErrWalletAlreadyExists (..)
, ErrNoSuchWallet (..)
, ErrListUTxOStatistics (..)
Expand Down
19 changes: 19 additions & 0 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Cardano.Wallet.Api
, PutWallet
, PutWalletPassphrase
, GetUTxOsStatistics
, ForceResyncWallet

, Addresses
, ListAddresses
Expand All @@ -49,6 +50,7 @@ module Cardano.Wallet.Api
, GetByronWallet
, ListByronWallets
, PostByronWallet
, ForceResyncByronWallet

, ByronTransactions
, ListByronTransactions
Expand Down Expand Up @@ -89,6 +91,7 @@ import Cardano.Wallet.Api.Types
, ApiCoinSelection
, ApiFee
, ApiNetworkInformation
, ApiNetworkTip
, ApiSelectCoinsData
, ApiStakePool
, ApiT
Expand Down Expand Up @@ -192,6 +195,7 @@ type Wallets =
:<|> PutWallet
:<|> PutWalletPassphrase
:<|> GetUTxOsStatistics
:<|> ForceResyncWallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/deleteWallet
type DeleteWallet = "wallets"
Expand Down Expand Up @@ -232,6 +236,13 @@ type GetUTxOsStatistics = "wallets"
:> "utxos"
:> Get '[JSON] ApiUtxoStatistics

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/forceResync
type ForceResyncWallet = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "tip"
:> ReqBody '[JSON] ApiNetworkTip
:> PutNoContent '[Any] NoContent

{-------------------------------------------------------------------------------
Addresses
Expand Down Expand Up @@ -362,6 +373,7 @@ type ByronWallets =
:<|> DeleteByronWallet
:<|> GetByronWallet
:<|> ListByronWallets
:<|> ForceResyncByronWallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postByronWallet
type PostByronWallet (style :: ByronWalletStyle) = "byron-wallets"
Expand All @@ -383,6 +395,13 @@ type GetByronWallet = "byron-wallets"
type ListByronWallets = "byron-wallets"
:> Get '[JSON] [ApiByronWallet]

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/forceResyncByron
type ForceResyncByronWallet = "byron-wallets"
:> Capture "walletId" (ApiT WalletId)
:> "tip"
:> ReqBody '[JSON] ApiNetworkTip
:> PutNoContent '[Any] NoContent

{-------------------------------------------------------------------------------
Byron Transactions
Expand Down
7 changes: 7 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Cardano.Wallet.Api.Types
( ApiAddress
, ApiFee
, ApiNetworkInformation (..)
, ApiNetworkTip
, ApiStakePool
, ApiT (..)
, ApiTransaction
Expand Down Expand Up @@ -103,6 +104,10 @@ data WalletClient t = WalletClient
:: ApiT WalletId
-> WalletPutPassphraseData
-> ClientM NoContent
, forceResyncWallet
:: ApiT WalletId
-> ApiNetworkTip
-> ClientM NoContent
, listTransactions
:: ApiT WalletId
-> Maybe Iso8601Time
Expand Down Expand Up @@ -167,6 +172,7 @@ walletClient =
:<|> _putWallet
:<|> _putWalletPassphrase
:<|> _getWalletUtxoStatistics
:<|> _forceResyncWallet
= wallets

_listAddresses =
Expand Down Expand Up @@ -200,6 +206,7 @@ walletClient =
, postWallet = _postWallet
, putWallet = _putWallet
, putWalletPassphrase = _putWalletPassphrase
, forceResyncWallet = _forceResyncWallet
, listTransactions = _listTransactions
, postTransaction = _postTransaction
, postExternalTransaction = _postExternalTransaction
Expand Down
14 changes: 14 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Cardano.Wallet.Api.Link
, getUTxOsStatistics
, getMigrationInfo
, migrateWallet
, forceResyncWallet

-- * Addresses
, listAddresses
Expand Down Expand Up @@ -241,6 +242,19 @@ getMigrationInfo w =
where
wid = w ^. typed @(ApiT WalletId)

forceResyncWallet
:: forall style w.
( HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> (Method, Text)
forceResyncWallet w = discriminate @style
(endpoint @Api.ForceResyncWallet (wid &))
(endpoint @Api.ForceResyncByronWallet (wid &))
where
wid = w ^. typed @(ApiT WalletId)

--
-- Addresses
--
Expand Down
36 changes: 35 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ import Control.DeepSeq
import Control.Exception
( IOException, bracket, tryJust )
import Control.Monad
( forM, forM_, void )
( forM, forM_, void, when )
import Control.Monad.IO.Class
( MonadIO, liftIO )
import Control.Monad.Trans.Except
Expand Down Expand Up @@ -458,6 +458,7 @@ server byron icarus shelley spl =
:<|> putWallet shelley mkShelleyWallet
:<|> putWalletPassphrase shelley
:<|> getUTxOsStatistics shelley
:<|> forceResyncWallet shelley

addresses :: Server (Addresses n)
addresses = listAddresses shelley
Expand Down Expand Up @@ -495,6 +496,10 @@ server byron icarus shelley spl =
:<|> liftA2 (\xs ys -> fmap fst $ sortOn snd $ xs ++ ys)
(listWallets byron mkLegacyWallet)
(listWallets icarus mkLegacyWallet)
:<|> (\wid tip -> withLegacyLayer wid
(byron , forceResyncWallet byron wid tip)
(icarus, forceResyncWallet icarus wid tip)
)

byronTransactions :: Server (ByronTransactions n)
byronTransactions =
Expand Down Expand Up @@ -877,6 +882,22 @@ getUTxOsStatistics ctx (ApiT wid) = do
where
liftE = throwE . ErrListUTxOStatisticsNoSuchWallet

forceResyncWallet
:: forall ctx s t k.
( ctx ~ ApiLayer s t k
)
=> ctx
-> ApiT WalletId
-> ApiNetworkTip
-> Handler NoContent
forceResyncWallet ctx (ApiT wid) tip = guardTip >> do
liftHandler $ withWorkerCtx ctx wid throwE $ \wrk ->
W.rollbackBlocks wrk wid W.slotMinBound
pure NoContent
where
guardTip :: Handler ()
guardTip = when (tip /= ApiNetworkTip (ApiT 0) (ApiT 0))
$ liftHandler $ throwE $ ErrRejectedTip tip

{-------------------------------------------------------------------------------
Coin Selections
Expand Down Expand Up @@ -1496,10 +1517,23 @@ data ErrCreateWallet
-- ^ Somehow, we couldn't create a worker or open a db connection
deriving (Eq, Show)

data ErrRejectedTip
= ErrRejectedTip ApiNetworkTip
deriving (Eq, Show)

-- | Small helper to easy show things to Text
showT :: Show a => a -> Text
showT = T.pack . show

instance LiftHandler ErrRejectedTip where
handler = \case
ErrRejectedTip {} ->
apiError err403 RejectedTip $ mconcat
[ "I am sorry but I refuse to rollback to the given point. "
, "Notwithstanding I'll willingly rollback to the genesis point "
, "(0, 0) if you demand it."
]

instance LiftHandler ErrSelectForMigration where
handler = \case
ErrSelectForMigrationNoSuchWallet e -> handler e
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ data ApiErrorCode
| PoolAlreadyJoined
| NotDelegatingTo
| InvalidRestorationParameters
| RejectedTip
deriving (Eq, Generic, Show)

-- | Defines a point in time that can be formatted as and parsed from an
Expand Down

0 comments on commit 206affc

Please sign in to comment.