Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for "active slot coefficient" in apparent performance calculation #1189

Merged
merged 4 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/core/src/Cardano/Pool/Metrics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ import Cardano.Wallet.Network
, staticBlockchainParameters
)
import Cardano.Wallet.Primitive.Types
( BlockHeader (..)
( ActiveSlotCoefficient (..)
, BlockHeader (..)
, EpochLength (..)
, EpochNo (..)
, PoolId (..)
Expand Down Expand Up @@ -304,7 +305,7 @@ newStakePoolLayer tr db@DBLayer{..} nl metadataDir = StakePoolLayer

else do
let sl = prodTip ^. #slotId
perfs <- liftIO $ readPoolsPerformances db epochLength sl
perfs <- liftIO $ readPoolsPerformances db activeSlotCoeff epochLength sl
combineWith (pure . sortByPerformance) distr (count prod) perfs

readPoolProductionTip = readPoolProductionCursor 1 <&> \case
Expand All @@ -331,6 +332,7 @@ newStakePoolLayer tr db@DBLayer{..} nl metadataDir = StakePoolLayer

(block0, bp) = staticBlockchainParameters nl
epochLength = bp ^. #getEpochLength
activeSlotCoeff = bp ^. #getActiveSlotCoefficient

combineWith
:: ([(StakePool, [PoolOwner])] -> IO [(StakePool, [PoolOwner])])
Expand Down Expand Up @@ -401,11 +403,13 @@ readNewcomers DBLayer{..} elders avg = do

readPoolsPerformances
:: DBLayer m
-> ActiveSlotCoefficient
-> EpochLength
-> SlotId
-> m (Map PoolId Double)
readPoolsPerformances DBLayer{..} (EpochLength el) tip = do
readPoolsPerformances DBLayer{..} activeSlotCoeff (EpochLength el) tip = do
atomically $ fmap avg $ forM historicalEpochs $ \ep -> calculatePerformance
activeSlotCoeff
(slotsInEpoch ep)
<$> (Map.fromList <$> readStakeDistribution ep)
<*> (count <$> readPoolProduction ep)
Expand Down Expand Up @@ -442,10 +446,11 @@ readPoolsPerformances DBLayer{..} (EpochLength el) tip = do
-- is a 'Double' between 0 and 1 as:
--
-- @
-- p = n / N * S / s
-- p = n / (f * N) * S / s
-- where
-- n = number of blocks produced in an epoch e
-- N = number of slots in e
-- f = active slot coeff, i.e. % of slots for which a leader can be elected.
-- s = stake owned by the pool in e
-- S = total stake delegated to pools in e
-- @
Expand All @@ -454,19 +459,20 @@ readPoolsPerformances DBLayer{..} (EpochLength el) tip = do
-- practice, be greater than 1 if a stake pool produces more than it is
-- expected.
calculatePerformance
:: Int
:: ActiveSlotCoefficient
-> Int
-> Map PoolId (Quantity "lovelace" Word64)
-> Map PoolId (Quantity "block" Word64)
-> Map PoolId Double
calculatePerformance nTotal mStake mProd =
calculatePerformance (ActiveSlotCoefficient f) nTotal mStake mProd =
let
stakeButNotProd = traverseMissing $ \_ _ -> 0
prodButNoStake = dropMissing
stakeAndProd sTotal = zipWithMatched $ \_ s n ->
if (nTotal == 0 || s == Quantity 0) then
0
else
min 1 ((double n / fromIntegral nTotal) * (sTotal / double s))
min 1 ((double n / (f * fromIntegral nTotal)) * (sTotal / double s))
in
Map.merge
stakeButNotProd
Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,21 @@ import Cardano.Wallet.Primitive.Fee
, computeFee
)
import Cardano.Wallet.Primitive.Model
( BlockchainParameters (..)
, Wallet
( Wallet
, applyBlocks
, availableUTxO
, blockchainParameters
, currentTip
, getState
, initWallet
, slotParams
, updateState
)
import Cardano.Wallet.Primitive.Types
( Address (..)
, AddressState (..)
, Block (..)
, BlockHeader (..)
, BlockchainParameters (..)
, ChimericAccount (..)
, Coin (..)
, DelegationCertificate (..)
Expand Down Expand Up @@ -233,6 +232,7 @@ import Cardano.Wallet.Primitive.Types
, computeUtxoStatistics
, dlgCertPoolId
, log10
, slotParams
, slotRangeFromTimeRange
, slotStartTime
, syncProgressRelativeToTime
Expand Down
3 changes: 1 addition & 2 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ import Cardano.Wallet.Network
( NetworkLayer )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth )
import Cardano.Wallet.Primitive.Model
( BlockchainParameters )
import Cardano.Wallet.Primitive.Types
( AddressState
, Block
, BlockchainParameters
, PoolId
, SortOrder (..)
, SyncTolerance
Expand Down
9 changes: 2 additions & 7 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,12 @@ import Cardano.Wallet.Primitive.CoinSelection
import Cardano.Wallet.Primitive.Fee
( Fee (..) )
import Cardano.Wallet.Primitive.Model
( BlockchainParameters
, Wallet
, availableBalance
, currentTip
, getState
, totalBalance
)
( Wallet, availableBalance, currentTip, getState, totalBalance )
import Cardano.Wallet.Primitive.Types
( Address
, AddressState
, Block
, BlockchainParameters
, ChimericAccount (..)
, Coin (..)
, Hash (..)
Expand Down
4 changes: 4 additions & 0 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ mkCheckpointEntity wid wal =
, checkpointEpochLength = coerce (bp ^. #getEpochLength)
, checkpointTxMaxSize = coerce (bp ^. #getTxMaxSize)
, checkpointEpochStability = coerce (bp ^. #getEpochStability)
, checkpointActiveSlotCoeff =
W.unActiveSlotCoefficient (bp ^. #getActiveSlotCoefficient)
}
utxo =
[ UTxO wid sl (TxId input) ix addr coin
Expand Down Expand Up @@ -589,6 +591,7 @@ checkpointFromEntity cp utxo s =
epochLength
txMaxSize
epochStability
activeSlotCoeff
) = cp
header = (W.BlockHeader slot (Quantity bh) headerHash parentHeaderHash)
utxo' = W.UTxO . Map.fromList $
Expand All @@ -603,6 +606,7 @@ checkpointFromEntity cp utxo s =
, getEpochLength = W.EpochLength epochLength
, getTxMaxSize = Quantity txMaxSize
, getEpochStability = Quantity epochStability
, getActiveSlotCoefficient = W.ActiveSlotCoefficient activeSlotCoeff
}

mkTxHistory
Expand Down
25 changes: 13 additions & 12 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,19 @@ TxOut
-- A checkpoint for a given wallet is referred to by (wallet_id, slot).
-- Volatile checkpoint data such as AD state will refer to this table.
Checkpoint
checkpointWalletId W.WalletId sql=wallet_id
checkpointSlot W.SlotId sql=slot
checkpointHeaderHash BlockId sql=header_hash
checkpointParentHash BlockId sql=parent_header_hash
checkpointBlockHeight Word32 sql=block_height
checkpointGenesisHash BlockId sql=genesis_hash
checkpointGenesisStart UTCTime sql=genesis_start
checkpointFeePolicy W.FeePolicy sql=fee_policy
checkpointSlotLength Word64 sql=slot_length
checkpointEpochLength Word32 sql=epoch_length
checkpointTxMaxSize Word16 sql=tx_max_size
checkpointEpochStability Word32 sql=epoch_stability
checkpointWalletId W.WalletId sql=wallet_id
checkpointSlot W.SlotId sql=slot
checkpointHeaderHash BlockId sql=header_hash
checkpointParentHash BlockId sql=parent_header_hash
checkpointBlockHeight Word32 sql=block_height
checkpointGenesisHash BlockId sql=genesis_hash
checkpointGenesisStart UTCTime sql=genesis_start
checkpointFeePolicy W.FeePolicy sql=fee_policy
checkpointSlotLength Word64 sql=slot_length
checkpointEpochLength Word32 sql=epoch_length
checkpointTxMaxSize Word16 sql=tx_max_size
checkpointEpochStability Word32 sql=epoch_stability
checkpointActiveSlotCoeff Double sql=active_slot_coeff

Primary checkpointWalletId checkpointSlot
Foreign Wallet checkpoint checkpointWalletId ! ON DELETE CASCADE
Expand Down
3 changes: 1 addition & 2 deletions lib/core/src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ import Prelude

import Cardano.BM.Trace
( Trace, logDebug, logError, logInfo, logWarning )
import Cardano.Wallet.Primitive.Model
( BlockchainParameters (..) )
import Cardano.Wallet.Primitive.Types
( BlockHeader (..)
, BlockchainParameters (..)
, ChimericAccount (..)
, EpochNo
, Hash (..)
Expand Down
67 changes: 3 additions & 64 deletions lib/core/src/Cardano/Wallet/Primitive/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ module Cardano.Wallet.Primitive.Model
(
-- * Type
Wallet
, BlockchainParameters (..)

-- * Construction & Modification
, FilteredBlock (..)
, initWallet
, updateState
, FilteredBlock (..)
, applyBlock
, applyBlocks
, unsafeInitWallet
Expand All @@ -53,9 +52,6 @@ module Cardano.Wallet.Primitive.Model
, availableUTxO
, utxo
, blockchainParameters

-- * Auxiliary
, slotParams
) where

import Prelude
Expand All @@ -66,16 +62,11 @@ import Cardano.Wallet.Primitive.Types
( Address (..)
, Block (..)
, BlockHeader (..)
, BlockchainParameters (..)
, ChimericAccount (..)
, DelegationCertificate (..)
, Direction (..)
, Dom (..)
, EpochLength (..)
, FeePolicy (..)
, Hash (..)
, SlotLength (..)
, SlotParameters (SlotParameters)
, StartTime (..)
, Tx (..)
, TxIn (..)
, TxMeta (..)
Expand All @@ -97,8 +88,6 @@ import Control.Monad.Extra
( mapMaybeM )
import Control.Monad.Trans.State.Strict
( State, evalState, runState, state )
import Data.ByteArray.Encoding
( Base (Base16), convertToBase )
import Data.Functor
( (<&>) )
import Data.Generics.Internal.VL.Lens
Expand All @@ -113,12 +102,8 @@ import Data.Quantity
( Quantity (..) )
import Data.Set
( Set )
import Data.Text.Class
( toText )
import Data.Word
( Word16, Word32 )
import Fmt
( Buildable (..), blockListF', indentF )
( Buildable (..), indentF )
import GHC.Generics
( Generic )
import Numeric.Natural
Expand All @@ -127,7 +112,6 @@ import Numeric.Natural
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text.Encoding as T

{-------------------------------------------------------------------------------
Type
Expand Down Expand Up @@ -193,51 +177,6 @@ instance Buildable s => Buildable (Wallet s) where
<> indentF 4 ("UTxO: " <> build u)
<> indentF 4 (build s)

data BlockchainParameters = BlockchainParameters
{ getGenesisBlockHash :: Hash "Genesis"
-- ^ Hash of the very first block
, getGenesisBlockDate :: StartTime
-- ^ Start time of the chain.
, getFeePolicy :: FeePolicy
-- ^ Policy regarding transaction fee.
, getSlotLength :: SlotLength
-- ^ Length, in seconds, of a slot.
, getEpochLength :: EpochLength
-- ^ Number of slots in a single epoch.
, getTxMaxSize :: Quantity "byte" Word16
-- ^ Maximum size of a transaction (soft or hard limit).
, getEpochStability :: Quantity "block" Word32
-- ^ Length of the suffix of the chain considered unstable
} deriving (Generic, Show, Eq)

instance NFData BlockchainParameters

instance Buildable BlockchainParameters where
build bp = blockListF' "" id
[ "Genesis block hash: " <> genesisF (getGenesisBlockHash bp)
, "Genesis block date: " <> startTimeF (getGenesisBlockDate bp)
, "Fee policy: " <> feePolicyF (getFeePolicy bp)
, "Slot length: " <> slotLengthF (getSlotLength bp)
, "Epoch length: " <> epochLengthF (getEpochLength bp)
, "Tx max size: " <> txMaxSizeF (getTxMaxSize bp)
, "Epoch stability: " <> epochStabilityF (getEpochStability bp)
]
where
genesisF = build . T.decodeUtf8 . convertToBase Base16 . getHash
startTimeF (StartTime s) = build s
feePolicyF = build . toText
slotLengthF (SlotLength s) = build s
epochLengthF (EpochLength s) = build s
txMaxSizeF (Quantity s) = build s
epochStabilityF (Quantity s) = build s

slotParams :: BlockchainParameters -> SlotParameters
slotParams bp =
SlotParameters
(bp ^. #getEpochLength)
(bp ^. #getSlotLength)
(bp ^. #getGenesisBlockDate)

{-------------------------------------------------------------------------------
Construction & Modification
-------------------------------------------------------------------------------}
Expand Down
Loading