Skip to content

Commit

Permalink
Add encoded_state in LedgerOperation struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Nov 5, 2023
1 parent 51b6640 commit 7506a34
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/archethic/bootstrap/network_init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ defmodule Archethic.Bootstrap.NetworkInit do
transaction_movements: Transaction.get_movements(tx),
tokens_to_mint: LedgerOperations.get_utxos_from_transaction(tx, timestamp)
}
|> LedgerOperations.consume_inputs(tx.address, unspent_outputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, unspent_outputs, timestamp)
|> elem(1)

validation_stamp =
Expand Down
6 changes: 3 additions & 3 deletions lib/archethic/mining/validation_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -829,13 +829,13 @@ defmodule Archethic.Mining.ValidationContext do
%LedgerOperations{
fee: fee,
transaction_movements: resolved_movements,
tokens_to_mint: LedgerOperations.get_utxos_from_transaction(tx, validation_time)
tokens_to_mint: LedgerOperations.get_utxos_from_transaction(tx, validation_time),
encoded_state: encoded_state
}
|> LedgerOperations.consume_inputs(
tx.address,
unspent_outputs,
validation_time |> DateTime.truncate(:millisecond),
encoded_state
validation_time |> DateTime.truncate(:millisecond)
)
end

Expand Down
19 changes: 8 additions & 11 deletions lib/archethic/replication/transaction_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,14 @@ defmodule Archethic.Replication.TransactionValidator do
inputs,
encoded_state
) do
case LedgerOperations.consume_inputs(
%LedgerOperations{
fee: fee,
transaction_movements: transaction_movements,
tokens_to_mint: LedgerOperations.get_utxos_from_transaction(tx, timestamp)
},
address,
inputs,
timestamp,
encoded_state
) do
ops = %LedgerOperations{
fee: fee,
transaction_movements: transaction_movements,
tokens_to_mint: LedgerOperations.get_utxos_from_transaction(tx, timestamp),
encoded_state: encoded_state
}

case LedgerOperations.consume_inputs(ops, address, inputs, timestamp) do
{false, _} ->
{:error, :insufficient_funds}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
defstruct transaction_movements: [],
unspent_outputs: [],
tokens_to_mint: [],
encoded_state: nil,
fee: 0

alias Archethic.Contracts.Contract.State
Expand All @@ -36,6 +37,7 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
transaction_movements: list(TransactionMovement.t()),
unspent_outputs: list(UnspentOutput.t()),
tokens_to_mint: list(UnspentOutput.t()),
encoded_state: State.encoded() | nil,
fee: non_neg_integer()
}

Expand Down Expand Up @@ -201,16 +203,14 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation
ledger_operations :: t(),
change_address :: binary(),
inputs :: list(UnspentOutput.t() | TransactionInput.t()),
timestamp :: DateTime.t(),
encoded_state :: State.encoded() | nil
timestamp :: DateTime.t()
) ::
{boolean(), t()}
def consume_inputs(
ops = %__MODULE__{tokens_to_mint: tokens_to_mint},
ops = %__MODULE__{tokens_to_mint: tokens_to_mint, encoded_state: encoded_state},
change_address,
inputs,
timestamp,
encoded_state
timestamp
)
when is_binary(change_address) and is_list(inputs) and not is_nil(timestamp) do
# Since AEIP-19 we can consume from minted tokens
Expand Down
10 changes: 3 additions & 7 deletions test/archethic/mining/fee_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Archethic.Mining.FeeTest do
alias Archethic.Mining.Fee
alias Archethic.P2P
alias Archethic.P2P.Node
alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.UnspentOutput
alias Archethic.TransactionChain.TransactionData
alias Archethic.TransactionChain.TransactionData.Ledger
alias Archethic.TransactionChain.TransactionData.Recipient
Expand Down Expand Up @@ -313,19 +312,16 @@ defmodule Archethic.Mining.FeeTest do
fee_without_state =
Fee.calculate(tx, nil, 0.2, DateTime.utc_now(), nil, current_protocol_version())

state_utxo = %UnspentOutput{
type: :state,
encoded_payload: :crypto.strong_rand_bytes(1000)
}
encoded_state = :crypto.strong_rand_bytes(10)

fee_with_state =
Fee.calculate(
tx,
nil,
0.2,
DateTime.utc_now(),
ArchethicCase.current_protocol_version(),
state_utxo
encoded_state,
current_protocol_version()
)

assert fee_with_state > fee_without_state
Expand Down
17 changes: 9 additions & 8 deletions test/support/transaction_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ defmodule Archethic.TransactionFactory do
ledger_operations =
%LedgerOperations{
fee: Fee.calculate(tx, nil, 0.07, timestamp, encoded_state, current_protocol_version()),
transaction_movements: Transaction.get_movements(tx)
transaction_movements: Transaction.get_movements(tx),
encoded_state: encoded_state
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, encoded_state)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

poi =
Expand Down Expand Up @@ -133,7 +134,7 @@ defmodule Archethic.TransactionFactory do
%LedgerOperations{
fee: Fee.calculate(tx, nil, 0.07, timestamp, nil, current_protocol_version())
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

validation_stamp =
Expand Down Expand Up @@ -168,7 +169,7 @@ defmodule Archethic.TransactionFactory do
%LedgerOperations{
fee: Fee.calculate(tx, nil, 0.07, timestamp, nil, current_protocol_version())
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

validation_stamp = %ValidationStamp{
Expand Down Expand Up @@ -204,7 +205,7 @@ defmodule Archethic.TransactionFactory do
%LedgerOperations{
fee: Fee.calculate(tx, nil, 0.07, timestamp, nil, current_protocol_version())
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

validation_stamp = %ValidationStamp{
Expand Down Expand Up @@ -234,7 +235,7 @@ defmodule Archethic.TransactionFactory do
%LedgerOperations{
fee: 1_000_000_000
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

validation_stamp =
Expand Down Expand Up @@ -269,7 +270,7 @@ defmodule Archethic.TransactionFactory do
%TransactionMovement{to: "@Bob4", amount: 30_330_000_000, type: :UCO}
]
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

validation_stamp =
Expand Down Expand Up @@ -319,7 +320,7 @@ defmodule Archethic.TransactionFactory do
fee: Fee.calculate(tx, nil, 0.07, timestamp, nil, current_protocol_version()),
transaction_movements: Transaction.get_movements(tx)
}
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp, nil)
|> LedgerOperations.consume_inputs(tx.address, inputs, timestamp)
|> elem(1)

validation_stamp =
Expand Down

0 comments on commit 7506a34

Please sign in to comment.