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

Commit

Permalink
[CO-347] Change computeUtxoStatistics API to take [Utxo]
Browse files Browse the repository at this point in the history
This is more semantically correct and type-safe than taking a raw list of 'Word64'. This way, we also get documentation
for free simply by looking at the function signature and also makes calls for callers simpler (provided they have a list
of available utxos, but why would they call the function if they hadn't? :) )
  • Loading branch information
KtorZ committed Aug 20, 2018
1 parent a360004 commit 166665c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
15 changes: 13 additions & 2 deletions wallet-new/integration/TransactionSpecs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import Universum
import Cardano.Wallet.API.V1.Errors hiding (describe)
import Cardano.Wallet.Client.Http
import Control.Lens
import qualified Pos.Core as Core
import Test.Hspec

import Control.Concurrent (threadDelay)
import Text.Show.Pretty (ppShow)
import Util

import qualified Data.Map.Strict as Map
import qualified Pos.Core as Core
import qualified Pos.Core.Txp as Txp


{-# ANN module ("HLint: ignore Reduce duplication" :: Text) #-}

log :: MonadIO m => Text -> m ()
Expand Down Expand Up @@ -216,7 +220,14 @@ transactionSpecs wRef wc =
void $ postTransaction wc (payment 1)
threadDelay 120000000

let txIn = Txp.TxInUnknown 0 "test"
let txOut = Txp.TxOutAux Txp.TxOut
{ Txp.txOutAddress = unV1 (addrId toAddr)
, Txp.txOutValue = Core.mkCoin 1
}
let utxos = [Map.fromList [(txIn, txOut)]]

eresp <- getUtxoStatistics wc (walId wallet)
utxoStatistics <- fmap wrData eresp `shouldPrism` _Right
let utxoStatisticsExpected = computeUtxoStatistics log10 [1]
let utxoStatisticsExpected = computeUtxoStatistics log10 utxos
utxoStatistics `shouldBe` utxoStatisticsExpected
21 changes: 6 additions & 15 deletions wallet-new/src/Cardano/Wallet/API/V1/Handlers/Wallets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ module Cardano.Wallet.API.V1.Handlers.Wallets where

import Universum

import Servant

import Cardano.Wallet.API.Request
import Cardano.Wallet.API.Response
import Cardano.Wallet.API.V1.Types as V1
import qualified Cardano.Wallet.API.V1.Wallets as Wallets

import Cardano.Wallet.WalletLayer (PassiveWalletLayer (..))
import qualified Cardano.Wallet.WalletLayer.Types as WalletLayer

import qualified Cardano.Wallet.API.V1.Wallets as Wallets
import qualified Cardano.Wallet.Kernel.DB.Util.IxSet as KernelIxSet
import qualified Cardano.Wallet.WalletLayer.Types as WalletLayer
import qualified Data.IxSet.Typed as IxSet
import Pos.Chain.Txp (Utxo)
import Pos.Core.Common (Coin (..))
import Pos.Core.Txp (TxOut (..), TxOutAux (..))

import qualified Data.Map.Strict as M (elems)
import Servant

-- | All the @Servant@ handlers for wallet-specific operations.
handlers :: PassiveWalletLayer IO -> ServerT Wallets.API Handler
Expand Down Expand Up @@ -110,10 +106,5 @@ getUtxoStatistics pwl wid = do
res <- liftIO $ WalletLayer.getUtxos pwl wid
case res of
Left e -> throwM e
Right w -> do
let extractValue :: TxOutAux -> Word64
extractValue = getCoin . txOutValue . toaOut
let utxosCoinValuesForAllAccounts :: [(Account, Utxo)] -> [Word64]
utxosCoinValuesForAllAccounts =
concatMap (\pair -> map extractValue (M.elems $ snd pair) )
return $ single (V1.computeUtxoStatistics V1.log10 $ utxosCoinValuesForAllAccounts w)
Right w ->
return $ single $ V1.computeUtxoStatistics V1.log10 (map snd w)
19 changes: 15 additions & 4 deletions wallet-new/src/Cardano/Wallet/Types/UtxoStatistics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import Test.QuickCheck (Arbitrary (..), arbitrary, choose, elements,
infiniteListOf, shuffle)

import Cardano.Wallet.API.V1.Swagger.Example (Example)
import Pos.Chain.Txp (Utxo)
import Pos.Core.Common (Coin (..))
import Pos.Core.Txp (TxOut (..), TxOutAux (..))
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..),
deriveSafeBuildable)

Expand Down Expand Up @@ -184,11 +187,19 @@ log10 = Log10
{-# INLINE log10 #-}

-- | Compute UtxoStatistics from a bunch of UTXOs
computeUtxoStatistics :: BoundType -> [Word64] -> UtxoStatistics
computeUtxoStatistics btype = L.fold $ UtxoStatistics
<$> foldBuckets (generateBounds btype)
<*> L.sum
computeUtxoStatistics :: BoundType -> [Utxo] -> UtxoStatistics
computeUtxoStatistics btype =
L.fold foldStatistics . concatMap getCoins
where
getCoins :: Utxo -> [Word64]
getCoins =
map (getCoin . txOutValue . toaOut) . Map.elems

foldStatistics :: L.Fold Word64 UtxoStatistics
foldStatistics = UtxoStatistics
<$> foldBuckets (generateBounds btype)
<*> L.sum

foldBuckets :: NonEmpty Word64 -> L.Fold Word64 [HistogramBar]
foldBuckets bounds =
let
Expand Down

0 comments on commit 166665c

Please sign in to comment.