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

Add New Types of Transaction #726 #791

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ config :archethic, :mut_dir, "data"

config :archethic, :marker, "-=%=-=%=-=%=-"

config :archethic, :ownership_max_authorized_keys, 256
apoorv-2204 marked this conversation as resolved.
Show resolved Hide resolved

# size represents in bytes binary
config :archethic, :transaction_data_content_max_size, 3_145_728

Expand Down
63 changes: 63 additions & 0 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,71 @@ defmodule Archethic.Mining.PendingTransactionValidation do
end
end

@code_max_size Application.compile_env!(:archethic, :transaction_data_code_max_size)
defp do_accept_transaction(%Transaction{type: :contract, data: %TransactionData{code: code}}, _) do
Neylix marked this conversation as resolved.
Show resolved Hide resolved
cond do
byte_size(code) == 0 ->
{:error, "invalid contract type transaction - code is empty "}

byte_size(code) >= @code_max_size ->
{:error, "invalid contract type transaction , code exceed max size "}

true ->
apoorv-2204 marked this conversation as resolved.
Show resolved Hide resolved
:ok
end
end

@content_max_size Application.compile_env!(:archethic, :transaction_data_content_max_size)
@ownership_max_keys Application.compile_env!(:archethic, :ownership_max_authorized_keys)
@max_ownerships @ownership_max_keys
defp do_accept_transaction(
%Transaction{
type: :data,
data: %TransactionData{content: content, ownerships: ownerships}
},
_
) do
# content_size = byte_size(content) already handled
nb_ownerships = length(ownerships)
apoorv-2204 marked this conversation as resolved.
Show resolved Hide resolved

cond do
content == "" && nb_ownerships == 0 ->
{:error, "invalid data type transaction - Both content & ownership are empty"}

nb_ownerships > @max_ownerships ->
{:error, "invalid data type transaction - ownerships exceeds limit"}

nb_ownerships != 0 ->
validate_ownerships(ownerships)
apoorv-2204 marked this conversation as resolved.
Show resolved Hide resolved
end
end

defp do_accept_transaction(_, _), do: :ok

defp validate_ownerships(ownerships) do
Enum.reduce_while(ownerships, :ok, fn
%Ownership{secret: "", authorized_keys: _}, :ok ->
{:halt, {:error, "invalid data type transaction - secret is empty"}}

%Ownership{secret: _, authorized_keys: %{}}, :ok ->
{:halt, {:error, "invalid data type transaction - authorized keys is empty"}}

%Ownership{secret: _, authorized_keys: authorized_keys}, :ok ->
Enum.reduce_while(authorized_keys, {:cont, :ok}, fn
{"", _}, _ ->
{:halt, {:error, "invalid data type transaction - public key is empty"}}

{_, ""}, _ ->
{:halt, {:error, "invalid data type transaction - encrypted key is empty"}}

{public_key, _}, acc ->
if Crypto.valid_public_key?(public_key),
do: {:cont, acc},
else: {:halt, {:error, "invalid data type transaction - invalid public key"}}
end)
apoorv-2204 marked this conversation as resolved.
Show resolved Hide resolved
end)
end

defp verify_token_creation(content) do
schema =
:archethic
Expand Down
6 changes: 5 additions & 1 deletion lib/archethic/transaction_chain/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ defmodule Archethic.TransactionChain.Transaction do
| :token
| :hosting
| :origin
| :data
| :contract

@transaction_types [
:node,
Expand All @@ -91,7 +93,9 @@ defmodule Archethic.TransactionChain.Transaction do
:transfer,
:hosting,
:token,
:origin
:origin,
:data,
:contract
]

@doc """
Expand Down
3 changes: 2 additions & 1 deletion lib/archethic_web/controllers/api/schema/ownership.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ArchethicWeb.API.Schema.Ownership do
@moduledoc false
@ownership_max_keys Application.compile_env!(:archethic, :ownership_max_authorized_keys)

use Ecto.Schema
import Ecto.Changeset
Expand All @@ -18,7 +19,7 @@ defmodule ArchethicWeb.API.Schema.Ownership do
|> cast_embed(:authorizedKeys, required: [:publicKey, :encryptedSecretKey])
|> validate_required([:secret])
|> validate_length(:authorizedKeys,
max: 256,
max: @ownership_max_keys,
message: "maximum number of authorized keys can be 256"
)
|> format_authorized_keys()
Expand Down