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

Load all contract transaction #917

Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/archethic/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ defmodule Archethic.Contracts do
Load transaction into the Smart Contract context leveraging the interpreter
"""
@spec load_transaction(Transaction.t(), list()) :: :ok
defdelegate load_transaction(tx, opts \\ []), to: Loader
defdelegate load_transaction(tx, opts), to: Loader

@spec accept_new_contract?(Transaction.t() | nil, Transaction.t(), DateTime.t()) :: boolean()
def accept_new_contract?(nil, _, _), do: true
Expand Down
38 changes: 16 additions & 22 deletions lib/archethic/contracts/loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule Archethic.Contracts.Loader do
alias Archethic.Contracts.TransactionLookup
alias Archethic.Contracts.Worker

alias Archethic.DB

alias Archethic.TransactionChain
alias Archethic.TransactionChain.Transaction
alias Archethic.TransactionChain.Transaction.ValidationStamp
Expand All @@ -26,15 +24,16 @@ defmodule Archethic.Contracts.Loader do
end

def init(_opts) do
DB.list_last_transaction_addresses()
|> Stream.map(&DB.get_transaction(&1, []))
|> Stream.filter(fn
{:ok, %Transaction{data: %TransactionData{code: ""}}} -> false
{:error, _} -> false
_ -> true
end)
|> Stream.map(fn {:ok, tx} -> tx end)
|> Stream.each(&load_transaction(&1, from_db: true))
TransactionChain.list_io_transactions([])
|> Stream.filter(&(&1.data.recipients != []))
|> Stream.each(&load_transaction(&1, execute_contract?: false, io_transaction?: true))
|> Stream.run()

# Network transactions does not contains trigger or recipient
TransactionChain.list_all([])
|> Stream.reject(&Transaction.network_type?(&1.type))
|> Stream.filter(&(&1.data.recipients != [] or &1.data.code != ""))
|> Stream.each(&load_transaction(&1, execute_contract?: false, io_transaction?: false))
|> Stream.run()

{:ok, []}
Expand All @@ -55,19 +54,14 @@ defmodule Archethic.Contracts.Loader do
protocol_version: protocol_version
}
},
opts \\ []
execute_contract?: execute_contract?,
io_transaction?: io_transaction?
) do
from_db? = Keyword.get(opts, :from_db, false)
from_self_repair? = Keyword.get(opts, :from_self_repair, false)
if from_db? and from_self_repair?, do: raise("Cant have tx with db and self repair flag")

# Stop previous transaction contract
unless from_db? do
stop_contract(Transaction.previous_address(tx))
end
stop_contract(Transaction.previous_address(tx))

# If transaction contains code, start a new worker for it
if code != "" do
# If transaction contains code and we are storage node, start a new worker for it
if code != "" and not io_transaction? do
%Contract{triggers: triggers} = Contracts.parse!(code)
triggers = Enum.reject(triggers, fn {_, actions} -> actions == {:__block__, [], []} end)

Expand Down Expand Up @@ -95,7 +89,7 @@ defmodule Archethic.Contracts.Loader do
protocol_version
)

unless from_db? or from_self_repair? do
if execute_contract? do
# execute contract asynchronously only if we are in live replication
Logger.info(
"Execute transaction on contract #{Base.encode16(contract_address)}",
Expand Down
7 changes: 6 additions & 1 deletion lib/archethic/replication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,12 @@ defmodule Archethic.Replication do
P2P.load_transaction(tx)
SharedSecrets.load_transaction(tx)
Account.load_transaction(tx, io_transaction?)
Contracts.load_transaction(tx, from_self_repair: self_repair?)

Contracts.load_transaction(tx,
execute_contract?: not self_repair?,
io_transaction?: io_transaction?
)

OracleChain.load_transaction(tx)
Reward.load_transaction(tx)
:ok
Expand Down
61 changes: 29 additions & 32 deletions test/archethic/contracts/loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Archethic.Contracts.LoaderTest do
}
}

assert :ok = Loader.load_transaction(tx)
assert :ok = Loader.load_transaction(tx, execute_contract?: false, io_transaction?: false)
[{pid, _}] = Registry.lookup(ContractRegistry, contract_address)

assert Enum.any?(
Expand Down Expand Up @@ -107,9 +107,9 @@ defmodule Archethic.Contracts.LoaderTest do
}
}

assert :ok = Loader.load_transaction(tx1)
assert :ok = Loader.load_transaction(tx1, execute_contract?: false, io_transaction?: false)
[{pid1, _}] = Registry.lookup(ContractRegistry, tx1.address)
assert :ok = Loader.load_transaction(tx2)
assert :ok = Loader.load_transaction(tx2, execute_contract?: false, io_transaction?: false)
[{pid2, _}] = Registry.lookup(ContractRegistry, tx2.address)

assert !Process.alive?(pid1)
Expand All @@ -128,36 +128,33 @@ defmodule Archethic.Contracts.LoaderTest do

contract_address = Crypto.derive_address(pub1)

tx = %Transaction{
address: contract_address,
data: %TransactionData{
code: """
condition transaction: [
content: "hello"
]

condition inherit: [
content: "hi"
]

actions triggered_by: transaction do
set_content "hi"
end
"""
},
previous_public_key: pub0,
validation_stamp: %ValidationStamp{
recipients: [],
timestamp: DateTime.utc_now()
}
}

MockDB
|> stub(:list_last_transaction_addresses, fn ->
[contract_address]
end)
|> stub(:get_transaction, fn ^contract_address, _ ->
{:ok,
%Transaction{
address: contract_address,
data: %TransactionData{
code: """
condition transaction: [
content: "hello"
]

condition inherit: [
content: "hi"
]

actions triggered_by: transaction do
set_content "hi"
end
"""
},
previous_public_key: pub0,
validation_stamp: %ValidationStamp{
recipients: [],
timestamp: DateTime.utc_now()
}
}}
end)
|> stub(:list_io_transactions, fn _ -> [] end)
|> stub(:list_transactions, fn _ -> [tx] end)

assert {:ok, _} = Loader.start_link()
[{pid, _}] = Registry.lookup(ContractRegistry, contract_address)
Expand Down