Skip to content

Commit

Permalink
Fix loading of inputs of burn address on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed Aug 3, 2022
1 parent 2019672 commit 6c74a89
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
16 changes: 12 additions & 4 deletions lib/archethic/account/mem_tables_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defmodule Archethic.Account.MemTablesLoader do

alias Archethic.Crypto

alias Archethic.Election

alias Archethic.P2P

alias Archethic.TransactionChain
alias Archethic.TransactionChain.Transaction
alias Archethic.TransactionChain.Transaction.ValidationStamp
Expand All @@ -17,6 +21,8 @@ defmodule Archethic.Account.MemTablesLoader do

alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.UnspentOutput

alias Archethic.Utils

require Logger

alias Archethic.Reward
Expand All @@ -27,14 +33,12 @@ defmodule Archethic.Account.MemTablesLoader do
:previous_public_key,
validation_stamp: [
:timestamp,
ledger_operations: [:unspent_outputs, :transaction_movements]
ledger_operations: [:fee, :unspent_outputs, :transaction_movements]
]
]

@excluded_types [
:node,
:beacon,
:beacon_summary,
:oracle,
:oracle_summary,
:node_shared_secrets,
Expand Down Expand Up @@ -80,7 +84,11 @@ defmodule Archethic.Account.MemTablesLoader do
:ok = set_transaction_movements(address, transaction_movements, timestamp, tx_type)
:ok = set_unspent_outputs(address, unspent_outputs, timestamp)

if fee > 0 do
burn_storage_nodes =
Election.storage_nodes(LedgerOperations.burning_address(), P2P.authorized_nodes(timestamp))

if Utils.key_in_node_list?(burn_storage_nodes, Crypto.first_node_public_key()) and
fee > 0 do
UCOLedger.add_unspent_output(
LedgerOperations.burning_address(),
%UnspentOutput{from: address, amount: fee, type: :UCO},
Expand Down
36 changes: 33 additions & 3 deletions test/archethic/account/mem_tables_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Archethic.Account.MemTablesLoaderTest do
alias Archethic.Account.MemTables.UCOLedger
alias Archethic.Account.MemTablesLoader

alias Archethic.Crypto

alias Archethic.P2P
alias Archethic.P2P.Node

Expand All @@ -29,9 +31,21 @@ defmodule Archethic.Account.MemTablesLoaderTest do
MockDB
|> stub(:list_transactions_by_type, fn :mint_rewards, [:address, :type] ->
[
%Transaction{address: "@RewardToken0", type: :mint_rewards},
%Transaction{address: "@RewardToken1", type: :mint_rewards},
%Transaction{address: "@RewardToken2", type: :mint_rewards}
%Transaction{
address: "@RewardToken0",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
},
%Transaction{
address: "@RewardToken1",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
},
%Transaction{
address: "@RewardToken2",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
}
]
end)

Expand All @@ -52,6 +66,17 @@ defmodule Archethic.Account.MemTablesLoaderTest do

describe "load_transaction/1" do
test "should distribute unspent outputs" do
P2P.add_and_connect_node(%Node{
ip: {127, 0, 0, 1},
port: 3000,
first_public_key: Crypto.first_node_public_key(),
last_public_key: Crypto.first_node_public_key(),
authorized?: true,
authorization_date: DateTime.utc_now() |> DateTime.add(-1000),
available?: true,
geo_patch: "AAA"
})

assert :ok = MemTablesLoader.load_transaction(create_transaction())

[
Expand All @@ -62,6 +87,9 @@ defmodule Archethic.Account.MemTablesLoaderTest do
[%UnspentOutput{from: "@Charlie3", amount: 3_400_000_000}] =
UCOLedger.get_unspent_outputs("@Tom4")

[%UnspentOutput{from: "@Charlie3", amount: 100_000_000}] =
UCOLedger.get_unspent_outputs(LedgerOperations.burning_address())

assert [
%UnspentOutput{
from: "@Charlie3",
Expand Down Expand Up @@ -108,6 +136,7 @@ defmodule Archethic.Account.MemTablesLoaderTest do
validation_stamp: %ValidationStamp{
timestamp: DateTime.utc_now(),
ledger_operations: %LedgerOperations{
fee: 100_000_000,
transaction_movements: [
%TransactionMovement{to: "@Tom4", amount: 3_400_000_000, type: :UCO},
%TransactionMovement{
Expand Down Expand Up @@ -180,6 +209,7 @@ defmodule Archethic.Account.MemTablesLoaderTest do
validation_stamp: %ValidationStamp{
timestamp: DateTime.utc_now(),
ledger_operations: %LedgerOperations{
fee: 0,
transaction_movements: [
%TransactionMovement{to: "@Tom4", amount: 3_400_000_000, type: :UCO},
%TransactionMovement{
Expand Down
32 changes: 27 additions & 5 deletions test/archethic/account_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Archethic.AccountTest do
alias Archethic.Account.MemTables.TokenLedger
alias Archethic.Account.MemTables.UCOLedger

alias Archethic.TransactionChain.Transaction.ValidationStamp
alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations
alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.UnspentOutput

alias Archethic.TransactionChain.Transaction
Expand All @@ -19,11 +21,31 @@ defmodule Archethic.AccountTest do
setup do
expect(MockDB, :list_transactions_by_type, fn _, _ ->
[
%Transaction{address: "@RewardToken0", type: :mint_rewards},
%Transaction{address: "@RewardToken1", type: :mint_rewards},
%Transaction{address: "@RewardToken2", type: :mint_rewards},
%Transaction{address: "@RewardToken3", type: :mint_rewards},
%Transaction{address: "@RewardToken4", type: :mint_rewards}
%Transaction{
address: "@RewardToken0",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
},
%Transaction{
address: "@RewardToken1",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
},
%Transaction{
address: "@RewardToken2",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
},
%Transaction{
address: "@RewardToken3",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
},
%Transaction{
address: "@RewardToken4",
type: :mint_rewards,
validation_stamp: %ValidationStamp{ledger_operations: %LedgerOperations{fee: 0}}
}
]
end)

Expand Down

0 comments on commit 6c74a89

Please sign in to comment.