Skip to content

Commit

Permalink
Make all conditions mandatory depending on the triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne authored and Neylix committed May 4, 2023
1 parent 95ffa96 commit 2d6bf4c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
24 changes: 16 additions & 8 deletions lib/archethic/contracts/interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ defmodule Archethic.Contracts.Interpreter do

defp parse_contract(1, ast) 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
# to be able to detect that user did not omit some condition blocks
initial_contract = %Contract{conditions: %{}}

case parse_ast_block(ast, initial_contract) do
Expand Down Expand Up @@ -539,15 +539,23 @@ defmodule Archethic.Contracts.Interpreter do
# -----------------------------------------
# contract validation
# -----------------------------------------

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

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"}
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 'condition transaction' block"}

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

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

true ->
{:ok, contract}
end
end
end
38 changes: 33 additions & 5 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,19 @@ defmodule Archethic.Contracts.InterpreterTest do
|> Interpreter.parse()
end

test "should return an human readable error if syntax is not elixir-valid" do
assert {:error, "Parse error: invalid language syntax"} =
"""
@version 1
actions triggered_by:transaction do
x = "missing space above"
end
"""
|> Interpreter.parse()
end

test "should return an human readable error 'condition inherit' block is missing" do
assert {:error, "missing inherit conditions"} =
assert {:error, "missing 'condition inherit' block"} =
"""
@version 1
condition transaction: []
Expand All @@ -131,12 +142,25 @@ defmodule Archethic.Contracts.InterpreterTest do
|> Interpreter.parse()
end

test "should return an human readable error if syntax is not elixir-valid" do
assert {:error, "Parse error: invalid language syntax"} =
test "should return an human readable error 'condition transaction' block is missing" do
assert {:error, "missing 'condition transaction' block"} =
"""
@version 1
actions triggered_by:transaction do
x = "missing space above"
condition inherit: []
actions triggered_by: transaction do
Contract.set_content "snobbish chameleon"
end
"""
|> Interpreter.parse()
end

test "should return an human readable error 'condition oracle' block is missing" do
assert {:error, "missing 'condition oracle' block"} =
"""
@version 1
condition inherit: []
actions triggered_by: oracle do
Contract.set_content "wise cow"
end
"""
|> Interpreter.parse()
Expand Down Expand Up @@ -375,6 +399,8 @@ defmodule Archethic.Contracts.InterpreterTest do
content: true
]
condition transaction: []
actions triggered_by: transaction do
x = 10 / 0
Contract.set_content x
Expand Down Expand Up @@ -409,6 +435,8 @@ defmodule Archethic.Contracts.InterpreterTest do
content: true
]
condition transaction: []
actions triggered_by: transaction do
Contract.add_uco_transfer amount: -1, to: "0000BFEF73346D20771614449D6BE9C705BF314067A0CF0ACBBF5E617EF5C978D0A1"
end
Expand Down

0 comments on commit 2d6bf4c

Please sign in to comment.