Skip to content

Commit

Permalink
Refactor format and comment sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix authored and samuelmanzanera committed Jan 6, 2023
1 parent a637672 commit 5dc532c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/archethic/transaction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,10 @@ defmodule Archethic.TransactionChain do
conflict_resolver = fn results ->
results
|> Enum.sort(
# Prioritize more? at true
# then length of transaction list
# then regarding order, the oldest or newest transaction timestamp
# of the first element of the list
&with false <- &1.more? and !&2.more?,
false <- length(&1.transactions) > length(&2.transactions) do
if Enum.empty?(&1.transactions) do
Expand Down
6 changes: 4 additions & 2 deletions lib/archethic_web/controllers/api/transaction_payload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule ArchethicWeb.API.TransactionPayload do

def to_map(%{changes: changes}, acc) do
Enum.reduce(changes, acc, fn {key, value}, acc ->
value = if key == :authorizedKeys, do: format_authorized_keys(value), else: value
value = format_change(key, value)

key = Macro.underscore(Atom.to_string(key))

Expand All @@ -69,7 +69,7 @@ defmodule ArchethicWeb.API.TransactionPayload do

def to_map(value, _), do: value

defp format_authorized_keys(authorized_keys) do
defp format_change(:authorizedKeys, authorized_keys) do
Enum.reduce(authorized_keys, %{}, fn %Ecto.Changeset{
changes: %{
publicKey: public_key,
Expand All @@ -81,6 +81,8 @@ defmodule ArchethicWeb.API.TransactionPayload do
end)
end

defp format_change(_, value), do: value

defp validate_data(changeset = %Ecto.Changeset{}) do
validate_change(changeset, :data, fn _, data_changeset ->
case data_changeset.valid? do
Expand Down

0 comments on commit 5dc532c

Please sign in to comment.