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

Commit 829f7da

Browse files
committed
[CO-347] Change computeUtxoStatistics API to take [Utxo]
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? :) )
1 parent 984eb7b commit 829f7da

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

wallet-new/cardano-sl-wallet-new.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ library
4343
Cardano.Wallet.API.V1.Handlers.Accounts
4444
Cardano.Wallet.API.V1.Handlers.Addresses
4545
Cardano.Wallet.API.V1.Handlers.Info
46-
Cardano.Wallet.API.V1.Handlers.Internal
4746
Cardano.Wallet.API.V1.Handlers.Settings
4847
Cardano.Wallet.API.V1.Handlers.Transactions
4948
Cardano.Wallet.API.V1.Handlers.Wallets

wallet-new/integration/TransactionSpecs.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import Universum
88
import Cardano.Wallet.Client.Http
99
import Control.Concurrent (threadDelay)
1010
import Control.Lens
11-
import qualified Pos.Core as Core
1211
import Test.Hspec
1312
import Text.Show.Pretty (ppShow)
1413

14+
import Util
15+
16+
import qualified Data.Map.Strict as Map
1517
import qualified Pos.Core as Core
18+
import qualified Pos.Core.Txp as Txp
1619

17-
import Util
1820

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

@@ -217,7 +219,14 @@ transactionSpecs wRef wc =
217219
void $ postTransaction wc (payment 1)
218220
threadDelay 120000000
219221

222+
let txIn = Txp.TxInUnknown 0 "test"
223+
let txOut = Txp.TxOutAux Txp.TxOut
224+
{ Txp.txOutAddress = unV1 (addrId toAddr)
225+
, Txp.txOutValue = Core.mkCoin 1
226+
}
227+
let utxos = [Map.fromList [(txIn, txOut)]]
228+
220229
eresp <- getUtxoStatistics wc (walId wallet)
221230
utxoStatistics <- fmap wrData eresp `shouldPrism` _Right
222-
let utxoStatisticsExpected = computeUtxoStatistics log10 [1]
231+
let utxoStatisticsExpected = computeUtxoStatistics log10 utxos
223232
utxoStatistics `shouldBe` utxoStatisticsExpected

wallet-new/src/Cardano/Wallet/API/V1/Handlers/Wallets.hs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ module Cardano.Wallet.API.V1.Handlers.Wallets where
22

33
import Universum
44

5+
import Servant
6+
57
import Cardano.Wallet.API.Request
68
import Cardano.Wallet.API.Response
79
import Cardano.Wallet.API.V1.Types as V1
810
import qualified Cardano.Wallet.API.V1.Wallets as Wallets
911
import Cardano.Wallet.WalletLayer (PassiveWalletLayer)
1012
import qualified Cardano.Wallet.WalletLayer as WalletLayer
1113

12-
import Pos.Chain.Txp (Utxo)
1314
import Pos.Core.Common (Coin (..))
14-
import Pos.Core.Txp (TxOut (..), TxOutAux (..))
1515

16-
import qualified Data.Map.Strict as M (elems)
17-
import Servant
1816

1917
-- | All the @Servant@ handlers for wallet-specific operations.
2018
handlers :: PassiveWalletLayer IO -> ServerT Wallets.API Handler
@@ -107,13 +105,8 @@ getUtxoStatistics pwl wid = do
107105
res <- liftIO $ WalletLayer.getUtxos pwl wid
108106
case res of
109107
Left e -> throwM e
110-
Right w -> do
111-
let extractValue :: TxOutAux -> Word64
112-
extractValue = getCoin . txOutValue . toaOut
113-
let utxosCoinValuesForAllAccounts :: [(Account, Utxo)] -> [Word64]
114-
utxosCoinValuesForAllAccounts =
115-
concatMap (\pair -> map extractValue (M.elems $ snd pair) )
116-
return $ single (V1.computeUtxoStatistics V1.log10 $ utxosCoinValuesForAllAccounts w)
108+
Right w ->
109+
return $ single $ V1.computeUtxoStatistics V1.log10 (map snd w)
117110

118111
checkExternalWallet :: PassiveWalletLayer IO
119112
-> PublicKeyAsBase58

wallet-new/src/Cardano/Wallet/API/V1/LegacyHandlers/Wallets.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import qualified Pos.Wallet.Web.State.Storage as V0
1717

1818
import Cardano.Wallet.API.Request
1919
import Cardano.Wallet.API.Response
20-
import Cardano.Wallet.API.V1.Errors
2120
import Cardano.Wallet.API.V1.Migration
2221
import Cardano.Wallet.API.V1.Types as V1
2322
import qualified Cardano.Wallet.API.V1.Wallets as Wallets

wallet-new/src/Cardano/Wallet/API/V1/Types.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ module Cardano.Wallet.API.V1.Types (
4545
, mkPublicKeyFromBase58
4646
, NewExternalWallet (..)
4747
, WalletAndTxHistory (..)
48-
, UtxoStatistics (..)
49-
, HistogramBar (..)
5048
-- * Addresses
5149
, AddressIndex
5250
, AddressValidity (..)
@@ -146,9 +144,7 @@ import Data.ByteString.Base58 (bitcoinAlphabet, decodeBase58,
146144
encodeBase58)
147145
import qualified Data.Char as C
148146
import Data.Default (Default (def))
149-
import qualified Data.HashMap.Strict as HMS
150147
import qualified Data.Map.Strict as Map
151-
import Data.Scientific (floatingOrInteger)
152148
import Data.Semigroup (Semigroup)
153149
import Data.Swagger hiding (Example, example)
154150
import qualified Data.Swagger as S

wallet-new/src/Cardano/Wallet/Types/UtxoStatistics.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Cardano.Wallet.API.V1.Swagger.Example (Example)
3737
import Pos.Chain.Txp (Utxo)
3838
import Pos.Core.Common (Coin (..))
3939
import Pos.Core.Txp (TxOut (..), TxOutAux (..))
40-
import Pos.Infra.Log.LogSafe (BuildableSafeGen (..),
40+
import Pos.Infra.Util.LogSafe (BuildableSafeGen (..),
4141
deriveSafeBuildable)
4242

4343
import qualified Control.Foldl as L
@@ -187,11 +187,19 @@ log10 = Log10
187187
{-# INLINE log10 #-}
188188

189189
-- | Compute UtxoStatistics from a bunch of UTXOs
190-
computeUtxoStatistics :: BoundType -> [Word64] -> UtxoStatistics
191-
computeUtxoStatistics btype = L.fold $ UtxoStatistics
192-
<$> foldBuckets (generateBounds btype)
193-
<*> L.sum
190+
computeUtxoStatistics :: BoundType -> [Utxo] -> UtxoStatistics
191+
computeUtxoStatistics btype =
192+
L.fold foldStatistics . concatMap getCoins
194193
where
194+
getCoins :: Utxo -> [Word64]
195+
getCoins =
196+
map (getCoin . txOutValue . toaOut) . Map.elems
197+
198+
foldStatistics :: L.Fold Word64 UtxoStatistics
199+
foldStatistics = UtxoStatistics
200+
<$> foldBuckets (generateBounds btype)
201+
<*> L.sum
202+
195203
foldBuckets :: NonEmpty Word64 -> L.Fold Word64 [HistogramBar]
196204
foldBuckets bounds =
197205
let

0 commit comments

Comments
 (0)