Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit cf9c5bd

Browse files
authored
Merge pull request #3911 from input-output-hk/KtorZ/DEVOPS-1153/fix-old-data-layer-restoration-addWAddress
[DEVOPS-1153] Extend addWAddress to support newly-found accounts
2 parents 788bcee + 3a09150 commit cf9c5bd

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118

119119
- Fix Haddock errors ([CDEC-585](https://iohk.myjetbrains.com/youtrack/issue/CDEC-585): [#3614](https://github.com/input-output-hk/cardano-sl/pull/3614))
120120

121+
- Fix restoration ignoring new accounts in legacy data layer ([DEVOPS-1153](https://iohk.myjetbrains.com/youtrack/issue/DEVOPS-1153): [#3911](https://github.com/input-output-hk/cardano-sl/pull/3911))
122+
121123
### Improvements
122124

123125
- Friendly error mistakes from deserializing invalid addresses instead of brutal 500 ([CBR-283](https://iohk.myjetbrains.com/youtrack/issue/CBR-283))

wallet/src/Pos/Wallet/Web/State/Storage.hs

+36-16
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ import Universum
110110
import qualified Data.Acid as Acid
111111

112112
import Control.Arrow ((***))
113-
import Control.Lens (at, has, ix, lens, makeClassy, makeLenses, non',
114-
to, toListOf, traversed, (%=), (+=), (.=), (<<.=), (?=),
115-
_Empty, _Just, _head)
113+
import Control.Lens (At, Index, IxValue, at, has, ix, lens,
114+
makeClassy, makeLenses, non', to, toListOf, traversed,
115+
(%=), (.=), (<<.=), (?=), (?~), _Empty, _Just, _head)
116116
import Control.Monad.State.Class (get, put)
117117
import Data.Default (Default, def)
118118
import qualified Data.HashMap.Strict as HM
119119
import qualified Data.Map as M
120+
import Data.Maybe (fromJust)
120121
import Data.SafeCopy (Migrate (..), base, deriveSafeCopySimple,
121122
extension)
122123
import Data.Time.Clock (nominalDay)
@@ -636,21 +637,40 @@ createWallet cWalId cWalMeta isReady curTime = do
636637
let info = WalletInfo cWalMeta curTime curTime NotSynced noSyncStatistics mempty isReady
637638
wsWalletInfos . at cWalId %= (<|> Just info)
638639

639-
-- | Add new address given 'CWAddressMeta' (which contains information about
640-
-- target wallet and account too).
640+
-- | Add new address given 'WAddressMeta' (which contains information about
641+
-- target wallet and account too). If the input account is /not/ there, creates it.
641642
addWAddress :: WAddressMeta -> Update ()
642643
addWAddress addrMeta = do
643-
let accInfo :: Traversal' WalletStorage AccountInfo
644-
accInfo = wsAccountInfos . ix (addrMeta ^. wamAccount)
645-
addr = addrMeta ^. wamAddress
646-
whenJustM (preuse accInfo) $ \info -> do
647-
let mAddr = info ^. aiAddresses . at addr
648-
when (isNothing mAddr) $ do
649-
-- Here we increment current account's last address index
650-
-- and assign its value to sorting index of newly created address.
651-
accInfo . aiUnusedKey += 1
652-
let key = info ^. aiUnusedKey
653-
accInfo . aiAddresses . at addr ?= AddressInfo addrMeta key
644+
let accId = addrMeta ^. wamAccount
645+
accountInfo <- getAccountInfo accId
646+
let maddr = accountInfo ^. aiAddresses . at (addrMeta ^. wamAddress)
647+
when (isNothing maddr) $
648+
modify $ (wsAccountInfos . at accId) ?~ (addAddress accountInfo)
649+
where
650+
getAccountInfo :: WebTypes.AccountId -> Update AccountInfo
651+
getAccountInfo accId = do
652+
ws <- get
653+
let infos = createIfMissing defaultAccount accId (ws ^. wsAccountInfos)
654+
put $ ws & wsAccountInfos .~ infos
655+
-- NOTE: 'fromJust' is safe since we just added the account
656+
return $ fromJust $ infos ^. at accId
657+
658+
addAddress :: AccountInfo -> AccountInfo
659+
addAddress accountInfo =
660+
let
661+
unusedKey = accountInfo ^. aiUnusedKey
662+
addrInfo = AddressInfo addrMeta unusedKey
663+
in accountInfo
664+
& aiAddresses . at (addrMeta ^. wamAddress) ?~ addrInfo
665+
& aiUnusedKey .~ unusedKey + 1
666+
667+
defaultAccount :: AccountInfo
668+
defaultAccount =
669+
AccountInfo (WebTypes.CAccountMeta "New account") mempty mempty 0
670+
671+
createIfMissing :: At m => IxValue m -> Index m -> m -> m
672+
createIfMissing val idx =
673+
at idx %~ (\x -> x <|> pure val)
654674

655675
-- | Update account metadata.
656676
setAccountMeta :: WebTypes.AccountId -> WebTypes.CAccountMeta -> Update ()

0 commit comments

Comments
 (0)