Skip to content

Commit

Permalink
move version1 into interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne authored and Neylix committed Mar 14, 2023
1 parent 13929d6 commit 9c935db
Show file tree
Hide file tree
Showing 38 changed files with 206 additions and 225 deletions.
66 changes: 62 additions & 4 deletions lib/archethic/contracts/interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ defmodule Archethic.Contracts.Interpreter do
require Logger

alias __MODULE__.Legacy
alias __MODULE__.Version1
alias __MODULE__.ActionInterpreter
alias __MODULE__.ConditionInterpreter
alias __MODULE__.ConditionValidator

alias Archethic.Contracts.Contract
alias Archethic.Contracts.ContractConditions, as: Conditions
Expand All @@ -23,7 +25,7 @@ defmodule Archethic.Contracts.Interpreter do
{:ok, block} ->
case block do
{:__block__, [], [{:@, _, [{{:atom, "version"}, _, [version]}]} | rest]} ->
Version1.parse({:__block__, [], rest}, version)
parse_contract(version, rest)

_ ->
Legacy.parse(block)
Expand Down Expand Up @@ -54,7 +56,7 @@ defmodule Archethic.Contracts.Interpreter do
end

def valid_conditions?(1, conditions, constants) do
Version1.valid_conditions?(conditions, constants)
ConditionValidator.valid_conditions?(conditions, constants)
end

@doc """
Expand All @@ -67,7 +69,7 @@ defmodule Archethic.Contracts.Interpreter do
end

def execute_trigger(1, ast, constants) do
Version1.execute_trigger(ast, constants)
ActionInterpreter.execute(ast, constants)
end

@doc """
Expand Down Expand Up @@ -126,6 +128,14 @@ defmodule Archethic.Contracts.Interpreter do
do_format_error_reason(reason, key, [])
end

# ------------------------------------------------------------
# _ _
# _ __ _ __(___ ____ _| |_ ___
# | '_ \| '__| \ \ / / _` | __/ _ \
# | |_) | | | |\ V | (_| | || __/
# | .__/|_| |_| \_/ \__,_|\__\___|
# |_|
# ------------------------------------------------------------
defp do_format_error_reason(message, cause, metadata) do
message = prepare_message(message)

Expand Down Expand Up @@ -153,4 +163,52 @@ defmodule Archethic.Contracts.Interpreter do
{:ok, {:atom, atom}}
end
end

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

{:error, node, reason} ->
{:error, format_error_reason(node, reason)}
end
end

defp parse_contract(_version, _ast) do
{:error, "@version not supported"}
end

defp parse_ast_block([ast | rest], contract) do
case parse_ast(ast, contract) do
{:ok, contract} ->
parse_ast_block(rest, contract)

{:error, _, _} = e ->
e
end
end

defp parse_ast_block([], contract), do: {:ok, contract}

defp parse_ast(ast = {{:atom, "condition"}, _, _}, contract) do
case ConditionInterpreter.parse(ast) do
{:ok, condition_type, condition} ->
{:ok, Contract.add_condition(contract, condition_type, condition)}

{:error, _, _} = e ->
e
end
end

defp parse_ast(ast = {{:atom, "actions"}, _, _}, contract) do
case ActionInterpreter.parse(ast) do
{:ok, trigger_type, actions} ->
{:ok, Contract.add_trigger(contract, trigger_type, actions)}

{:error, _, _} = e ->
e
end
end

defp parse_ast(ast, _), do: {:error, ast, "unexpected term"}
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreter do
defmodule Archethic.Contracts.Interpreter.ActionInterpreter do
@moduledoc false

alias Archethic.TransactionChain.Transaction
alias Archethic.TransactionChain.TransactionData

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Version1.CommonInterpreter
alias Archethic.Contracts.Interpreter.Version1.Library
alias Archethic.Contracts.Interpreter.Version1.Scope
alias Archethic.Contracts.Interpreter.CommonInterpreter
alias Archethic.Contracts.Interpreter.Library
alias Archethic.Contracts.Interpreter.Scope

@doc """
Parse the given node and return the trigger and the actions block.
Expand Down Expand Up @@ -160,7 +160,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreter do
# it is bound in the root scope
new_node =
quote do
Archethic.Contracts.Interpreter.Version1.Library.Contract.get_calls(
Archethic.Contracts.Interpreter.Library.Contract.get_calls(
Scope.read_global(["contract", "address"])
)
end
Expand All @@ -177,7 +177,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ActionInterpreter do
{{:., _meta, [{:__aliases__, _, [atom: "Contract"]}, {:atom, function_name}]}, _, args},
acc
) do
absolute_module_atom = Archethic.Contracts.Interpreter.Version1.Library.Contract
absolute_module_atom = Archethic.Contracts.Interpreter.Library.Contract

# check function exists
unless Library.function_exists?(absolute_module_atom, function_name) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Archethic.Contracts.Interpreter.Version1.CommonInterpreter do
defmodule Archethic.Contracts.Interpreter.CommonInterpreter do
@moduledoc """
The prewalk and postwalk functions receive an `acc` for convenience.
They should see it as an opaque variable and just forward it.
Expand All @@ -7,8 +7,8 @@ defmodule Archethic.Contracts.Interpreter.Version1.CommonInterpreter do
"""

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Version1.Library
alias Archethic.Contracts.Interpreter.Version1.Scope
alias Archethic.Contracts.Interpreter.Library
alias Archethic.Contracts.Interpreter.Scope

@modules_whitelisted Library.list_common_modules()

Expand Down Expand Up @@ -282,7 +282,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.CommonInterpreter do
absolute_module_atom =
Code.ensure_loaded!(
String.to_existing_atom(
"Elixir.Archethic.Contracts.Interpreter.Version1.Library.Common.#{module_name}"
"Elixir.Archethic.Contracts.Interpreter.Library.Common.#{module_name}"
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreter do
defmodule Archethic.Contracts.Interpreter.ConditionInterpreter do
@moduledoc false

alias Archethic.Contracts.Interpreter.Version1.CommonInterpreter
alias Archethic.Contracts.Interpreter.Version1.Library
alias Archethic.Contracts.Interpreter.Version1.Scope
alias Archethic.Contracts.Interpreter.CommonInterpreter
alias Archethic.Contracts.Interpreter.Library
alias Archethic.Contracts.Interpreter.Scope
alias Archethic.Contracts.ContractConditions, as: Conditions
alias Archethic.Contracts.Interpreter.ASTHelper, as: AST

Expand Down Expand Up @@ -135,7 +135,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreter do

absolute_module_atom =
String.to_existing_atom(
"Elixir.Archethic.Contracts.Interpreter.Version1.Library.Common.#{module_name}"
"Elixir.Archethic.Contracts.Interpreter.Library.Common.#{module_name}"
)

new_node =
Expand Down Expand Up @@ -180,7 +180,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ConditionInterpreter do
) do
# new_node =
# quote do
# Archethic.Contracts.Interpreter.Version1.Library.Contract.get_calls(
# Archethic.Contracts.Interpreter.Library.Contract.get_calls(
# Scope.read_global([unquote(global_variable), "address"])
# )
# end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Archethic.Contracts.Interpreter.Version1.ConditionValidator do
defmodule Archethic.Contracts.Interpreter.ConditionValidator do
@moduledoc """
This is pretty much a copy of Legacy.ConditionInterpreter.
The difference is where the scope is stored (process dict VS global variable)
Expand All @@ -7,7 +7,7 @@ defmodule Archethic.Contracts.Interpreter.Version1.ConditionValidator do
alias Archethic.Contracts.ContractConditions, as: Conditions
alias Archethic.Contracts.ContractConstants, as: Constants
alias Archethic.Contracts.Interpreter
alias Archethic.Contracts.Interpreter.Version1.Scope
alias Archethic.Contracts.Interpreter.Scope

require Logger

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library do
defmodule Archethic.Contracts.Interpreter.Library do
@moduledoc false

@doc """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Chain do
defmodule Archethic.Contracts.Interpreter.Library.Common.Chain do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Legacy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Crypto do
defmodule Archethic.Contracts.Interpreter.Library.Common.Crypto do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Legacy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Json do
defmodule Archethic.Contracts.Interpreter.Library.Common.Json do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Legacy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.List do
defmodule Archethic.Contracts.Interpreter.Library.Common.List do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Map do
defmodule Archethic.Contracts.Interpreter.Library.Common.Map do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Regex do
defmodule Archethic.Contracts.Interpreter.Library.Common.Regex do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Legacy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.String do
defmodule Archethic.Contracts.Interpreter.Library.Common.String do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Time do
defmodule Archethic.Contracts.Interpreter.Library.Common.Time do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

@doc """
Returns current time in unix timestamp format.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Common.Token do
defmodule Archethic.Contracts.Interpreter.Library.Common.Token do
@moduledoc false
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.Contracts.Interpreter.Legacy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Archethic.Contracts.Interpreter.Version1.Library.Contract do
defmodule Archethic.Contracts.Interpreter.Library.Contract do
@moduledoc """
We are delegating to the legacy transaction statements.
This is fine as long as we don't need to change anything.
If there's something to change for version 1, do the change in here, not in legacy.
"""
@behaviour Archethic.Contracts.Interpreter.Version1.Library
@behaviour Archethic.Contracts.Interpreter.Library

alias Archethic.Contracts.Interpreter.ASTHelper, as: AST
alias Archethic.TransactionChain.Transaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Archethic.Contracts.Interpreter.Version1.Scope do
defmodule Archethic.Contracts.Interpreter.Scope do
@moduledoc """
Helper functions to deal with scopes
"""
Expand Down
Loading

0 comments on commit 9c935db

Please sign in to comment.