Skip to content

Commit

Permalink
sc: allow dot access to all maps
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Jan 25, 2023
1 parent 9eb1586 commit 2f8cb1f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/archethic/contracts/interpreter/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,12 @@ defmodule Archethic.Contracts.Interpreter.Utils do
def prewalk(node = Access, acc), do: {node, acc}
def prewalk(node = :get, acc), do: {node, acc}

# Whitelist the usage of transaction fields in references: "transaction/contract/previous/next"
# Whitelist the usage of dot statement (maps)
def prewalk(
node = {:., _, [{{:atom, transaction_ref}, _, nil}, {:atom, transaction_field}]},
node = {:., _, [{{:atom, _}, _, nil}, {:atom, _}]},
acc = {:ok, %{scope: scope}}
)
when scope != :root and transaction_ref in ["next", "previous", "transaction", "contract"] and
transaction_field in @transaction_fields do
when scope != :root do
{node, acc}
end

Expand Down
37 changes: 37 additions & 0 deletions test/archethic/contracts/interpreter/action_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,43 @@ defmodule Archethic.Contracts.ActionInterpreterTest do
}
})
end

test "should be able to modify acc if it is an object" do
~S"""
actions triggered_by: transaction do
list = ["X"]
transaction2 = reduce(list, transaction, fn item, acc ->
set(acc, address, item)
end)
set_content transaction2.address
end
"""
|> assert_content_after_execute("X", %{
"transaction" => %{
"address" => "@addr"
}
})
end

test "should be able to reduce in a reduce" do
~S"""
actions triggered_by: transaction do
list = [[1,2,3], [10,11,12]]
content = reduce(list, [], fn item, acc ->
sum = reduce(item, 0, fn item2, acc2 ->
item2 + acc2
end)
append(acc, sum)
end)
set_content content
end
"""
|> assert_content_after_execute("[6,33]", %{
"transaction" => %{
"address" => "@addr"
}
})
end
end

defp assert_content_after_execute(code, content, constants \\ %{}) do
Expand Down

0 comments on commit 2f8cb1f

Please sign in to comment.