Skip to content

Commit

Permalink
Refactor replication validation (#1559)
Browse files Browse the repository at this point in the history
* Refactor PendingTransactionValidation

* Use ValidationContext in replication validation

* Add missing cross stamp error serialization

* Verify pending validation in transaction validator

* Refactor LedgerOperations

* Add LedgerValidation to perform LedgerOperations calculation
  • Loading branch information
Neylix authored Oct 2, 2024
1 parent 3a76aac commit 9c6ee55
Show file tree
Hide file tree
Showing 24 changed files with 3,001 additions and 3,120 deletions.
20 changes: 8 additions & 12 deletions lib/archethic/bootstrap/network_init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Archethic.Bootstrap.NetworkInit do
alias Archethic.Election

alias Archethic.Mining
alias Archethic.Mining.LedgerValidation

alias Archethic.PubSub

Expand All @@ -26,7 +27,6 @@ defmodule Archethic.Bootstrap.NetworkInit do
alias Archethic.TransactionChain.Transaction
alias Archethic.TransactionChain.Transaction.CrossValidationStamp
alias Archethic.TransactionChain.Transaction.ValidationStamp
alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations
alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.UnspentOutput

alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.VersionedUnspentOutput
Expand Down Expand Up @@ -187,20 +187,16 @@ defmodule Archethic.Bootstrap.NetworkInit do
timestamp = DateTime.utc_now() |> DateTime.truncate(:millisecond)
fee = Mining.get_transaction_fee(tx, nil, 0.07, timestamp, nil)
movements = Transaction.get_movements(tx)

resolved_addresses = Enum.map(movements, &{&1.to, &1.to}) |> Map.new()

operations =
%LedgerOperations{fee: fee}
|> LedgerOperations.consume_inputs(
address,
timestamp,
unspent_outputs,
Transaction.get_movements(tx),
LedgerOperations.get_utxos_from_transaction(tx, timestamp, 1)
)
|> elem(1)
|> LedgerOperations.build_resolved_movements(movements, resolved_addresses, tx_type)
%LedgerValidation{fee: fee}
|> LedgerValidation.filter_usable_inputs(unspent_outputs, nil)
|> LedgerValidation.mint_token_utxos(tx, timestamp, 1)
|> LedgerValidation.build_resolved_movements(movements, resolved_addresses, tx_type)
|> LedgerValidation.validate_sufficient_funds()
|> LedgerValidation.consume_inputs(address, timestamp)
|> LedgerValidation.to_ledger_operations()

validation_stamp =
%ValidationStamp{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.ChainImpl do

alias Archethic.Crypto

alias Archethic.Tag
alias Archethic.Mining.LedgerValidation

alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations
alias Archethic.Tag

alias Archethic.Utils

Expand Down Expand Up @@ -85,7 +85,7 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.ChainImpl do
end

@impl Chain
def get_burn_address(), do: LedgerOperations.burning_address() |> Base.encode16()
def get_burn_address(), do: LedgerValidation.burning_address() |> Base.encode16()

@impl Chain
def get_previous_address(previous_public_key) when is_binary(previous_public_key),
Expand Down
18 changes: 2 additions & 16 deletions lib/archethic/mining/distributed_workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Archethic.Mining.DistributedWorkflow do

alias Archethic.Mining.Error
alias Archethic.Mining.MaliciousDetection
alias Archethic.Mining.PendingTransactionValidation
alias Archethic.Mining.TransactionContext
alias Archethic.Mining.ValidationContext
alias Archethic.Mining.WorkflowRegistry
Expand Down Expand Up @@ -403,24 +402,11 @@ defmodule Archethic.Mining.DistributedWorkflow do
:prior_validation,
state,
data = %{
context:
context = %ValidationContext{transaction: tx, validation_time: validation_time},
context: context = %ValidationContext{transaction: tx},
ref_timestamp: ref_timestamp
}
) do
new_context =
case PendingTransactionValidation.validate(tx, validation_time) do
:ok ->
context

{:error, error} ->
Logger.debug("Invalid pending transaction - #{inspect(error)}",
transaction_address: Base.encode16(tx.address),
transaction_type: tx.type
)

ValidationContext.set_mining_error(context, error)
end
new_context = ValidationContext.validate_pending_transaction(context)

new_data = Map.put(data, :context, new_context)

Expand Down
Loading

0 comments on commit 9c6ee55

Please sign in to comment.