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 transaction's content verification #736

Merged
merged 5 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
75 changes: 75 additions & 0 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Archethic.Mining.PendingTransactionValidation do
TransactionData,
TransactionData.Ledger,
TransactionData.Ownership,
TransactionData.UCOLedger,
TransactionData.TokenLedger,
TransactionSummary
}
Expand Down Expand Up @@ -256,6 +257,47 @@ defmodule Archethic.Mining.PendingTransactionValidation do
end
end

defp do_accept_transaction(
%Transaction{
type: :transfer,
data: %TransactionData{
ledger: %Ledger{
uco: %UCOLedger{transfers: uco_transfers},
token: %TokenLedger{transfers: token_transfers}
},
recipients: recipients
}
},
_
) do
if length(uco_transfers) > 0 or length(token_transfers) > 0 or length(recipients) > 0 do
:ok
else
{:error,
"Transfer's transaction requires some recipients for ledger or smart contract calls"}
end
end

defp do_accept_transaction(
%Transaction{
type: :hosting,
data: %TransactionData{content: content}
},
_
) do
with {:ok, json} <- Jason.decode(content),
:ok <- check_aeweb_format(json) do
:ok
else
:error ->
{:error, "Invalid AEWeb transaction"}

{:error, reason} ->
Logger.debug("Invalid AEWeb format #{inspect(reason)}")
{:error, "Invalid AEWeb transaction"}
end
end

defp do_accept_transaction(
tx = %Transaction{
type: :node_rewards,
Expand Down Expand Up @@ -781,4 +823,37 @@ defmodule Archethic.Mining.PendingTransactionValidation do
{:error, :invalid_ip}
end
end

defp check_aeweb_format(json) do
ref_schema =
:archethic
|> Application.app_dir("priv/json-schemas/aeweb_ref.json")
|> File.read!()
|> Jason.decode!()
|> ExJsonSchema.Schema.resolve()

file_schema =
:archethic
|> Application.app_dir("priv/json-schemas/aeweb_file.json")
|> File.read!()
|> Jason.decode!()
|> ExJsonSchema.Schema.resolve()

case ExJsonSchema.Validator.validate(ref_schema, json) do
:ok ->
:ok

{:error, [{"Required properties aewebVersion, metaData were not present.", _}]} ->
case ExJsonSchema.Validator.validate(file_schema, json) do
:ok ->
:ok

{:error, error_file} ->
{:error, error_file}
end

{:error, error_ref} ->
{:error, error_ref}
end
end
end
9 changes: 9 additions & 0 deletions priv/json-schemas/aeweb_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$chema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"description": "List of files",
"minProperties": 1,
"additionalProperties": {
"type": "string"
}
}
60 changes: 60 additions & 0 deletions priv/json-schemas/aeweb_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"aewebVersion": {
"type": "number",
"exclusiveMinimum": 0,
"description": "AEWeb's version"
},
"sslCertificate": {
"type": "string",
"description": "SSL certificate of the website"
},
"metaData": {
"description": "List of files",
"type": "object",
"minProperties": 1,
"additionalProperties": {
"type": "object",
"properties": {
"hash": {
"type": "string"
},
"size": {
"type": "number",
"exclusiveMinimum": 0
},
"encoding": {
"description": "The encoding of the file",
"type": "string",
"enum": [
"gzip"
]
},
"addresses": {
"description": "List of addresses to storage the file",
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"pattern": "^([0-9A-Fa-f])*$",
"minLength": 64,
"maxLength": 68
}
}
},
"required": [
"hash",
"size",
"encoding",
"addresses"
]
}
}
},
"required": [
"aewebVersion",
"metaData"
]
}
47 changes: 47 additions & 0 deletions test/archethic/mining/pending_transaction_validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ defmodule Archethic.Mining.PendingTransactionValidationTest do

alias Archethic.TransactionChain.Transaction
alias Archethic.TransactionChain.TransactionData
alias Archethic.TransactionChain.TransactionData.Ledger
alias Archethic.TransactionChain.TransactionData.UCOLedger
alias Archethic.TransactionChain.TransactionData.UCOLedger.Transfer
alias Archethic.TransactionChain.TransactionData.Ownership

alias Archethic.SharedSecrets
Expand Down Expand Up @@ -494,6 +497,13 @@ defmodule Archethic.Mining.PendingTransactionValidationTest do
Transaction.new(
:transfer,
%TransactionData{
ledger: %Ledger{
uco: %UCOLedger{
transfers: [
%Transfer{to: :crypto.strong_rand_bytes(32), amount: 100_000}
]
}
},
code: """
condition inherit: [
content: "hello"
Expand Down Expand Up @@ -728,5 +738,42 @@ defmodule Archethic.Mining.PendingTransactionValidationTest do
assert {:error, "Invalid node rewards trigger time"} =
PendingTransactionValidation.validate(tx, ~U[2022-01-01 00:00:03Z])
end

test "should return :ok when we deploy a aeweb ref transaction" do
tx =
Transaction.new(:hosting, %TransactionData{
content:
Jason.encode!(%{
"aewebVersion" => 1,
"metaData" => %{
"index.html" => %{
"encoding" => "gzip",
"hash" => "abcd123",
"size" => 144,
"addresses" => [
Crypto.derive_keypair("seed", 0)
|> elem(0)
|> Crypto.derive_address()
|> Base.encode16()
]
}
}
})
})

assert :ok = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end

test "should return :ok when we deploy a aeweb file transaction" do
tx =
Transaction.new(:hosting, %TransactionData{
content:
Jason.encode!(%{
"index.html" => Base.url_encode64(:crypto.strong_rand_bytes(1000))
})
})

assert :ok = PendingTransactionValidation.validate(tx, DateTime.utc_now())
end
end
end