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

fix conditionInterpreter v0 comparaison #961

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
29 changes: 15 additions & 14 deletions lib/archethic/contracts/interpreter/legacy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,22 @@ defmodule Archethic.Contracts.Interpreter.Legacy do
address: nil,
authorized_keys: nil,
code: nil,
content: {
:>,
[line: 11],
content: {:>, [line: 11],
[
{
:==,
[line: 11],
[
{:get_in, [line: 11], [{:scope, [line: 11], nil}, ["transaction", "content"]]},
{{:., [line: 11], [{:__aliases__, [alias: Archethic.Contracts.Interpreter.Legacy.Library], [:Library]}, :json_path_extract]}, [line: 11], [{:get_in, [line: 11], [{:scope, [line: 11], nil}, ["transaction", "content"]]}, "$.uco.eur"]}
]
},
1
]
},
{{:., [line: 11],
[
{:__aliases__,
[alias: Archethic.Contracts.Interpreter.Legacy.Library],
[:Library]},
:json_path_extract
]}, [line: 11],
[
{:get_in, [line: 11],
[{:scope, [line: 11], nil}, ["transaction", "content"]]},
"$.uco.eur"
]},
1
]},
origin_family: :all,
previous_public_key: nil,
secrets: nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ConditionInterpreter do

# Flatten comparison operations
defp to_boolean_expression({op, _, [{:==, metadata, [{:get_in, _, _}, comp_a]}, comp_b]}, _, _)
when op in [:==, :>=, :<=] do
when op in [:==, :>=, :<=, :>, :<] do
{op, metadata, [comp_a, comp_b]}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,57 @@ defmodule Archethic.Contracts.Interpreter.Legacy.ConditionInterpreterTest do
}
})
end

test "should validate condition on uco_transfers size" do
address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

assert Interpreter.sanitize_code(~s"""
condition transaction: [
uco_transfers: size() < 10
]
""")
|> elem(1)
|> ConditionInterpreter.parse()
|> elem(2)
|> ConditionInterpreter.valid_conditions?(%{
"transaction" => %{
"uco_transfers" => %{"#{address}" => 12}
}
})
end

test "should invalidate condition on uco_transfers size" do
address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

refute Interpreter.sanitize_code(~s"""
condition transaction: [
uco_transfers: size() > 10
]
""")
|> elem(1)
|> ConditionInterpreter.parse()
|> elem(2)
|> ConditionInterpreter.valid_conditions?(%{
"transaction" => %{
"uco_transfers" => %{"#{address}" => 12}
}
})
end

test "should validate oracle condition" do
assert Interpreter.sanitize_code(~s"""
condition oracle: [
content: json_path_extract(\"$.uco.eur\") > 1
]
""")
|> elem(1)
|> ConditionInterpreter.parse()
|> elem(2)
|> ConditionInterpreter.valid_conditions?(%{
"transaction" => %{
"content" => Jason.encode!(%{uco: %{eur: 2}})
}
})
end
end
end