<commit_before>defmodule NeoscanSync.FastSync do
  @moduledoc false

  @moduledoc """

    External process to fetch blockchain from RCP node sync, and print json

  """

  alias NeoscanSync.Blockchain
  alias Neoscan.Pool
  alias NeoscanSync.BlockSync

  @me __MODULE__

  #Starts the application
  def start_link() do
    Agent.start_link(fn -> start() end , name: @me)
  end


  #Start process, create file and get current height from the chain
  def start(n \\ 500) do
    count = Pool.get_highest_block_in_pool()
    fetch_chain(n, count)
  end

  def check_process() do
    alive = Process.whereis(Neoscan.Supervisor)
    |> Process.alive?
    case alive do
      true ->
        true
      false ->
        IO.puts("Main process was killed")
        Process.exit(self(), :shutdown)
    end
  end

  def fetch_chain(n, count) do
    check_process()
    get_current_height()
    |> evaluate(n, count+1)
  end

  #evaluate number of process, current block count, and start async functions
  defp evaluate(result, n, count) do
    case result do
      {:ok, height} when (height-1000) > count  ->
        cond do
          height - 1000 - count >= n ->
            Enum.to_list(count..(count+n-1))
            |> Enum.map(&Task.async(fn -> cross_check(&1) end))
            |> Enum.map(&Task.await(&1, 20000))
            |> Enum.map(fn x -> add_block(x) end)
           fetch_chain(n, count+n-1)
         height - 1000 - count < n ->
          Enum.to_list(count..(height-1000))
          |> Enum.map(&Task.async(fn -> cross_check(&1) end))
          |> Enum.map(&Task.await(&1, 20000))
          |> Enum.map(fn x -> add_block(x) end)
          BlockSync.start()
        end
      {:ok, height} when (height-1000) == count  ->
        BlockSync.start()
      {:ok, height} when (height-1000) < count  ->
        start(n)
    end
  end

  #write block to the file
  def add_block(%{"index" => num} = block) do
    %{"height" => num, "block" => block}
    |> Pool.create_data()
    IO.puts("Block #{num} saved in pool")
  end
<commit_msg>current height<commit_after>defmodule NeoscanSync.FastSync do
  @moduledoc false

  @moduledoc """

    External process to fetch blockchain from RCP node sync, and print json

  """

  alias NeoscanSync.Blockchain
  alias Neoscan.Pool
  alias NeoscanSync.BlockSync

  @me __MODULE__

  #Starts the application
  def start_link() do
    Agent.start_link(fn -> start() end , name: @me)
  end


  #Start process, create file and get current height from the chain
  def start(n \\ 500) do
    count = Pool.get_highest_block_in_pool()
    fetch_chain(n, count)
  end

  def check_process() do
    alive = Process.whereis(Neoscan.Supervisor)
    |> Process.alive?
    case alive do
      true ->
        true
      false ->
        IO.puts("Main process was killed")
        Process.exit(self(), :shutdown)
    end
  end

  def fetch_chain(n, count) do
    check_process()
    get_current_height()
    |> evaluate(n, count+1)
  end

  #evaluate number of process, current block count, and start async functions
  defp evaluate(result, n, count) do
    case result do
      {:ok, height} when (height) > count  ->
        cond do
          height  - count >= n ->
            Enum.to_list(count..(count+n-1))
            |> Enum.map(&Task.async(fn -> cross_check(&1) end))
            |> Enum.map(&Task.await(&1, 20000))
            |> Enum.map(fn x -> add_block(x) end)
           fetch_chain(n, count+n-1)
         height  - count < n ->
          Enum.to_list(count..(height))
          |> Enum.map(&Task.async(fn -> cross_check(&1) end))
          |> Enum.map(&Task.await(&1, 20000))
          |> Enum.map(fn x -> add_block(x) end)
          BlockSync.start()
        end
      {:ok, height} when (height) == count  ->
        BlockSync.start()
      {:ok, height} when (height) < count  ->
        start(n)
    end
  end

  #write block to the file
  def add_block(%{"index" => num} = block) do
    %{"height" => num, "block" => block}
    |> Pool.create_data()
    IO.puts("Block #{num} saved in pool")
  end

{'fraction_non_alphanumeric': 0.12990861941220055, 'fraction_numerical': 0.0158063719436898, 'mean_word_length': 2.314238952536825, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1083785', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1317, 'n_words': 359}