From 61aec50a796b03709872dda49d7e80f575f62900 Mon Sep 17 00:00:00 2001 From: Samuel Manzanera Date: Fri, 2 Dec 2022 15:23:46 +0100 Subject: [PATCH] Fix condition map restrictions --- .../contracts/interpreter/condition.ex | 10 +++---- .../contracts/interpreter/condition_test.exs | 10 ++++--- test/archethic/contracts/interpreter_test.exs | 26 +++++++++---------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/archethic/contracts/interpreter/condition.ex b/lib/archethic/contracts/interpreter/condition.ex index 4e182721ee..14c887c45b 100644 --- a/lib/archethic/contracts/interpreter/condition.ex +++ b/lib/archethic/contracts/interpreter/condition.ex @@ -190,11 +190,11 @@ defmodule Archethic.Contracts.ConditionInterpreter do {:%{}, _, _} -> {node, acc} - {{:atom, _}, _, _} -> + {op, _, _} when op in [:==, :<, :>, :<=, :>=, :if] -> {node, acc} _ -> - {node, {:error, "must be a map"}} + {node, {:error, "must be a map or a code instruction starting by an comparator"}} end end @@ -203,11 +203,11 @@ defmodule Archethic.Contracts.ConditionInterpreter do {:%{}, _, _} -> {node, acc} - {{:atom, _}, _, _} -> + {op, _, _} when op in [:==, :<, :>, :<=, :>=, :if] -> {node, acc} _ -> - {node, {:error, "must be a map"}} + {node, {:error, "must be a map or a code instruction starting by an comparator"}} end end @@ -271,7 +271,7 @@ defmodule Archethic.Contracts.ConditionInterpreter do {node, acc} end - # Whitelist usage of maps in the field of a condition + # Whitelist usage of taps in the field of a condition defp prewalk(node = {{:atom, _key}, _val}, acc = {:ok, %{scope: {:condition, _, _}}}) do {node, acc} end diff --git a/test/archethic/contracts/interpreter/condition_test.exs b/test/archethic/contracts/interpreter/condition_test.exs index e64f64e373..f54c4d540f 100644 --- a/test/archethic/contracts/interpreter/condition_test.exs +++ b/test/archethic/contracts/interpreter/condition_test.exs @@ -138,10 +138,11 @@ defmodule Archethic.Contracts.ConditionInterpreterTest do end test "parse invalid uco transfers type definition" do - assert {:error, "must be a map - uco_transfers"} = + assert {:error, + "must be a map or a code instruction starting by an comparator - uco_transfers"} = """ condition inherit: [ - uco_transfers: [%{ to: "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3", amount: 1040000000 }] + uco_transfers: [%{ "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3" => 1040000000 }] ] """ |> Interpreter.sanitize_code() @@ -150,10 +151,11 @@ defmodule Archethic.Contracts.ConditionInterpreterTest do end test "parse invalid token transfers type definition" do - assert {:error, "must be a map - token_transfers"} = + assert {:error, + "must be a map or a code instruction starting by an comparator - token_transfers"} = """ condition inherit: [ - token_transfers: [%{ to: "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3", amount: 1040000000 }] + token_transfers: [%{ "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3" => 1040000000 }] ] """ |> Interpreter.sanitize_code() diff --git a/test/archethic/contracts/interpreter_test.exs b/test/archethic/contracts/interpreter_test.exs index d0d15ec07c..1a27cdb996 100644 --- a/test/archethic/contracts/interpreter_test.exs +++ b/test/archethic/contracts/interpreter_test.exs @@ -35,24 +35,24 @@ defmodule Archethic.Contracts.InterpreterTest do assert {:ok, _} = """ condition inherit: [ - type: transfer, - uco_transfers: size() == 1 - # TODO: to provide more security, we should check the destination address is within the previous transaction inputs + token_transfers: size() == 1 ] + condition transaction: [ + uco_transfers: size() > 0, + timestamp: transaction.timestamp < 1665750161 + ] actions triggered_by: transaction do - # Get the amount of uco send to this contract - amount_send = transaction.uco_transfers[contract.address] - - if amount_send > 0 do - # Convert UCO to the number of tokens to credit. Each UCO worth 10000 token - token_to_credit = amount_send * 10000 + # Get the amount of uco send to this contract + amount_send = transaction.uco_transfers[contract.address] + if amount_send > 0 do + # Convert UCO to the number of tokens to credit. Each UCO worth 10 token + token_to_credit = amount_send * 10 - # Send the new transaction - set_type transfer - add_token_transfer to: transaction.address, token_address: contract.address, amount: token_to_credit, token_id: token_id - end + # Send the new transaction + add_token_transfer to: transaction.address, token_address: contract.address, amount: token_to_credit + end end """ |> Interpreter.parse()