diff --git a/haskell-src/exec/Chainweb/RichList.hs b/haskell-src/exec/Chainweb/RichList.hs index 618c118..18d6309 100644 --- a/haskell-src/exec/Chainweb/RichList.hs +++ b/haskell-src/exec/Chainweb/RichList.hs @@ -70,7 +70,7 @@ richList logger fp (ChainwebVersion version) = do (fdl, cdl) = foldMap go files chains = cdl [] isConsecutive = all (\(x,y) -> succ x == y) - . (zip <*> tail) + . (zip <*> (drop 1)) . sort unless (isConsecutive chains) $ ioError $ userError diff --git a/haskell-src/lib/ChainwebData/Backfill.hs b/haskell-src/lib/ChainwebData/Backfill.hs index 774b5e7..d849f4c 100644 --- a/haskell-src/lib/ChainwebData/Backfill.hs +++ b/haskell-src/lib/ChainwebData/Backfill.hs @@ -14,6 +14,7 @@ import Data.Maybe (mapMaybe) import Chainweb.Api.ChainId (ChainId(..)) import ChainwebData.Genesis import ChainwebData.Types +import qualified Data.List.NonEmpty as NE lookupPlan :: GenesisInfo -> Map ChainId Int -> [(ChainId, Low, High)] @@ -30,7 +31,7 @@ lookupPlan gi = M.foldrWithKey go [] -- calculate 100-entry batches using min blockheight @cmin@ -- and genesis height -- - ranges = map (Low . last &&& High . head) $ + ranges = map (Low . NE.last &&& High . NE.head) $ groupsOf blockRequestSize [cmin - 1, cmin - 2 .. genesis] -- calculate high water entry against minimum block height for cid @@ -63,7 +64,7 @@ oldLookupPlan mins = concatMap (\pair -> mapMaybe (g pair) asList) ranges asList = map (second (\n -> High . max 0 $ n - 1)) $ M.toList mins ranges :: [(Low, High)] - ranges = map (Low . last &&& High . head) $ groupsOf blockRequestSize [maxi, maxi-1 .. 0] + ranges = map (Low . NE.last &&& High . NE.head) $ groupsOf blockRequestSize [maxi, maxi-1 .. 0] g :: (Low, High) -> (ChainId, High) -> Maybe (ChainId, Low, High) g (l@(Low l'), u) (cid, mx@(High mx')) diff --git a/haskell-src/lib/ChainwebData/Types.hs b/haskell-src/lib/ChainwebData/Types.hs index afe5085..8bc915d 100644 --- a/haskell-src/lib/ChainwebData/Types.hs +++ b/haskell-src/lib/ChainwebData/Types.hs @@ -35,6 +35,7 @@ import Data.Time.Clock.POSIX (posixSecondsToUTCTime) import Control.Lens import Data.Aeson.Lens import Network.Wai.EventSource.Streaming +import Data.List.NonEmpty (nonEmpty) --- @@ -72,7 +73,7 @@ hashToDbHash = DbHash . hashB64U -- | Break a list into groups of @n@ elements. The last item in the result is -- not guaranteed to have the same length as the others. -groupsOf :: Int -> [a] -> [[a]] +groupsOf :: Int -> [a] -> [NonEmpty a] groupsOf n as | n <= 0 = [] | otherwise = go as @@ -80,7 +81,9 @@ groupsOf n as go [] = [] go bs = xs : go rest where - (xs, rest) = splitAt n bs + -- Since n > 0, _xs is never Empty, fromJust will never throw an error + xs = (fromJust . nonEmpty) _xs + (_xs, rest) = splitAt n bs newtype Low = Low Int deriving newtype (Eq, Ord, Show, Num)