Skip to content

Commit

Permalink
check for unknown origin as part of the valid pending transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Aug 26, 2024
1 parent 01a44bb commit 81374ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do
alias Archethic.Networking

alias Archethic.Mining.Error
alias Archethic.Mining.ProofOfWork

alias Archethic.OracleChain

Expand Down Expand Up @@ -82,6 +83,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do
with :ok <- do_accept_transaction(tx, validation_time),
:ok <- valid_previous_public_key(tx),
:ok <- valid_previous_signature(tx),
:ok <- valid_origin_signature(tx),
:ok <- validate_size(tx),
:ok <- validate_contract(tx),
:ok <- validate_ownerships(tx),
Expand Down Expand Up @@ -139,6 +141,18 @@ defmodule Archethic.Mining.PendingTransactionValidation do
end
end

defp valid_origin_signature(tx = %Transaction{}) do
result =
tx
|> ProofOfWork.list_origin_public_keys_candidates()
|> ProofOfWork.find_transaction_origin_public_key(tx)

case result do
{:ok, _} -> :ok
{:error, :not_found} -> {:error, "Unknown origin"}
end
end

defp validate_contract(%Transaction{data: %TransactionData{code: ""}}), do: :ok

defp validate_contract(%Transaction{
Expand Down
22 changes: 20 additions & 2 deletions lib/archethic/mining/validation_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ defmodule Archethic.Mining.ValidationContext do
{context, encoded_state, contract_recipients_fee} =
validate_smart_contract(context, resolved_recipients)

{context, pow} = validate_origin_signature(context)

fee =
calculate_fee(
tx,
Expand All @@ -691,7 +693,7 @@ defmodule Archethic.Mining.ValidationContext do
validation_stamp = %ValidationStamp{
protocol_version: Mining.protocol_version(),
timestamp: validation_time,
proof_of_work: do_proof_of_work(tx),
proof_of_work: pow,
proof_of_integrity: TransactionChain.proof_of_integrity([tx, prev_tx]),
proof_of_election: Election.validation_nodes_election_seed_sorting(tx, validation_time),
ledger_operations: ledger_operations,
Expand All @@ -705,6 +707,21 @@ defmodule Archethic.Mining.ValidationContext do
%__MODULE__{context | validation_stamp: validation_stamp}
end

defp validate_origin_signature(context = %__MODULE__{transaction: tx}) do
pow = do_proof_of_work(tx)

{
case pow do
"" ->
context |> set_mining_error(Error.new(:invalid_pending_transaction, "Unknown origin"))

_ ->
context
end,
pow
}
end

defp validate_smart_contract(context, resolved_recipients) do
with :ok <- validate_contract_context_inputs(context),
:ok <- validate_distinct_contract_recipients(resolved_recipients),
Expand Down Expand Up @@ -1137,7 +1154,8 @@ defmodule Archethic.Mining.ValidationContext do
transaction: tx
}) do
case pow do
"" ->
<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0>> ->
do_proof_of_work(tx) == ""

_ ->
Expand Down

0 comments on commit 81374ba

Please sign in to comment.