From 9ee7ab64f58e6bcd4c751986983a3022efd9466f Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Thu, 12 Jul 2018 14:48:15 -0500 Subject: [PATCH] Fix chunk_range/3 for descending ranges --- apps/indexer/lib/indexer/block_fetcher.ex | 34 +++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/apps/indexer/lib/indexer/block_fetcher.ex b/apps/indexer/lib/indexer/block_fetcher.ex index 663a05c35d..6b8c14a5e4 100644 --- a/apps/indexer/lib/indexer/block_fetcher.ex +++ b/apps/indexer/lib/indexer/block_fetcher.ex @@ -288,20 +288,38 @@ defmodule Indexer.BlockFetcher do [range] end - defp chunk_range(range, _, size) do - first..last = range - + defp chunk_range(first..last, _, size) when first < last do first |> Stream.iterate(&(&1 + size)) |> Enum.reduce_while([], fn chunk_first, acc -> next_chunk_first = chunk_first + size full_chunk_last = next_chunk_first - 1 - {action, chunk_last} = if full_chunk_last >= last do - {:halt, last} - else - {:cont, full_chunk_last} - end + {action, chunk_last} = + if full_chunk_last >= last do + {:halt, last} + else + {:cont, full_chunk_last} + end + + {action, [chunk_first..chunk_last | acc]} + end) + |> Enum.reverse() + end + + defp chunk_range(first..last, _, size) when last < first do + first + |> Stream.iterate(&(&1 - size)) + |> Enum.reduce_while([], fn chunk_first, acc -> + next_chunk_first = chunk_first - size + full_chunk_last = next_chunk_first + 1 + + {action, chunk_last} = + if full_chunk_last <= last do + {:halt, last} + else + {:cont, full_chunk_last} + end {action, [chunk_first..chunk_last | acc]} end)