Skip to content

Commit

Permalink
Add test data and ingestion engine for this
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Apr 25, 2019
1 parent d8796b3 commit a930f1c
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ test-suite unit
, bytestring
, cardano-crypto
, cardano-wallet
, cassava
, cborg
, containers
, cryptonite
Expand All @@ -133,6 +134,7 @@ test-suite unit
, swagger2
, text
, transformers
, vector
, yaml
type:
exitcode-stdio-1.0
Expand Down
115 changes: 115 additions & 0 deletions test/data/Cardano/Wallet/fees
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
inputs|outputs|fee
[200000]|[1]|171905
[200000]|[9]|171905
[200000]|[11]|171905
[200000]|[15]|171905
[200000]|[99]|171993
[200000]|[101]|171993
[200000]|[500]|171993
[200000]|[999]|171993
[200000]|[1000]|171993
[200000]|[9999]|171993
[200000]|[1000]|171993
[200000]|[1009]|171993
[200000]|[1011]|171993
[200000]|[1015]|171993
[200000]|[1099]|171993
[200000]|[1101]|171993
[200000]|[1500]|171993
[200000]|[1999]|171993
[200000]|[10000]|171993
[500000]|[100000]|172081
[500000]|[200000]|172081
[500000]|[300000]|172081
[1000000]|[500000]|172081
[1000000]|[750000]|172081
[1000000]|[800000]|172081
[5000000]|[1000000]|172081
[5000000]|[2000000]|172081
[5000000]|[3000000]|172081
[10000000]|[9000000]|172081
[500000,500000]|[750000]|180211
[330000,330000,330000]|[750000]|188341
[250000,250000,250000,250000]|[750000]|196471
[200000,200000,200000,200000,200000]|[750000]|204601
[185000,185000,185000,185000,185000,185000]|[750000]|212731
[142000,142000,142000,142000,142000,142000,142000]|[750000]|220861
[125000,125000,125000,125000,125000,125000,125000,125000]|[750000]|228991
[930210,1]|[750000]|180211
[930210,10]|[750000]|180211
[930210,100]|[750000]|180211
[171906]|[1]|171905
[930209,5000,5000]|[750000]|188341
[930208,6000,6000,6000]|[750000]|196471
[930207,7000,7000,7000,7000]|[750000]|204601
[930206,8000,8000,8000,8000,8000]|[750000]|212731
[930205,9000,9000,9000,9000,9000,9000]|[750000]|220861
[111000,111000,111000,111000,111000,111000,111000,111000,111000]|[750000]|237121
[100000,100000,100000,100000,100000,100000,100000,100000,100000,100000]|[750000]|245251
[930209,8130,5]|[750000]|188341
[930208,8130,8130,10]|[750000]|196471
[930207,8130,8130,8130,10]|[750000]|204601
[930206,8130,8130,8130,8130,10]|[750000]|212731
[930205,8130,8130,8130,8130,8130,10]|[750000]|220861
[50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,50000,5000]|[750000]|350941
[200000,1]|[1,1]|187770
[200000,1]|[1,100]|187945
[250000,250000]|[100000,100000]|188298
[200000,200000,200000]|[150000,150000]|196428
[187945,200]|[1,100]|187946
[250000,250000]|[100000,1]|188122
[250000,250000]|[100000,10]|188122
[250000,250000]|[100000,100]|188298
[250000,250000]|[100000,1000]|188298
[250000,250000]|[100000,10000]|188298
[120000,120000,120000]|[100000,1]|196252
[120000,120000,120000]|[100000,10]|196252
[120000,120000,120000]|[100000,100]|196428
[120000,120000,120000]|[100000,1000]|196428
[120000,120000,120000]|[100000,10000]|196428
[90000,90000,90000,90000]|[100000,1]|204382
[90000,90000,90000,90000]|[100000,10]|204382
[90000,90000,90000,90000]|[100000,100]|204558
[90000,90000,90000,90000]|[100000,1000]|204558
[90000,90000,90000,90000]|[100000,10000]|204558
[70000,70000,70000,70000,70000]|[100000,1]|212512
[70000,70000,70000,70000,70000]|[100000,10]|212512
[70000,70000,70000,70000,70000]|[100000,100]|212688
[70000,70000,70000,70000,70000]|[100000,1000]|212688
[70000,70000,70000,70000,70000]|[100000,10000]|212688
[60000,60000,60000,60000,60000,60000]|[100000,1]|220642
[60000,60000,60000,60000,60000,60000]|[100000,10]|220642
[60000,60000,60000,60000,60000,60000]|[100000,100]|220818
[60000,60000,60000,60000,60000,60000]|[100000,1000]|220818
[60000,60000,60000,60000,60000,60000]|[100000,10000]|220818
[51000,51000,51000,51000,51000,51000,51000]|[100000,1]|228772
[51000,51000,51000,51000,51000,51000,51000]|[100000,10]|228772
[51000,51000,51000,51000,51000,51000,51000]|[100000,100]|228948
[51000,51000,51000,51000,51000,51000,51000]|[100000,1000]|228948
[51000,51000,51000,51000,51000,51000,51000]|[100000,10000]|228948
[100000,100000,100000]|[1,1,1]|203988
[60000,60000,60000,60000]|[1,1,1]|212118
[50000,50000,50000,50000,50000]|[1,1,1]|220248
[60000,60000,60000,60000]|[100,1,1]|212381
[60000,60000,60000,60000]|[100,1,100]|212380
[60000,60000,60000,60000]|[100,100,100]|212379
[50000,50000,50000,50000,50000]|[100,1,1]|220510
[50000,50000,50000,50000,50000]|[100,100,1]|220510
[50000,50000,50000,50000,50000]|[100,100,100]|220510
[42000,42000,42000,42000,42000,42000]|[1,1,1]|228378
[42000,42000,42000,42000,42000,42000]|[1,1,100]|228640
[42000,42000,42000,42000,42000,42000]|[100,1,1]|228640
[42000,42000,42000,42000,42000,42000]|[1,100,1]|228640
[42000,42000,42000,42000,42000,42000]|[100,100,1]|228641
[42000,42000,42000,42000,42000,42000]|[1,100,100]|228641
[42000,42000,42000,42000,42000,42000]|[100,1,100]|228641
[42000,42000,42000,42000,42000,42000]|[100,100,100]|228639
[42000,42000,42000,42000,42000,52000]|[1,10000,1]|228640
[42000,42000,42000,42000,52000,52000]|[1,10000,10000]|228641
[52000,42000,42000,42000,42000,52000]|[10000,10000,100]|228640
[62000,62000,62000,62000,62000,42000]|[100000,100,1]|228640
[82000,82000,82000,82000,82000,42000]|[100000,100000,1]|228640
[42000,42000,42000,42000,42000,42000,42000,42000,42000,42000,42000,42000,42000,42000,42000]|[100000,100000,100000]|302073
[42000,42000,42000,42000,42000,42000,42000,42000,42000]|[100000,1000,100]|253294
[42000,42000,42000,42000,42000,42000,42000,42000,42000]|[100000,10000,1]|253032
[75000,75000,75000,60000]|[100,1,1]|212381
72 changes: 70 additions & 2 deletions test/unit/Cardano/Wallet/CoinSelection/FeeSpec.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.Wallet.CoinSelection.FeeSpec
Expand All @@ -20,27 +22,50 @@ import Cardano.Wallet.Primitive.Types
( Coin (..), ShowFmt (..), TxOut (..), UTxO (..) )
import Control.Arrow
( left )
import Control.Monad
( forM_ )
import Control.Monad.Trans.Except
( runExceptT )
import Crypto.Random
( SystemDRG, getSystemDRG )
import Crypto.Random.Types
( withDRG )
import Data.Char
( ord )
import Data.Csv
( DecodeOptions (..), FromField (..), FromNamedRecord (..), (.:) )
import Data.Either
( isRight )
import Data.Functor.Identity
( Identity (runIdentity) )
import Data.Vector
( Vector )
import Data.Word
( Word64 )
import Fmt
( Buildable (..), nameF, tupleF )
import Test.Hspec
( Spec, SpecWith, before, describe, it, shouldBe, shouldSatisfy )
( Expectation
, Spec
, SpecWith
, before
, describe
, it
, shouldBe
, shouldSatisfy
)
import Test.QuickCheck
( Arbitrary (..), Property, choose, disjoin, generate, property, (==>) )
import Text.Read
( read )

import qualified Data.ByteString.Lazy as BL
import qualified Data.Csv as CSV
import qualified Data.List as L
import qualified Data.Map.Strict as Map

import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Vector as V

spec :: Spec
spec = do
Expand Down Expand Up @@ -233,6 +258,8 @@ spec = do
, csChngs = [3,3]
})

realisticFeeUnitTest

before getSystemDRG $ describe "Fee calculation properties" $ do
it "No fee gives back the same selection"
(\_ -> property propSameSelection)
Expand Down Expand Up @@ -317,6 +344,47 @@ propReducedChanges drg (ShowFmt (FeeProp (CoinSelProp utxo txOuts) utxo' (fee, d
Fee Adjustment - Unit Tests
-------------------------------------------------------------------------------}

realisticFeeUnitTest :: SpecWith ()
realisticFeeUnitTest = it "realistic fee calculations" $ do
vector <- V.filter verifyFeeCase <$> setup
forM_ (V.toList vector) $ \feecase -> do
checkFee feecase
where
setup :: IO (Vector FeeCase)
setup = do
let myOptions = CSV.defaultDecodeOptions {
decDelimiter = fromIntegral (ord '|')
}
bytes <- BL.readFile "test/data/Cardano/Wallet/fees"
case CSV.decodeByNameWith myOptions bytes of
Left err -> fail err
Right (_, vector) -> return vector
verifyFeeCase :: FeeCase -> Bool
verifyFeeCase (FeeCase inps outs fee) =
let sum' = sum $ L.tail $ L.sort inps
in (sum' < (fee + sum outs))
checkFee :: FeeCase -> Expectation
checkFee (FeeCase _inps _outs fee) =
fee `shouldSatisfy` (> 10000)


data FeeCase = FeeCase
{ csvInputs :: [Word64]
, csvOutputs :: [Word64]
, csvFee :: Word64
} deriving Show

instance FromNamedRecord FeeCase where
parseNamedRecord m = do
csvInputs <- m .: "inputs"
csvOutputs <- m .: "outputs"
csvFee <- m .: "fee"
return (FeeCase {..})

instance FromField [Word64] where
parseField = pure . read . T.unpack . T.decodeUtf8


feeOptions
:: Word64
-> Word64
Expand Down

0 comments on commit a930f1c

Please sign in to comment.