Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete burn of the transaction fee #259

Merged
3 commits merged into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions lib/archethic/account/mem_tables_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ defmodule ArchEthic.Account.MemTablesLoader do

alias ArchEthic.Crypto

alias ArchEthic.P2P
alias ArchEthic.P2P.Node

alias ArchEthic.TransactionChain
alias ArchEthic.TransactionChain.Transaction
alias ArchEthic.TransactionChain.Transaction.ValidationStamp
alias ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperations
alias ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.NodeMovement

alias ArchEthic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.TransactionMovement

Expand All @@ -29,7 +25,7 @@ defmodule ArchEthic.Account.MemTablesLoader do
:previous_public_key,
validation_stamp: [
:timestamp,
ledger_operations: [:node_movements, :unspent_outputs, :transaction_movements]
ledger_operations: [:unspent_outputs, :transaction_movements]
]
]

Expand Down Expand Up @@ -69,7 +65,6 @@ defmodule ArchEthic.Account.MemTablesLoader do
timestamp: timestamp,
ledger_operations: %LedgerOperations{
unspent_outputs: unspent_outputs,
node_movements: node_movements,
transaction_movements: transaction_movements
}
}
Expand All @@ -81,7 +76,6 @@ defmodule ArchEthic.Account.MemTablesLoader do

:ok = set_transaction_movements(address, transaction_movements, timestamp)
:ok = set_unspent_outputs(address, unspent_outputs, timestamp)
:ok = set_node_rewards(address, node_movements, timestamp)

Logger.info("Loaded into in memory account tables",
transaction_address: Base.encode16(address),
Expand Down Expand Up @@ -125,18 +119,4 @@ defmodule ArchEthic.Account.MemTablesLoader do
NFTLedger.add_unspent_output(address, unspent_output, timestamp)
end)
end

defp set_node_rewards(address, node_movements, timestamp) do
node_movements
|> Enum.filter(&(&1.amount > 0))
|> Enum.each(fn %NodeMovement{to: to, amount: amount} ->
%Node{reward_address: reward_address} = P2P.get_node_info!(to)

UCOLedger.add_unspent_output(
reward_address,
%UnspentOutput{amount: amount, from: address, type: :UCO, reward?: true},
timestamp
)
end)
end
end
6 changes: 0 additions & 6 deletions lib/archethic/bootstrap/network_init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule ArchEthic.Bootstrap.NetworkInit do
alias ArchEthic.Mining

alias ArchEthic.PubSub
alias ArchEthic.P2P.Node

alias ArchEthic.Replication

Expand Down Expand Up @@ -133,11 +132,6 @@ defmodule ArchEthic.Bootstrap.NetworkInit do
transaction_movements: Transaction.get_movements(tx)
}
|> LedgerOperations.from_transaction(tx)
|> LedgerOperations.distribute_rewards(
%Node{last_public_key: Crypto.last_node_public_key()},
[%Node{last_public_key: Crypto.last_node_public_key()}],
[]
)
|> LedgerOperations.consume_inputs(tx.address, unspent_outputs)

validation_stamp =
Expand Down
40 changes: 0 additions & 40 deletions lib/archethic/mining.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule ArchEthic.Mining do
alias ArchEthic.P2P.Node

alias ArchEthic.SelfRepair
alias ArchEthic.SharedSecrets

alias ArchEthic.TransactionChain.Transaction
alias ArchEthic.TransactionChain.Transaction.CrossValidationStamp
Expand Down Expand Up @@ -102,45 +101,6 @@ defmodule ArchEthic.Mining do
validation_node_public_keys == Enum.map(validation_nodes, & &1.last_public_key)
end

def valid_election?(
tx = %Transaction{
address: tx_address,
type: tx_type,
validation_stamp: %ValidationStamp{timestamp: timestamp, proof_of_election: poe}
},
validation_node_public_keys
)
when is_list(validation_node_public_keys) do
daily_nonce_public_key = SharedSecrets.get_daily_nonce_public_key(timestamp)

if daily_nonce_public_key == SharedSecrets.genesis_daily_nonce_public_key() do
# Should happens only during the network bootstrapping
true
else
node_list = transaction_validation_node_list(tx_type, timestamp)
storage_nodes = Election.chain_storage_nodes_with_type(tx_address, tx_type, node_list)

constraints = Election.get_validation_constraints()

with true <-
Election.valid_proof_of_election?(tx, poe, daily_nonce_public_key),
nodes = [_ | _] <-
Election.validation_nodes(
tx,
poe,
node_list,
storage_nodes,
constraints
),
set_of_validation_node_public_keys <- Enum.map(nodes, & &1.last_public_key) do
Enum.all?(validation_node_public_keys, &(&1 in set_of_validation_node_public_keys))
else
_ ->
false
end
end
end

@doc """
Add transaction mining context which built by another cross validation node
"""
Expand Down
74 changes: 0 additions & 74 deletions lib/archethic/mining/validation_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -699,15 +699,11 @@ defmodule ArchEthic.Mining.ValidationContext do
transaction: tx,
previous_transaction: prev_tx,
unspent_outputs: unspent_outputs,
coordinator_node: coordinator_node,
previous_storage_nodes: previous_storage_nodes,
valid_pending_transaction?: valid_pending_transaction?
}
) do
initial_error = if valid_pending_transaction?, do: nil, else: :pending_transaction

confirmed_cross_validation_nodes = get_confirmed_validation_nodes(context)

validation_stamp =
%ValidationStamp{
timestamp: DateTime.utc_now(),
Expand All @@ -728,11 +724,6 @@ defmodule ArchEthic.Mining.ValidationContext do
)
}
|> LedgerOperations.from_transaction(tx)
|> LedgerOperations.distribute_rewards(
coordinator_node,
confirmed_cross_validation_nodes,
previous_storage_nodes
)
|> LedgerOperations.consume_inputs(tx.address, unspent_outputs),
recipients: resolve_transaction_recipients(tx),
errors: [initial_error, chain_error(prev_tx, tx)] |> Enum.filter(& &1)
Expand Down Expand Up @@ -907,7 +898,6 @@ defmodule ArchEthic.Mining.ValidationContext do
transaction_fee: fn -> valid_stamp_fee?(stamp, context) end,
transaction_movements: fn -> valid_stamp_transaction_movements?(stamp, context) end,
recipients: fn -> valid_stamp_recipients?(stamp, context) end,
node_movements: fn -> valid_stamp_node_movements?(stamp, context) end,
unspent_outputs: fn -> valid_stamp_unspent_outputs?(stamp, context) end,
errors: fn -> valid_stamp_errors?(stamp, context) end
]
Expand Down Expand Up @@ -1011,70 +1001,6 @@ defmodule ArchEthic.Mining.ValidationContext do
expected_unspent_outputs == next_unspent_outputs
end

defp valid_stamp_node_movements?(
%ValidationStamp{ledger_operations: ops},
context = %__MODULE__{
transaction: tx,
coordinator_node: %Node{last_public_key: coordinator_node_public_key},
unspent_outputs: unspent_outputs
}
) do
previous_storage_nodes =
P2P.distinct_nodes([unspent_storage_nodes(unspent_outputs), previous_storage_nodes(tx)])

cross_validation_nodes = get_confirmed_validation_nodes(context)

[
fn -> LedgerOperations.valid_node_movements_roles?(ops) end,
fn ->
LedgerOperations.valid_node_movements_cross_validation_nodes?(
ops,
Enum.map(cross_validation_nodes, & &1.last_public_key)
)
end,
fn ->
LedgerOperations.valid_node_movements_previous_storage_nodes?(
ops,
Enum.map(previous_storage_nodes, & &1.last_public_key)
)
end,
fn -> LedgerOperations.valid_reward_distribution?(ops) end,
fn ->
LedgerOperations.has_node_movement_with_role?(
ops,
coordinator_node_public_key,
:coordinator_node
)
end,
fn ->
Enum.all?(
cross_validation_nodes,
&LedgerOperations.has_node_movement_with_role?(
ops,
&1.last_public_key,
:cross_validation_node
)
)
end
]
|> Task.async_stream(& &1.(), ordered: false)
|> Enum.all?(&match?({:ok, true}, &1))
end

defp unspent_storage_nodes([]), do: []

defp unspent_storage_nodes(unspent_outputs) do
unspent_outputs
|> Stream.map(&Election.chain_storage_nodes(&1.from, P2P.available_nodes()))
|> Enum.to_list()
end

defp previous_storage_nodes(tx) do
tx
|> Transaction.previous_address()
|> Election.chain_storage_nodes(P2P.available_nodes())
end

@doc """
Get the chain storage node position
"""
Expand Down
2 changes: 1 addition & 1 deletion lib/archethic/p2p/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ defmodule ArchEthic.P2P.Message do
:type,
validation_stamp: [
:timestamp,
ledger_operations: [:node_movements, :transaction_movements]
ledger_operations: [:transaction_movements]
]
]) do
{:ok, tx} ->
Expand Down
Loading