Skip to content

Commit

Permalink
Fix ledger movements resolution to add bburn movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed May 2, 2022
1 parent c87f1aa commit b850451
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
20 changes: 13 additions & 7 deletions lib/archethic/mining/validation_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -706,25 +706,31 @@ defmodule ArchEthic.Mining.ValidationContext do
) do
initial_error = if valid_pending_transaction?, do: nil, else: :pending_transaction

validation_time = DateTime.utc_now()

usd_price =
validation_time
|> OracleChain.get_uco_price()
|> Keyword.fetch!(:usd)

validation_stamp =
%ValidationStamp{
timestamp: DateTime.utc_now(),
proof_of_work: do_proof_of_work(tx),
proof_of_integrity: TransactionChain.proof_of_integrity([tx, prev_tx]),
proof_of_election:
Election.validation_nodes_election_seed_sorting(tx, DateTime.utc_now()),
proof_of_election: Election.validation_nodes_election_seed_sorting(tx, validation_time),
ledger_operations:
%LedgerOperations{
transaction_movements:
tx
|> Transaction.get_movements()
|> LedgerOperations.resolve_transaction_movements(DateTime.utc_now()),
fee:
Fee.calculate(
tx,
OracleChain.get_uco_price(DateTime.utc_now()) |> Keyword.fetch!(:usd)
usd_price
)
}
|> LedgerOperations.resolve_transaction_movements(
Transaction.get_movements(tx),
validation_time
)
|> LedgerOperations.from_transaction(tx)
|> LedgerOperations.consume_inputs(tx.address, unspent_outputs),
recipients: resolve_transaction_recipients(tx),
Expand Down
7 changes: 2 additions & 5 deletions lib/archethic/replication/transaction_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,9 @@ defmodule ArchEthic.Replication.TransactionValidator do
previous_unspent_outputs
) do
%LedgerOperations{
fee: get_transaction_fee(tx),
transaction_movements:
tx
|> Transaction.get_movements()
|> LedgerOperations.resolve_transaction_movements(timestamp)
fee: get_transaction_fee(tx)
}
|> LedgerOperations.resolve_transaction_movements(Transaction.get_movements(tx), timestamp)
|> LedgerOperations.from_transaction(tx)
|> LedgerOperations.consume_inputs(tx.address, previous_unspent_outputs)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,35 +518,44 @@ defmodule ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
tx_movements,
timestamp = %DateTime{}
) do
expected_movements = [
%TransactionMovement{
to: @burning_address,
amount: fee,
type: :UCO
}
| resolve_transaction_movements(tx_movements, timestamp)
]
%__MODULE__{transaction_movements: expected_movements} =
resolve_transaction_movements(%__MODULE__{fee: fee}, tx_movements, timestamp)

Enum.all?(resolved_transaction_movements, &(&1 in expected_movements))
end

@doc """
Resolve the last transaction addresses from the transaction movements
Resolve the transaction movements including the transaction's fee burning and retrieval of the last transaction addresses
"""
@spec resolve_transaction_movements(list(TransactionMovement.t()), DateTime.t()) ::
list(TransactionMovement.t())
@spec resolve_transaction_movements(t(), list(TransactionMovement.t()), DateTime.t()) :: t()
def resolve_transaction_movements(
ops = %__MODULE__{fee: fee},
tx_movements,
timestamp = %DateTime{}
) do
tx_movements
|> Task.async_stream(
fn mvt = %TransactionMovement{to: to} ->
%{mvt | to: TransactionChain.resolve_last_address(to, timestamp)}
end,
on_timeout: :kill_task
)
|> Stream.filter(&match?({:ok, _}, &1))
|> Enum.into([], fn {:ok, res} -> res end)
burn_movement = %TransactionMovement{
to: @burning_address,
amount: fee,
type: :UCO
}

resolved_movements =
tx_movements
|> Task.async_stream(
fn mvt = %TransactionMovement{to: to} ->
%{mvt | to: TransactionChain.resolve_last_address(to, timestamp)}
end,
on_timeout: :kill_task
)
|> Stream.filter(&match?({:ok, _}, &1))
|> Enum.reduce([burn_movement], fn {:ok, res}, acc ->
[res | acc]
end)
|> Enum.reverse()

%{
ops
| transaction_movements: resolved_movements
}
end
end

0 comments on commit b850451

Please sign in to comment.