From 2e88853312b630cfee76968562028348b79f7903 Mon Sep 17 00:00:00 2001 From: Neylix Date: Wed, 26 Jul 2023 23:29:59 +0200 Subject: [PATCH] Catch condition execution error --- lib/archethic/contracts.ex | 3 +++ test/archethic/contracts_test.exs | 34 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/archethic/contracts.ex b/lib/archethic/contracts.ex index 71063c73e..48fd73344 100644 --- a/lib/archethic/contracts.ex +++ b/lib/archethic/contracts.ex @@ -156,6 +156,9 @@ defmodule Archethic.Contracts do constants = get_condition_constants(condition_type, contract, transaction, datetime) Interpreter.valid_conditions?(version, condition, constants) end + rescue + _ -> + false end defp validate_trigger({:datetime, datetime}, validation_datetime, _contract_address) do diff --git a/test/archethic/contracts_test.exs b/test/archethic/contracts_test.exs index 4f5059a80..845858b34 100644 --- a/test/archethic/contracts_test.exs +++ b/test/archethic/contracts_test.exs @@ -586,6 +586,40 @@ defmodule Archethic.ContractsTest do DateTime.utc_now() ) end + + test "should return false if condition execution raise an error" do + code = """ + @version 1 + condition transaction: [ + type: 1 + "one" + ] + + actions triggered_by: transaction do + Contract.set_content "hello" + end + """ + + contract_tx = %Transaction{ + address: random_address(), + data: %TransactionData{ + code: code + } + } + + trigger_tx = %Transaction{ + type: :transfer, + address: random_address(), + data: %TransactionData{}, + validation_stamp: ValidationStamp.generate_dummy() + } + + refute Contracts.valid_condition?( + :transaction, + Contract.from_transaction!(contract_tx), + trigger_tx, + DateTime.utc_now() + ) + end end describe "valid_condition?/4 (oracle)" do