Skip to content

Commit

Permalink
Adapt playbook since we cannot use datetime with seconds anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Jun 27, 2023
1 parent e657c9d commit 9c148f2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 65 deletions.
3 changes: 2 additions & 1 deletion lib/archethic/utils/regression.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ defmodule Archethic.Utils.Regression do

alias Archethic.Utils

alias Archethic.Utils.Regression.Playbook.SmartContract
alias Archethic.Utils.Regression.Playbook.UCO

alias Archethic.Utils.WebClient
alias Archethic.Utils.Regression.Benchmark.EndToEndValidation
alias Archethic.Utils.Regression.Benchmark.P2PMessage

@playbooks [UCO]
@playbooks [UCO, SmartContract]
@benchmarks [P2PMessage, EndToEndValidation]

def run_playbooks(nodes, opts \\ []) do
Expand Down
1 change: 1 addition & 0 deletions lib/archethic/utils/regression/playbooks/smart_contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract do
trigger_seed,
:transfer,
%TransactionData{
content: Keyword.get(opts, :content, ""),
recipients: [contract_address]
},
endpoint
Expand Down
22 changes: 4 additions & 18 deletions lib/archethic/utils/regression/playbooks/smart_contract/counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Counter do
endpoint
)

close_datetime =
DateTime.utc_now()
|> DateTime.add(10)

contract_address =
SmartContract.deploy(
contract_seed,
%TransactionData{
content: "0",
code: contract_code(close_datetime)
code: contract_code()
},
storage_nonce_pubkey,
endpoint
Expand All @@ -53,26 +49,16 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Counter do
end
end

defp contract_code(datetime) do
defp contract_code() do
~s"""
@version 1
# GENERATED BY PLAYBOOK
condition transaction: []
condition inherit: [
content: true,
code: true
]
actions triggered_by: datetime, at: #{DateTime.to_unix(datetime)} do
Contract.set_content("")
Contract.set_code("@version 1\ncondition inherit: []")
end
actions triggered_by: transaction do
count = String.to_number(contract.content) + 1
Contract.set_content count
count = String.to_number(contract.content) + 1
Contract.set_content count
end
"""
end
Expand Down
19 changes: 10 additions & 9 deletions lib/archethic/utils/regression/playbooks/smart_contract/faucet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Faucet do
trigger2_seed = SmartContract.random_seed()
trigger3_seed = SmartContract.random_seed()

now = DateTime.utc_now()

faucet_time =
DateTime.utc_now()
|> DateTime.add(5)
if now.second > 50 do
# if we're too close to the next tick, we'll add one more minute to be sure
%DateTime{(now |> DateTime.add(2, :minute)) | second: 0, microsecond: {0, 0}}
else
%DateTime{(now |> DateTime.add(1, :minute)) | second: 0, microsecond: {0, 0}}
end

Api.send_funds_to_seeds(
%{
Expand Down Expand Up @@ -47,7 +53,7 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Faucet do
trigger3_tx = SmartContract.trigger(trigger3_seed, contract_address, endpoint)

# wait for the tick (+ delta)
sleep_time = DateTime.diff(faucet_time, DateTime.utc_now(), :millisecond) + 500
sleep_time = DateTime.diff(faucet_time, DateTime.utc_now(), :millisecond) + 1_000
Logger.debug("Sleeping for (#{div(sleep_time, 1000)} seconds)")
Process.sleep(sleep_time)

Expand All @@ -74,11 +80,7 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Faucet do
# GENERATED BY PLAYBOOK
condition inherit: [
code: true,
uco_transfers: true,
content: true
]
condition transaction: []
actions triggered_by: datetime, at: ##DATETIME## do
now = Time.now()
Expand All @@ -94,7 +96,6 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Faucet do
end
Contract.set_content(content)
Contract.set_code("@version 1\ncondition inherit: []")
end
"""
|> String.replace("##DATETIME##", datetime |> DateTime.to_unix() |> Integer.to_string())
Expand Down
58 changes: 34 additions & 24 deletions lib/archethic/utils/regression/playbooks/smart_contract/legacy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,53 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Legacy do
require Logger

def play(storage_nonce_pubkey, endpoint) do
seed = SmartContract.random_seed()
trigger_seed = SmartContract.random_seed()
contract_seed = SmartContract.random_seed()
recipient_address = SmartContract.random_address()
amount_to_send = Utils.to_bigint(0.1)
ticks_count = 4

Api.send_funds_to_seeds(%{seed => 10}, endpoint)

sleep_ms = 200 + ticks_count * Contracts.minimum_trigger_interval()
close_datetime = DateTime.utc_now() |> DateTime.add(sleep_ms + 500, :millisecond)

SmartContract.deploy(
seed,
%TransactionData{
code: contract_code(recipient_address, amount_to_send, close_datetime)
Api.send_funds_to_seeds(
%{
contract_seed => 10,
trigger_seed => 10
},
storage_nonce_pubkey,
endpoint
)

sleep_ms = 200 + ticks_count * Contracts.minimum_trigger_interval()

contract_address =
SmartContract.deploy(
contract_seed,
%TransactionData{
code: contract_code(recipient_address, amount_to_send)
},
storage_nonce_pubkey,
endpoint
)

# wait some ticks
Logger.debug("Sleeping for #{ticks_count} ticks (#{div(sleep_ms, 1000)} seconds)")
Process.sleep(sleep_ms)

balance = Api.get_uco_balance(recipient_address, endpoint)
expected_balance = ticks_count * amount_to_send

case balance do
^expected_balance ->
Logger.info(
"Smart contract 'legacy' received #{Utils.from_bigint(balance)} UCOs after #{ticks_count} ticks"
)
SmartContract.trigger(trigger_seed, contract_address, endpoint, content: "CLOSE_CONTRACT")

_ ->
Logger.error(
"Smart contract 'legacy' received #{Utils.from_bigint(balance)} UCOs after #{ticks_count} ticks"
)
# there's a slight change there will be 1 more tick due to playbook code
if balance in [ticks_count * amount_to_send, (1 + ticks_count) * amount_to_send] do
Logger.info(
"Smart contract 'legacy' received #{Utils.from_bigint(balance)} UCOs after #{ticks_count} ticks"
)
else
Logger.error(
"Smart contract 'legacy' received #{Utils.from_bigint(balance)} UCOs after #{ticks_count} ticks"
)
end
end

defp contract_code(address, amount, datetime) do
defp contract_code(address, amount) do
~s"""
# GENERATED BY PLAYBOOK
Expand All @@ -61,8 +68,11 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.Legacy do
uco_transfers: true
]
actions triggered_by: datetime, at: #{DateTime.to_unix(datetime)} do
set_code("condition inherit: []")
condition transaction: []
actions triggered_by: transaction do
if transaction.content == "CLOSE_CONTRACT" do
set_code("condition inherit: []")
end
end
actions triggered_by: interval, at: "* * * * * *" do
Expand Down
16 changes: 3 additions & 13 deletions lib/archethic/utils/regression/playbooks/smart_contract/uco_ath.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.UcoAth do
endpoint
)

close_datetime =
DateTime.utc_now()
|> DateTime.add(70)

contract_address =
SmartContract.deploy(
contract_seed,
%TransactionData{
content: Jason.encode!(%{"ucoMaxPrice" => -1}),
code: contract_code(close_datetime)
code: contract_code()
},
storage_nonce_pubkey,
endpoint
Expand All @@ -49,29 +45,23 @@ defmodule Archethic.Utils.Regression.Playbook.SmartContract.UcoAth do
end
end

defp contract_code(datetime) do
defp contract_code() do
~s"""
@version 1
# GENERATED BY PLAYBOOK
condition inherit: [content: true, code: true]
condition oracle: [
content: Json.path_match?("$.uco.usd")
]
actions triggered_by: datetime, at: #{DateTime.to_unix(datetime)} do
Contract.set_content("")
Contract.set_code("@version 1\ncondition inherit: []")
end
actions triggered_by: oracle do
contract_data = Json.parse(contract.content)
uco_price = Json.path_extract(transaction.content, "$.uco.usd")
if uco_price > contract_data.ucoMaxPrice do
Contract.set_content(Json.to_string(ucoMaxPrice: uco_price))
Contract.set_code("@version 1\ncondition inherit: []")
end
end
"""
Expand Down

0 comments on commit 9c148f2

Please sign in to comment.