Skip to content

Commit

Permalink
Make the 'condition inherit' block mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne authored and Neylix committed May 4, 2023
1 parent 804fded commit 8cfb89d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
27 changes: 11 additions & 16 deletions lib/archethic/contracts/interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ defmodule Archethic.Contracts.Interpreter do
end

defp parse_contract(1, ast) do
case parse_ast_block(ast, %Contract{}) do
# we need to force the initialization of conditions to an empty map
# to be able to detect that user did not omit the condition inherit block
initial_contract = %Contract{conditions: %{}}

case parse_ast_block(ast, initial_contract) do
{:ok, contract} ->
{:ok, %{contract | version: 1}}

Expand Down Expand Up @@ -538,21 +542,12 @@ defmodule Archethic.Contracts.Interpreter do

defp check_contract_blocks({:error, reason}), do: {:error, reason}

defp check_contract_blocks(
{:ok, contract = %Contract{triggers: triggers, conditions: conditions}}
) do
cond do
Map.has_key?(triggers, :transaction) and !Map.has_key?(conditions, :transaction) ->
{:error, "missing transaction conditions"}

Map.has_key?(triggers, :oracle) and !Map.has_key?(conditions, :oracle) ->
{:error, "missing oracle conditions"}

!Map.has_key?(conditions, :inherit) ->
{:error, "missing inherit conditions"}

true ->
{:ok, contract}
defp check_contract_blocks({:ok, contract = %Contract{conditions: conditions}}) do
# Only inherit condition are mandatory
if Map.has_key?(conditions, :inherit) do
{:ok, contract}
else
{:error, "missing inherit conditions"}
end
end
end
31 changes: 27 additions & 4 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ defmodule Archethic.Contracts.InterpreterTest do
assert {:error, _} =
"""
@version 1
condition inherit: [
content: true
]
condition transaction: [
uco_transfers: List.size() > 0
]
Expand All @@ -64,10 +67,12 @@ defmodule Archethic.Contracts.InterpreterTest do
assert {:ok, %Contract{}} =
"""
@version 1
condition inherit: [
content: true
]
condition transaction: [
uco_transfers: List.size() > 0
]
actions triggered_by: transaction do
Contract.set_content "hello"
end
Expand All @@ -76,9 +81,10 @@ defmodule Archethic.Contracts.InterpreterTest do
end

test "should return an human readable error if lib fn is called with bad arg" do
assert {:error, "invalid function arguments - List.empty?(12) - L4"} =
assert {:error, "invalid function arguments - List.empty?(12) - L5"} =
"""
@version 1
condition inherit: []
condition transaction: []
actions triggered_by: transaction do
x = List.empty?(12)
Expand All @@ -88,9 +94,10 @@ defmodule Archethic.Contracts.InterpreterTest do
end

test "should return an human readable error if lib fn is called with bad arity" do
assert {:error, "invalid function arity - List.empty?([1], \"foobar\") - L4"} =
assert {:error, "invalid function arity - List.empty?([1], \"foobar\") - L5"} =
"""
@version 1
condition inherit: []
condition transaction: []
actions triggered_by: transaction do
x = List.empty?([1], "foobar")
Expand All @@ -100,9 +107,10 @@ defmodule Archethic.Contracts.InterpreterTest do
end

test "should return an human readable error if lib fn does not exists" do
assert {:error, "unknown function - List.non_existing([1, 2, 3]) - L4"} =
assert {:error, "unknown function - List.non_existing([1, 2, 3]) - L5"} =
"""
@version 1
condition inherit: []
condition transaction: []
actions triggered_by: transaction do
x = List.non_existing([1,2,3])
Expand All @@ -111,6 +119,18 @@ defmodule Archethic.Contracts.InterpreterTest do
|> Interpreter.parse()
end

test "should return an human readable error 'condition inherit' block is missing" do
assert {:error, "missing inherit conditions"} =
"""
@version 1
condition transaction: []
actions triggered_by: transaction do
Contract.set_content "symptomatic grizzly bear"
end
"""
|> Interpreter.parse()
end

test "should return an human readable error if syntax is not elixir-valid" do
assert {:error, "Parse error: invalid language syntax"} =
"""
Expand Down Expand Up @@ -143,6 +163,9 @@ defmodule Archethic.Contracts.InterpreterTest do
test "should return the contract if format is OK" do
assert {:ok, %Contract{}} =
"""
condition inherit: [
content: true
]
condition transaction: [
uco_transfers: size() > 0
]
Expand Down

0 comments on commit 8cfb89d

Please sign in to comment.