Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

HdRootBase <-> HdAccountBase #359

Open
wants to merge 5 commits into
base: KtorZ/357/restructure-pool-construction
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions src/Cardano/Wallet/API/V1/Handlers/Wallets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,8 @@ listEosWallets
-> SortOperations EosWallet
-> Handler (APIResponse [EosWallet])
listEosWallets pwl params fops sops = do
res <- liftIO $ WalletLayer.getEosWallets pwl
case res of
Left e -> throwM e
Right wallets ->
respondWith params
fops
sops
(pure wallets)
wallets <- liftIO $ WalletLayer.getEosWallets pwl
respondWith params
fops
sops
(pure wallets)
11 changes: 0 additions & 11 deletions src/Cardano/Wallet/API/V1/ReifyWalletError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,12 @@ getWalletError e = case e of
(GetWalletWalletIdDecodingFailed _txt) ->
V1.WalletNotFound

getAddressPoolGapError :: GetAddressPoolGapError -> V1.WalletError
getAddressPoolGapError e = case e of
ex@(GetEosWalletErrorNoAccounts _txt) ->
V1.EosWalletDoesNotHaveAccounts (sformat build ex)
ex@(GetEosWalletErrorWrongAccounts _txt) ->
V1.EosWalletHasWrongAccounts (sformat build ex)
ex@(GetEosWalletErrorGapsDiffer _txt) ->
V1.EosWalletGapsDiffer (sformat build ex)

getEosWalletError :: GetEosWalletError -> V1.WalletError
getEosWalletError e = case e of
(GetEosWalletError (HD.UnknownHdRoot _rootId)) ->
V1.WalletNotFound
(GetEosWalletWalletIdDecodingFailed _txt) ->
V1.WalletNotFound
(GetEosWalletErrorAddressPoolGap e') ->
getAddressPoolGapError e'

updateWalletError :: UpdateWalletError -> V1.WalletError
updateWalletError e = case e of
Expand Down
25 changes: 5 additions & 20 deletions src/Cardano/Wallet/Kernel/Accounts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Cardano.Wallet.Kernel.Accounts (
createAccount
, deleteAccount
, updateAccount
, updateAccountGap
-- * Errors
, CreateAccountError(..)
) where
Expand All @@ -20,16 +19,13 @@ import Data.Acid (update)
import Pos.Core.NetworkMagic (makeNetworkMagic)
import Pos.Crypto (EncryptedSecretKey, PassPhrase)

import Cardano.Wallet.Kernel.AddressPoolGap (AddressPoolGap)
import Cardano.Wallet.Kernel.DB.AcidState (CreateHdAccount (..), DB,
DeleteHdAccount (..), UpdateHdAccountGap (..),
UpdateHdAccountName (..))
DeleteHdAccount (..), UpdateHdAccountName (..))
import Cardano.Wallet.Kernel.DB.HdRootId (HdRootId)
import Cardano.Wallet.Kernel.DB.HdWallet (AccountName (..),
HdAccount (..), HdAccountBase (..), HdAccountId (..),
HdAccountIx (..), HdAccountState (..),
HdAccountUpToDate (..), UnknownHdAccount (..),
UpdateGapError (..), hdAccountName)
HdAccount (..), HdAccountId (..), HdAccountIx (..),
HdAccountState (..), HdAccountUpToDate (..),
UnknownHdAccount (..), hdAccountName)
import Cardano.Wallet.Kernel.DB.HdWallet.Create
(CreateHdAccountError (..), initHdAccount)
import Cardano.Wallet.Kernel.DB.HdWallet.Derivation
Expand Down Expand Up @@ -123,8 +119,7 @@ createHdRndAccount _spendingPassword accountName _esk rootId pw = do
tryGenerateAccount gen collisions = do
newIndex <- deriveIndex (flip uniformR gen) HdAccountIx HardDerivation
let hdAccountId = HdAccountId rootId newIndex
newAccount = initHdAccount (HdAccountBaseFO hdAccountId) initState &
hdAccountName .~ accountName
newAccount = initHdAccount hdAccountId initState & hdAccountName .~ accountName
db = pw ^. wallets
res <- update db (CreateHdAccount newAccount)
case res of
Expand Down Expand Up @@ -171,13 +166,3 @@ updateAccount hdAccountId newAccountName pw = do
return $ case res of
Left dbError -> Left dbError
Right (db, account) -> Right (db, account)

-- | Updates address pool gap in an HD 'Account' (in EOS-wallet).
updateAccountGap
:: HdAccountId
-> AddressPoolGap
-- ^ The new adress pool gap for this account.
-> PassiveWallet
-> IO (Either UpdateGapError (DB, HdAccount))
updateAccountGap hdAccountId newGap pw = liftIO $
update (pw ^. wallets) (UpdateHdAccountGap hdAccountId newGap)
1 change: 1 addition & 0 deletions src/Cardano/Wallet/Kernel/BListener.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ prefilterContext pw = do
\address pool for the given account: " <> sformat build accId <> " ( "
<> sformat build err <> " )"


-- | Prefilter a resolved block for all wallets. If no wallets are present
-- we return Nothing. If either wallet type is present, we return only the
-- relevant results.
Expand Down
54 changes: 26 additions & 28 deletions src/Cardano/Wallet/Kernel/DB/AcidState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module Cardano.Wallet.Kernel.DB.AcidState (
-- *** UPDATE
, UpdateHdWallet(..)
, UpdateHdRootPassword(..)
, UpdateHdRootGap(..)
, UpdateHdAccountName(..)
, UpdateHdAccountGap(..)
, ResetAllHdWalletAccounts(..)
-- *** DELETE
, DeleteHdRoot(..)
Expand Down Expand Up @@ -278,7 +278,7 @@ applyBlock k context restriction blocks = runUpdateDiscardSnapshot $ do
mkUpdate :: (HdAccountId, Maybe PrefilteredBlock)
-> AccountUpdate Void (Either Spec.ApplyBlockFailed (Set TxId))
mkUpdate (accId, mPB) = AccountUpdate {
accountUpdateBase = HdAccountBaseFO accId
accountUpdateId = accId
, accountUpdateAddrs = pfbAddrs pb
, accountUpdateNew = AccountUpdateNewUpToDate Map.empty
, accountUpdate =
Expand Down Expand Up @@ -338,7 +338,7 @@ applyHistoricalBlock k context rootId blocks =
mkUpdate :: (HdAccountId, Maybe PrefilteredBlock)
-> AccountUpdate Spec.ApplyBlockFailed ()
mkUpdate (accId, mPb) = AccountUpdate {
accountUpdateBase = HdAccountBaseFO accId
accountUpdateId = accId
, accountUpdateAddrs = pfbAddrs pb
, accountUpdateNew = AccountUpdateNewIncomplete mempty mempty context
, accountUpdate = void $ Z.wrap $ \acc -> do
Expand Down Expand Up @@ -450,7 +450,7 @@ switchToFork k oldest blocks toSkip =
mkUpdate :: (HdAccountId, OldestFirst [] (BlockContext, PrefilteredBlock))
-> AccountUpdate SwitchToForkInternalError (Pending, Set TxId)
mkUpdate (accId, pbs) = AccountUpdate {
accountUpdateBase = HdAccountBaseFO accId
accountUpdateId = accId
, accountUpdateAddrs = concatMap (pfbAddrs . snd) pbs
, accountUpdateNew = AccountUpdateNewUpToDate Map.empty
, accountUpdate =
Expand Down Expand Up @@ -508,17 +508,17 @@ observableRollbackUseInTestsOnly = runUpdateDiscardSnapshot $
-- definitely we do not want it to show up in our acid-state logs.
--
createHdWallet :: HdRoot
-> Map HdAccountBase (Utxo,[HdAddress])
-> Map HdAccountId (Utxo,[HdAddress])
-> Update DB (Either HD.CreateHdRootError ())
createHdWallet newRoot utxo0 =
runUpdateDiscardSnapshot . zoom dbHdWallets $ do
HD.createHdRoot newRoot
updateAccounts_ $ map mkUpdate (Map.toList utxo0)
where
mkUpdate :: (HdAccountBase, (Utxo, [HdAddress]))
mkUpdate :: (HdAccountId, (Utxo, [HdAddress]))
-> AccountUpdate HD.CreateHdRootError ()
mkUpdate (accBase, (utxo, addrs)) = AccountUpdate {
accountUpdateBase = accBase
mkUpdate (accId, (utxo, addrs)) = AccountUpdate {
accountUpdateId = accId
, accountUpdateNew = AccountUpdateNewUpToDate utxo
, accountUpdateAddrs = addrs
, accountUpdate = return () -- just need to create it, no more
Expand Down Expand Up @@ -554,7 +554,7 @@ restoreHdWallet newRoot defaultHdAccountId defaultHdAddress ctx utxoByAccount =
mkUpdate :: (HdAccountId, (Utxo, Utxo, [HdAddress]))
-> AccountUpdate HD.CreateHdRootError ()
mkUpdate (accId, (curUtxo, genUtxo, addrs)) = AccountUpdate {
accountUpdateBase = HdAccountBaseFO accId
accountUpdateId = accId
, accountUpdateNew = AccountUpdateNewIncomplete curUtxo genUtxo ctx
, accountUpdateAddrs = addrs
, accountUpdate = return () -- Create it only
Expand All @@ -579,7 +579,7 @@ restoreHdWallet newRoot defaultHdAccountId defaultHdAddress ctx utxoByAccount =
-- See 'updateAccount' or 'updateAccounts'.
data AccountUpdate e a = AccountUpdate {
-- | Account to update
accountUpdateBase :: !HdAccountBase
accountUpdateId :: !HdAccountId

-- | Information needed when we need to create the account from scratch
, accountUpdateNew :: !AccountUpdateNew
Expand Down Expand Up @@ -627,9 +627,9 @@ data AccountUpdateNew =
| AccountUpdateNewIncomplete !Utxo !Utxo !BlockContext

-- | Brand new account (if one needs to be created)
accountUpdateCreate :: HdAccountBase -> AccountUpdateNew -> HdAccount
accountUpdateCreate accUpdateBase (AccountUpdateNewUpToDate utxo) =
HD.initHdAccount accUpdateBase initState
accountUpdateCreate :: HdAccountId -> AccountUpdateNew -> HdAccount
accountUpdateCreate accId (AccountUpdateNewUpToDate utxo) =
HD.initHdAccount accId initState
where
initState :: HdAccountState
initState = HdAccountStateUpToDate HdAccountUpToDate {
Expand All @@ -646,12 +646,11 @@ accountUpdateCreate accUpdateBase (AccountUpdateNewIncomplete curUtxo genUtxo ct

updateAccount :: AccountUpdate e a -> Update' e HdWallets (HdAccountId, a)
updateAccount AccountUpdate{..} = do
let accountId = accountUpdateBase ^. hdAccountBaseId
res <- zoomOrCreateHdAccount
assumeHdRootExists
(accountUpdateCreate accountUpdateBase accountUpdateNew)
accountId
((accountId, ) <$> accountUpdate)
(accountUpdateCreate accountUpdateId accountUpdateNew)
accountUpdateId
((accountUpdateId, ) <$> accountUpdate)
mapM_ createAddress accountUpdateAddrs
return res
where
Expand All @@ -676,9 +675,7 @@ updateAccountsWithErrors :: [AccountUpdate e a]
-> Update' [HdAccountId] HdWallets (Map HdAccountId a)
updateAccountsWithErrors updates = do
results <- forM updates $ \upd ->
let accountId = (accountUpdateBase upd) ^. hdAccountBaseId
in
tryUpdate (mapUpdateErrors (const accountId) (updateAccount upd))
tryUpdate (mapUpdateErrors (const $ accountUpdateId upd) (updateAccount upd))
let (errors, successes) = partitionEithers results
case errors of
[] -> return (Map.fromList successes)
Expand Down Expand Up @@ -711,18 +708,19 @@ updateHdRootPassword rootId hasSpendingPassword = do
runUpdate' . zoom dbHdWallets $
HD.updateHdRootPassword rootId hasSpendingPassword

updateHdRootGap
:: HdRootId
-> AddressPoolGap
-> Update DB (Either UnknownHdRoot (DB, HdRoot))
updateHdRootGap rootId gap = do
runUpdate' . zoom dbHdWallets $ HD.updateHdRootGap rootId gap

updateHdAccountName :: HdAccountId
-> AccountName
-> Update DB (Either UnknownHdAccount (DB, HdAccount))
updateHdAccountName accId name = do
runUpdate' . zoom dbHdWallets $ HD.updateHdAccountName accId name

updateHdAccountGap :: HdAccountId
-> AddressPoolGap
-> Update DB (Either UpdateGapError (DB, HdAccount))
updateHdAccountGap accId gap = do
runUpdate' . zoom dbHdWallets $ HD.updateHdAccountGap accId gap

deleteHdRoot :: HdRootId -> Update DB (Either UnknownHdRoot ())
deleteHdRoot rootId = runUpdateDiscardSnapshot . zoom dbHdWallets $
HD.deleteHdRoot rootId
Expand Down Expand Up @@ -757,7 +755,7 @@ resetAllHdWalletAccounts rootId context utxoByAccount = mustBeRight <$> do
mkUpdate :: (HdAccountId, Maybe (Utxo, Utxo, [HdAddress]))
-> AccountUpdate Void ()
mkUpdate (accId, utxos) = AccountUpdate {
accountUpdateBase = HdAccountBaseFO accId
accountUpdateId = accId
, accountUpdateAddrs = []
, accountUpdateNew = AccountUpdateNewIncomplete mempty mempty context
, accountUpdate =
Expand Down Expand Up @@ -823,8 +821,8 @@ makeAcidic ''DB [
, 'createHdWallet
, 'updateHdWallet
, 'updateHdRootPassword
, 'updateHdRootGap
, 'updateHdAccountName
, 'updateHdAccountGap
, 'deleteHdRoot
, 'deleteHdAccount
, 'resetAllHdWalletAccounts
Expand Down
Loading