Skip to content

Commit

Permalink
additional test vectors for rewards calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jan 21, 2020
1 parent 99dd776 commit 8ec5ef6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Rewards.hs
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Wallet.Jormungandr.Rewards
(
Expand Down Expand Up @@ -136,7 +137,7 @@ rewardsAt limit tax epochNo = q . taxCut tax . capDrawing limit . \case
linearAbsorption RewardParams{rFixed,rRatio,rEpochStart,rEpochRate} =
if a > c then 0 else c - a
where
a = r * (n / e)
a = r * fromIntegral (floor @Double @Integer (n / e))
c = fromIntegral rFixed
r = ratio rRatio
n = fromIntegral (ep - rEpochStart)
Expand All @@ -145,7 +146,7 @@ rewardsAt limit tax epochNo = q . taxCut tax . capDrawing limit . \case
halvingAbsorption RewardParams{rFixed,rRatio,rEpochStart,rEpochRate} =
c * a
where
a = r * (n / e)
a = r ** fromIntegral (floor @Double @Integer (n / e))
c = fromIntegral rFixed
r = ratio rRatio
n = fromIntegral (ep - rEpochStart)
Expand Down
@@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}
Expand All @@ -21,7 +22,7 @@ import Cardano.Wallet.Primitive.Types
import Data.Quantity
( Quantity (..) )
import Test.Hspec
( Spec, describe, it )
( Spec, describe, it, shouldBe )
import Test.QuickCheck
( Arbitrary (..), choose, property, (===) )

Expand All @@ -48,5 +49,71 @@ spec = describe "rewardsAt" $ do
rewardsAt drawingLimit treasuryTax epochNo rewardFormula
=== Quantity 3452054796000

describe "Halving, C = 10000, ratio = 1/1, estart=10, rate=1" $ do
let rewardFormula = HalvingFormula $ RewardParams
{ rFixed = 10000
, rEpochRate = 1
, rEpochStart = 10
, rRatio = Ratio 1 1
}
mapM_ (testReward noDrawingLimit noTreasuryTax rewardFormula) $ mconcat
[ [ (e, 0) | e <- [0..9] ]
, [ (10, 10000) ]
, [ (11, 10000) ]
, [ (12, 10000) ]
, [ (13, 10000) ]
, [ (14, 10000) ]
]

describe "Linear, C = 10000, ratio = 1000/1, estart=10, rate=2" $ do
let rewardFormula = LinearFormula $ RewardParams
{ rFixed = 10000
, rEpochRate = 2
, rEpochStart = 10
, rRatio = Ratio 1000 1
}
mapM_ (testReward noDrawingLimit noTreasuryTax rewardFormula) $ mconcat
[ [ (e, 0) | e <- [0..9] ]
, [ (10, 10000) ]
, [ (11, 10000) ]
, [ (12, 9000) ]
, [ (13, 9000) ]
, [ (14, 8000) ]
]

describe "Halving, C = 10000, ratio = 1/2, estart=10, rate=2" $ do
let rewardFormula = HalvingFormula $ RewardParams
{ rFixed = 10000
, rEpochRate = 2
, rEpochStart = 10
, rRatio = Ratio 1 2
}
mapM_ (testReward noDrawingLimit noTreasuryTax rewardFormula) $ mconcat
[ [ (e, 0) | e <- [0..9] ]
, [ (10, 10000) ]
, [ (11, 10000) ]
, [ (12, 5000) ]
, [ (13, 5000) ]
, [ (14, 2500) ]
]
where
testReward drawingLimit treasuryTax rewardFormula (epochNo, reward) =
it title $
rewardsAt drawingLimit treasuryTax epochNo rewardFormula
`shouldBe` Quantity reward
where
title = "epoch #" <> show (unEpochNo epochNo) <> " --> " <> show reward

noDrawingLimit =
( RewardLimitNone
, Quantity 0
)

noTreasuryTax = TaxParameters
{ taxFixed = 0
, taxRatio = Ratio 0 1
, taxLimit = Nothing
}

instance Arbitrary EpochNo where
arbitrary = EpochNo . fromIntegral @Int <$> choose (0, 1000)

0 comments on commit 8ec5ef6

Please sign in to comment.