From 0f08cccf7d872a67f4b62770cf1a8e0f9b4402ca Mon Sep 17 00:00:00 2001 From: Alexander Diemand Date: Wed, 19 Sep 2018 16:15:21 +0200 Subject: [PATCH] [CBR-345] clean formatting of slots/epoch indexes Signed-off-by: Alexander Diemand --- chain/src/Pos/Chain/Ssc/Error/Verify.hs | 7 ++++--- chain/src/Pos/Chain/Ssc/Toss/Base.hs | 13 +++++++------ core/src/Pos/Core/Slotting/SlotId.hs | 23 ++--------------------- db/src/Pos/DB/Block/Lrc.hs | 8 ++++---- lib/src/Pos/Worker/Ssc.hs | 8 ++++---- util/src/Pos/Util/Util.hs | 20 ++++++++++++++++++++ 6 files changed, 41 insertions(+), 38 deletions(-) diff --git a/chain/src/Pos/Chain/Ssc/Error/Verify.hs b/chain/src/Pos/Chain/Ssc/Error/Verify.hs index f4e4d8ad687..29751b87148 100644 --- a/chain/src/Pos/Chain/Ssc/Error/Verify.hs +++ b/chain/src/Pos/Chain/Ssc/Error/Verify.hs @@ -7,12 +7,13 @@ module Pos.Chain.Ssc.Error.Verify import Universum -import Formatting (bprint, build, ords, stext, (%)) +import Formatting (bprint, build, stext, (%)) import qualified Formatting.Buildable import Serokell.Util (listJson) import Pos.Chain.Ssc.VssCertificate (VssCertificate) -import Pos.Core (EpochIndex, SlotId, StakeholderId) +import Pos.Core (EpochIndex (..), SlotId, StakeholderId) +import Pos.Util.Util (intords) type NEStIds = NonEmpty StakeholderId @@ -66,7 +67,7 @@ instance Buildable SscVerifyError where build (NoRichmen epoch) = bprint ("no richmen for epoch"%build) epoch build (TossUnknownRichmen epoch) = - bprint ("richmen aren't know for "%ords%" epoch") epoch + bprint ("richmen aren't know for "%intords%" epoch") (getEpochIndex epoch) build (CommitmentInvalid ids) = bprint ("verifySignedCommitment has failed for some commitments: "%listJson) ids diff --git a/chain/src/Pos/Chain/Ssc/Toss/Base.hs b/chain/src/Pos/Chain/Ssc/Toss/Base.hs index c9bb7df508d..9652f2e896c 100644 --- a/chain/src/Pos/Chain/Ssc/Toss/Base.hs +++ b/chain/src/Pos/Chain/Ssc/Toss/Base.hs @@ -44,7 +44,7 @@ import qualified Data.HashMap.Strict as HM import qualified Data.HashSet as HS import qualified Data.List.NonEmpty as NE import Data.STRef (newSTRef, readSTRef, writeSTRef) -import Formatting (ords, sformat, (%)) +import Formatting (sformat, (%)) import Pos.Binary.Class (AsBinary, fromBinary) import Pos.Chain.Genesis as Genesis (Config) @@ -66,10 +66,11 @@ import Pos.Chain.Ssc.VssCertificate (vcSigningKey, vcVssKey) import Pos.Chain.Ssc.VssCertificatesMap (VssCertificatesMap (..), lookupVss, memberVss) import Pos.Chain.Update.BlockVersionData (bvdMpcThd) -import Pos.Core (CoinPortion, EpochIndex, StakeholderId, addressHash, - coinPortionDenominator, getCoinPortion, unsafeGetCoin) +import Pos.Core (CoinPortion, EpochIndex (..), StakeholderId, + addressHash, coinPortionDenominator, getCoinPortion, + unsafeGetCoin) import Pos.Crypto (DecShare, verifyDecShare, verifyEncShares) -import Pos.Util.Util (getKeys) +import Pos.Util.Util (getKeys, intords) import Pos.Util.Wlog (logWarning) ---------------------------------------------------------------------------- @@ -131,9 +132,9 @@ checkShares -> m Bool checkShares genesisConfig epoch (id, sh) = do certs <- getStableCertificates genesisConfig epoch - let warnFmt = ("checkShares: no richmen for "%ords%" epoch") + let warnFmt = ("checkShares: no richmen for "%intords%" epoch") getRichmen epoch >>= \case - Nothing -> False <$ logWarning (sformat warnFmt epoch) + Nothing -> False <$ logWarning (sformat warnFmt (getEpochIndex epoch)) Just richmen -> do let parts = computeParticipants (getKeys richmen) certs coms <- getCommitments diff --git a/core/src/Pos/Core/Slotting/SlotId.hs b/core/src/Pos/Core/Slotting/SlotId.hs index fd21dd380c3..80816e8bd97 100644 --- a/core/src/Pos/Core/Slotting/SlotId.hs +++ b/core/src/Pos/Core/Slotting/SlotId.hs @@ -26,15 +26,13 @@ import Universum import Control.Lens (Iso', iso, lens, makeLensesFor) import Data.Aeson.TH (defaultOptions, deriveJSON) import Data.SafeCopy (base, deriveSafeCopySimple) -import Data.Text (pack) -import Data.Text.Lazy.Builder (fromText) -import Formatting (Format, bprint, build, later, (%)) +import Formatting (Format, bprint, build, (%)) import qualified Formatting.Buildable as Buildable import Pos.Binary.Class (Cons (..), Field (..), deriveSimpleBi) import Pos.Core.Common (BlockCount) import Pos.Core.ProtocolConstants (kEpochSlots, kSlotSecurityParam) -import Pos.Util.Util (leftToPanic) +import Pos.Util.Util (intords, leftToPanic) import Pos.Core.Slotting.EpochIndex import Pos.Core.Slotting.LocalSlotIndex @@ -53,23 +51,6 @@ instance Buildable SlotId where bprint (intords%" slot of "%intords%" epoch") (getSlotIndex siSlot) (getEpochIndex siEpoch) --- | temporary reimplementation of 'ords' from "Formatting" --- because the original function converts the integer value to a real number -intords :: (Show n, Integral n) => Format r (n -> r) -intords = later go - where - fmt = fromText . pack . show - go n - | tens > 3 && tens < 21 = fmt n <> "th" - | otherwise = - fmt n <> - case n `mod` 10 of - 1 -> "st" - 2 -> "nd" - 3 -> "rd" - _ -> "th" - where tens = n `mod` 100 - instance NFData SlotId deriveJSON defaultOptions ''SlotId diff --git a/db/src/Pos/DB/Block/Lrc.hs b/db/src/Pos/DB/Block/Lrc.hs index 0f139c1d0fd..880ae0c62fa 100644 --- a/db/src/Pos/DB/Block/Lrc.hs +++ b/db/src/Pos/DB/Block/Lrc.hs @@ -19,7 +19,7 @@ import Data.Coerce (coerce) import Data.Conduit (ConduitT, runConduitRes, (.|)) import qualified Data.HashMap.Strict as HM import qualified Data.HashSet as HS -import Formatting (build, ords, sformat, (%)) +import Formatting (build, sformat, (%)) import qualified System.Metrics.Counter as Metrics import UnliftIO (MonadUnliftIO) @@ -31,7 +31,7 @@ import Pos.Chain.Lrc (LrcError (..), RichmenStakes, followTheSatoshiM) import Pos.Chain.Ssc (MonadSscMem, noReportNoSecretsForEpoch1) import Pos.Chain.Update (BlockVersionState (..)) -import Pos.Core (Coin, EpochIndex, EpochOrSlot (..), SharedSeed, +import Pos.Core (Coin, EpochIndex (..), EpochOrSlot (..), SharedSeed, SlotCount, StakeholderId, crucialSlot, epochIndexL, getEpochOrSlot) import Pos.Core.Chrono (NE, NewestFirst (..), toOldestFirst) @@ -56,7 +56,7 @@ import Pos.DB.Ssc (sscCalculateSeed) import qualified Pos.DB.Txp.Stakes as GS import Pos.DB.Update (getCompetingBVStates) import Pos.Util (maybeThrow) -import Pos.Util.Util (HasLens (..)) +import Pos.Util.Util (HasLens (..), intords) import Pos.Util.Wlog (logDebug, logInfo, logWarning) @@ -90,7 +90,7 @@ lrcSingleShot genesisConfig epoch = do tryAcquireExclusiveLock epoch lock onAcquiredLock where consumers = allLrcConsumers @ctx @m (configBlockVersionData genesisConfig) - for_thEpochMsg = sformat (" for "%ords%" epoch") epoch + for_thEpochMsg = sformat (" for "%intords%" epoch") (getEpochIndex epoch) onAcquiredLock = do logDebug "lrcSingleShot has acquired LRC lock" (need, filteredConsumers) <- diff --git a/lib/src/Pos/Worker/Ssc.hs b/lib/src/Pos/Worker/Ssc.hs index 98fa9de09cd..47c35eda075 100644 --- a/lib/src/Pos/Worker/Ssc.hs +++ b/lib/src/Pos/Worker/Ssc.hs @@ -13,7 +13,7 @@ import Control.Monad.Except (runExceptT) import qualified Data.HashMap.Strict as HM import qualified Data.List.NonEmpty as NE import Data.Time.Units (Microsecond, Millisecond, convertUnit) -import Formatting (build, ords, sformat, shown, (%)) +import Formatting (build, sformat, shown, (%)) import Serokell.Util.Exceptions () import Serokell.Util.Text (listJson) import qualified System.Metrics.Gauge as Metrics @@ -66,7 +66,7 @@ import Pos.Infra.Slotting (MonadSlots, defaultOnNewSlotParams, import Pos.Infra.Util.LogSafe (logDebugS, logErrorS, logInfoS, logWarningS) import Pos.Util.AssertMode (inAssertMode) -import Pos.Util.Util (HasLens (..), getKeys, leftToPanic) +import Pos.Util.Util (HasLens (..), getKeys, intords, leftToPanic) import Pos.Util.Wlog (WithLogger) @@ -219,12 +219,12 @@ onNewSlotCommitment genesisConfig slotId@SlotId {..} sendCommitment onNewSlotCommDo = do ourSk <- getOurSecretKey - logDebugS $ sformat ("Generating secret for "%ords%" epoch") siEpoch + logDebugS $ sformat ("Generating secret for "%intords%" epoch") siEpoch generated <- generateAndSetNewSecret genesisConfig ourSk slotId case generated of Nothing -> logWarningS "I failed to generate secret for SSC" Just comm -> do - logInfoS (sformat ("Generated secret for "%ords%" epoch") siEpoch) + logInfoS (sformat ("Generated secret for "%intords%" epoch") siEpoch) sendOurCommitment comm sendOurCommitment comm = do diff --git a/util/src/Pos/Util/Util.hs b/util/src/Pos/Util/Util.hs index 370a39fe794..a6aed0fef38 100644 --- a/util/src/Pos/Util/Util.hs +++ b/util/src/Pos/Util/Util.hs @@ -70,6 +70,7 @@ module Pos.Util.Util , () , divRoundUp , sleep + , intords , tMeasureLog , tMeasureIO @@ -96,6 +97,8 @@ import qualified Data.Map as M import Data.Ratio ((%)) import qualified Data.Semigroup as Smg import qualified Data.Serialize as Cereal +import Data.Text (pack) +import Data.Text.Lazy.Builder (fromText) import Data.Time.Clock (NominalDiffTime, UTCTime, diffUTCTime, getCurrentTime) import Data.Time.Clock.POSIX (getPOSIXTime, posixSecondsToUTCTime) @@ -434,6 +437,23 @@ median l = NE.sort l NE.!! middle sleep :: MonadIO m => NominalDiffTime -> m () sleep n = liftIO (threadDelay (truncate (n * 10^(6::Int)))) +-- | temporary reimplementation of 'ords' from "Formatting" +-- because the original function converts the integer value to a real number +intords :: (Show n, Integral n) => F.Format r (n -> r) +intords = F.later go + where + fmt = fromText . pack . show + go n + | tens > 3 && tens < 21 = fmt n <> "th" + | otherwise = + fmt n <> + case n `mod` 10 of + 1 -> "st" + 2 -> "nd" + 3 -> "rd" + _ -> "th" + where tens = n `mod` 100 + -- | 'tMeasure' with 'logDebug'. tMeasureLog :: (MonadIO m, WithLogger m) => Text -> m a -> m a tMeasureLog label = fmap fst . tMeasure logDebug label