Skip to content

Commit

Permalink
Smart Contracts: Allow a contract without trigger:transaction to rece…
Browse files Browse the repository at this point in the history
…ive calls (#1124)

* Allow a contract without trigger:transaction to receive calls

* Do not force the 'condition transaction' to be able to remove calls
  • Loading branch information
bchamagne authored and samuelmanzanera committed Jul 4, 2023
1 parent 0c7816d commit 0af81c7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/archethic/p2p/message/validate_smart_contract_call.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Archethic.P2P.Message.ValidateSmartContractCall do
defstruct [:contract_address, :transaction, :inputs_before]

alias Archethic.Contracts
alias Archethic.Contracts.Contract
alias Archethic.Crypto
alias Archethic.P2P.Message.SmartContractCallValidation
alias Archethic.TransactionChain
Expand Down Expand Up @@ -173,10 +174,7 @@ defmodule Archethic.P2P.Message.ValidateSmartContractCall do
{:ok, contract} <- Contracts.from_transaction(contract_tx),
true <-
Contracts.valid_condition?(:transaction, contract, transaction, datetime),
{:ok, _} <-
Contracts.execute_trigger(:transaction, contract, transaction, [transaction],
time_now: datetime
) do
:ok <- maybe_execute_trigger(contract, transaction, time_now: datetime) do
true
else
_ ->
Expand All @@ -187,4 +185,18 @@ defmodule Archethic.P2P.Message.ValidateSmartContractCall do
valid?: valid?
}
end

defp maybe_execute_trigger(contract = %Contract{triggers: triggers}, transaction, opts) do
if Map.has_key?(triggers, :transaction) do
case Contracts.execute_trigger(:transaction, contract, transaction, [transaction], opts) do
{:ok, _} ->
:ok

{:error, reason} ->
{:error, reason}
end
else
:ok
end
end
end
33 changes: 33 additions & 0 deletions test/archethic/p2p/message/validate_smart_contract_call_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ defmodule Archethic.P2P.Message.ValidateSmartContractCallTest do
|> ValidateSmartContractCall.process(:crypto.strong_rand_bytes(32))
end

test "should validate smart contract that does not have a transaction trigger" do
MockDB
|> expect(:get_transaction, fn "@SC1", _, _ ->
{:ok,
%Transaction{
data: %TransactionData{
code: ~s"""
@version 1
actions triggered_by: datetime, at: 1687874880 do
calls = Contract.get_calls
Contract.set_content List.size(calls)
end
"""
}
}}
end)

incoming_tx = %Transaction{
data: %TransactionData{
content: "hola"
}
}

assert %SmartContractCallValidation{valid?: true} =
%ValidateSmartContractCall{
contract_address: "@SC1",
transaction: incoming_tx,
inputs_before: DateTime.utc_now()
}
|> ValidateSmartContractCall.process(:crypto.strong_rand_bytes(32))
end

test "should validate smart contract call and return invalid message" do
MockDB
|> expect(:get_transaction, fn "@SC1", _, _ ->
Expand Down

0 comments on commit 0af81c7

Please sign in to comment.