Skip to content

Commit

Permalink
Up version 1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Jan 3, 2025
1 parent 7182439 commit 5ddcec4
Show file tree
Hide file tree
Showing 5 changed files with 855 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/archethic/contracts/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Archethic.Contracts.Worker do
require Logger

use GenStateMachine, callback_mode: :handle_event_function
@vsn 2
@vsn 3

@schedule_trigger {:next_event, :internal, :start_schedulers}
@process_trigger {:next_event, :internal, :process_next_trigger}
Expand Down Expand Up @@ -244,6 +244,15 @@ defmodule Archethic.Contracts.Worker do
{:keep_state, new_data, @schedule_and_process_trigger}
end

def code_change(2, state, data, _extra) do
new_data =
Map.update!(data, :contract, fn %{transaction: contract_tx} ->
InterpretedContract.from_transaction!(contract_tx)
end)

{:ok, state, new_data}
end

def code_change(_old_vsn, state, data, _extra), do: {:ok, state, data}

# ----------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Archethic.MixProject do
def project do
[
app: :archethic,
version: "1.5.14",
version: "1.6.0",
build_path: "_build",
config_path: "config/config.exs",
deps_path: "deps",
Expand Down
78 changes: 78 additions & 0 deletions priv/migration_tasks/prod/1.6.0@summary_cache.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
defmodule Migration_1_6_0 do
@moduledoc false

alias Archethic.BeaconChain
alias Archethic.BeaconChain.Slot
alias Archethic.BeaconChain.Subset.SummaryCache
alias Archethic.BeaconChain.Subset.SummaryCacheSupervisor

alias Archethic.Utils
alias Archethic.Utils.VarInt

require Logger

def run() do
summary_date = BeaconChain.next_summary_date(DateTime.utc_now())

current_summary_path = Utils.mut_dir("slot_backup-#{DateTime.to_unix(summary_date)}")

if File.exists?(current_summary_path) do
Logger.info("Migration 1.6.0 starting slot migration")

processes_pid = start_processes()

current_summary_path
|> File.read!()
|> deserialize()
|> tap(fn slots -> Logger.info("Migration 1.6.0 #{length(slots)} slots to migrate}") end)
|> Task.async_stream(fn {slot, key} -> SummaryCache.add_slot(slot, key) end)
|> Stream.run()

maybe_stop_processes(processes_pid)

Logger.info("Migration 1.6.0 removing old slot_backup")
Utils.mut_dir("slot_backup-*") |> Path.wildcard() |> Enum.each(&File.rm/1)
else
Logger.info("Migration 1.6.0 no slot to migrate")
end
end

defp start_processes() do
{:ok, pubsub_pid} =
case Process.whereis(Archethic.PubSubRegistry) do
nil -> Registry.start_link(keys: :duplicate, name: Archethic.PubSubRegistry)
_ -> {:ok, nil}
end

{:ok, supervisor_pid} =
case Process.whereis(SummaryCacheSupervisor) do
nil ->
PartitionSupervisor.start_link(
child_spec: SummaryCache,
name: SummaryCacheSupervisor,
partitions: 64
)

_ ->
{:ok, nil}
end

{pubsub_pid, supervisor_pid}
end

defp maybe_stop_processes({pubsub_pid, supervisor_pid}) do
if is_pid(supervisor_pid), do: Supervisor.stop(supervisor_pid)
if is_pid(pubsub_pid), do: Supervisor.stop(pubsub_pid)
end

defp deserialize(rest, acc \\ [])
defp deserialize(<<>>, acc), do: acc

defp deserialize(rest, acc) do
{slot_size, rest} = VarInt.get_value(rest)
<<slot_bin::binary-size(slot_size), rest::binary>> = rest
{slot, _} = Slot.deserialize(slot_bin)
{node_public_key, rest} = Utils.deserialize_public_key(rest)
deserialize(rest, [{slot, node_public_key} | acc])
end
end
Loading

0 comments on commit 5ddcec4

Please sign in to comment.