Skip to content

Commit

Permalink
Improve linting for WASM modules
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Aug 29, 2024
1 parent f394a72 commit 45595f0
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/archethic/contracts/wasm/instance.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
defmodule Archethic.Contracts.WasmInstance do
@moduledoc false
use GenServer

@vsn 1

alias Archethic.Contracts.WasmModule
alias Archethic.Contracts.WasmMemory
alias Archethic.Contracts.WasmSpec
alias Archethic.Contracts.Wasm.ReadResult
alias Archethic.Contracts.Wasm.UpdateResult

@reserved_functions ["spec", "init", "onUpgrade"]

@spec start_link(list()) :: GenServer.on_start()
@spec start_link(bytes: binary(), transaction: nil | map()) :: GenServer.on_start()
def start_link(arg) do
GenServer.start_link(__MODULE__, arg)
end

@spec execute(pid(), String.t(), nil | map(), [state: map(), transaction: map()] | nil) ::
{:ok, ReadResult.t() | UpdateResult.t()} | {:error, any()}
def execute(pid, function_name, arg, opts) do
GenServer.call(pid, {:execute, function_name, arg, opts})
end
Expand All @@ -20,6 +28,7 @@ defmodule Archethic.Contracts.WasmInstance do
GenServer.call(pid, :spec)
end

@spec exported_functions(pid) :: list(String.t())
def exported_functions(pid) do
GenServer.call(pid, :exported_functions)
end
Expand Down Expand Up @@ -83,12 +92,11 @@ defmodule Archethic.Contracts.WasmInstance do
exported_functions
) do
spec_functions = Enum.map(triggers, & &1.function_name) ++ public_functions
reservedFunctions = ["spec", "init", "onUpgrade"]

case exported_functions
|> MapSet.new()
|> MapSet.difference(MapSet.new(spec_functions))
|> MapSet.reject(&(&1 in reservedFunctions))
|> MapSet.reject(&(&1 in @reserved_functions))
|> MapSet.to_list() do
[] ->
:ok
Expand Down
7 changes: 5 additions & 2 deletions lib/archethic/contracts/wasm/memory.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule Archethic.Contracts.WasmMemory do
@moduledoc false
use GenServer

@vsn 1

def start_link() do
GenServer.start_link(__MODULE__, [])
end
Expand Down Expand Up @@ -85,8 +88,8 @@ defmodule Archethic.Contracts.WasmMemory do
end

def handle_cast({:set_error, offset, length}, state = %{output_buffer: output_buffer}) do
errorPayload = :erlang.binary_part(output_buffer, offset, length)
err = Jason.decode!(errorPayload)
err_payload = :erlang.binary_part(output_buffer, offset, length)
err = Jason.decode!(err_payload)
{:noreply, Map.put(state, :error, err)}
end

Expand Down
10 changes: 6 additions & 4 deletions lib/archethic/contracts/wasm/module.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Archethic.Contracts.WasmModule do
@moduledoc false
alias Archethic.Contracts.WasmResult
alias Archethic.Contracts.WasmSpec
alias Archethic.Contracts.Wasm.ReadResult
Expand Down Expand Up @@ -51,7 +52,7 @@ defmodule Archethic.Contracts.WasmModule do
end

@spec execute(instance :: pid(), io_mem_pid :: pid(), functionName :: String.t(), opts()) ::
{:ok, ReadResult.t() | UpdateResult.t(), consumed_gas :: pos_integer()}
{:ok, ReadResult.t() | UpdateResult.t()}
| {:ok, WasmSpec.t()}
| {:error, :function_not_exists}
| {:error, {:invalid_output, nil}}
Expand Down Expand Up @@ -87,15 +88,16 @@ defmodule Archethic.Contracts.WasmModule do
nil ->
{:error, e}

customError ->
{:error, customError}
custom_error ->
{:error, custom_error}
end
end
end

defp imports(io_mem_pid) do
log = fn _, offset, length ->
WasmMemory.read(io_mem_pid, offset, length) |> IO.inspect(label: "log wasm")
log_msg = WasmMemory.read(io_mem_pid, offset, length)
IO.puts("WASM log => #{log_msg}")
end

store_u8 = fn _, offset, value ->
Expand Down
10 changes: 10 additions & 0 deletions lib/archethic/contracts/wasm/result.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
defmodule Archethic.Contracts.Wasm.UpdateResult do
@moduledoc false
@type t :: %__MODULE__{
state: map(),
transaction: map()
}
defstruct [:state, :transaction]
end

defmodule Archethic.Contracts.Wasm.ReadResult do
@moduledoc false
@type t :: %__MODULE__{
value: any()
}
defstruct [:value]
end

defmodule Archethic.Contracts.WasmResult do
@moduledoc false
alias Archethic.Contracts.Wasm.UpdateResult
alias Archethic.Contracts.Wasm.ReadResult

Expand Down
13 changes: 13 additions & 0 deletions lib/archethic/contracts/wasm/spec.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule Archethic.Contracts.WasmSpec do
@moduledoc false

alias Archethic.Contracts.WasmTrigger

@type t :: %__MODULE__{
version: pos_integer(),
triggers: list(WasmTrigger.t()),
public_functions: list(String.t()),
upgrade_opts: nil | __MODULE__.UpgradeOpts.t()
}
defstruct [:version, triggers: [], public_functions: [], upgrade_opts: nil]

def cast(%{
Expand All @@ -18,6 +26,11 @@ defmodule Archethic.Contracts.WasmSpec do
end

defmodule UpgradeOpts do
@moduledoc false

@type t :: %__MODULE__{
from: binary()
}
defstruct [:from]

def cast(%{"from" => from}) do
Expand Down
5 changes: 5 additions & 0 deletions lib/archethic/contracts/wasm/trigger.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
defmodule Archethic.Contracts.WasmTrigger do
@moduledoc false
@type t :: %__MODULE__{
function_name: String.t(),
type: :transaction | {:datetime, DateTime.t()} | {:interval, String.t()} | :oracle
}
defstruct [:function_name, :type]

def cast({function_name, %{"type" => 0}}),
Expand Down

0 comments on commit 45595f0

Please sign in to comment.