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

Catch the contract execution errors, and return an error msg to the user #963

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
3 changes: 2 additions & 1 deletion lib/archethic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ defmodule Archethic do
) ::
{:ok, nil | Transaction.t()}
| {:error,
:invalid_triggers_execution
:contract_failure
| :invalid_triggers_execution
| :invalid_transaction_constraints
| :invalid_oracle_constraints
| :invalid_inherit_constraints}
Expand Down
7 changes: 6 additions & 1 deletion lib/archethic/contracts/interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ defmodule Archethic.Contracts.Interpreter do
) ::
{:ok, nil | Transaction.t()}
| {:error,
:invalid_triggers_execution
:contract_failure
| :invalid_triggers_execution
| :invalid_transaction_constraints
| :invalid_oracle_constraints
| :invalid_inherit_constraints}
Expand Down Expand Up @@ -251,6 +252,10 @@ defmodule Archethic.Contracts.Interpreter do
else
{:error, :invalid_transaction_constraints}
end
rescue
_ ->
# it's ok to loose the error because it's user-code
{:error, :contract_failure}
end

defp do_execute(
Expand Down
3 changes: 3 additions & 0 deletions lib/archethic_web/controllers/api/transaction_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ defmodule ArchethicWeb.API.TransactionController do
{:error, "Network issue, please try again later."}

# execute_contract errors
{:error, :contract_failure} ->
{:error, "Contract execution produced an error."}

{:error, :invalid_triggers_execution} ->
{:error, "Contract does not have a `actions triggered_by: transaction` block."}

Expand Down
35 changes: 35 additions & 0 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,41 @@ defmodule Archethic.Contracts.InterpreterTest do
)
end

test "should return contract_failure if contract code crash" do
code = """
@version 1
condition inherit: [
content: true
]

actions triggered_by: transaction do
x = 10 / 0
Contract.set_content x
end
"""

contract_tx = %Transaction{
type: :contract,
data: %TransactionData{
code: code
}
}

incoming_tx = %Transaction{
type: :transfer,
data: %TransactionData{}
}

assert match?(
{:error, :contract_failure},
Interpreter.execute(
:transaction,
Contract.from_transaction!(contract_tx),
incoming_tx
)
)
end

test "should be able to simulate a trigger: datetime" do
code = """
@version 1
Expand Down
61 changes: 61 additions & 0 deletions test/archethic/contracts/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -579,5 +579,66 @@ defmodule Archethic.Contracts.WorkerTest do
raise "Timeout"
end
end

test "should not crash the worker if contract code crashes", %{
constants: constants = %{"address" => contract_address}
} do
# the contract need uco to be executed
Archethic.Account.MemTables.TokenLedger.add_unspent_output(
contract_address,
%VersionedUnspentOutput{
unspent_output: %UnspentOutput{
from: "@Bob3",
amount: 100_000_000 * 10_000,
type: {:token, contract_address, 0},
timestamp: DateTime.utc_now() |> DateTime.truncate(:millisecond)
},
protocol_version: ArchethicCase.current_protocol_version()
}
)

code = """
@version 1
condition transaction: []

actions triggered_by: transaction do
n = 10 / 0
Contract.set_content n
end

condition inherit: [
content: true
]
"""

{:ok, contract} = Interpreter.parse(code)

contract = %{
contract
| constants: %ContractConstants{contract: Map.put(constants, "code", code)}
}

{:ok, worker_pid} = Worker.start_link(contract)

Worker.execute(contract_address, %Transaction{
address: @bob_address,
type: :transfer,
data: %TransactionData{
ledger: %Ledger{
uco: %UCOLedger{
transfers: [
%Transfer{to: contract_address, amount: 100_000_000}
]
}
},
recipients: [contract_address]
},
validation_stamp: %ValidationStamp{timestamp: DateTime.utc_now()}
})

Process.sleep(100)

assert Process.alive?(worker_pid)
end
end
end
2 changes: 0 additions & 2 deletions test/archethic/oracle_chain/scheduler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ defmodule Archethic.OracleChain.SchedulerTest do
alias Archethic.TransactionChain.Transaction.ValidationStamp
alias Archethic.TransactionChain.TransactionData

alias ArchethicCache.HydratingCache

import ArchethicCase, only: [setup_before_send_tx: 0]

import Mox
Expand Down