Skip to content

Commit

Permalink
Bug Fix - Incorrect Cached Balance computation in updateUtxo. Also fi…
Browse files Browse the repository at this point in the history
…xed Total/Available balances to use cached balance
  • Loading branch information
uroboros committed Apr 25, 2018
1 parent da7ef72 commit 3981c2c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions wallet-new/src/Cardano/Wallet/Kernel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ applyBlock' pw b wid = do
let prefBlock = prefilterBlock (w ^. walletESK) b
(utxo'', balanceDelta) = updateUtxo prefBlock utxo'
pending'' = updatePending prefBlock pending'
balance'' = balanceDelta + balance'
balance'' = balance' + balanceDelta

updateWalletState pw wid $ State utxo'' pending'' balance''

Expand All @@ -295,7 +295,7 @@ updateUtxo PrefilteredBlock{..} currentUtxo =
unionUtxo = Map.union pfbOutputs currentUtxo
utxo' = utxoRemoveInputs unionUtxo pfbInputs
unionUtxoRestricted = utxoRestrictToInputs unionUtxo pfbInputs
balanceDelta = balance unionUtxo - balance unionUtxoRestricted
balanceDelta = balance pfbOutputs - balance unionUtxoRestricted

updatePending :: PrefilteredBlock -> Pending -> Pending
updatePending PrefilteredBlock{..} =
Expand Down Expand Up @@ -328,16 +328,15 @@ utxoInputs = Map.keysSet
utxoOutputs :: Utxo -> [TxOut]
utxoOutputs = map toaOut . Map.elems

txIns' :: Pending -> Set TxIn
txIns' = Set.fromList . concatMap (NE.toList . _txInputs . taTx) . Map.elems

available :: PassiveWallet -> WalletId -> IO Utxo
available pw wid = do
State utxo pending _ <- getWalletState pw wid

return $ utxoRemoveInputs utxo (txIns' pending)

where
txIns' :: Map TxId TxAux -> Set TxIn
txIns' = Set.fromList . concatMap (NE.toList . _txInputs . taTx) . Map.elems

change :: PassiveWallet -> WalletId -> IO Utxo
change pw wid = do
State _ pending _ <- getWalletState pw wid
Expand All @@ -346,17 +345,23 @@ change pw wid = do
w <- fromJust <$> findWallet pw wid
return $ ourUtxo (w ^. walletESK) pendingUtxo

total :: PassiveWallet -> WalletId -> IO Utxo
total pw wid = Map.union <$> available pw wid <*> change pw wid
_total :: PassiveWallet -> WalletId -> IO Utxo
_total pw wid = Map.union <$> available pw wid <*> change pw wid

balance :: Utxo -> Balance
balance = sumCoins . map txOutValue . utxoOutputs

availableBalance :: PassiveWallet -> WalletId -> IO Balance
availableBalance pw wid = balance <$> available pw wid
availableBalance pw wid = do
State utxo pending utxoBalance <- getWalletState pw wid
let balanceDelta = balance (utxoRestrictToInputs utxo (txIns' pending))
return $ utxoBalance - balanceDelta

totalBalance :: PassiveWallet -> WalletId -> IO Balance
totalBalance pw wid = balance <$> total pw wid
totalBalance pw wid = do
availableBalance' <- availableBalance pw wid
changeBalance' <- balance <$> change pw wid
return $ availableBalance' + changeBalance'

{-------------------------------------------------------------------------------
Active wallet
Expand Down
2 changes: 1 addition & 1 deletion wallet-new/test/unit/Wallet/Abstract/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ equivalentT activeWallet esk = \mkWallet w ->
-> TranslateT (EquivalenceViolation h) m ()
checkWalletState ctxt@InductiveCtxt{..} wid = do
cmp "utxo" utxo (`Kernel.getWalletUtxo` wid)
cmp "totalUtxoBalance" totalBalance getWalletTotalBalance
cmp "totalBalance" totalBalance getWalletTotalBalance
-- TODO: check other properties
where
getWalletTotalBalance :: Kernel.PassiveWallet -> IO Coin
Expand Down

0 comments on commit 3981c2c

Please sign in to comment.