Skip to content

Commit

Permalink
Fix transaction movement type mapping
Browse files Browse the repository at this point in the history
Renaming `from_map` to `cast` remove the confusion with the function
`to_map` which is used by the api while the `cast` is used by the db to
convert map to struct.
  • Loading branch information
Samuel authored and Samuel committed Aug 9, 2022
1 parent b421cf3 commit 390e75b
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 68 deletions.
4 changes: 2 additions & 2 deletions lib/archethic/beacon_chain/slot/end_of_node_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ defmodule Archethic.BeaconChain.Slot.EndOfNodeSync do
}
end

@spec from_map(map()) :: t()
def from_map(%{public_key: public_key, timestamp: timestamp}) do
@spec cast(map()) :: t()
def cast(%{public_key: public_key, timestamp: timestamp}) do
%__MODULE__{
public_key: public_key,
timestamp: timestamp
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/db/embedded_impl/chain_reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Archethic.DB.EmbeddedImpl.ChainReader do
Encoding.decode(version, column, data, acc)
end)
|> Utils.atomize_keys()
|> Transaction.from_map()
|> Transaction.cast()

:file.close(fd)

Expand Down Expand Up @@ -153,7 +153,7 @@ defmodule Archethic.DB.EmbeddedImpl.ChainReader do
Encoding.decode(version, column, data, acc)
end)
|> Utils.atomize_keys()
|> Transaction.from_map()
|> Transaction.cast()

if tx.address == limit_address do
{Enum.reverse([tx | acc]), false, nil}
Expand Down
10 changes: 5 additions & 5 deletions lib/archethic/transaction_chain/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,8 @@ defmodule Archethic.TransactionChain.Transaction do
}
end

@spec from_map(map()) :: t()
def from_map(tx = %{}) do
@spec cast(map()) :: t()
def cast(tx = %{}) do
type =
case Map.get(tx, :type) do
nil ->
Expand All @@ -841,14 +841,14 @@ defmodule Archethic.TransactionChain.Transaction do
version: Map.get(tx, :version),
address: Map.get(tx, :address),
type: type,
data: Map.get(tx, :data, %TransactionData{}) |> TransactionData.from_map(),
data: Map.get(tx, :data, %TransactionData{}) |> TransactionData.cast(),
previous_public_key: Map.get(tx, :previous_public_key),
previous_signature: Map.get(tx, :previous_signature),
origin_signature: Map.get(tx, :origin_signature),
validation_stamp: Map.get(tx, :validation_stamp) |> ValidationStamp.from_map(),
validation_stamp: Map.get(tx, :validation_stamp) |> ValidationStamp.cast(),
cross_validation_stamps:
(Map.get(tx, :cross_validation_stamps) || [])
|> Enum.map(&CrossValidationStamp.from_map/1)
|> Enum.map(&CrossValidationStamp.cast/1)
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ defmodule Archethic.TransactionChain.Transaction.CrossValidationStamp do
defp do_reduce_inconsistencies(<<8::8, rest::bitstring>>), do: {:node_movements, rest}
defp do_reduce_inconsistencies(<<9::8, rest::bitstring>>), do: {:errors, rest}

@spec from_map(map()) :: t()
def from_map(stamp = %{}) do
@spec cast(map()) :: t()
def cast(stamp = %{}) do
%__MODULE__{
node_public_key: Map.get(stamp, :node_public_key),
signature: Map.get(stamp, :signature),
Expand Down
8 changes: 4 additions & 4 deletions lib/archethic/transaction_chain/transaction/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ defmodule Archethic.TransactionChain.TransactionData do
reduce_recipients(rest, nb_recipients, [recipient_address | acc])
end

@spec from_map(map()) :: t()
def from_map(data = %{}) do
@spec cast(map()) :: t()
def cast(data = %{}) do
%__MODULE__{
content: Map.get(data, :content, ""),
code: Map.get(data, :code, ""),
ledger: Map.get(data, :ledger, %Ledger{}) |> Ledger.from_map(),
ownerships: Map.get(data, :ownerships, []) |> Enum.map(&Ownership.from_map/1),
ledger: Map.get(data, :ledger, %Ledger{}) |> Ledger.cast(),
ownerships: Map.get(data, :ownerships, []) |> Enum.map(&Ownership.cast/1),
recipients: Map.get(data, :recipients, [])
}
end
Expand Down
8 changes: 4 additions & 4 deletions lib/archethic/transaction_chain/transaction/data/ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ defmodule Archethic.TransactionChain.TransactionData.Ledger do
}
end

@spec from_map(map()) :: t()
def from_map(ledger = %{}) do
@spec cast(map()) :: t()
def cast(ledger = %{}) do
%__MODULE__{
uco: Map.get(ledger, :uco, %UCOLedger{}) |> UCOLedger.from_map(),
token: Map.get(ledger, :token, %TokenLedger{}) |> TokenLedger.from_map()
uco: Map.get(ledger, :uco, %UCOLedger{}) |> UCOLedger.cast(),
token: Map.get(ledger, :token, %TokenLedger{}) |> TokenLedger.cast()
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger do
do_reduce_transfers(rest, nb_transfers, [transfer | acc])
end

@spec from_map(map()) :: t()
def from_map(token_ledger = %{}) do
@spec cast(map()) :: t()
def cast(token_ledger = %{}) do
%__MODULE__{
transfers: Map.get(token_ledger, :transfers, []) |> Enum.map(&Transfer.from_map/1)
transfers: Map.get(token_ledger, :transfers, []) |> Enum.map(&Transfer.cast/1)
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger.Transfer do
...> amount: 1_050_000_000,
...> token_id: 0
...> }
...> |> Transfer.from_map()
...> |> Transfer.cast()
%Transfer{
token: <<0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74,
197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175>>,
Expand All @@ -118,8 +118,8 @@ defmodule Archethic.TransactionChain.TransactionData.TokenLedger.Transfer do
token_id: 0
}
"""
@spec from_map(map()) :: t()
def from_map(transfer = %{}) do
@spec cast(map()) :: t()
def cast(transfer = %{}) do
%__MODULE__{
token: Map.get(transfer, :token),
to: Map.get(transfer, :to),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger do
do_reduce_transfers(rest, nb_transfers, [transfer | acc])
end

@spec from_map(map()) :: t()
def from_map(uco_ledger = %{}) do
@spec cast(map()) :: t()
def cast(uco_ledger = %{}) do
%__MODULE__{
transfers: Map.get(uco_ledger, :transfers, []) |> Enum.map(&Transfer.from_map/1)
transfers: Map.get(uco_ledger, :transfers, []) |> Enum.map(&Transfer.cast/1)
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ defmodule Archethic.TransactionChain.TransactionData.UCOLedger.Transfer do
}
end

@spec from_map(map()) :: t()
def from_map(transfer = %{}) do
@spec cast(map()) :: t()
def cast(transfer = %{}) do
%__MODULE__{
to: Map.get(transfer, :to),
amount: Map.get(transfer, :amount)
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/transaction_chain/transaction/data/ownership.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ defmodule Archethic.TransactionChain.TransactionData.Ownership do
)
end

@spec from_map(map()) :: t()
def from_map(ownership = %{}) do
@spec cast(map()) :: t()
def cast(ownership = %{}) do
%__MODULE__{
secret: Map.get(ownership, :secret, <<>>),
authorized_keys: Map.get(ownership, :authorized_keys, %{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,22 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp do
}
end

@spec from_map(map()) :: __MODULE__.t()
def from_map(stamp = %{}) do
@spec cast(map()) :: __MODULE__.t()
def cast(stamp = %{}) do
%__MODULE__{
timestamp: Map.get(stamp, :timestamp),
proof_of_work: Map.get(stamp, :proof_of_work),
proof_of_integrity: Map.get(stamp, :proof_of_integrity),
proof_of_election: Map.get(stamp, :proof_of_election),
ledger_operations:
Map.get(stamp, :ledger_operations, %LedgerOperations{}) |> LedgerOperations.from_map(),
Map.get(stamp, :ledger_operations, %LedgerOperations{}) |> LedgerOperations.cast(),
recipients: Map.get(stamp, :recipients, []),
signature: Map.get(stamp, :signature),
errors: Map.get(stamp, :errors, [])
}
end

def from_map(nil), do: nil
def cast(nil), do: nil

@spec to_map(__MODULE__.t()) :: map()
def to_map(%__MODULE__{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,15 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
reduce_unspent_outputs(rest, nb, [unspent_output | acc])
end

@spec from_map(map()) :: t()
def from_map(ledger_ops = %{}) do
@spec cast(map()) :: t()
def cast(ledger_ops = %{}) do
%__MODULE__{
transaction_movements:
Map.get(ledger_ops, :transaction_movements, [])
|> Enum.map(&TransactionMovement.from_map/1),
|> Enum.map(&TransactionMovement.cast/1),
unspent_outputs:
Map.get(ledger_ops, :unspent_outputs, [])
|> Enum.map(&UnspentOutput.from_map/1),
|> Enum.map(&UnspentOutput.cast/1),
fee: Map.get(ledger_ops, :fee)
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
iex> %{
...> to: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194, 159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
...> amount: 30_000_000,
...> type: "UCO"
...> type: :UCO
...> }
...> |> TransactionMovement.from_map()
...> |> TransactionMovement.cast()
%TransactionMovement{
to: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194, 159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
amount: 30_000_000,
Expand All @@ -142,11 +142,10 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
...> to: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68,
...> 194,159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
...> amount: 30_000_000,
...> type: "Token", token_address: <<0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92,
...> 122, 206, 185, 71, 140, 74, 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215,
...> 143, 175>>, token_id: 0
...> type: {:token, <<0, 0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71,
...> 140, 74, 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175>>, 0}
...> }
...> |> TransactionMovement.from_map()
...> |> TransactionMovement.cast()
%TransactionMovement{
to: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194,
159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
Expand All @@ -156,20 +155,13 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
}
"""
@spec from_map(map()) :: t()
def from_map(movement = %{}) do
res = %__MODULE__{
@spec cast(map()) :: t()
def cast(movement = %{}) do
%__MODULE__{
to: Map.get(movement, :to),
amount: Map.get(movement, :amount)
amount: Map.get(movement, :amount),
type: Map.get(movement, :type)
}

case Map.get(movement, :type) do
"Token" ->
%{res | type: {:token, Map.get(movement, :token_address), Map.get(movement, :token_id)}}

_ ->
%{res | type: :UCO}
end
end

@doc """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
...> 159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
...> amount: 1_050_000_000,
...> type: :UCO
...> } |> UnspentOutput.from_map()
...> } |> UnspentOutput.cast()
%UnspentOutput{
from: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194,159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
amount: 1_050_000_000,
Expand All @@ -147,7 +147,7 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
...> amount: 1_050_000_000,
...> type: {:token, <<0, 49, 101, 72, 154, 152, 3, 174, 47, 2, 35, 7, 92, 122, 206, 185, 71, 140, 74,
...> 197, 46, 99, 117, 89, 96, 100, 20, 0, 34, 181, 215, 143, 175>>, 0}
...> } |> UnspentOutput.from_map()
...> } |> UnspentOutput.cast()
%UnspentOutput{
from: <<0, 0, 214, 107, 17, 107, 227, 11, 17, 43, 204, 48, 78, 129, 145, 126, 45, 68, 194,159, 19, 92, 240, 29, 37, 105, 183, 232, 56, 42, 163, 236, 251, 186>>,
amount: 1_050_000_000,
Expand All @@ -156,8 +156,8 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
timestamp: nil
}
"""
@spec from_map(map()) :: __MODULE__.t()
def from_map(unspent_output = %{}) do
@spec cast(map()) :: __MODULE__.t()
def cast(unspent_output = %{}) do
%__MODULE__{
from: Map.get(unspent_output, :from),
amount: Map.get(unspent_output, :amount),
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/transaction_chain/transaction_input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ defmodule Archethic.TransactionChain.TransactionInput do
end
end

@spec from_map(map()) :: __MODULE__.t()
def from_map(input = %{}) do
@spec cast(map()) :: __MODULE__.t()
def cast(input = %{}) do
res = %__MODULE__{
amount: Map.get(input, :amount),
from: Map.get(input, :from),
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic/transaction_chain/transaction_summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ defmodule Archethic.TransactionChain.TransactionSummary do
}
end

@spec from_map(map()) :: t()
def from_map(%{
@spec cast(map()) :: t()
def cast(%{
address: address,
timestamp: timestamp,
type: type,
Expand Down
4 changes: 2 additions & 2 deletions lib/archethic_web/controllers/api/transaction_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule ArchethicWeb.API.TransactionController do
tx =
changeset
|> TransactionPayload.to_map()
|> Transaction.from_map()
|> Transaction.cast()

case Archethic.send_new_transaction(tx) do
:ok ->
Expand Down Expand Up @@ -99,7 +99,7 @@ defmodule ArchethicWeb.API.TransactionController do
fee =
changeset
|> TransactionPayload.to_map()
|> Transaction.from_map()
|> Transaction.cast()
|> Mining.get_transaction_fee(uco_usd)

conn
Expand Down

0 comments on commit 390e75b

Please sign in to comment.