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

AEIP-14 State UTXO #1287

Merged
merged 23 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1b140fc
Add State module to the Library
bchamagne Oct 2, 2023
08f9195
Create UnspentOutput of type :state
bchamagne Oct 2, 2023
759bf8b
State persisted in Account.StateLedger
bchamagne Oct 2, 2023
5b1046a
Contracts execution take a state and return a result
bchamagne Oct 2, 2023
deaf321
Contracts validation workflow considers the state
bchamagne Oct 2, 2023
2180382
Adapt APIs and Explorer
bchamagne Oct 2, 2023
d483e2f
Add state to conditions' constants
bchamagne Oct 2, 2023
2e3bb05
Limit the state to 256KB
bchamagne Oct 2, 2023
bc1ecd7
Enforce keys to be string
bchamagne Oct 3, 2023
ee8b9ff
State serialization algorithm
bchamagne Oct 3, 2023
65c0d9c
Fees consider state's size
bchamagne Oct 3, 2023
138c867
fixes after rebase
bchamagne Oct 4, 2023
0129c61
Use same naming in Noop and Success for the state_utxo
bchamagne Oct 4, 2023
0864d93
Forward the previous transaction payload if the state changed and the…
bchamagne Oct 9, 2023
aeebbeb
Merge branch 'develop' into 1217-stateful-utxo
bchamagne Oct 10, 2023
c80f404
Remove type Result and rename Noop/Success/Error
bchamagne Oct 12, 2023
a899415
Move State into Contract
bchamagne Oct 12, 2023
5974bdc
Contracts returns encoded_state over utxo
Neylix Nov 5, 2023
bf1e644
Add encoded_state in LedgerOperation struct
Neylix Nov 5, 2023
cc9e7c0
Add new fiels state in Contract struct
Neylix Nov 6, 2023
8dc3587
Fix contract verification with nil Context
Neylix Nov 6, 2023
f2b2e91
Merge branch 'develop' into 1217-stateful-utxo
Neylix Nov 6, 2023
5e272af
State uses typed encoding
Neylix Nov 6, 2023
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
15 changes: 14 additions & 1 deletion lib/archethic/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Archethic.Account do

alias __MODULE__.MemTables.TokenLedger
alias __MODULE__.MemTables.UCOLedger
alias __MODULE__.MemTables.StateLedger
alias __MODULE__.MemTablesLoader

alias Archethic.Crypto
Expand Down Expand Up @@ -38,6 +39,9 @@ defmodule Archethic.Account do
},
acc ->
update_in(acc, [:token, Access.key({token_address, token_id}, 0)], &(&1 + amount))

_, acc ->
acc
end)
end

Expand All @@ -46,7 +50,16 @@ defmodule Archethic.Account do
"""
@spec get_unspent_outputs(binary()) :: list(VersionedUnspentOutput.t())
def get_unspent_outputs(address) when is_binary(address) do
UCOLedger.get_unspent_outputs(address) ++ TokenLedger.get_unspent_outputs(address)
uco_tokens_utxos =
UCOLedger.get_unspent_outputs(address) ++ TokenLedger.get_unspent_outputs(address)

case StateLedger.get_unspent_output(address) do
nil ->
uco_tokens_utxos

state_utxo ->
[state_utxo | uco_tokens_utxos]
end
end

@doc """
Expand Down
88 changes: 88 additions & 0 deletions lib/archethic/account/mem_tables/state_ledger.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule Archethic.Account.MemTables.StateLedger do
@moduledoc """
A bit different than the token & uco ledgers.
This one can contain only 1 value by chain (instead of N values per transaction)
"""

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

alias Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperations.VersionedUnspentOutput

use GenServer
@vsn Mix.Project.config()[:version]

require Logger

@ledger_table :archethic_state_ledger

@doc """
Starts the GenServer that owns the ETS table
"""
@spec start_link(args :: list()) :: GenServer.on_start()
def start_link([]) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end

@doc """
Get the state of given contract or nil
"""
# @spec get_unspent_output(Crypto.prepended_hash()) :: nil | VersionedUnspentOutput.t()
def get_unspent_output(address) when is_binary(address) do
case :ets.lookup(@ledger_table, address) do
[] ->
nil

[{^address, encoded_payload, protocol_version}] ->
%VersionedUnspentOutput{
protocol_version: protocol_version,
unspent_output: %UnspentOutput{
type: :state,
encoded_payload: encoded_payload
}
}
end
end

@doc """
Set the state for given contract's address
"""
@spec add_unspent_output(Crypto.prepended_hash(), VersionedUnspentOutput.t()) :: :ok
def add_unspent_output(address, %VersionedUnspentOutput{
unspent_output: %UnspentOutput{
type: :state,
encoded_payload: encoded_payload
},
protocol_version: protocol_version
})
when is_binary(address) do
true = Crypto.valid_address?(address)

true =
:ets.insert(
@ledger_table,
{address, encoded_payload, protocol_version}
)

:ok
end

@doc """
Spend the state (deletes it)
"""
@spec spend_all_unspent_outputs(Crypto.prepended_hash()) :: :ok
def spend_all_unspent_outputs(address) when is_binary(address) do
:ets.delete(@ledger_table, address)
:ok
end

###############
# CALLBACKS
###############
@spec init(args :: list()) :: {:ok, :no_state}
def init([]) do
Logger.info("Initialize InMemory State Ledger...")
:ets.new(@ledger_table, [:set, :named_table, :public, read_concurrency: true])
{:ok, :no_state}
end
end
5 changes: 5 additions & 0 deletions lib/archethic/account/mem_tables_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Archethic.Account.MemTablesLoader do

alias Archethic.Account.MemTables.TokenLedger
alias Archethic.Account.MemTables.UCOLedger
alias Archethic.Account.MemTables.StateLedger

alias Archethic.Crypto

Expand Down Expand Up @@ -87,6 +88,7 @@ defmodule Archethic.Account.MemTablesLoader do

UCOLedger.spend_all_unspent_outputs(previous_address)
TokenLedger.spend_all_unspent_outputs(previous_address)
StateLedger.spend_all_unspent_outputs(previous_address)
end

:ok =
Expand Down Expand Up @@ -126,6 +128,9 @@ defmodule Archethic.Account.MemTablesLoader do
} ->
TokenLedger.add_unspent_output(address, unspent_output)

unspent_output = %VersionedUnspentOutput{unspent_output: %UnspentOutput{type: :state}} ->
StateLedger.add_unspent_output(address, unspent_output)

_ ->
# Ignore smart contract calls
:ignore
Expand Down
2 changes: 2 additions & 0 deletions lib/archethic/account/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Archethic.Account.Supervisor do

alias Archethic.Account.MemTables.TokenLedger
alias Archethic.Account.MemTables.UCOLedger
alias Archethic.Account.MemTables.StateLedger
alias Archethic.Account.MemTablesLoader

alias Archethic.Utils
Expand All @@ -17,6 +18,7 @@ defmodule Archethic.Account.Supervisor do
children = [
TokenLedger,
UCOLedger,
StateLedger,
MemTablesLoader
]

Expand Down
Loading
Loading